blob: 34a1b271137301d177d535407197328bf238215d [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.onosproject.net.DeviceId;
24import org.onosproject.net.pi.model.PiPipeconfId;
Carmelo Cascone39c28ca2017-11-15 13:03:57 -080025import org.onosproject.net.pi.service.PiPipeconfDeviceMappingEvent;
26import org.onosproject.net.pi.service.PiPipeconfMappingStore;
27import org.onosproject.net.pi.service.PiPipeconfMappingStoreDelegate;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020028import org.onosproject.store.AbstractStore;
29import org.onosproject.store.serializers.KryoNamespaces;
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070030import org.onosproject.store.service.ConsistentMap;
31import org.onosproject.store.service.MapEvent;
32import org.onosproject.store.service.MapEventListener;
33import org.onosproject.store.service.Serializer;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020034import org.onosproject.store.service.StorageService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035import org.osgi.service.component.annotations.Activate;
36import org.osgi.service.component.annotations.Component;
37import org.osgi.service.component.annotations.Deactivate;
38import org.osgi.service.component.annotations.Reference;
39import org.osgi.service.component.annotations.ReferenceCardinality;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020040import org.slf4j.Logger;
41
42import java.util.Set;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020043
44import static org.slf4j.LoggerFactory.getLogger;
45
46/**
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070047 * Manages information of pipeconf to device binding.
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020048 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049@Component(immediate = true, service = PiPipeconfMappingStore.class)
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020050public class DistributedDevicePipeconfMappingStore
51 extends AbstractStore<PiPipeconfDeviceMappingEvent, PiPipeconfMappingStoreDelegate>
52 implements PiPipeconfMappingStore {
53
54 private final Logger log = getLogger(getClass());
55
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020057 protected StorageService storageService;
58
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070059 protected ConsistentMap<DeviceId, PiPipeconfId> deviceToPipeconf;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020060
Carmelo Cascone75a9a892019-04-22 12:12:23 -070061 protected final MapEventListener<DeviceId, PiPipeconfId> mapListener =
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020062 new InternalPiPipeconfListener();
63
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070064 protected SetMultimap<PiPipeconfId, DeviceId> pipeconfToDevices =
65 Multimaps.synchronizedSetMultimap(HashMultimap.create());
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020066
67 @Activate
68 public void activate() {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070069 deviceToPipeconf = storageService.<DeviceId, PiPipeconfId>consistentMapBuilder()
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020070 .withName("onos-pipeconf-table")
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070071 .withSerializer(Serializer.using(KryoNamespaces.API))
72 .build();
Carmelo Cascone75a9a892019-04-22 12:12:23 -070073 deviceToPipeconf.addListener(mapListener);
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020074 log.info("Started");
75 }
76
77 @Deactivate
78 public void deactivate() {
Carmelo Cascone75a9a892019-04-22 12:12:23 -070079 deviceToPipeconf.removeListener(mapListener);
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020080 deviceToPipeconf = null;
81 pipeconfToDevices = null;
82 log.info("Stopped");
83 }
84
85 @Override
86 public PiPipeconfId getPipeconfId(DeviceId deviceId) {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070087 if (!deviceToPipeconf.containsKey(deviceId)) {
88 return null;
89 }
90 return deviceToPipeconf.get(deviceId).value();
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020091 }
92
93 @Override
94 public Set<DeviceId> getDevices(PiPipeconfId pipeconfId) {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -070095 return ImmutableSet.copyOf(pipeconfToDevices.get(pipeconfId));
Andrea Campanellaf9c409a2017-07-13 14:14:41 +020096 }
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 Cascone9e4972c2018-08-30 00:29:16 -0700108 private class InternalPiPipeconfListener implements MapEventListener<DeviceId, PiPipeconfId> {
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200109
110 @Override
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700111 public void event(MapEvent<DeviceId, PiPipeconfId> mapEvent) {
112 PiPipeconfDeviceMappingEvent.Type eventType = null;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200113 final DeviceId deviceId = mapEvent.key();
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700114 final PiPipeconfId newPipeconfId = mapEvent.newValue() != null
115 ? mapEvent.newValue().value() : null;
116 final PiPipeconfId oldPipeconfId = mapEvent.oldValue() != null
117 ? mapEvent.oldValue().value() : null;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200118 switch (mapEvent.type()) {
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700119 case INSERT:
120 case UPDATE:
121 if (newPipeconfId != null) {
122 if (!newPipeconfId.equals(oldPipeconfId)) {
123 eventType = PiPipeconfDeviceMappingEvent.Type.CREATED;
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200124 }
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700125 pipeconfToDevices.put(newPipeconfId, deviceId);
126 }
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200127 break;
128 case REMOVE:
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700129 if (oldPipeconfId != null) {
130 eventType = PiPipeconfDeviceMappingEvent.Type.REMOVED;
131 pipeconfToDevices.remove(oldPipeconfId, deviceId);
132 }
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200133 break;
134 default:
135 throw new IllegalArgumentException("Wrong event type " + mapEvent.type());
136 }
Carmelo Cascone9e4972c2018-08-30 00:29:16 -0700137 if (eventType != null) {
138 notifyDelegate(new PiPipeconfDeviceMappingEvent(eventType, deviceId));
139 }
Andrea Campanellaf9c409a2017-07-13 14:14:41 +0200140 }
141 }
Carmelo Cascone39c28ca2017-11-15 13:03:57 -0800142}