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