blob: 83dc5341cf13934b6466e583b5706319723ae659 [file] [log] [blame]
Brian Stankeca93d9a2016-02-10 09:17:35 -05001/*
2 * Copyright 2016 Open Networking Laboratory
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.incubator.store.key.impl;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
Claudine Chiu31ad5272016-02-17 20:56:24 +000025import org.onosproject.net.key.DeviceKey;
26import org.onosproject.net.key.DeviceKeyEvent;
27import org.onosproject.net.key.DeviceKeyId;
28import org.onosproject.net.key.DeviceKeyStore;
29import org.onosproject.net.key.DeviceKeyStoreDelegate;
Brian Stankeca93d9a2016-02-10 09:17:35 -050030import org.onosproject.store.AbstractStore;
31import org.onosproject.store.serializers.KryoNamespaces;
32import org.onosproject.store.service.ConsistentMap;
33import org.onosproject.store.service.MapEvent;
34import org.onosproject.store.service.MapEventListener;
35import org.onosproject.store.service.Serializer;
36import org.onosproject.store.service.StorageService;
37import org.slf4j.Logger;
38
Brian Stankee312fc72016-02-16 15:07:13 -050039import java.util.Arrays;
Brian Stankeca93d9a2016-02-10 09:17:35 -050040import java.util.Collection;
41import java.util.Map;
42
43import static com.google.common.base.Preconditions.checkNotNull;
44import static org.slf4j.LoggerFactory.getLogger;
45
46/**
47 * A distributed device key store implementation, device keys are stored consistently
48 * across the cluster.
49 */
50@Component(immediate = true)
51@Service
52public class DistributedDeviceKeyStore
53 extends AbstractStore<DeviceKeyEvent, DeviceKeyStoreDelegate>
54 implements DeviceKeyStore {
55
56 private final Logger log = getLogger(getClass());
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected StorageService storageService;
60
61 private ConsistentMap<DeviceKeyId, DeviceKey> deviceKeys;
62 private Map<DeviceKeyId, DeviceKey> deviceKeysMap;
63
64 private final MapEventListener<DeviceKeyId, DeviceKey> listener = new InternalMapListener();
65
66 /**
67 * Activate the distributed device key store.
68 */
69 @Activate
70 public void activate() {
71 deviceKeys = storageService.<DeviceKeyId, DeviceKey>consistentMapBuilder()
Brian Stankee312fc72016-02-16 15:07:13 -050072 .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
73 DeviceKey.class,
74 DeviceKeyId.class,
75 DeviceKey.Type.class))
Brian Stankeca93d9a2016-02-10 09:17:35 -050076 .withName("onos-device-keys")
77 .withRelaxedReadConsistency()
78 .build();
79 deviceKeys.addListener(listener);
80 deviceKeysMap = deviceKeys.asJavaMap();
81
82 log.info("Started");
83 }
84
85 /**
86 * Deactivate the distributed device key store.
87 */
88 @Deactivate
89 public void deactivate() {
90 deviceKeys.removeListener(listener);
91 log.info("Stopped");
92 }
93
94 @Override
95 public void createOrUpdateDeviceKey(DeviceKey deviceKey) {
96
97 // Add the device key to the store, if the device key already exists
98 // then it will be replaced with the new one.
99 deviceKeys.put(deviceKey.deviceKeyId(), deviceKey);
100 }
101
102 @Override
103 public void deleteDeviceKey(DeviceKeyId deviceKeyId) {
104 // Remove the device key from the store if the device key identifier exists.
105 deviceKeys.remove(deviceKeyId);
106 }
107
108 @Override
109 public Collection<DeviceKey> getDeviceKeys() {
110 return deviceKeysMap.values();
111 }
112
113 @Override
114 public DeviceKey getDeviceKey(DeviceKeyId deviceKeyId) {
115 return deviceKeysMap.get(deviceKeyId);
116 }
117
118 /**
119 * Listener class to map listener events to the device key events.
120 */
121 private class InternalMapListener implements MapEventListener<DeviceKeyId, DeviceKey> {
122 @Override
123 public void event(MapEvent<DeviceKeyId, DeviceKey> event) {
124 DeviceKey deviceKey = null;
125
126 DeviceKeyEvent.Type type = null;
127 switch (event.type()) {
128 case INSERT:
129 type = DeviceKeyEvent.Type.DEVICE_KEY_ADDED;
130 deviceKey = checkNotNull(event.newValue().value());
131 break;
132 case UPDATE:
133 type = DeviceKeyEvent.Type.DEVICE_KEY_UPDATED;
134 deviceKey = checkNotNull(event.newValue().value());
135 break;
136 case REMOVE:
137 type = DeviceKeyEvent.Type.DEVICE_KEY_REMOVED;
138 deviceKey = checkNotNull(event.oldValue().value());
139 break;
140 default:
141 log.error("Unsupported event type: " + event.type());
142 }
143 notifyDelegate(new DeviceKeyEvent(type, deviceKey));
144 }
145 }
146}