Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +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 | */ |
| 16 | package org.onosproject.vtnrsc.portpair.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.util.Collections; |
| 22 | |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Component; |
| 25 | import org.apache.felix.scr.annotations.Deactivate; |
| 26 | import org.apache.felix.scr.annotations.Reference; |
| 27 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 28 | import org.apache.felix.scr.annotations.Service; |
| 29 | import org.onlab.util.KryoNamespace; |
Mahesh Poojary S | 28bfba7 | 2015-12-05 14:14:37 +0530 | [diff] [blame] | 30 | import org.onosproject.event.AbstractListenerManager; |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 31 | import org.onosproject.store.serializers.KryoNamespaces; |
| 32 | import org.onosproject.store.service.EventuallyConsistentMap; |
Bharat saraswal | 89f5f0d | 2015-12-11 01:01:37 +0530 | [diff] [blame] | 33 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 34 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 35 | import org.onosproject.store.service.MultiValuedTimestamp; |
| 36 | import org.onosproject.store.service.StorageService; |
| 37 | import org.onosproject.store.service.WallClockTimestamp; |
| 38 | import org.onosproject.vtnrsc.PortPair; |
| 39 | import org.onosproject.vtnrsc.PortPairId; |
Mahesh Poojary S | 28bfba7 | 2015-12-05 14:14:37 +0530 | [diff] [blame] | 40 | import org.onosproject.vtnrsc.portpair.PortPairEvent; |
Mahesh Poojary Huawei | 3fe5eff | 2015-11-29 15:13:18 +0530 | [diff] [blame] | 41 | import org.onosproject.vtnrsc.portpair.PortPairListener; |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 42 | import org.onosproject.vtnrsc.portpair.PortPairService; |
| 43 | import org.slf4j.Logger; |
| 44 | |
| 45 | /** |
| 46 | * Provides implementation of the portPairService. |
| 47 | */ |
| 48 | @Component(immediate = true) |
| 49 | @Service |
Mahesh Poojary S | 28bfba7 | 2015-12-05 14:14:37 +0530 | [diff] [blame] | 50 | public class PortPairManager extends AbstractListenerManager<PortPairEvent, PortPairListener> implements |
| 51 | PortPairService { |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 52 | |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 53 | private static final String PORT_PAIR_ID_NULL = "PortPair ID cannot be null"; |
| 54 | private static final String PORT_PAIR_NULL = "PortPair cannot be null"; |
Mahesh Poojary Huawei | 3fe5eff | 2015-11-29 15:13:18 +0530 | [diff] [blame] | 55 | private static final String LISTENER_NOT_NULL = "Listener cannot be null"; |
Bharat saraswal | 89f5f0d | 2015-12-11 01:01:37 +0530 | [diff] [blame] | 56 | private static final String EVENT_NOT_NULL = "event cannot be null"; |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 57 | |
Mahesh Poojary Huawei | 3fe5eff | 2015-11-29 15:13:18 +0530 | [diff] [blame] | 58 | private final Logger log = getLogger(getClass()); |
Mahesh Poojary S | 28bfba7 | 2015-12-05 14:14:37 +0530 | [diff] [blame] | 59 | |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 60 | private EventuallyConsistentMap<PortPairId, PortPair> portPairStore; |
| 61 | |
Bharat saraswal | 89f5f0d | 2015-12-11 01:01:37 +0530 | [diff] [blame] | 62 | private EventuallyConsistentMapListener<PortPairId, PortPair> portPairListener = |
| 63 | new InnerPortPairStoreListener(); |
| 64 | |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 65 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 66 | protected StorageService storageService; |
| 67 | |
| 68 | @Activate |
| 69 | public void activate() { |
| 70 | |
Bharat saraswal | 89f5f0d | 2015-12-11 01:01:37 +0530 | [diff] [blame] | 71 | eventDispatcher.addSink(PortPairEvent.class, listenerRegistry); |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 72 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 73 | .register(KryoNamespaces.API) |
| 74 | .register(MultiValuedTimestamp.class) |
| 75 | .register(PortPair.class); |
| 76 | |
Mahesh Poojary S | 28bfba7 | 2015-12-05 14:14:37 +0530 | [diff] [blame] | 77 | portPairStore = storageService.<PortPairId, PortPair>eventuallyConsistentMapBuilder() |
| 78 | .withName("portpairstore") |
| 79 | .withSerializer(serializer) |
| 80 | .withTimestampProvider((k, v) -> new WallClockTimestamp()) |
| 81 | .build(); |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 82 | |
Bharat saraswal | 89f5f0d | 2015-12-11 01:01:37 +0530 | [diff] [blame] | 83 | portPairStore.addListener(portPairListener); |
| 84 | |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 85 | log.info("Started"); |
| 86 | } |
| 87 | |
| 88 | @Deactivate |
| 89 | public void deactivate() { |
Bharat saraswal | 89f5f0d | 2015-12-11 01:01:37 +0530 | [diff] [blame] | 90 | eventDispatcher.removeSink(PortPairEvent.class); |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 91 | portPairStore.destroy(); |
| 92 | log.info("Stopped"); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean exists(PortPairId portPairId) { |
| 97 | checkNotNull(portPairId, PORT_PAIR_ID_NULL); |
| 98 | return portPairStore.containsKey(portPairId); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public int getPortPairCount() { |
| 103 | return portPairStore.size(); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public Iterable<PortPair> getPortPairs() { |
| 108 | return Collections.unmodifiableCollection(portPairStore.values()); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public PortPair getPortPair(PortPairId portPairId) { |
| 113 | checkNotNull(portPairId, PORT_PAIR_ID_NULL); |
| 114 | return portPairStore.get(portPairId); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean createPortPair(PortPair portPair) { |
| 119 | checkNotNull(portPair, PORT_PAIR_NULL); |
| 120 | |
| 121 | portPairStore.put(portPair.portPairId(), portPair); |
| 122 | if (!portPairStore.containsKey(portPair.portPairId())) { |
Mahesh Poojary S | 28bfba7 | 2015-12-05 14:14:37 +0530 | [diff] [blame] | 123 | log.debug("The portPair is created failed which identifier was {}", portPair.portPairId().toString()); |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public boolean updatePortPair(PortPair portPair) { |
| 131 | checkNotNull(portPair, PORT_PAIR_NULL); |
| 132 | |
| 133 | if (!portPairStore.containsKey(portPair.portPairId())) { |
Mahesh Poojary S | 28bfba7 | 2015-12-05 14:14:37 +0530 | [diff] [blame] | 134 | log.debug("The portPair is not exist whose identifier was {} ", portPair.portPairId().toString()); |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 135 | return false; |
| 136 | } |
| 137 | |
| 138 | portPairStore.put(portPair.portPairId(), portPair); |
| 139 | |
| 140 | if (!portPair.equals(portPairStore.get(portPair.portPairId()))) { |
Mahesh Poojary S | 28bfba7 | 2015-12-05 14:14:37 +0530 | [diff] [blame] | 141 | log.debug("The portPair is updated failed whose identifier was {} ", portPair.portPairId().toString()); |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 142 | return false; |
| 143 | } |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public boolean removePortPair(PortPairId portPairId) { |
| 149 | checkNotNull(portPairId, PORT_PAIR_NULL); |
| 150 | |
| 151 | portPairStore.remove(portPairId); |
| 152 | if (portPairStore.containsKey(portPairId)) { |
Mahesh Poojary S | 28bfba7 | 2015-12-05 14:14:37 +0530 | [diff] [blame] | 153 | log.debug("The portPair is removed failed whose identifier was {}", portPairId.toString()); |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 154 | return false; |
| 155 | } |
| 156 | return true; |
| 157 | } |
Bharat saraswal | 89f5f0d | 2015-12-11 01:01:37 +0530 | [diff] [blame] | 158 | |
| 159 | private class InnerPortPairStoreListener |
| 160 | implements |
| 161 | EventuallyConsistentMapListener<PortPairId, PortPair> { |
| 162 | |
| 163 | @Override |
| 164 | public void event(EventuallyConsistentMapEvent<PortPairId, PortPair> event) { |
| 165 | checkNotNull(event, EVENT_NOT_NULL); |
| 166 | PortPair portPair = event.value(); |
| 167 | if (EventuallyConsistentMapEvent.Type.PUT == event.type()) { |
| 168 | notifyListeners(new PortPairEvent( |
| 169 | PortPairEvent.Type.PORT_PAIR_PUT, |
| 170 | portPair)); |
| 171 | } |
| 172 | if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) { |
| 173 | notifyListeners(new PortPairEvent( |
| 174 | PortPairEvent.Type.PORT_PAIR_DELETE, |
| 175 | portPair)); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Notifies specify event to all listeners. |
| 182 | * |
| 183 | * @param event port pair event |
| 184 | */ |
| 185 | private void notifyListeners(PortPairEvent event) { |
| 186 | checkNotNull(event, EVENT_NOT_NULL); |
| 187 | post(event); |
| 188 | } |
Phaneendra Manda | 05db8b7 | 2015-10-28 17:15:38 +0530 | [diff] [blame] | 189 | } |