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 static org.slf4j.LoggerFactory.getLogger;
19 import static com.google.common.base.Preconditions.checkNotNull;
21 import org.apache.felix.scr.annotations.Activate;
22 import org.apache.felix.scr.annotations.Component;
23 import org.apache.felix.scr.annotations.Deactivate;
24 import org.apache.felix.scr.annotations.Reference;
25 import org.apache.felix.scr.annotations.ReferenceCardinality;
26 import org.apache.felix.scr.annotations.Service;
27 import org.onlab.util.KryoNamespace;
28 import org.onosproject.event.AbstractListenerManager;
29 import org.onosproject.store.serializers.KryoNamespaces;
30 import org.onosproject.store.service.EventuallyConsistentMap;
31 import org.onosproject.store.service.MultiValuedTimestamp;
32 import org.onosproject.store.service.StorageService;
33 import org.onosproject.store.service.WallClockTimestamp;
34 import org.onosproject.vtnrsc.FlowClassifierId;
35 import org.onosproject.vtnrsc.FlowClassifier;
36 import org.onosproject.vtnrsc.flowclassifier.FlowClassifierEvent;
37 import org.onosproject.vtnrsc.flowclassifier.FlowClassifierListener;
38 import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
39 import org.slf4j.Logger;
41 import com.google.common.collect.ImmutableList;
44 * Provides implementation of the Flow Classifier Service.
46 @Component(immediate = true)
48 public class FlowClassifierManager extends AbstractListenerManager<FlowClassifierEvent, FlowClassifierListener>
49 implements FlowClassifierService {
51 private static final String FLOW_CLASSIFIER_NOT_NULL = "Flow Classifier cannot be null";
52 private static final String FLOW_CLASSIFIER_ID_NOT_NULL = "Flow Classifier Id cannot be null";
53 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
55 private final Logger log = getLogger(FlowClassifierManager.class);
57 private EventuallyConsistentMap<FlowClassifierId, FlowClassifier> flowClassifierStore;
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected StorageService storageService;
63 protected void activate() {
64 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
65 .register(KryoNamespaces.API)
66 .register(MultiValuedTimestamp.class)
67 .register(FlowClassifier.class);
68 flowClassifierStore = storageService
69 .<FlowClassifierId, FlowClassifier>eventuallyConsistentMapBuilder()
70 .withName("flowclassifierstore").withSerializer(serializer)
71 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
72 log.info("Flow Classifier service activated");
76 protected void deactivate() {
77 flowClassifierStore.destroy();
78 log.info("Flow Classifier service deactivated");
82 public boolean exists(FlowClassifierId id) {
83 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
84 return flowClassifierStore.containsKey(id);
88 public int getFlowClassifierCount() {
89 return flowClassifierStore.size();
93 public Iterable<FlowClassifier> getFlowClassifiers() {
94 return ImmutableList.copyOf(flowClassifierStore.values());
98 public FlowClassifier getFlowClassifier(FlowClassifierId id) {
99 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
100 return flowClassifierStore.get(id);
104 public boolean createFlowClassifier(FlowClassifier flowClassifier) {
105 log.debug("createFlowClassifier");
106 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
107 FlowClassifierId id = flowClassifier.flowClassifierId();
109 flowClassifierStore.put(id, flowClassifier);
110 if (!flowClassifierStore.containsKey(id)) {
111 log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
118 public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
119 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
121 if (!flowClassifierStore.containsKey(flowClassifier.flowClassifierId())) {
122 log.debug("The flowClassifier is not exist whose identifier was {} ", flowClassifier.flowClassifierId()
127 flowClassifierStore.put(flowClassifier.flowClassifierId(), flowClassifier);
129 if (!flowClassifier.equals(flowClassifierStore.get(flowClassifier.flowClassifierId()))) {
130 log.debug("Updation of flowClassifier is failed whose identifier was {} ", flowClassifier
131 .flowClassifierId().toString());
138 public boolean removeFlowClassifier(FlowClassifierId id) {
139 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
140 flowClassifierStore.remove(id);
141 if (flowClassifierStore.containsKey(id)) {
142 log.debug("The Flow Classifier removal is failed whose identifier is {}", id.toString());