blob: 216a2c54cf2a30c6ec2082c24e5df9f317cb5100 [file] [log] [blame]
Bharat saraswal93aaf512015-10-26 18:10:07 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import com.google.common.collect.ImmutableList;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053019import org.onlab.packet.IpPrefix;
samueljccb3b2dd32015-10-27 16:22:37 -070020import org.onlab.util.KryoNamespace;
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053021import org.onosproject.event.AbstractListenerManager;
samueljccb3b2dd32015-10-27 16:22:37 -070022import org.onosproject.store.serializers.KryoNamespaces;
23import org.onosproject.store.service.EventuallyConsistentMap;
Bharat saraswal94358502015-12-11 00:58:15 +053024import org.onosproject.store.service.EventuallyConsistentMapEvent;
25import org.onosproject.store.service.EventuallyConsistentMapListener;
samueljccb3b2dd32015-10-27 16:22:37 -070026import org.onosproject.store.service.MultiValuedTimestamp;
27import org.onosproject.store.service.StorageService;
28import org.onosproject.store.service.WallClockTimestamp;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053029import org.onosproject.vtnrsc.DefaultFlowClassifier;
Bharat saraswal93aaf512015-10-26 18:10:07 +053030import org.onosproject.vtnrsc.FlowClassifier;
Phaneendra Manda8db7d092016-06-04 00:17:24 +053031import org.onosproject.vtnrsc.FlowClassifierId;
32import org.onosproject.vtnrsc.TenantId;
33import org.onosproject.vtnrsc.VirtualPortId;
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053034import org.onosproject.vtnrsc.flowclassifier.FlowClassifierEvent;
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053035import org.onosproject.vtnrsc.flowclassifier.FlowClassifierListener;
Jonathan Hart317f4762015-11-09 16:05:36 -080036import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037import org.osgi.service.component.annotations.Activate;
38import org.osgi.service.component.annotations.Component;
39import org.osgi.service.component.annotations.Deactivate;
40import org.osgi.service.component.annotations.Reference;
41import org.osgi.service.component.annotations.ReferenceCardinality;
Bharat saraswal93aaf512015-10-26 18:10:07 +053042import org.slf4j.Logger;
Bharat saraswal93aaf512015-10-26 18:10:07 +053043
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044import java.util.UUID;
45
46import static com.google.common.base.Preconditions.checkNotNull;
47import static org.slf4j.LoggerFactory.getLogger;
Bharat saraswal93aaf512015-10-26 18:10:07 +053048
49/**
50 * Provides implementation of the Flow Classifier Service.
51 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052@Component(immediate = true, service = FlowClassifierService.class)
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053053public class FlowClassifierManager extends AbstractListenerManager<FlowClassifierEvent, FlowClassifierListener>
54 implements FlowClassifierService {
Bharat saraswal93aaf512015-10-26 18:10:07 +053055
Bharat saraswal93aaf512015-10-26 18:10:07 +053056 private static final String FLOW_CLASSIFIER_NOT_NULL = "Flow Classifier cannot be null";
57 private static final String FLOW_CLASSIFIER_ID_NOT_NULL = "Flow Classifier Id cannot be null";
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053058 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
Bharat saraswal94358502015-12-11 00:58:15 +053059 private static final String EVENT_NOT_NULL = "event cannot be null";
Bharat saraswal93aaf512015-10-26 18:10:07 +053060
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053061 private final Logger log = getLogger(FlowClassifierManager.class);
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053062
samueljccb3b2dd32015-10-27 16:22:37 -070063 private EventuallyConsistentMap<FlowClassifierId, FlowClassifier> flowClassifierStore;
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053064
Bharat saraswal94358502015-12-11 00:58:15 +053065 private EventuallyConsistentMapListener<FlowClassifierId, FlowClassifier> flowClassifierListener =
66 new InnerFlowClassifierStoreListener();
67
Ray Milkeyd84f89b2018-08-17 14:54:17 -070068 @Reference(cardinality = ReferenceCardinality.MANDATORY)
samueljccb3b2dd32015-10-27 16:22:37 -070069 protected StorageService storageService;
Bharat saraswal93aaf512015-10-26 18:10:07 +053070
71 @Activate
Mahesh Poojary Sea0775a2015-11-19 15:32:52 +053072 protected void activate() {
Bharat saraswal94358502015-12-11 00:58:15 +053073 eventDispatcher.addSink(FlowClassifierEvent.class, listenerRegistry);
samueljccb3b2dd32015-10-27 16:22:37 -070074 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
75 .register(KryoNamespaces.API)
76 .register(MultiValuedTimestamp.class)
Phaneendra Manda8db7d092016-06-04 00:17:24 +053077 .register(FlowClassifier.class, FlowClassifierId.class, UUID.class, IpPrefix.class,
78 VirtualPortId.class, DefaultFlowClassifier.class, TenantId.class);
samueljccb3b2dd32015-10-27 16:22:37 -070079 flowClassifierStore = storageService
80 .<FlowClassifierId, FlowClassifier>eventuallyConsistentMapBuilder()
81 .withName("flowclassifierstore").withSerializer(serializer)
82 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
Bharat saraswal94358502015-12-11 00:58:15 +053083 flowClassifierStore.addListener(flowClassifierListener);
Bharat saraswal93aaf512015-10-26 18:10:07 +053084 log.info("Flow Classifier service activated");
85 }
86
87 @Deactivate
Mahesh Poojary Sea0775a2015-11-19 15:32:52 +053088 protected void deactivate() {
Bharat saraswal94358502015-12-11 00:58:15 +053089 eventDispatcher.removeSink(FlowClassifierEvent.class);
samueljccb3b2dd32015-10-27 16:22:37 -070090 flowClassifierStore.destroy();
Bharat saraswal93aaf512015-10-26 18:10:07 +053091 log.info("Flow Classifier service deactivated");
92 }
93
94 @Override
Mahesh Poojary Sfac02262015-11-20 19:13:22 +053095 public boolean exists(FlowClassifierId id) {
96 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
97 return flowClassifierStore.containsKey(id);
98 }
99
100 @Override
101 public int getFlowClassifierCount() {
102 return flowClassifierStore.size();
103 }
104
105 @Override
106 public Iterable<FlowClassifier> getFlowClassifiers() {
107 return ImmutableList.copyOf(flowClassifierStore.values());
108 }
109
110 @Override
111 public FlowClassifier getFlowClassifier(FlowClassifierId id) {
112 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
113 return flowClassifierStore.get(id);
114 }
115
116 @Override
Bharat saraswal93aaf512015-10-26 18:10:07 +0530117 public boolean createFlowClassifier(FlowClassifier flowClassifier) {
118 log.debug("createFlowClassifier");
119 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
120 FlowClassifierId id = flowClassifier.flowClassifierId();
121
122 flowClassifierStore.put(id, flowClassifier);
123 if (!flowClassifierStore.containsKey(id)) {
124 log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
125 return false;
126 }
127 return true;
128 }
129
130 @Override
Bharat saraswal93aaf512015-10-26 18:10:07 +0530131 public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
132 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530133
134 if (!flowClassifierStore.containsKey(flowClassifier.flowClassifierId())) {
135 log.debug("The flowClassifier is not exist whose identifier was {} ", flowClassifier.flowClassifierId()
136 .toString());
137 return false;
138 }
139
140 flowClassifierStore.put(flowClassifier.flowClassifierId(), flowClassifier);
141
142 if (!flowClassifier.equals(flowClassifierStore.get(flowClassifier.flowClassifierId()))) {
143 log.debug("Updation of flowClassifier is failed whose identifier was {} ", flowClassifier
144 .flowClassifierId().toString());
145 return false;
146 }
samueljccb3b2dd32015-10-27 16:22:37 -0700147 return true;
Bharat saraswal93aaf512015-10-26 18:10:07 +0530148 }
149
150 @Override
151 public boolean removeFlowClassifier(FlowClassifierId id) {
152 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
153 flowClassifierStore.remove(id);
154 if (flowClassifierStore.containsKey(id)) {
155 log.debug("The Flow Classifier removal is failed whose identifier is {}", id.toString());
156 return false;
157 }
158 return true;
159 }
Bharat saraswal94358502015-12-11 00:58:15 +0530160
161 private class InnerFlowClassifierStoreListener
162 implements
163 EventuallyConsistentMapListener<FlowClassifierId, FlowClassifier> {
164
165 @Override
166 public void event(EventuallyConsistentMapEvent<FlowClassifierId, FlowClassifier> event) {
167 checkNotNull(event, EVENT_NOT_NULL);
168 FlowClassifier flowClassifier = event.value();
169 if (EventuallyConsistentMapEvent.Type.PUT == event.type()) {
170 notifyListeners(new FlowClassifierEvent(
171 FlowClassifierEvent.Type.FLOW_CLASSIFIER_PUT,
172 flowClassifier));
173 }
174 if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) {
175 notifyListeners(new FlowClassifierEvent(
176 FlowClassifierEvent.Type.FLOW_CLASSIFIER_DELETE,
177 flowClassifier));
178 }
179 }
180 }
181
182 /**
183 * Notifies specify event to all listeners.
184 *
185 * @param event flow classifier event
186 */
187 private void notifyListeners(FlowClassifierEvent event) {
188 checkNotNull(event, EVENT_NOT_NULL);
189 post(event);
190 }
Jonathan Hart317f4762015-11-09 16:05:36 -0800191}