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