Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +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 | f5c14e6 | 2015-10-29 19:29:52 +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.portchain.impl; |
| 17 | |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 18 | import org.onlab.util.KryoNamespace; |
Mahesh Poojary S | a1075a9 | 2015-11-29 15:53:56 +0530 | [diff] [blame] | 19 | import org.onosproject.event.AbstractListenerManager; |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 20 | import org.onosproject.net.DeviceId; |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 21 | import org.onosproject.store.serializers.KryoNamespaces; |
| 22 | import org.onosproject.store.service.EventuallyConsistentMap; |
Bharat saraswal | a3f51b5 | 2015-12-11 01:09:02 +0530 | [diff] [blame] | 23 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 24 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 25 | import org.onosproject.store.service.MultiValuedTimestamp; |
| 26 | import org.onosproject.store.service.StorageService; |
| 27 | import org.onosproject.store.service.WallClockTimestamp; |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 28 | import org.onosproject.vtnrsc.DefaultPortChain; |
| 29 | import org.onosproject.vtnrsc.FiveTuple; |
| 30 | import org.onosproject.vtnrsc.FlowClassifierId; |
| 31 | import org.onosproject.vtnrsc.LoadBalanceId; |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 32 | import org.onosproject.vtnrsc.PortChain; |
| 33 | import org.onosproject.vtnrsc.PortChainId; |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 34 | import org.onosproject.vtnrsc.PortPairGroupId; |
| 35 | import org.onosproject.vtnrsc.PortPairId; |
| 36 | import org.onosproject.vtnrsc.TenantId; |
Mahesh Poojary S | a1075a9 | 2015-11-29 15:53:56 +0530 | [diff] [blame] | 37 | import org.onosproject.vtnrsc.portchain.PortChainEvent; |
| 38 | import org.onosproject.vtnrsc.portchain.PortChainListener; |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 39 | import org.onosproject.vtnrsc.portchain.PortChainService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | import org.osgi.service.component.annotations.Activate; |
| 41 | import org.osgi.service.component.annotations.Component; |
| 42 | import org.osgi.service.component.annotations.Deactivate; |
| 43 | import org.osgi.service.component.annotations.Reference; |
| 44 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 47 | import java.util.Collections; |
| 48 | import java.util.UUID; |
| 49 | |
| 50 | import static com.google.common.base.Preconditions.checkNotNull; |
| 51 | import static org.slf4j.LoggerFactory.getLogger; |
| 52 | |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 53 | /** |
| 54 | * Provides implementation of the portChainService. |
| 55 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 56 | @Component(immediate = true, service = PortChainService.class) |
Mahesh Poojary S | a1075a9 | 2015-11-29 15:53:56 +0530 | [diff] [blame] | 57 | public class PortChainManager extends AbstractListenerManager<PortChainEvent, PortChainListener> implements |
| 58 | PortChainService { |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 59 | |
| 60 | private static final String PORT_CHAIN_ID_NULL = "PortChain ID cannot be null"; |
| 61 | private static final String PORT_CHAIN_NULL = "PortChain cannot be null"; |
Bharat saraswal | a3f51b5 | 2015-12-11 01:09:02 +0530 | [diff] [blame] | 62 | private static final String EVENT_NOT_NULL = "event cannot be null"; |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 63 | |
Mahesh Poojary S | a1075a9 | 2015-11-29 15:53:56 +0530 | [diff] [blame] | 64 | private final Logger log = getLogger(getClass()); |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 65 | private EventuallyConsistentMap<PortChainId, PortChain> portChainStore; |
| 66 | |
Bharat saraswal | a3f51b5 | 2015-12-11 01:09:02 +0530 | [diff] [blame] | 67 | private EventuallyConsistentMapListener<PortChainId, PortChain> portChainListener = |
| 68 | new InnerPortChainStoreListener(); |
| 69 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 71 | protected StorageService storageService; |
| 72 | |
| 73 | @Activate |
| 74 | public void activate() { |
| 75 | |
Bharat saraswal | a3f51b5 | 2015-12-11 01:09:02 +0530 | [diff] [blame] | 76 | eventDispatcher.addSink(PortChainEvent.class, listenerRegistry); |
| 77 | |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 78 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 79 | .register(KryoNamespaces.API) |
| 80 | .register(MultiValuedTimestamp.class) |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 81 | .register(PortChain.class, PortChainId.class, UUID.class, PortPairGroupId.class, |
| 82 | FlowClassifierId.class, FiveTuple.class, LoadBalanceId.class, DeviceId.class, |
| 83 | DefaultPortChain.class, PortPairId.class, TenantId.class); |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 84 | |
| 85 | portChainStore = storageService |
| 86 | .<PortChainId, PortChain>eventuallyConsistentMapBuilder() |
| 87 | .withName("portchainstore").withSerializer(serializer) |
| 88 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
| 89 | |
Bharat saraswal | a3f51b5 | 2015-12-11 01:09:02 +0530 | [diff] [blame] | 90 | portChainStore.addListener(portChainListener); |
| 91 | |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 92 | log.info("Started"); |
| 93 | } |
| 94 | |
| 95 | @Deactivate |
| 96 | public void deactivate() { |
Bharat saraswal | a3f51b5 | 2015-12-11 01:09:02 +0530 | [diff] [blame] | 97 | eventDispatcher.removeSink(PortChainEvent.class); |
Phaneendra Manda | b212bc9 | 2016-07-08 16:50:11 +0530 | [diff] [blame] | 98 | portChainStore.removeListener(portChainListener); |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 99 | portChainStore.destroy(); |
| 100 | log.info("Stopped"); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public boolean exists(PortChainId portChainId) { |
| 105 | checkNotNull(portChainId, PORT_CHAIN_ID_NULL); |
| 106 | return portChainStore.containsKey(portChainId); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public int getPortChainCount() { |
| 111 | return portChainStore.size(); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public Iterable<PortChain> getPortChains() { |
| 116 | return Collections.unmodifiableCollection(portChainStore.values()); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public PortChain getPortChain(PortChainId portChainId) { |
| 121 | checkNotNull(portChainId, PORT_CHAIN_ID_NULL); |
| 122 | return portChainStore.get(portChainId); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean createPortChain(PortChain portChain) { |
| 127 | checkNotNull(portChain, PORT_CHAIN_NULL); |
| 128 | |
| 129 | portChainStore.put(portChain.portChainId(), portChain); |
| 130 | if (!portChainStore.containsKey(portChain.portChainId())) { |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 131 | log.error("The portChain created is failed which identifier was {}", portChain.portChainId() |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 132 | .toString()); |
| 133 | return false; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public boolean updatePortChain(PortChain portChain) { |
| 140 | checkNotNull(portChain, PORT_CHAIN_NULL); |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 141 | PortChain oldPortChain = null; |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 142 | if (!portChainStore.containsKey(portChain.portChainId())) { |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 143 | log.warn("The portChain is not exist whose identifier was {} ", |
| 144 | portChain.portChainId().toString()); |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 145 | return false; |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 146 | } else { |
| 147 | oldPortChain = portChainStore.get(portChain.portChainId()); |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 148 | } |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 149 | PortChain newPortChain = DefaultPortChain.create(portChain, oldPortChain); |
| 150 | portChainStore.put(newPortChain.portChainId(), newPortChain); |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 151 | |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 152 | if (!newPortChain.equals(portChainStore.get(newPortChain.portChainId()))) { |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 153 | log.debug("The portChain is updated failed whose identifier was {} ", |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 154 | newPortChain.portChainId().toString()); |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public boolean removePortChain(PortChainId portChainId) { |
| 162 | checkNotNull(portChainId, PORT_CHAIN_NULL); |
| 163 | |
| 164 | portChainStore.remove(portChainId); |
| 165 | if (portChainStore.containsKey(portChainId)) { |
| 166 | log.debug("The portChain is removed failed whose identifier was {}", |
| 167 | portChainId.toString()); |
| 168 | return false; |
| 169 | } |
| 170 | return true; |
| 171 | } |
Bharat saraswal | a3f51b5 | 2015-12-11 01:09:02 +0530 | [diff] [blame] | 172 | |
| 173 | private class InnerPortChainStoreListener |
| 174 | implements |
| 175 | EventuallyConsistentMapListener<PortChainId, PortChain> { |
| 176 | |
| 177 | @Override |
| 178 | public void event(EventuallyConsistentMapEvent<PortChainId, PortChain> event) { |
| 179 | checkNotNull(event, EVENT_NOT_NULL); |
| 180 | PortChain portChain = event.value(); |
| 181 | if (EventuallyConsistentMapEvent.Type.PUT == event.type()) { |
| 182 | notifyListeners(new PortChainEvent( |
| 183 | PortChainEvent.Type.PORT_CHAIN_PUT, |
| 184 | portChain)); |
| 185 | } |
| 186 | if (EventuallyConsistentMapEvent.Type.REMOVE == event.type()) { |
| 187 | notifyListeners(new PortChainEvent( |
| 188 | PortChainEvent.Type.PORT_CHAIN_DELETE, |
| 189 | portChain)); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Notifies specify event to all listeners. |
| 196 | * |
| 197 | * @param event port chain event |
| 198 | */ |
| 199 | private void notifyListeners(PortChainEvent event) { |
| 200 | checkNotNull(event, EVENT_NOT_NULL); |
| 201 | post(event); |
| 202 | } |
Phaneendra Manda | f5c14e6 | 2015-10-29 19:29:52 +0530 | [diff] [blame] | 203 | } |