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 | |
| 19 | import com.google.common.collect.Sets; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onlab.util.KryoNamespace; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.pi.model.PiPipeconfId; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 29 | import org.onosproject.net.pi.service.PiPipeconfDeviceMappingEvent; |
| 30 | import org.onosproject.net.pi.service.PiPipeconfMappingStore; |
| 31 | import org.onosproject.net.pi.service.PiPipeconfMappingStoreDelegate; |
Andrea Campanella | f9c409a | 2017-07-13 14:14:41 +0200 | [diff] [blame] | 32 | import org.onosproject.store.AbstractStore; |
| 33 | import org.onosproject.store.serializers.KryoNamespaces; |
| 34 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 35 | import org.onosproject.store.service.EventuallyConsistentMapEvent; |
| 36 | import org.onosproject.store.service.EventuallyConsistentMapListener; |
| 37 | import org.onosproject.store.service.MultiValuedTimestamp; |
| 38 | import org.onosproject.store.service.StorageService; |
| 39 | import org.onosproject.store.service.WallClockTimestamp; |
| 40 | import org.slf4j.Logger; |
| 41 | |
| 42 | import java.util.Set; |
| 43 | import java.util.concurrent.ConcurrentHashMap; |
| 44 | import java.util.concurrent.ConcurrentMap; |
| 45 | |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | /** |
| 49 | * Manages information of pipeconf to device binding using gossip protocol to distribute |
| 50 | * information. |
| 51 | */ |
| 52 | @Component(immediate = true) |
| 53 | @Service |
| 54 | public class DistributedDevicePipeconfMappingStore |
| 55 | extends AbstractStore<PiPipeconfDeviceMappingEvent, PiPipeconfMappingStoreDelegate> |
| 56 | implements PiPipeconfMappingStore { |
| 57 | |
| 58 | private final Logger log = getLogger(getClass()); |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected StorageService storageService; |
| 62 | |
| 63 | protected EventuallyConsistentMap<DeviceId, PiPipeconfId> deviceToPipeconf; |
| 64 | |
| 65 | protected final EventuallyConsistentMapListener<DeviceId, PiPipeconfId> pipeconfListener = |
| 66 | new InternalPiPipeconfListener(); |
| 67 | |
| 68 | protected ConcurrentMap<PiPipeconfId, Set<DeviceId>> pipeconfToDevices = new ConcurrentHashMap<>(); |
| 69 | |
| 70 | @Activate |
| 71 | public void activate() { |
| 72 | KryoNamespace.Builder serializer = KryoNamespace.newBuilder() |
| 73 | .register(KryoNamespaces.API) |
| 74 | .register(MultiValuedTimestamp.class); |
| 75 | deviceToPipeconf = storageService.<DeviceId, PiPipeconfId>eventuallyConsistentMapBuilder() |
| 76 | .withName("onos-pipeconf-table") |
| 77 | .withSerializer(serializer) |
| 78 | .withTimestampProvider((k, v) -> new WallClockTimestamp()).build(); |
| 79 | deviceToPipeconf.addListener(pipeconfListener); |
| 80 | log.info("Started"); |
| 81 | } |
| 82 | |
| 83 | @Deactivate |
| 84 | public void deactivate() { |
| 85 | deviceToPipeconf.removeListener(pipeconfListener); |
| 86 | deviceToPipeconf = null; |
| 87 | pipeconfToDevices = null; |
| 88 | log.info("Stopped"); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public PiPipeconfId getPipeconfId(DeviceId deviceId) { |
| 93 | return deviceToPipeconf.get(deviceId); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public Set<DeviceId> getDevices(PiPipeconfId pipeconfId) { |
| 98 | return pipeconfToDevices.get(pipeconfId); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void createOrUpdateBinding(DeviceId deviceId, PiPipeconfId pipeconfId) { |
| 103 | deviceToPipeconf.put(deviceId, pipeconfId); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void removeBinding(DeviceId deviceId) { |
| 108 | deviceToPipeconf.remove(deviceId); |
| 109 | } |
| 110 | |
| 111 | private class InternalPiPipeconfListener implements EventuallyConsistentMapListener<DeviceId, PiPipeconfId> { |
| 112 | |
| 113 | @Override |
| 114 | public void event(EventuallyConsistentMapEvent<DeviceId, PiPipeconfId> mapEvent) { |
| 115 | final PiPipeconfDeviceMappingEvent.Type type; |
| 116 | final DeviceId deviceId = mapEvent.key(); |
| 117 | final PiPipeconfId pipeconfId = mapEvent.value(); |
| 118 | switch (mapEvent.type()) { |
| 119 | case PUT: |
| 120 | type = PiPipeconfDeviceMappingEvent.Type.CREATED; |
| 121 | pipeconfToDevices.compute(pipeconfId, (pipeconf, devices) -> { |
| 122 | if (devices == null) { |
| 123 | devices = Sets.newConcurrentHashSet(); |
| 124 | } |
| 125 | devices.add(deviceId); |
| 126 | return devices; |
| 127 | }); |
| 128 | break; |
| 129 | case REMOVE: |
| 130 | type = PiPipeconfDeviceMappingEvent.Type.REMOVED; |
| 131 | pipeconfToDevices.computeIfPresent(pipeconfId, (pipeconf, devices) -> { |
| 132 | devices.remove(deviceId); |
| 133 | return devices; |
| 134 | }); |
| 135 | break; |
| 136 | default: |
| 137 | throw new IllegalArgumentException("Wrong event type " + mapEvent.type()); |
| 138 | } |
| 139 | notifyDelegate(new PiPipeconfDeviceMappingEvent(type, deviceId)); |
| 140 | } |
| 141 | } |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 142 | } |