blob: 18f63b301f0a2d09d745b32adae972a389886829 [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;
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;
31import org.onosproject.store.service.MultiValuedTimestamp;
32import org.onosproject.store.service.StorageService;
33import org.onosproject.store.service.WallClockTimestamp;
Bharat saraswal93aaf512015-10-26 18:10:07 +053034import org.onosproject.vtnrsc.FlowClassifierId;
35import org.onosproject.vtnrsc.FlowClassifier;
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053036import org.onosproject.vtnrsc.flowclassifier.FlowClassifierEvent;
Mahesh Poojary Huaweia589d692015-11-29 15:42:19 +053037import org.onosproject.vtnrsc.flowclassifier.FlowClassifierListener;
Jonathan Hart317f4762015-11-09 16:05:36 -080038import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
Bharat saraswal93aaf512015-10-26 18:10:07 +053039import org.slf4j.Logger;
Bharat saraswal93aaf512015-10-26 18:10:07 +053040
Bharat saraswal93aaf512015-10-26 18:10:07 +053041import com.google.common.collect.ImmutableList;
42
43/**
44 * Provides implementation of the Flow Classifier Service.
45 */
46@Component(immediate = true)
47@Service
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053048public class FlowClassifierManager extends AbstractListenerManager<FlowClassifierEvent, FlowClassifierListener>
49 implements FlowClassifierService {
Bharat saraswal93aaf512015-10-26 18:10:07 +053050
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);
Mahesh Poojary Huaweiecf971d2015-12-05 14:25:53 +053056
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();
Bharat saraswal93aaf512015-10-26 18:10:07 +053078 log.info("Flow Classifier service deactivated");
79 }
80
81 @Override
Mahesh Poojary Sfac02262015-11-20 19:13:22 +053082 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
Bharat saraswal93aaf512015-10-26 18:10:07 +0530104 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
Bharat saraswal93aaf512015-10-26 18:10:07 +0530118 public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
119 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
Mahesh Poojary Sfac02262015-11-20 19:13:22 +0530120
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 }
samueljccb3b2dd32015-10-27 16:22:37 -0700134 return true;
Bharat saraswal93aaf512015-10-26 18:10:07 +0530135 }
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 }
Jonathan Hart317f4762015-11-09 16:05:36 -0800147}