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