blob: 25fca3394cbdb0b862de9459a7d1f13c9f1ce0f2 [file] [log] [blame]
Bharat saraswal93aaf512015-10-26 18:10:07 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bharat saraswal93aaf512015-10-26 18:10:07 +05303 *
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 */
Jonathan Hart317f4762015-11-09 16:05:36 -080016package org.onosproject.vtnrsc.flowclassifier.impl;
Bharat saraswal93aaf512015-10-26 18:10:07 +053017
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053018import static org.slf4j.LoggerFactory.getLogger;
19import static com.google.common.base.Preconditions.checkNotNull;
20
Bharat saraswal93aaf512015-10-26 18:10:07 +053021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
samueljccb3b2dd32015-10-27 16:22:37 -070024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Bharat saraswal93aaf512015-10-26 18:10:07 +053026import org.apache.felix.scr.annotations.Service;
samueljccb3b2dd32015-10-27 16:22:37 -070027import org.onlab.util.KryoNamespace;
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053028import org.onosproject.event.AbstractListenerManager;
samueljccb3b2dd32015-10-27 16:22:37 -070029import org.onosproject.store.serializers.KryoNamespaces;
30import org.onosproject.store.service.EventuallyConsistentMap;
Bharat saraswal94358502015-12-11 00:58:15 +053031import org.onosproject.store.service.EventuallyConsistentMapEvent;
32import org.onosproject.store.service.EventuallyConsistentMapListener;
samueljccb3b2dd32015-10-27 16:22:37 -070033import org.onosproject.store.service.MultiValuedTimestamp;
34import org.onosproject.store.service.StorageService;
35import org.onosproject.store.service.WallClockTimestamp;
Bharat saraswal93aaf512015-10-26 18:10:07 +053036import org.onosproject.vtnrsc.FlowClassifierId;
37import org.onosproject.vtnrsc.FlowClassifier;
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053038import org.onosproject.vtnrsc.flowclassifier.FlowClassifierEvent;
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053039import org.onosproject.vtnrsc.flowclassifier.FlowClassifierListener;
Jonathan Hart317f4762015-11-09 16:05:36 -080040import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
Bharat saraswal93aaf512015-10-26 18:10:07 +053041import org.slf4j.Logger;
Bharat saraswal93aaf512015-10-26 18:10:07 +053042
Bharat saraswal93aaf512015-10-26 18:10:07 +053043import com.google.common.collect.ImmutableList;
44
45/**
46 * Provides implementation of the Flow Classifier Service.
47 */
48@Component(immediate = true)
49@Service
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053050public class FlowClassifierManager extends AbstractListenerManager<FlowClassifierEvent, FlowClassifierListener>
51 implements FlowClassifierService {
Bharat saraswal93aaf512015-10-26 18:10:07 +053052
Bharat saraswal93aaf512015-10-26 18:10:07 +053053 private static final String FLOW_CLASSIFIER_NOT_NULL = "Flow Classifier cannot be null";
54 private static final String FLOW_CLASSIFIER_ID_NOT_NULL = "Flow Classifier Id cannot be null";
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053055 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
Bharat saraswal94358502015-12-11 00:58:15 +053056 private static final String EVENT_NOT_NULL = "event cannot be null";
Bharat saraswal93aaf512015-10-26 18:10:07 +053057
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053058 private final Logger log = getLogger(FlowClassifierManager.class);
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053059
samueljccb3b2dd32015-10-27 16:22:37 -070060 private EventuallyConsistentMap<FlowClassifierId, FlowClassifier> flowClassifierStore;
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053061
Bharat saraswal94358502015-12-11 00:58:15 +053062 private EventuallyConsistentMapListener<FlowClassifierId, FlowClassifier> flowClassifierListener =
63 new InnerFlowClassifierStoreListener();
64
samueljccb3b2dd32015-10-27 16:22:37 -070065 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected StorageService storageService;
Bharat saraswal93aaf512015-10-26 18:10:07 +053067
68 @Activate
Mahesh Poojary Sea0775a2015-11-19 15:32:52 +053069 protected void activate() {
Bharat saraswal94358502015-12-11 00:58:15 +053070 eventDispatcher.addSink(FlowClassifierEvent.class, listenerRegistry);
samueljccb3b2dd32015-10-27 16:22:37 -070071 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
72 .register(KryoNamespaces.API)
73 .register(MultiValuedTimestamp.class)
74 .register(FlowClassifier.class);
75 flowClassifierStore = storageService
76 .<FlowClassifierId, FlowClassifier>eventuallyConsistentMapBuilder()
77 .withName("flowclassifierstore").withSerializer(serializer)
78 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
Bharat saraswal94358502015-12-11 00:58:15 +053079 flowClassifierStore.addListener(flowClassifierListener);
Bharat saraswal93aaf512015-10-26 18:10:07 +053080 log.info("Flow Classifier service activated");
81 }
82
83 @Deactivate
Mahesh Poojary Sea0775a2015-11-19 15:32:52 +053084 protected void deactivate() {
Bharat saraswal94358502015-12-11 00:58:15 +053085 eventDispatcher.removeSink(FlowClassifierEvent.class);
samueljccb3b2dd32015-10-27 16:22:37 -070086 flowClassifierStore.destroy();
Bharat saraswal93aaf512015-10-26 18:10:07 +053087 log.info("Flow Classifier service deactivated");
88 }
89
90 @Override
Mahesh Poojary Sfac02262015-11-20 19:13:22 +053091 public boolean exists(FlowClassifierId id) {
92 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
93 return flowClassifierStore.containsKey(id);
94 }
95
96 @Override
97 public int getFlowClassifierCount() {
98 return flowClassifierStore.size();
99 }
100
101 @Override
102 public Iterable<FlowClassifier> getFlowClassifiers() {
103 return ImmutableList.copyOf(flowClassifierStore.values());
104 }
105
106 @Override
107 public FlowClassifier getFlowClassifier(FlowClassifierId id) {
108 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
109 return flowClassifierStore.get(id);
110 }
111
112 @Override
Bharat saraswal93aaf512015-10-26 18:10:07 +0530113 public boolean createFlowClassifier(FlowClassifier flowClassifier) {
114 log.debug("createFlowClassifier");
115 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
116 FlowClassifierId id = flowClassifier.flowClassifierId();
117
118 flowClassifierStore.put(id, flowClassifier);
119 if (!flowClassifierStore.containsKey(id)) {
120 log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
121 return false;
122 }
123 return true;
124 }
125
126 @Override
Bharat saraswal93aaf512015-10-26 18:10:07 +0530127 public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
128 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530129
130 if (!flowClassifierStore.containsKey(flowClassifier.flowClassifierId())) {
131 log.debug("The flowClassifier is not exist whose identifier was {} ", flowClassifier.flowClassifierId()
132 .toString());
133 return false;
134 }
135
136 flowClassifierStore.put(flowClassifier.flowClassifierId(), flowClassifier);
137
138 if (!flowClassifier.equals(flowClassifierStore.get(flowClassifier.flowClassifierId()))) {
139 log.debug("Updation of flowClassifier is failed whose identifier was {} ", flowClassifier
140 .flowClassifierId().toString());
141 return false;
142 }
samueljccb3b2dd32015-10-27 16:22:37 -0700143 return true;
Bharat saraswal93aaf512015-10-26 18:10:07 +0530144 }
145
146 @Override
147 public boolean removeFlowClassifier(FlowClassifierId id) {
148 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
149 flowClassifierStore.remove(id);
150 if (flowClassifierStore.containsKey(id)) {
151 log.debug("The Flow Classifier removal is failed whose identifier is {}", id.toString());
152 return false;
153 }
154 return true;
155 }
Bharat saraswal94358502015-12-11 00:58:15 +0530156
157 private class InnerFlowClassifierStoreListener
158 implements
159 EventuallyConsistentMapListener<FlowClassifierId, FlowClassifier> {
160
161 @Override
162 public void event(EventuallyConsistentMapEvent<FlowClassifierId, FlowClassifier> event) {
163 checkNotNull(event, EVENT_NOT_NULL);
164 FlowClassifier flowClassifier = event.value();
165 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
166 notifyListeners(new FlowClassifierEvent(
167 FlowClassifierEvent.Type.FLOW_CLASSIFIER_PUT,
168 flowClassifier));
169 }
170 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
171 notifyListeners(new FlowClassifierEvent(
172 FlowClassifierEvent.Type.FLOW_CLASSIFIER_DELETE,
173 flowClassifier));
174 }
175 }
176 }
177
178 /**
179 * Notifies specify event to all listeners.
180 *
181 * @param event flow classifier event
182 */
183 private void notifyListeners(FlowClassifierEvent event) {
184 checkNotNull(event, EVENT_NOT_NULL);
185 post(event);
186 }
Jonathan Hart317f4762015-11-09 16:05:36 -0800187}