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