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