18f63b301f0a2d09d745b32adae972a389886829
[onosfw.git] /
1 /*
2  * Copyright 2015 Open Networking Laboratory
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onosproject.vtnrsc.flowclassifier.impl;
17
18 import static org.slf4j.LoggerFactory.getLogger;
19 import static com.google.common.base.Preconditions.checkNotNull;
20
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;
40
41 import com.google.common.collect.ImmutableList;
42
43 /**
44  * Provides implementation of the Flow Classifier Service.
45  */
46 @Component(immediate = true)
47 @Service
48 public class FlowClassifierManager extends AbstractListenerManager<FlowClassifierEvent, FlowClassifierListener>
49         implements FlowClassifierService {
50
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";
54
55     private final Logger log = getLogger(FlowClassifierManager.class);
56
57     private EventuallyConsistentMap<FlowClassifierId, FlowClassifier> flowClassifierStore;
58
59     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60     protected StorageService storageService;
61
62     @Activate
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");
73     }
74
75     @Deactivate
76     protected void deactivate() {
77         flowClassifierStore.destroy();
78         log.info("Flow Classifier service deactivated");
79     }
80
81     @Override
82     public boolean exists(FlowClassifierId id) {
83         checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
84         return flowClassifierStore.containsKey(id);
85     }
86
87     @Override
88     public int getFlowClassifierCount() {
89         return flowClassifierStore.size();
90     }
91
92     @Override
93     public Iterable<FlowClassifier> getFlowClassifiers() {
94         return ImmutableList.copyOf(flowClassifierStore.values());
95     }
96
97     @Override
98     public FlowClassifier getFlowClassifier(FlowClassifierId id) {
99         checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
100         return flowClassifierStore.get(id);
101     }
102
103     @Override
104     public boolean createFlowClassifier(FlowClassifier flowClassifier) {
105         log.debug("createFlowClassifier");
106         checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
107         FlowClassifierId id = flowClassifier.flowClassifierId();
108
109         flowClassifierStore.put(id, flowClassifier);
110         if (!flowClassifierStore.containsKey(id)) {
111             log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
112             return false;
113         }
114         return true;
115     }
116
117     @Override
118     public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
119         checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
120
121         if (!flowClassifierStore.containsKey(flowClassifier.flowClassifierId())) {
122             log.debug("The flowClassifier is not exist whose identifier was {} ", flowClassifier.flowClassifierId()
123                     .toString());
124             return false;
125         }
126
127         flowClassifierStore.put(flowClassifier.flowClassifierId(), flowClassifier);
128
129         if (!flowClassifier.equals(flowClassifierStore.get(flowClassifier.flowClassifierId()))) {
130             log.debug("Updation of flowClassifier is failed whose identifier was {} ", flowClassifier
131                     .flowClassifierId().toString());
132             return false;
133         }
134         return true;
135     }
136
137     @Override
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());
143             return false;
144         }
145         return true;
146     }
147 }