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