2 * Copyright 2015 Open Networking Laboratory
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onosproject.vtnrsc.flowclassifier.impl;
18 import org.apache.felix.scr.annotations.Activate;
19 import org.apache.felix.scr.annotations.Component;
20 import org.apache.felix.scr.annotations.Deactivate;
21 import org.apache.felix.scr.annotations.Reference;
22 import org.apache.felix.scr.annotations.ReferenceCardinality;
23 import org.apache.felix.scr.annotations.Service;
24 import org.onlab.util.KryoNamespace;
25 import org.onosproject.store.serializers.KryoNamespaces;
26 import org.onosproject.store.service.EventuallyConsistentMap;
27 import org.onosproject.store.service.MultiValuedTimestamp;
28 import org.onosproject.store.service.StorageService;
29 import org.onosproject.store.service.WallClockTimestamp;
30 import org.onosproject.vtnrsc.FlowClassifierId;
31 import org.onosproject.vtnrsc.FlowClassifier;
32 import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
33 import org.slf4j.Logger;
35 import static org.slf4j.LoggerFactory.getLogger;
36 import static com.google.common.base.Preconditions.checkNotNull;
38 import com.google.common.collect.ImmutableList;
41 * Provides implementation of the Flow Classifier Service.
43 @Component(immediate = true)
45 public class FlowClassifierManager implements FlowClassifierService {
47 private final Logger log = getLogger(FlowClassifierManager.class);
49 private static final String FLOW_CLASSIFIER_NOT_NULL = "Flow Classifier cannot be null";
50 private static final String FLOW_CLASSIFIER_ID_NOT_NULL = "Flow Classifier Id cannot be null";
52 private EventuallyConsistentMap<FlowClassifierId, FlowClassifier> flowClassifierStore;
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected StorageService storageService;
57 protected void activate() {
58 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
59 .register(KryoNamespaces.API)
60 .register(MultiValuedTimestamp.class)
61 .register(FlowClassifier.class);
62 flowClassifierStore = storageService
63 .<FlowClassifierId, FlowClassifier>eventuallyConsistentMapBuilder()
64 .withName("flowclassifierstore").withSerializer(serializer)
65 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
66 log.info("Flow Classifier service activated");
70 protected void deactivate() {
71 flowClassifierStore.destroy();
72 log.info("Flow Classifier service deactivated");
76 public boolean exists(FlowClassifierId id) {
77 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
78 return flowClassifierStore.containsKey(id);
82 public int getFlowClassifierCount() {
83 return flowClassifierStore.size();
87 public Iterable<FlowClassifier> getFlowClassifiers() {
88 return ImmutableList.copyOf(flowClassifierStore.values());
92 public FlowClassifier getFlowClassifier(FlowClassifierId id) {
93 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
94 return flowClassifierStore.get(id);
98 public boolean createFlowClassifier(FlowClassifier flowClassifier) {
99 log.debug("createFlowClassifier");
100 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
101 FlowClassifierId id = flowClassifier.flowClassifierId();
103 flowClassifierStore.put(id, flowClassifier);
104 if (!flowClassifierStore.containsKey(id)) {
105 log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
112 public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
113 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
115 if (!flowClassifierStore.containsKey(flowClassifier.flowClassifierId())) {
116 log.debug("The flowClassifier is not exist whose identifier was {} ", flowClassifier.flowClassifierId()
121 flowClassifierStore.put(flowClassifier.flowClassifierId(), flowClassifier);
123 if (!flowClassifier.equals(flowClassifierStore.get(flowClassifier.flowClassifierId()))) {
124 log.debug("Updation of flowClassifier is failed whose identifier was {} ", flowClassifier
125 .flowClassifierId().toString());
132 public boolean removeFlowClassifier(FlowClassifierId id) {
133 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
134 flowClassifierStore.remove(id);
135 if (flowClassifierStore.containsKey(id)) {
136 log.debug("The Flow Classifier removal is failed whose identifier is {}", id.toString());