blob: 7086321e724e454a014716b6ac3099905a5af5cb [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
42import java.util.Set;
43import java.util.concurrent.ConcurrentHashMap;
44import java.util.concurrent.ConcurrentMap;
45
46import 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
54public 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 Cascone39c28ca2017-11-15 13:03:57 -0800142}