blob: 5521d2e8aaba90e454e4c413e0b5b304cd882290 [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 com.google.common.base.Preconditions.checkNotNull;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053019import static org.slf4j.LoggerFactory.getLogger;
20
21import java.util.UUID;
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053022
Bharat saraswal93aaf512015-10-26 18:10:07 +053023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
samueljccb3b2dd32015-10-27 16:22:37 -070026import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
Bharat saraswal93aaf512015-10-26 18:10:07 +053028import org.apache.felix.scr.annotations.Service;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053029import org.onlab.packet.IpPrefix;
samueljccb3b2dd32015-10-27 16:22:37 -070030import org.onlab.util.KryoNamespace;
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053031import org.onosproject.event.AbstractListenerManager;
samueljccb3b2dd32015-10-27 16:22:37 -070032import org.onosproject.store.serializers.KryoNamespaces;
33import org.onosproject.store.service.EventuallyConsistentMap;
Bharat saraswal94358502015-12-11 00:58:15 +053034import org.onosproject.store.service.EventuallyConsistentMapEvent;
35import org.onosproject.store.service.EventuallyConsistentMapListener;
samueljccb3b2dd32015-10-27 16:22:37 -070036import org.onosproject.store.service.MultiValuedTimestamp;
37import org.onosproject.store.service.StorageService;
38import org.onosproject.store.service.WallClockTimestamp;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053039import org.onosproject.vtnrsc.DefaultFlowClassifier;
Bharat saraswal93aaf512015-10-26 18:10:07 +053040import org.onosproject.vtnrsc.FlowClassifier;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053041import org.onosproject.vtnrsc.FlowClassifierId;
42import org.onosproject.vtnrsc.TenantId;
43import org.onosproject.vtnrsc.VirtualPortId;
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053044import org.onosproject.vtnrsc.flowclassifier.FlowClassifierEvent;
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053045import org.onosproject.vtnrsc.flowclassifier.FlowClassifierListener;
Jonathan Hart317f4762015-11-09 16:05:36 -080046import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
Bharat saraswal93aaf512015-10-26 18:10:07 +053047import org.slf4j.Logger;
Bharat saraswal93aaf512015-10-26 18:10:07 +053048
Bharat saraswal93aaf512015-10-26 18:10:07 +053049import com.google.common.collect.ImmutableList;
50
51/**
52 * Provides implementation of the Flow Classifier Service.
53 */
54@Component(immediate = true)
55@Service
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053056public class FlowClassifierManager extends AbstractListenerManager<FlowClassifierEvent, FlowClassifierListener>
57 implements FlowClassifierService {
Bharat saraswal93aaf512015-10-26 18:10:07 +053058
Bharat saraswal93aaf512015-10-26 18:10:07 +053059 private static final String FLOW_CLASSIFIER_NOT_NULL = "Flow Classifier cannot be null";
60 private static final String FLOW_CLASSIFIER_ID_NOT_NULL = "Flow Classifier Id cannot be null";
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053061 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
Bharat saraswal94358502015-12-11 00:58:15 +053062 private static final String EVENT_NOT_NULL = "event cannot be null";
Bharat saraswal93aaf512015-10-26 18:10:07 +053063
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053064 private final Logger log = getLogger(FlowClassifierManager.class);
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053065
samueljccb3b2dd32015-10-27 16:22:37 -070066 private EventuallyConsistentMap<FlowClassifierId, FlowClassifier> flowClassifierStore;
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053067
Bharat saraswal94358502015-12-11 00:58:15 +053068 private EventuallyConsistentMapListener<FlowClassifierId, FlowClassifier> flowClassifierListener =
69 new InnerFlowClassifierStoreListener();
70
samueljccb3b2dd32015-10-27 16:22:37 -070071 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected StorageService storageService;
Bharat saraswal93aaf512015-10-26 18:10:07 +053073
74 @Activate
Mahesh Poojary Sea0775a2015-11-19 15:32:52 +053075 protected void activate() {
Bharat saraswal94358502015-12-11 00:58:15 +053076 eventDispatcher.addSink(FlowClassifierEvent.class, listenerRegistry);
samueljccb3b2dd32015-10-27 16:22:37 -070077 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
78 .register(KryoNamespaces.API)
79 .register(MultiValuedTimestamp.class)
Phaneendra Manda8db7d092016-06-04 00:17:24 +053080 .register(FlowClassifier.class, FlowClassifierId.class, UUID.class, IpPrefix.class,
81 VirtualPortId.class, DefaultFlowClassifier.class, TenantId.class);
samueljccb3b2dd32015-10-27 16:22:37 -070082 flowClassifierStore = storageService
83 .<FlowClassifierId, FlowClassifier>eventuallyConsistentMapBuilder()
84 .withName("flowclassifierstore").withSerializer(serializer)
85 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
Bharat saraswal94358502015-12-11 00:58:15 +053086 flowClassifierStore.addListener(flowClassifierListener);
Bharat saraswal93aaf512015-10-26 18:10:07 +053087 log.info("Flow Classifier service activated");
88 }
89
90 @Deactivate
Mahesh Poojary Sea0775a2015-11-19 15:32:52 +053091 protected void deactivate() {
Bharat saraswal94358502015-12-11 00:58:15 +053092 eventDispatcher.removeSink(FlowClassifierEvent.class);
samueljccb3b2dd32015-10-27 16:22:37 -070093 flowClassifierStore.destroy();
Bharat saraswal93aaf512015-10-26 18:10:07 +053094 log.info("Flow Classifier service deactivated");
95 }
96
97 @Override
Mahesh Poojary Sfac02262015-11-20 19:13:22 +053098 public boolean exists(FlowClassifierId id) {
99 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
100 return flowClassifierStore.containsKey(id);
101 }
102
103 @Override
104 public int getFlowClassifierCount() {
105 return flowClassifierStore.size();
106 }
107
108 @Override
109 public Iterable<FlowClassifier> getFlowClassifiers() {
110 return ImmutableList.copyOf(flowClassifierStore.values());
111 }
112
113 @Override
114 public FlowClassifier getFlowClassifier(FlowClassifierId id) {
115 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
116 return flowClassifierStore.get(id);
117 }
118
119 @Override
Bharat saraswal93aaf512015-10-26 18:10:07 +0530120 public boolean createFlowClassifier(FlowClassifier flowClassifier) {
121 log.debug("createFlowClassifier");
122 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
123 FlowClassifierId id = flowClassifier.flowClassifierId();
124
125 flowClassifierStore.put(id, flowClassifier);
126 if (!flowClassifierStore.containsKey(id)) {
127 log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
128 return false;
129 }
130 return true;
131 }
132
133 @Override
Bharat saraswal93aaf512015-10-26 18:10:07 +0530134 public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
135 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530136
137 if (!flowClassifierStore.containsKey(flowClassifier.flowClassifierId())) {
138 log.debug("The flowClassifier is not exist whose identifier was {} ", flowClassifier.flowClassifierId()
139 .toString());
140 return false;
141 }
142
143 flowClassifierStore.put(flowClassifier.flowClassifierId(), flowClassifier);
144
145 if (!flowClassifier.equals(flowClassifierStore.get(flowClassifier.flowClassifierId()))) {
146 log.debug("Updation of flowClassifier is failed whose identifier was {} ", flowClassifier
147 .flowClassifierId().toString());
148 return false;
149 }
samueljccb3b2dd32015-10-27 16:22:37 -0700150 return true;
Bharat saraswal93aaf512015-10-26 18:10:07 +0530151 }
152
153 @Override
154 public boolean removeFlowClassifier(FlowClassifierId id) {
155 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
156 flowClassifierStore.remove(id);
157 if (flowClassifierStore.containsKey(id)) {
158 log.debug("The Flow Classifier removal is failed whose identifier is {}", id.toString());
159 return false;
160 }
161 return true;
162 }
Bharat saraswal94358502015-12-11 00:58:15 +0530163
164 private class InnerFlowClassifierStoreListener
165 implements
166 EventuallyConsistentMapListener<FlowClassifierId, FlowClassifier> {
167
168 @Override
169 public void event(EventuallyConsistentMapEvent<FlowClassifierId, FlowClassifier> event) {
170 checkNotNull(event, EVENT_NOT_NULL);
171 FlowClassifier flowClassifier = event.value();
172 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
173 notifyListeners(new FlowClassifierEvent(
174 FlowClassifierEvent.Type.FLOW_CLASSIFIER_PUT,
175 flowClassifier));
176 }
177 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
178 notifyListeners(new FlowClassifierEvent(
179 FlowClassifierEvent.Type.FLOW_CLASSIFIER_DELETE,
180 flowClassifier));
181 }
182 }
183 }
184
185 /**
186 * Notifies specify event to all listeners.
187 *
188 * @param event flow classifier event
189 */
190 private void notifyListeners(FlowClassifierEvent event) {
191 checkNotNull(event, EVENT_NOT_NULL);
192 post(event);
193 }
Jonathan Hart317f4762015-11-09 16:05:36 -0800194}