Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 1 | /* |
| 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 Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 16 | package org.onosproject.vtnrsc.flowclassifier.impl; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 17 | |
Mahesh Poojary Huawei | a589d69 | 2015-11-29 15:42:19 +0530 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Service; |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 27 | import org.onlab.util.KryoNamespace; |
Mahesh Poojary Huawei | ecf971d | 2015-12-05 14:25:53 +0530 | [diff] [blame^] | 28 | import org.onosproject.event.AbstractListenerManager; |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 29 | import org.onosproject.store.serializers.KryoNamespaces; |
| 30 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 31 | import org.onosproject.store.service.MultiValuedTimestamp; |
| 32 | import org.onosproject.store.service.StorageService; |
| 33 | import org.onosproject.store.service.WallClockTimestamp; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 34 | import org.onosproject.vtnrsc.FlowClassifierId; |
| 35 | import org.onosproject.vtnrsc.FlowClassifier; |
Mahesh Poojary Huawei | ecf971d | 2015-12-05 14:25:53 +0530 | [diff] [blame^] | 36 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierEvent; |
Mahesh Poojary Huawei | a589d69 | 2015-11-29 15:42:19 +0530 | [diff] [blame] | 37 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierListener; |
Jonathan Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 38 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 39 | import org.slf4j.Logger; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 40 | |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 41 | import com.google.common.collect.ImmutableList; |
| 42 | |
| 43 | /** |
| 44 | * Provides implementation of the Flow Classifier Service. |
| 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | @Service |
Mahesh Poojary Huawei | ecf971d | 2015-12-05 14:25:53 +0530 | [diff] [blame^] | 48 | public class FlowClassifierManager extends AbstractListenerManager<FlowClassifierEvent, FlowClassifierListener> |
| 49 | implements FlowClassifierService { |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 50 | |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 51 | 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 Huawei | a589d69 | 2015-11-29 15:42:19 +0530 | [diff] [blame] | 53 | private static final String LISTENER_NOT_NULL = "Listener cannot be null"; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 54 | |
Mahesh Poojary Huawei | a589d69 | 2015-11-29 15:42:19 +0530 | [diff] [blame] | 55 | private final Logger log = getLogger(FlowClassifierManager.class); |
Mahesh Poojary Huawei | ecf971d | 2015-12-05 14:25:53 +0530 | [diff] [blame^] | 56 | |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 57 | private EventuallyConsistentMap<FlowClassifierId, FlowClassifier> flowClassifierStore; |
Mahesh Poojary Huawei | a589d69 | 2015-11-29 15:42:19 +0530 | [diff] [blame] | 58 | |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected StorageService storageService; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 61 | |
| 62 | @Activate |
Mahesh Poojary S | ea0775a | 2015-11-19 15:32:52 +0530 | [diff] [blame] | 63 | protected void activate() { |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 64 | 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 saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 72 | log.info("Flow Classifier service activated"); |
| 73 | } |
| 74 | |
| 75 | @Deactivate |
Mahesh Poojary S | ea0775a | 2015-11-19 15:32:52 +0530 | [diff] [blame] | 76 | protected void deactivate() { |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 77 | flowClassifierStore.destroy(); |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 78 | log.info("Flow Classifier service deactivated"); |
| 79 | } |
| 80 | |
| 81 | @Override |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 82 | 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 saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 104 | 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 saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 118 | public boolean updateFlowClassifier(FlowClassifier flowClassifier) { |
| 119 | checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL); |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 120 | |
| 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 | } |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 134 | return true; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 135 | } |
| 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 Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 147 | } |