blob: 092b8ebf066c63080c136d1bafcd3e76df61a225 [file] [log] [blame]
Andrea Campanellaf9c409a2017-07-13 14:14:41 +02001/*
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
17package org.onosproject.store.pi.impl;
18
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070019import com.google.common.collect.HashMultimap;
20import com.google.common.collect.ImmutableSet;
21import com.google.common.collect.Multimaps;
22import com.google.common.collect.SetMultimap;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020023import org.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Reference;
27import org.apache.felix.scr.annotations.ReferenceCardinality;
28import org.apache.felix.scr.annotations.Service;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020029import org.onosproject.net.DeviceId;
30import org.onosproject.net.pi.model.PiPipeconfId;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080031import org.onosproject.net.pi.service.PiPipeconfDeviceMappingEvent;
32import org.onosproject.net.pi.service.PiPipeconfMappingStore;
33import org.onosproject.net.pi.service.PiPipeconfMappingStoreDelegate;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020034import org.onosproject.store.AbstractStore;
35import org.onosproject.store.serializers.KryoNamespaces;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070036import org.onosproject.store.service.ConsistentMap;
37import org.onosproject.store.service.MapEvent;
38import org.onosproject.store.service.MapEventListener;
39import org.onosproject.store.service.Serializer;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020040import org.onosproject.store.service.StorageService;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020041import org.slf4j.Logger;
42
43import java.util.Set;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020044
45import static org.slf4j.LoggerFactory.getLogger;
46
47/**
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070048 * Manages information of pipeconf to device binding.
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020049 */
50@Component(immediate = true)
51@Service
52public 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 Cascone9e4972c2018-08-30 00:29:16 -070061 protected ConsistentMap<DeviceId, PiPipeconfId> deviceToPipeconf;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020062
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070063 protected final MapEventListener<DeviceId, PiPipeconfId> pipeconfListener =
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020064 new InternalPiPipeconfListener();
65
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070066 protected SetMultimap<PiPipeconfId, DeviceId> pipeconfToDevices =
67 Multimaps.synchronizedSetMultimap(HashMultimap.create());
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020068
69 @Activate
70 public void activate() {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070071 deviceToPipeconf = storageService.<DeviceId, PiPipeconfId>consistentMapBuilder()
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020072 .withName("onos-pipeconf-table")
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070073 .withSerializer(Serializer.using(KryoNamespaces.API))
74 .build();
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020075 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 Cascone9e4972c2018-08-30 00:29:16 -070089 if (!deviceToPipeconf.containsKey(deviceId)) {
90 return null;
91 }
92 return deviceToPipeconf.get(deviceId).value();
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020093 }
94
95 @Override
96 public Set<DeviceId> getDevices(PiPipeconfId pipeconfId) {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070097 return ImmutableSet.copyOf(pipeconfToDevices.get(pipeconfId));
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020098 }
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 Cascone9e4972c2018-08-30 00:29:16 -0700110 private class InternalPiPipeconfListener implements MapEventListener<DeviceId, PiPipeconfId> {
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200111
112 @Override
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700113 public void event(MapEvent<DeviceId, PiPipeconfId> mapEvent) {
114 PiPipeconfDeviceMappingEvent.Type eventType = null;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200115 final DeviceId deviceId = mapEvent.key();
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700116 final PiPipeconfId newPipeconfId = mapEvent.newValue() != null
117 ? mapEvent.newValue().value() : null;
118 final PiPipeconfId oldPipeconfId = mapEvent.oldValue() != null
119 ? mapEvent.oldValue().value() : null;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200120 switch (mapEvent.type()) {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700121 case INSERT:
122 case UPDATE:
123 if (newPipeconfId != null) {
124 if (!newPipeconfId.equals(oldPipeconfId)) {
125 eventType = PiPipeconfDeviceMappingEvent.Type.CREATED;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200126 }
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700127 pipeconfToDevices.put(newPipeconfId, deviceId);
128 }
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200129 break;
130 case REMOVE:
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700131 if (oldPipeconfId != null) {
132 eventType = PiPipeconfDeviceMappingEvent.Type.REMOVED;
133 pipeconfToDevices.remove(oldPipeconfId, deviceId);
134 }
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200135 break;
136 default:
137 throw new IllegalArgumentException("Wrong event type " + mapEvent.type());
138 }
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700139 if (eventType != null) {
140 notifyDelegate(new PiPipeconfDeviceMappingEvent(eventType, deviceId));
141 }
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200142 }
143 }
Carmelo Cascone39c28ca2017-11-15 13:03:57 -0800144}