Brian Stanke
Committed by Gerrit Code Review

ONOS-3655 - This Device key subsystem update adds the implementation of the

DeviceKeyManager and the distributed device key store.

Change-Id: I32544040590b31a59e20f3375fca453a117f940c
...@@ -34,6 +34,9 @@ public class AppPermission extends BasicPermission { ...@@ -34,6 +34,9 @@ public class AppPermission extends BasicPermission {
34 CLUSTER_READ, 34 CLUSTER_READ,
35 CLUSTER_WRITE, 35 CLUSTER_WRITE,
36 CLUSTER_EVENT, 36 CLUSTER_EVENT,
37 + DEVICE_KEY_EVENT,
38 + DEVICE_KEY_READ,
39 + DEVICE_KEY_WRITE,
37 DEVICE_READ, 40 DEVICE_READ,
38 DEVICE_EVENT, 41 DEVICE_EVENT,
39 DRIVER_READ, 42 DRIVER_READ,
......
...@@ -48,7 +48,7 @@ public final class DeviceKeyId { ...@@ -48,7 +48,7 @@ public final class DeviceKeyId {
48 * @param id for the device key identifier 48 * @param id for the device key identifier
49 * @return device key identifier 49 * @return device key identifier
50 */ 50 */
51 - static final DeviceKeyId deviceKeyId(String id) { 51 + public static final DeviceKeyId deviceKeyId(String id) {
52 return new DeviceKeyId(id); 52 return new DeviceKeyId(id);
53 } 53 }
54 54
......
...@@ -17,26 +17,29 @@ ...@@ -17,26 +17,29 @@
17 package org.onosproject.incubator.net.key; 17 package org.onosproject.incubator.net.key;
18 18
19 import com.google.common.annotations.Beta; 19 import com.google.common.annotations.Beta;
20 +import org.onosproject.event.ListenerService;
21 +
22 +import java.util.Collection;
20 23
21 /** 24 /**
22 * Service for querying device keys. 25 * Service for querying device keys.
23 */ 26 */
24 @Beta 27 @Beta
25 -public interface DeviceKeyService { 28 +public interface DeviceKeyService extends ListenerService<DeviceKeyEvent, DeviceKeyListener> {
26 29
27 /** 30 /**
28 * Returns all device keys. 31 * Returns all device keys.
29 * 32 *
30 - * @return collection of device keys 33 + * @return Collection of device keys
31 */ 34 */
32 - Iterable<DeviceKey> getDeviceKeys(); 35 + Collection<DeviceKey> getDeviceKeys();
33 36
34 /** 37 /**
35 - * Returns the device key(s) using a device key identifier. 38 + * Returns the device key using a device key identifier.
36 * 39 *
37 - * @param id device key identifier 40 + * @param deviceKeyId device key identifier
38 - * @return collection of device keys 41 + * @return device key
39 */ 42 */
40 - Iterable<DeviceKey> getDeviceKey(DeviceKeyId id); 43 + DeviceKey getDeviceKey(DeviceKeyId deviceKeyId);
41 } 44 }
42 45
......
...@@ -19,6 +19,8 @@ package org.onosproject.incubator.net.key; ...@@ -19,6 +19,8 @@ package org.onosproject.incubator.net.key;
19 import com.google.common.annotations.Beta; 19 import com.google.common.annotations.Beta;
20 import org.onosproject.store.Store; 20 import org.onosproject.store.Store;
21 21
22 +import java.util.Collection;
23 +
22 /** 24 /**
23 * Manages inventory of device keys; not intended for direct use. 25 * Manages inventory of device keys; not intended for direct use.
24 */ 26 */
...@@ -28,15 +30,28 @@ public interface DeviceKeyStore extends Store<DeviceKeyEvent, DeviceKeyStoreDele ...@@ -28,15 +30,28 @@ public interface DeviceKeyStore extends Store<DeviceKeyEvent, DeviceKeyStoreDele
28 * Creates or updates a device key. 30 * Creates or updates a device key.
29 * 31 *
30 * @param deviceKey device key 32 * @param deviceKey device key
31 - * @return device key event
32 */ 33 */
33 - DeviceKeyEvent createOrUpdateDeviceKey(DeviceKey deviceKey); 34 + void createOrUpdateDeviceKey(DeviceKey deviceKey);
34 35
35 /** 36 /**
36 * Deletes a device key by a specific device key identifier. 37 * Deletes a device key by a specific device key identifier.
37 * 38 *
38 * @param deviceKeyId device key unique identifier 39 * @param deviceKeyId device key unique identifier
39 - * @return device key event
40 */ 40 */
41 - DeviceKeyEvent deleteDeviceKey(DeviceKeyId deviceKeyId); 41 + void deleteDeviceKey(DeviceKeyId deviceKeyId);
42 +
43 + /**
44 + * Returns all device keys.
45 + *
46 + * @return set of device keys
47 + */
48 + Collection<DeviceKey> getDeviceKeys();
49 +
50 + /**
51 + * Returns the device key matching a device key identifier.
52 + *
53 + * @param deviceKeyId device key unique identifier
54 + * @return device key
55 + */
56 + DeviceKey getDeviceKey(DeviceKeyId deviceKeyId);
42 } 57 }
......
1 +/*
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 +
17 +package org.onosproject.incubator.net.key.impl;
18 +
19 +import org.apache.felix.scr.annotations.Activate;
20 +import org.apache.felix.scr.annotations.Deactivate;
21 +import org.apache.felix.scr.annotations.Reference;
22 +import org.apache.felix.scr.annotations.ReferenceCardinality;
23 +import org.onosproject.event.AbstractListenerManager;
24 +import org.onosproject.incubator.net.key.DeviceKey;
25 +import org.onosproject.incubator.net.key.DeviceKeyAdminService;
26 +import org.onosproject.incubator.net.key.DeviceKeyEvent;
27 +import org.onosproject.incubator.net.key.DeviceKeyId;
28 +import org.onosproject.incubator.net.key.DeviceKeyListener;
29 +import org.onosproject.incubator.net.key.DeviceKeyService;
30 +import org.onosproject.incubator.net.key.DeviceKeyStore;
31 +import org.onosproject.incubator.net.key.DeviceKeyStoreDelegate;
32 +import org.slf4j.Logger;
33 +
34 +import java.util.Collection;
35 +
36 +import static com.google.common.base.Preconditions.checkNotNull;
37 +import static org.onosproject.security.AppGuard.checkPermission;
38 +import static org.onosproject.security.AppPermission.Type.DEVICE_KEY_READ;
39 +import static org.onosproject.security.AppPermission.Type.DEVICE_KEY_WRITE;
40 +import static org.slf4j.LoggerFactory.getLogger;
41 +
42 +/**
43 + * Implementation of device key services.
44 + */
45 +public class DeviceKeyManager extends AbstractListenerManager<DeviceKeyEvent, DeviceKeyListener>
46 + implements DeviceKeyService, DeviceKeyAdminService {
47 +
48 + private final Logger log = getLogger(getClass());
49 +
50 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 + protected DeviceKeyService deviceKeyService;
52 +
53 + private DeviceKeyStoreDelegate delegate = this::post;
54 +
55 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 + protected DeviceKeyStore store;
57 +
58 + @Activate
59 + public void activate() {
60 + store.setDelegate(delegate);
61 + eventDispatcher.addSink(DeviceKeyEvent.class, listenerRegistry);
62 + log.info("Started");
63 + }
64 +
65 + @Deactivate
66 + public void deactivate() {
67 + store.unsetDelegate(delegate);
68 + eventDispatcher.removeSink(DeviceKeyEvent.class);
69 + log.info("Stopped");
70 + }
71 +
72 + @Override
73 + public void addKey(DeviceKey deviceKey) {
74 + checkPermission(DEVICE_KEY_WRITE);
75 + checkNotNull(deviceKey, "Device key cannot be null");
76 + store.createOrUpdateDeviceKey(deviceKey);
77 + }
78 +
79 + @Override
80 + public void removeKey(DeviceKeyId deviceKeyId) {
81 + checkPermission(DEVICE_KEY_WRITE);
82 + checkNotNull(deviceKeyId, "Device key identifier cannot be null");
83 + store.deleteDeviceKey(deviceKeyId);
84 + }
85 +
86 + @Override
87 + public Collection<DeviceKey> getDeviceKeys() {
88 + checkPermission(DEVICE_KEY_READ);
89 + return store.getDeviceKeys();
90 + }
91 +
92 + @Override
93 + public DeviceKey getDeviceKey(DeviceKeyId deviceKeyId) {
94 + checkPermission(DEVICE_KEY_READ);
95 + checkNotNull(deviceKeyId, "Device key identifier cannot be null");
96 + return store.getDeviceKey(deviceKeyId);
97 + }
98 +}
99 +
1 +/*
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 +
17 +/**
18 + * Implementation of device key subsystem.
19 + */
20 +package org.onosproject.incubator.net.key.impl;
1 +/*
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 +
17 +package org.onosproject.incubator.net.key.impl;
18 +
19 +import com.google.common.collect.Lists;
20 +import org.junit.After;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.junit.TestTools;
24 +import org.onlab.junit.TestUtils;
25 +import org.onosproject.common.event.impl.TestEventDispatcher;
26 +import org.onosproject.event.Event;
27 +import org.onosproject.incubator.net.key.DeviceKey;
28 +import org.onosproject.incubator.net.key.DeviceKeyEvent;
29 +import org.onosproject.incubator.net.key.DeviceKeyId;
30 +import org.onosproject.incubator.net.key.DeviceKeyListener;
31 +import org.onosproject.incubator.net.key.DeviceKeyService;
32 +import org.onosproject.incubator.store.key.impl.DistributedDeviceKeyStore;
33 +import org.onosproject.net.NetTestTools;
34 +import org.onosproject.store.service.TestStorageService;
35 +
36 +import java.util.Collection;
37 +import java.util.List;
38 +
39 +import static org.junit.Assert.*;
40 +
41 +/**
42 + * Tests for DeviceKeyManager.
43 + */
44 +public class DeviceKeyManagerTest {
45 +
46 + final String deviceKeyIdValue = "DeviceKeyId";
47 + final String deviceKeyLabel = "DeviceKeyLabel";
48 + final String deviceKeyLabel2 = "DeviceKeyLabel2";
49 + final String deviceKeySnmpName = "DeviceKeySnmpName";
50 +
51 + private DeviceKeyManager manager;
52 + private DeviceKeyService deviceKeyService;
53 + private DistributedDeviceKeyStore deviceKeyStore;
54 + protected TestListener listener = new TestListener();
55 +
56 + @Before
57 + public void setUp() throws Exception {
58 + deviceKeyStore = new DistributedDeviceKeyStore();
59 + TestUtils.setField(deviceKeyStore, "storageService", new TestStorageService());
60 + deviceKeyStore.activate();
61 +
62 + manager = new DeviceKeyManager();
63 + manager.store = deviceKeyStore;
64 + manager.addListener(listener);
65 + NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher());
66 + manager.activate();
67 + deviceKeyService = manager;
68 + }
69 +
70 + @After
71 + public void tearDown() {
72 + deviceKeyStore.deactivate();
73 + manager.removeListener(listener);
74 + manager.deactivate();
75 + NetTestTools.injectEventDispatcher(manager, null);
76 + }
77 +
78 + /**
79 + * Tests adding, query and removal of a device key.
80 + */
81 + @Test(expected = NullPointerException.class)
82 + public void testAddNullKey() {
83 + manager.addKey(null);
84 + }
85 +
86 + /**
87 + * Tests adding a device key using the device key manager.
88 + * This also tests the device key manager query methods.
89 + */
90 + @Test
91 + public void testAddKey() {
92 + DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
93 +
94 + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
95 + deviceKeyLabel, deviceKeySnmpName);
96 +
97 + // Test to make sure that the device key store is empty
98 + Collection<DeviceKey> deviceKeys = manager.getDeviceKeys();
99 + assertTrue("The device key set should be empty.", deviceKeys.isEmpty());
100 +
101 + // Add the new device key using the device key manager.
102 + manager.addKey(deviceKey);
103 +
104 + // Test the getDeviceKeys method to make sure that the new device key exists
105 + deviceKeys = manager.getDeviceKeys();
106 + assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
107 +
108 + // Test the getDeviceKey method using the device key unique identifier
109 + deviceKey = manager.getDeviceKey(deviceKeyId);
110 + assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
111 +
112 + // Validate that only the DEVICE_KEY_ADDED event was received.
113 + validateEvents(DeviceKeyEvent.Type.DEVICE_KEY_ADDED);
114 +
115 + }
116 +
117 + /**
118 + * Tests re-adding the same device key to the store but with a different label.
119 + */
120 + @Test
121 + public void testAddSameKey() {
122 + DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
123 +
124 + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
125 + deviceKeyLabel, deviceKeySnmpName);
126 +
127 + // Add the first device key via the device key manager
128 + manager.addKey(deviceKey);
129 +
130 + // Test the getDeviceKeys method
131 + Collection<DeviceKey> deviceKeys = manager.getDeviceKeys();
132 + assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
133 +
134 + // Now let's create a new device key with the same device key identifier as exists in the store.
135 + DeviceKey deviceKey2 = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
136 + deviceKeyLabel2, deviceKeySnmpName);
137 +
138 + // Replace the new device key in the store
139 + manager.addKey(deviceKey2);
140 +
141 + // Test the getDeviceKeys method to ensure that only 1 device key exists in the store.
142 + deviceKeys = manager.getDeviceKeys();
143 + assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
144 +
145 + // Test the getDeviceKey method using the device key unique identifier
146 + deviceKey = manager.getDeviceKey(deviceKeyId);
147 + assertNotNull("The device key should not be null.", deviceKey);
148 + assertEquals("The device key label should match.", deviceKeyLabel2, deviceKey.label());
149 +
150 + // Validate that the following events were received in order,
151 + // DEVICE_KEY_ADDED, DEVICE_KEY_REMOVED, DEVICE_KEY_ADDED.
152 + validateEvents(DeviceKeyEvent.Type.DEVICE_KEY_ADDED, DeviceKeyEvent.Type.DEVICE_KEY_UPDATED);
153 +
154 + }
155 +
156 + /**
157 + * Tests removal of a device key from the store using the device key identifier.
158 + */
159 + @Test
160 + public void testRemoveKey() {
161 + DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
162 + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
163 + deviceKeyLabel, deviceKeySnmpName);
164 + // Add the new device key using the device key manager
165 + manager.addKey(deviceKey);
166 +
167 + // Remove the device key from the store
168 + manager.removeKey(deviceKeyId);
169 +
170 + // Validate that the device key was removed from the store by querying it.
171 + deviceKey = manager.getDeviceKey(deviceKeyId);
172 + assertNull("The device key set should be empty.", deviceKey);
173 +
174 + // Validate that the following events were received in order,
175 + // DEVICE_KEY_ADDED, DEVICE_KEY_REMOVED.
176 + validateEvents(DeviceKeyEvent.Type.DEVICE_KEY_ADDED, DeviceKeyEvent.Type.DEVICE_KEY_REMOVED);
177 + }
178 +
179 + /**
180 + * Method to validate that actual versus expected device key events were
181 + * received correctly.
182 + *
183 + * @param types expected device key events.
184 + */
185 + private void validateEvents(Enum... types) {
186 + TestTools.assertAfter(100, () -> {
187 + int i = 0;
188 + assertEquals("wrong events received", types.length, listener.events.size());
189 + for (Event event : listener.events) {
190 + assertEquals("incorrect event type", types[i], event.type());
191 + i++;
192 + }
193 + listener.events.clear();
194 + });
195 + }
196 +
197 + /**
198 + * Test listener class to receive device key events.
199 + */
200 + private static class TestListener implements DeviceKeyListener {
201 +
202 + protected List<DeviceKeyEvent> events = Lists.newArrayList();
203 +
204 + @Override
205 + public void event(DeviceKeyEvent event) {
206 + events.add(event);
207 + }
208 +
209 + }
210 +}
1 +/*
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 +
17 +package org.onosproject.incubator.store.key.impl;
18 +
19 +import org.apache.felix.scr.annotations.Activate;
20 +import org.apache.felix.scr.annotations.Component;
21 +import org.apache.felix.scr.annotations.Deactivate;
22 +import org.apache.felix.scr.annotations.Reference;
23 +import org.apache.felix.scr.annotations.ReferenceCardinality;
24 +import org.apache.felix.scr.annotations.Service;
25 +import org.onosproject.incubator.net.key.DeviceKey;
26 +import org.onosproject.incubator.net.key.DeviceKeyEvent;
27 +import org.onosproject.incubator.net.key.DeviceKeyId;
28 +import org.onosproject.incubator.net.key.DeviceKeyStore;
29 +import org.onosproject.incubator.net.key.DeviceKeyStoreDelegate;
30 +import org.onosproject.store.AbstractStore;
31 +import org.onosproject.store.serializers.KryoNamespaces;
32 +import org.onosproject.store.service.ConsistentMap;
33 +import org.onosproject.store.service.MapEvent;
34 +import org.onosproject.store.service.MapEventListener;
35 +import org.onosproject.store.service.Serializer;
36 +import org.onosproject.store.service.StorageService;
37 +import org.slf4j.Logger;
38 +
39 +import java.util.Collection;
40 +import java.util.Map;
41 +
42 +import static com.google.common.base.Preconditions.checkNotNull;
43 +import 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
51 +public 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 +}
1 +/*
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 +
17 +/**
18 + * A distributed device key store implementation that stores device key
19 + * data consistently across the cluster.
20 + */
21 +package org.onosproject.incubator.store.key.impl;
...\ No newline at end of file ...\ No newline at end of file
1 +/*
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 +
17 +package org.onosproject.incubator.store.key.impl;
18 +
19 +import org.junit.After;
20 +import org.junit.Before;
21 +import org.junit.Test;
22 +import org.onosproject.incubator.net.key.DeviceKey;
23 +import org.onosproject.incubator.net.key.DeviceKeyId;
24 +import org.onosproject.store.service.TestStorageService;
25 +
26 +import java.util.Collection;
27 +
28 +import static org.junit.Assert.*;
29 +
30 +/**
31 + * Test class for DistributedDeviceKeyStore.
32 + */
33 +public class DistributedDeviceKeyStoreTest {
34 + private DistributedDeviceKeyStore deviceKeyStore;
35 +
36 + final String deviceKeyIdValue = "DeviceKeyId";
37 + final String deviceKeyLabel = "DeviceKeyLabel";
38 + final String deviceKeyLabel2 = "DeviceKeyLabel2";
39 + final String deviceKeySnmpName = "DeviceKeySnmpName";
40 +
41 + /**
42 + * Sets up the device key store and the storage service test harness.
43 + */
44 + @Before
45 + public void setUp() {
46 + deviceKeyStore = new DistributedDeviceKeyStore();
47 + deviceKeyStore.storageService = new TestStorageService();
48 + deviceKeyStore.setDelegate(event -> {
49 + });
50 + deviceKeyStore.activate();
51 + }
52 +
53 + /**
54 + * Tears down the device key store.
55 + */
56 + @After
57 + public void tearDown() {
58 + deviceKeyStore.deactivate();
59 + }
60 +
61 + /**
62 + * Tests adding, query and removal of a device key.
63 + */
64 + @Test(expected = NullPointerException.class)
65 + public void testAddNullKey() {
66 + deviceKeyStore.createOrUpdateDeviceKey(null);
67 + }
68 +
69 + /**
70 + * Tests adding a device key to the store. This also tests the device key store
71 + * query methods.
72 + */
73 + @Test
74 + public void testAddKey() {
75 + DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
76 +
77 + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
78 + deviceKeyLabel, deviceKeySnmpName);
79 +
80 + // Test to make sure that the device key store is empty
81 + Collection<DeviceKey> deviceKeys = deviceKeyStore.getDeviceKeys();
82 + assertTrue("The device key set should be empty.", deviceKeys.isEmpty());
83 +
84 + // Add the new device key to the store
85 + deviceKeyStore.createOrUpdateDeviceKey(deviceKey);
86 +
87 + // Test the getDeviceKeys method to make sure that the new device key exists
88 + deviceKeys = deviceKeyStore.getDeviceKeys();
89 + assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
90 +
91 + // Test the getDeviceKey method using the device key unique identifier
92 + deviceKey = deviceKeyStore.getDeviceKey(deviceKeyId);
93 + assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
94 + }
95 +
96 + /**
97 + * Tests re-adding the same device key to the store but with a different label.
98 + */
99 + @Test
100 + public void testAddSameKey() {
101 + DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
102 +
103 + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
104 + deviceKeyLabel, deviceKeySnmpName);
105 +
106 + // Add the first device key to the store
107 + deviceKeyStore.createOrUpdateDeviceKey(deviceKey);
108 +
109 + // Test the getDeviceKeys method
110 + Collection<DeviceKey> deviceKeys = deviceKeyStore.getDeviceKeys();
111 + assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
112 +
113 + // Now let's create a new device key with the same device key identifier as exists in the store.
114 + DeviceKey deviceKey2 = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
115 + deviceKeyLabel2, deviceKeySnmpName);
116 +
117 + // Replace the new device key in the store
118 + deviceKeyStore.createOrUpdateDeviceKey(deviceKey2);
119 +
120 + // Test the getDeviceKeys method to ensure that only 1 device key exists in the store.
121 + deviceKeys = deviceKeyStore.getDeviceKeys();
122 + assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
123 +
124 + // Test the getDeviceKey method using the device key unique identifier
125 + deviceKey = deviceKeyStore.getDeviceKey(deviceKeyId);
126 + assertNotNull("The device key should not be null.", deviceKey);
127 + assertEquals("The device key label should match.", deviceKeyLabel2, deviceKey.label());
128 + }
129 +
130 + /**
131 + * Tests removal of a device key from the store using the device key identifier.
132 + */
133 + @Test
134 + public void testRemoveKey() {
135 + DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
136 + DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId,
137 + deviceKeyLabel, deviceKeySnmpName);
138 + // Add the new device key to the store
139 + deviceKeyStore.createOrUpdateDeviceKey(deviceKey);
140 +
141 + // Remove the device key from the store
142 + deviceKeyStore.deleteDeviceKey(deviceKeyId);
143 +
144 + // Validate that the device key was removed from the store by querying it.
145 + deviceKey = deviceKeyStore.getDeviceKey(deviceKeyId);
146 + assertNull("The device key set should be empty.", deviceKey);
147 + }
148 +}