blob: b29dc7a3371089c55fe45adb4d65a22a7cbab2ea [file] [log] [blame]
Bharat saraswal93aaf512015-10-26 18:10:07 +05301/*
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 */
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;
28import org.onosproject.store.serializers.KryoNamespaces;
29import org.onosproject.store.service.EventuallyConsistentMap;
30import org.onosproject.store.service.MultiValuedTimestamp;
31import org.onosproject.store.service.StorageService;
32import org.onosproject.store.service.WallClockTimestamp;
Bharat saraswal93aaf512015-10-26 18:10:07 +053033import org.onosproject.vtnrsc.FlowClassifierId;
34import org.onosproject.vtnrsc.FlowClassifier;
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;
Bharat saraswal93aaf512015-10-26 18:10:07 +053037import org.slf4j.Logger;
Bharat saraswal93aaf512015-10-26 18:10:07 +053038
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053039import java.util.Set;
samueljccb3b2dd32015-10-27 16:22:37 -070040
Bharat saraswal93aaf512015-10-26 18:10:07 +053041import com.google.common.collect.ImmutableList;
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053042import com.google.common.collect.Sets;
Bharat saraswal93aaf512015-10-26 18:10:07 +053043
44/**
45 * Provides implementation of the Flow Classifier Service.
46 */
47@Component(immediate = true)
48@Service
49public class FlowClassifierManager implements FlowClassifierService {
50
Bharat saraswal93aaf512015-10-26 18:10:07 +053051 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";
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053053 private static final String LISTENER_NOT_NULL = "Listener cannot be null";
Bharat saraswal93aaf512015-10-26 18:10:07 +053054
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053055 private final Logger log = getLogger(FlowClassifierManager.class);
56 private final Set<FlowClassifierListener> listeners = Sets.newCopyOnWriteArraySet();
samueljccb3b2dd32015-10-27 16:22:37 -070057 private EventuallyConsistentMap<FlowClassifierId, FlowClassifier> flowClassifierStore;
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053058
samueljccb3b2dd32015-10-27 16:22:37 -070059 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected StorageService storageService;
Bharat saraswal93aaf512015-10-26 18:10:07 +053061
62 @Activate
Mahesh Poojary Sea0775a2015-11-19 15:32:52 +053063 protected void activate() {
samueljccb3b2dd32015-10-27 16:22:37 -070064 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();
Bharat saraswal93aaf512015-10-26 18:10:07 +053072 log.info("Flow Classifier service activated");
73 }
74
75 @Deactivate
Mahesh Poojary Sea0775a2015-11-19 15:32:52 +053076 protected void deactivate() {
samueljccb3b2dd32015-10-27 16:22:37 -070077 flowClassifierStore.destroy();
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053078 listeners.clear();
Bharat saraswal93aaf512015-10-26 18:10:07 +053079 log.info("Flow Classifier service deactivated");
80 }
81
82 @Override
Mahesh Poojary Sfac02262015-11-20 19:13:22 +053083 public boolean exists(FlowClassifierId id) {
84 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
85 return flowClassifierStore.containsKey(id);
86 }
87
88 @Override
89 public int getFlowClassifierCount() {
90 return flowClassifierStore.size();
91 }
92
93 @Override
94 public Iterable<FlowClassifier> getFlowClassifiers() {
95 return ImmutableList.copyOf(flowClassifierStore.values());
96 }
97
98 @Override
99 public FlowClassifier getFlowClassifier(FlowClassifierId id) {
100 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
101 return flowClassifierStore.get(id);
102 }
103
104 @Override
Bharat saraswal93aaf512015-10-26 18:10:07 +0530105 public boolean createFlowClassifier(FlowClassifier flowClassifier) {
106 log.debug("createFlowClassifier");
107 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
108 FlowClassifierId id = flowClassifier.flowClassifierId();
109
110 flowClassifierStore.put(id, flowClassifier);
111 if (!flowClassifierStore.containsKey(id)) {
112 log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
113 return false;
114 }
115 return true;
116 }
117
118 @Override
Bharat saraswal93aaf512015-10-26 18:10:07 +0530119 public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
120 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530121
122 if (!flowClassifierStore.containsKey(flowClassifier.flowClassifierId())) {
123 log.debug("The flowClassifier is not exist whose identifier was {} ", flowClassifier.flowClassifierId()
124 .toString());
125 return false;
126 }
127
128 flowClassifierStore.put(flowClassifier.flowClassifierId(), flowClassifier);
129
130 if (!flowClassifier.equals(flowClassifierStore.get(flowClassifier.flowClassifierId()))) {
131 log.debug("Updation of flowClassifier is failed whose identifier was {} ", flowClassifier
132 .flowClassifierId().toString());
133 return false;
134 }
samueljccb3b2dd32015-10-27 16:22:37 -0700135 return true;
Bharat saraswal93aaf512015-10-26 18:10:07 +0530136 }
137
138 @Override
139 public boolean removeFlowClassifier(FlowClassifierId id) {
140 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
141 flowClassifierStore.remove(id);
142 if (flowClassifierStore.containsKey(id)) {
143 log.debug("The Flow Classifier removal is failed whose identifier is {}", id.toString());
144 return false;
145 }
146 return true;
147 }
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +0530148
149 @Override
150 public void addListener(FlowClassifierListener listener) {
151 checkNotNull(listener, LISTENER_NOT_NULL);
152 listeners.add(listener);
153 }
154
155 @Override
156 public void removeListener(FlowClassifierListener listener) {
157 checkNotNull(listener, LISTENER_NOT_NULL);
158 listeners.remove(listener);
159 }
Jonathan Hart317f4762015-11-09 16:05:36 -0800160}