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