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