blob: ee5873d69c43e168e362f17e3b1bd1bd6b56f0b7 [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
Bharat saraswal93aaf512015-10-26 18:10:07 +053018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
samueljccb3b2dd32015-10-27 16:22:37 -070021import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Bharat saraswal93aaf512015-10-26 18:10:07 +053023import org.apache.felix.scr.annotations.Service;
samueljccb3b2dd32015-10-27 16:22:37 -070024import org.onlab.util.KryoNamespace;
25import org.onosproject.store.serializers.KryoNamespaces;
26import org.onosproject.store.service.EventuallyConsistentMap;
27import org.onosproject.store.service.MultiValuedTimestamp;
28import org.onosproject.store.service.StorageService;
29import org.onosproject.store.service.WallClockTimestamp;
Bharat saraswal93aaf512015-10-26 18:10:07 +053030import org.onosproject.vtnrsc.FlowClassifierId;
31import org.onosproject.vtnrsc.FlowClassifier;
Jonathan Hart317f4762015-11-09 16:05:36 -080032import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService;
Bharat saraswal93aaf512015-10-26 18:10:07 +053033import org.slf4j.Logger;
Bharat saraswal93aaf512015-10-26 18:10:07 +053034
samueljccb3b2dd32015-10-27 16:22:37 -070035import static org.slf4j.LoggerFactory.getLogger;
Bharat saraswal93aaf512015-10-26 18:10:07 +053036import static com.google.common.base.Preconditions.checkNotNull;
samueljccb3b2dd32015-10-27 16:22:37 -070037
Bharat saraswal93aaf512015-10-26 18:10:07 +053038import com.google.common.collect.ImmutableList;
39
40/**
41 * Provides implementation of the Flow Classifier Service.
42 */
43@Component(immediate = true)
44@Service
45public class FlowClassifierManager implements FlowClassifierService {
46
47 private final Logger log = getLogger(FlowClassifierManager.class);
48
49 private static final String FLOW_CLASSIFIER_NOT_NULL = "Flow Classifier cannot be null";
50 private static final String FLOW_CLASSIFIER_ID_NOT_NULL = "Flow Classifier Id cannot be null";
51
samueljccb3b2dd32015-10-27 16:22:37 -070052 private EventuallyConsistentMap<FlowClassifierId, FlowClassifier> flowClassifierStore;
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected StorageService storageService;
Bharat saraswal93aaf512015-10-26 18:10:07 +053055
56 @Activate
57 private void activate() {
samueljccb3b2dd32015-10-27 16:22:37 -070058 KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
59 .register(KryoNamespaces.API)
60 .register(MultiValuedTimestamp.class)
61 .register(FlowClassifier.class);
62 flowClassifierStore = storageService
63 .<FlowClassifierId, FlowClassifier>eventuallyConsistentMapBuilder()
64 .withName("flowclassifierstore").withSerializer(serializer)
65 .withTimestampProvider((k, v) -> new WallClockTimestamp()).build();
Bharat saraswal93aaf512015-10-26 18:10:07 +053066 log.info("Flow Classifier service activated");
67 }
68
69 @Deactivate
70 private void deactivate() {
samueljccb3b2dd32015-10-27 16:22:37 -070071 flowClassifierStore.destroy();
Bharat saraswal93aaf512015-10-26 18:10:07 +053072 log.info("Flow Classifier service deactivated");
73 }
74
75 @Override
76 public boolean createFlowClassifier(FlowClassifier flowClassifier) {
77 log.debug("createFlowClassifier");
78 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
79 FlowClassifierId id = flowClassifier.flowClassifierId();
80
81 flowClassifierStore.put(id, flowClassifier);
82 if (!flowClassifierStore.containsKey(id)) {
83 log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString());
84 return false;
85 }
86 return true;
87 }
88
89 @Override
90 public Iterable<FlowClassifier> getFlowClassifiers() {
91 return ImmutableList.copyOf(flowClassifierStore.values());
92 }
93
94 @Override
95 public boolean hasFlowClassifier(FlowClassifierId id) {
96 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
97 return flowClassifierStore.containsKey(id);
98 }
99
100 @Override
101 public FlowClassifier getFlowClassifier(FlowClassifierId id) {
102 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
103 return flowClassifierStore.get(id);
104 }
105
106 @Override
107 public boolean updateFlowClassifier(FlowClassifier flowClassifier) {
108 checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL);
109 FlowClassifierId id = flowClassifier.flowClassifierId();
samueljccb3b2dd32015-10-27 16:22:37 -0700110 flowClassifierStore.put(id, flowClassifier);
111 return true;
Bharat saraswal93aaf512015-10-26 18:10:07 +0530112 }
113
114 @Override
115 public boolean removeFlowClassifier(FlowClassifierId id) {
116 checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL);
117 flowClassifierStore.remove(id);
118 if (flowClassifierStore.containsKey(id)) {
119 log.debug("The Flow Classifier removal is failed whose identifier is {}", id.toString());
120 return false;
121 }
122 return true;
123 }
Jonathan Hart317f4762015-11-09 16:05:36 -0800124}