Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 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; |
Bharat saraswal | 9435850 | 2015-12-11 00:58:15 +0530 | [diff] [blame] | 31 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 32 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 33 | import org.onosproject.store.service.MultiValuedTimestamp; |
| 34 | import org.onosproject.store.service.StorageService; |
| 35 | import org.onosproject.store.service.WallClockTimestamp; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 36 | import org.onosproject.vtnrsc.FlowClassifierId; |
| 37 | import org.onosproject.vtnrsc.FlowClassifier; |
Mahesh Poojary Huawei | ecf971d | 2015-12-05 14:25:53 +0530 | [diff] [blame] | 38 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierEvent; |
Mahesh Poojary Huawei | a589d69 | 2015-11-29 15:42:19 +0530 | [diff] [blame] | 39 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierListener; |
Jonathan Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 40 | import org.onosproject.vtnrsc.flowclassifier.FlowClassifierService; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 41 | import org.slf4j.Logger; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 42 | |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 43 | import com.google.common.collect.ImmutableList; |
| 44 | |
| 45 | /** |
| 46 | * Provides implementation of the Flow Classifier Service. |
| 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | @Service |
Mahesh Poojary Huawei | ecf971d | 2015-12-05 14:25:53 +0530 | [diff] [blame] | 50 | public class FlowClassifierManager extends AbstractListenerManager<FlowClassifierEvent, FlowClassifierListener> |
| 51 | implements FlowClassifierService { |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 52 | |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 53 | private static final String FLOW_CLASSIFIER_NOT_NULL = "Flow Classifier cannot be null"; |
| 54 | 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] | 55 | private static final String LISTENER_NOT_NULL = "Listener cannot be null"; |
Bharat saraswal | 9435850 | 2015-12-11 00:58:15 +0530 | [diff] [blame] | 56 | private static final String EVENT_NOT_NULL = "event cannot be null"; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 57 | |
Mahesh Poojary Huawei | a589d69 | 2015-11-29 15:42:19 +0530 | [diff] [blame] | 58 | private final Logger log = getLogger(FlowClassifierManager.class); |
Mahesh Poojary Huawei | ecf971d | 2015-12-05 14:25:53 +0530 | [diff] [blame] | 59 | |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 60 | private EventuallyConsistentMap<FlowClassifierId, FlowClassifier> flowClassifierStore; |
Mahesh Poojary Huawei | a589d69 | 2015-11-29 15:42:19 +0530 | [diff] [blame] | 61 | |
Bharat saraswal | 9435850 | 2015-12-11 00:58:15 +0530 | [diff] [blame] | 62 | private EventuallyConsistentMapListener<FlowClassifierId, FlowClassifier> flowClassifierListener = |
| 63 | new InnerFlowClassifierStoreListener(); |
| 64 | |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected StorageService storageService; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 67 | |
| 68 | @Activate |
Mahesh Poojary S | ea0775a | 2015-11-19 15:32:52 +0530 | [diff] [blame] | 69 | protected void activate() { |
Bharat saraswal | 9435850 | 2015-12-11 00:58:15 +0530 | [diff] [blame] | 70 | eventDispatcher.addSink(FlowClassifierEvent.class, listenerRegistry); |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 71 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 72 | .register(KryoNamespaces.API) |
| 73 | .register(MultiValuedTimestamp.class) |
| 74 | .register(FlowClassifier.class); |
| 75 | flowClassifierStore = storageService |
| 76 | .<FlowClassifierId, FlowClassifier>eventuallyConsistentMapBuilder() |
| 77 | .withName("flowclassifierstore").withSerializer(serializer) |
| 78 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
Bharat saraswal | 9435850 | 2015-12-11 00:58:15 +0530 | [diff] [blame] | 79 | flowClassifierStore.addListener(flowClassifierListener); |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 80 | log.info("Flow Classifier service activated"); |
| 81 | } |
| 82 | |
| 83 | @Deactivate |
Mahesh Poojary S | ea0775a | 2015-11-19 15:32:52 +0530 | [diff] [blame] | 84 | protected void deactivate() { |
Bharat saraswal | 9435850 | 2015-12-11 00:58:15 +0530 | [diff] [blame] | 85 | eventDispatcher.removeSink(FlowClassifierEvent.class); |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 86 | flowClassifierStore.destroy(); |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 87 | log.info("Flow Classifier service deactivated"); |
| 88 | } |
| 89 | |
| 90 | @Override |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 91 | public boolean exists(FlowClassifierId id) { |
| 92 | checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL); |
| 93 | return flowClassifierStore.containsKey(id); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public int getFlowClassifierCount() { |
| 98 | return flowClassifierStore.size(); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Iterable<FlowClassifier> getFlowClassifiers() { |
| 103 | return ImmutableList.copyOf(flowClassifierStore.values()); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public FlowClassifier getFlowClassifier(FlowClassifierId id) { |
| 108 | checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL); |
| 109 | return flowClassifierStore.get(id); |
| 110 | } |
| 111 | |
| 112 | @Override |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 113 | public boolean createFlowClassifier(FlowClassifier flowClassifier) { |
| 114 | log.debug("createFlowClassifier"); |
| 115 | checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL); |
| 116 | FlowClassifierId id = flowClassifier.flowClassifierId(); |
| 117 | |
| 118 | flowClassifierStore.put(id, flowClassifier); |
| 119 | if (!flowClassifierStore.containsKey(id)) { |
| 120 | log.debug("Flow Classifier creation is failed whose identifier is {}.", id.toString()); |
| 121 | return false; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | @Override |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 127 | public boolean updateFlowClassifier(FlowClassifier flowClassifier) { |
| 128 | checkNotNull(flowClassifier, FLOW_CLASSIFIER_NOT_NULL); |
Mahesh Poojary S | fac0226 | 2015-11-20 19:13:22 +0530 | [diff] [blame] | 129 | |
| 130 | if (!flowClassifierStore.containsKey(flowClassifier.flowClassifierId())) { |
| 131 | log.debug("The flowClassifier is not exist whose identifier was {} ", flowClassifier.flowClassifierId() |
| 132 | .toString()); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | flowClassifierStore.put(flowClassifier.flowClassifierId(), flowClassifier); |
| 137 | |
| 138 | if (!flowClassifier.equals(flowClassifierStore.get(flowClassifier.flowClassifierId()))) { |
| 139 | log.debug("Updation of flowClassifier is failed whose identifier was {} ", flowClassifier |
| 140 | .flowClassifierId().toString()); |
| 141 | return false; |
| 142 | } |
samueljcc | b3b2dd3 | 2015-10-27 16:22:37 -0700 | [diff] [blame] | 143 | return true; |
Bharat saraswal | 93aaf51 | 2015-10-26 18:10:07 +0530 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public boolean removeFlowClassifier(FlowClassifierId id) { |
| 148 | checkNotNull(id, FLOW_CLASSIFIER_ID_NOT_NULL); |
| 149 | flowClassifierStore.remove(id); |
| 150 | if (flowClassifierStore.containsKey(id)) { |
| 151 | log.debug("The Flow Classifier removal is failed whose identifier is {}", id.toString()); |
| 152 | return false; |
| 153 | } |
| 154 | return true; |
| 155 | } |
Bharat saraswal | 9435850 | 2015-12-11 00:58:15 +0530 | [diff] [blame] | 156 | |
| 157 | private class InnerFlowClassifierStoreListener |
| 158 | implements |
| 159 | EventuallyConsistentMapListener<FlowClassifierId, FlowClassifier> { |
| 160 | |
| 161 | @Override |
| 162 | public void event(EventuallyConsistentMapEvent<FlowClassifierId, FlowClassifier> event) { |
| 163 | checkNotNull(event, EVENT_NOT_NULL); |
| 164 | FlowClassifier flowClassifier = event.value(); |
| 165 | if (EventuallyConsistentMapEvent.Type.PUT == event.type()) { |
| 166 | notifyListeners(new FlowClassifierEvent( |
| 167 | FlowClassifierEvent.Type.FLOW_CLASSIFIER_PUT, |
| 168 | flowClassifier)); |
| 169 | } |
| 170 | if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) { |
| 171 | notifyListeners(new FlowClassifierEvent( |
| 172 | FlowClassifierEvent.Type.FLOW_CLASSIFIER_DELETE, |
| 173 | flowClassifier)); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Notifies specify event to all listeners. |
| 180 | * |
| 181 | * @param event flow classifier event |
| 182 | */ |
| 183 | private void notifyListeners(FlowClassifierEvent event) { |
| 184 | checkNotNull(event, EVENT_NOT_NULL); |
| 185 | post(event); |
| 186 | } |
Jonathan Hart | 317f476 | 2015-11-09 16:05:36 -0800 | [diff] [blame] | 187 | } |