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