blob: df2c1c2728e918ad620f8c8d55417c1b5fa2362c [file] [log] [blame]
Brian Stankeca93d9a2016-02-10 09:17:35 -05001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Brian Stankeca93d9a2016-02-10 09:17:35 -05003 *
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
Claudine Chiue014b3a2016-02-18 19:56:42 +000017package org.onosproject.store.key.impl;
Brian Stankeca93d9a2016-02-10 09:17:35 -050018
Claudine Chiu31ad5272016-02-17 20:56:24 +000019import org.onosproject.net.key.DeviceKey;
20import org.onosproject.net.key.DeviceKeyEvent;
21import org.onosproject.net.key.DeviceKeyId;
22import org.onosproject.net.key.DeviceKeyStore;
23import org.onosproject.net.key.DeviceKeyStoreDelegate;
Brian Stankeca93d9a2016-02-10 09:17:35 -050024import org.onosproject.store.AbstractStore;
25import org.onosproject.store.serializers.KryoNamespaces;
26import org.onosproject.store.service.ConsistentMap;
27import org.onosproject.store.service.MapEvent;
28import org.onosproject.store.service.MapEventListener;
29import org.onosproject.store.service.Serializer;
30import org.onosproject.store.service.StorageService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031import org.osgi.service.component.annotations.Activate;
32import org.osgi.service.component.annotations.Component;
33import org.osgi.service.component.annotations.Deactivate;
34import org.osgi.service.component.annotations.Reference;
35import org.osgi.service.component.annotations.ReferenceCardinality;
Brian Stankeca93d9a2016-02-10 09:17:35 -050036import org.slf4j.Logger;
37
Brian Stankee312fc72016-02-16 15:07:13 -050038import java.util.Arrays;
Brian Stankeca93d9a2016-02-10 09:17:35 -050039import 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 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049@Component(immediate = true, service = DeviceKeyStore.class)
Brian Stankeca93d9a2016-02-10 09:17:35 -050050public class DistributedDeviceKeyStore
51 extends AbstractStore<DeviceKeyEvent, DeviceKeyStoreDelegate>
52 implements DeviceKeyStore {
53
54 private final Logger log = getLogger(getClass());
55
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Brian Stankeca93d9a2016-02-10 09:17:35 -050057 protected StorageService storageService;
58
59 private ConsistentMap<DeviceKeyId, DeviceKey> deviceKeys;
60 private Map<DeviceKeyId, DeviceKey> deviceKeysMap;
61
62 private final MapEventListener<DeviceKeyId, DeviceKey> listener = new InternalMapListener();
63
64 /**
65 * Activate the distributed device key store.
66 */
67 @Activate
68 public void activate() {
69 deviceKeys = storageService.<DeviceKeyId, DeviceKey>consistentMapBuilder()
Brian Stankee312fc72016-02-16 15:07:13 -050070 .withSerializer(Serializer.using(Arrays.asList(KryoNamespaces.API),
71 DeviceKey.class,
72 DeviceKeyId.class,
73 DeviceKey.Type.class))
Brian Stankeca93d9a2016-02-10 09:17:35 -050074 .withName("onos-device-keys")
75 .withRelaxedReadConsistency()
76 .build();
77 deviceKeys.addListener(listener);
78 deviceKeysMap = deviceKeys.asJavaMap();
79
80 log.info("Started");
81 }
82
83 /**
84 * Deactivate the distributed device key store.
85 */
86 @Deactivate
87 public void deactivate() {
88 deviceKeys.removeListener(listener);
89 log.info("Stopped");
90 }
91
92 @Override
93 public void createOrUpdateDeviceKey(DeviceKey deviceKey) {
94
95 // Add the device key to the store, if the device key already exists
96 // then it will be replaced with the new one.
97 deviceKeys.put(deviceKey.deviceKeyId(), deviceKey);
98 }
99
100 @Override
101 public void deleteDeviceKey(DeviceKeyId deviceKeyId) {
102 // Remove the device key from the store if the device key identifier exists.
103 deviceKeys.remove(deviceKeyId);
104 }
105
106 @Override
107 public Collection<DeviceKey> getDeviceKeys() {
108 return deviceKeysMap.values();
109 }
110
111 @Override
112 public DeviceKey getDeviceKey(DeviceKeyId deviceKeyId) {
113 return deviceKeysMap.get(deviceKeyId);
114 }
115
116 /**
117 * Listener class to map listener events to the device key events.
118 */
119 private class InternalMapListener implements MapEventListener<DeviceKeyId, DeviceKey> {
120 @Override
121 public void event(MapEvent<DeviceKeyId, DeviceKey> event) {
122 DeviceKey deviceKey = null;
123
124 DeviceKeyEvent.Type type = null;
125 switch (event.type()) {
126 case INSERT:
127 type = DeviceKeyEvent.Type.DEVICE_KEY_ADDED;
128 deviceKey = checkNotNull(event.newValue().value());
129 break;
130 case UPDATE:
131 type = DeviceKeyEvent.Type.DEVICE_KEY_UPDATED;
132 deviceKey = checkNotNull(event.newValue().value());
133 break;
134 case REMOVE:
135 type = DeviceKeyEvent.Type.DEVICE_KEY_REMOVED;
136 deviceKey = checkNotNull(event.oldValue().value());
137 break;
138 default:
139 log.error("Unsupported event type: " + event.type());
140 }
141 notifyDelegate(new DeviceKeyEvent(type, deviceKey));
142 }
143 }
144}