Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.onosproject.store.pi.impl; |
| 18 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 19 | import com.google.common.collect.HashMultimap; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Multimaps; |
| 22 | import com.google.common.collect.SetMultimap; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 25 | import org.onosproject.net.pi.service.PiPipeconfDeviceMappingEvent; |
| 26 | import org.onosproject.net.pi.service.PiPipeconfMappingStore; |
| 27 | import org.onosproject.net.pi.service.PiPipeconfMappingStoreDelegate; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 28 | import org.onosproject.store.AbstractStore; |
| 29 | import org.onosproject.store.serializers.KryoNamespaces; |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 30 | import org.onosproject.store.service.ConsistentMap; |
| 31 | import org.onosproject.store.service.MapEvent; |
| 32 | import org.onosproject.store.service.MapEventListener; |
| 33 | import org.onosproject.store.service.Serializer; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 34 | import org.onosproject.store.service.StorageService; |
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; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 40 | import org.slf4j.Logger; |
| 41 | |
| 42 | import java.util.Set; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 43 | |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 47 | * Manages information of pipeconf to device binding. |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 48 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 49 | @Component(immediate = true, service = PiPipeconfMappingStore.class) |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 50 | public class DistributedDevicePipeconfMappingStore |
| 51 | extends AbstractStore<PiPipeconfDeviceMappingEvent, PiPipeconfMappingStoreDelegate> |
| 52 | implements PiPipeconfMappingStore { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 57 | protected StorageService storageService; |
| 58 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 59 | protected ConsistentMap<DeviceId, PiPipeconfId> deviceToPipeconf; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 60 | |
Carmelo Cascone | 75a9a89 | 2019-04-22 12:12:23 -0700 | [diff] [blame^] | 61 | protected final MapEventListener<DeviceId, PiPipeconfId> mapListener = |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 62 | new InternalPiPipeconfListener(); |
| 63 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 64 | protected SetMultimap<PiPipeconfId, DeviceId> pipeconfToDevices = |
| 65 | Multimaps.synchronizedSetMultimap(HashMultimap.create()); |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 66 | |
| 67 | @Activate |
| 68 | public void activate() { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 69 | deviceToPipeconf = storageService.<DeviceId, PiPipeconfId>consistentMapBuilder() |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 70 | .withName("onos-pipeconf-table") |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 71 | .withSerializer(Serializer.using(KryoNamespaces.API)) |
| 72 | .build(); |
Carmelo Cascone | 75a9a89 | 2019-04-22 12:12:23 -0700 | [diff] [blame^] | 73 | deviceToPipeconf.addListener(mapListener); |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 74 | log.info("Started"); |
| 75 | } |
| 76 | |
| 77 | @Deactivate |
| 78 | public void deactivate() { |
Carmelo Cascone | 75a9a89 | 2019-04-22 12:12:23 -0700 | [diff] [blame^] | 79 | deviceToPipeconf.removeListener(mapListener); |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 80 | deviceToPipeconf = null; |
| 81 | pipeconfToDevices = null; |
| 82 | log.info("Stopped"); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public PiPipeconfId getPipeconfId(DeviceId deviceId) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 87 | if (!deviceToPipeconf.containsKey(deviceId)) { |
| 88 | return null; |
| 89 | } |
| 90 | return deviceToPipeconf.get(deviceId).value(); |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public Set<DeviceId> getDevices(PiPipeconfId pipeconfId) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 95 | return ImmutableSet.copyOf(pipeconfToDevices.get(pipeconfId)); |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public void createOrUpdateBinding(DeviceId deviceId, PiPipeconfId pipeconfId) { |
| 100 | deviceToPipeconf.put(deviceId, pipeconfId); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void removeBinding(DeviceId deviceId) { |
| 105 | deviceToPipeconf.remove(deviceId); |
| 106 | } |
| 107 | |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 108 | private class InternalPiPipeconfListener implements MapEventListener<DeviceId, PiPipeconfId> { |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 109 | |
| 110 | @Override |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 111 | public void event(MapEvent<DeviceId, PiPipeconfId> mapEvent) { |
| 112 | PiPipeconfDeviceMappingEvent.Type eventType = null; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 113 | final DeviceId deviceId = mapEvent.key(); |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 114 | final PiPipeconfId newPipeconfId = mapEvent.newValue() != null |
| 115 | ? mapEvent.newValue().value() : null; |
| 116 | final PiPipeconfId oldPipeconfId = mapEvent.oldValue() != null |
| 117 | ? mapEvent.oldValue().value() : null; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 118 | switch (mapEvent.type()) { |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 119 | case INSERT: |
| 120 | case UPDATE: |
| 121 | if (newPipeconfId != null) { |
| 122 | if (!newPipeconfId.equals(oldPipeconfId)) { |
| 123 | eventType = PiPipeconfDeviceMappingEvent.Type.CREATED; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 124 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 125 | pipeconfToDevices.put(newPipeconfId, deviceId); |
| 126 | } |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 127 | break; |
| 128 | case REMOVE: |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 129 | if (oldPipeconfId != null) { |
| 130 | eventType = PiPipeconfDeviceMappingEvent.Type.REMOVED; |
| 131 | pipeconfToDevices.remove(oldPipeconfId, deviceId); |
| 132 | } |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 133 | break; |
| 134 | default: |
| 135 | throw new IllegalArgumentException("Wrong event type " + mapEvent.type()); |
| 136 | } |
Carmelo Cascone | 9e4972c | 2018-08-30 00:29:16 -0700 | [diff] [blame] | 137 | if (eventType != null) { |
| 138 | notifyDelegate(new PiPipeconfDeviceMappingEvent(eventType, deviceId)); |
| 139 | } |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 140 | } |
| 141 | } |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 142 | } |