use of org.onosproject.net.key.DeviceKey in project onos by opennetworkinglab.
the class DeviceKeyWebResource method removeDeviceKey.
/**
* Removes a device key by device key identifier.
*
* @param id device key identifier
* @return 200 OK with a removed device key, 404 not found
*/
@DELETE
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response removeDeviceKey(@PathParam("id") String id) {
DeviceKey deviceKey = nullIsNotFound(get(DeviceKeyService.class).getDeviceKey(DeviceKeyId.deviceKeyId(id)), DEVICE_KEY_NOT_FOUND);
get(DeviceKeyAdminService.class).removeKey(DeviceKeyId.deviceKeyId(id));
return ok(codec(DeviceKey.class).encode(deviceKey, this)).build();
}
use of org.onosproject.net.key.DeviceKey in project onos by opennetworkinglab.
the class DeviceKeyWebResource method addDeviceKey.
/**
* Adds a new device key from the JSON input stream.
*
* @param stream device key JSON stream
* @return status of the request - CREATED if the JSON is correct,
* BAD_REQUEST if the JSON is invalid
* @onos.rsModel Devicekey
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response addDeviceKey(InputStream stream) {
try {
DeviceKeyAdminService service = get(DeviceKeyAdminService.class);
ObjectNode root = readTreeFromStream(mapper(), stream);
DeviceKey deviceKey = codec(DeviceKey.class).decode(root, this);
service.addKey(deviceKey);
UriBuilder locationBuilder = uriInfo.getBaseUriBuilder().path("keys").path(deviceKey.deviceKeyId().id());
return Response.created(locationBuilder.build()).build();
} catch (IOException ioe) {
throw new IllegalArgumentException(ioe);
}
}
use of org.onosproject.net.key.DeviceKey in project onos by opennetworkinglab.
the class DeviceKeyManagerTest method testAddKey.
/**
* Tests adding a device key using the device key manager.
* This also tests the device key manager query methods.
*/
@Test
public void testAddKey() {
DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, deviceKeyLabel, deviceKeySnmpName);
// Test to make sure that the device key store is empty
Collection<DeviceKey> deviceKeys = manager.getDeviceKeys();
assertTrue("The device key set should be empty.", deviceKeys.isEmpty());
// Add the new device key using the device key manager.
manager.addKey(deviceKey);
// Test the getDeviceKeys method to make sure that the new device key exists
deviceKeys = manager.getDeviceKeys();
assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
// Test the getDeviceKey method using the device key unique identifier
deviceKey = manager.getDeviceKey(deviceKeyId);
assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
// Validate that only the DEVICE_KEY_ADDED event was received.
validateEvents(DeviceKeyEvent.Type.DEVICE_KEY_ADDED);
}
use of org.onosproject.net.key.DeviceKey in project onos by opennetworkinglab.
the class DeviceKeyManagerTest method testRemoveKey.
/**
* Tests removal of a device key from the store using the device key identifier.
*/
@Test
public void testRemoveKey() {
DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, deviceKeyLabel, deviceKeySnmpName);
// Add the new device key using the device key manager
manager.addKey(deviceKey);
// Remove the device key from the store
manager.removeKey(deviceKeyId);
// Validate that the device key was removed from the store by querying it.
deviceKey = manager.getDeviceKey(deviceKeyId);
assertNull("The device key set should be empty.", deviceKey);
// Validate that the following events were received in order,
// DEVICE_KEY_ADDED, DEVICE_KEY_REMOVED.
validateEvents(DeviceKeyEvent.Type.DEVICE_KEY_ADDED, DeviceKeyEvent.Type.DEVICE_KEY_REMOVED);
}
use of org.onosproject.net.key.DeviceKey in project onos by opennetworkinglab.
the class DeviceKeyManagerTest method testAddSameKey.
/**
* Tests re-adding the same device key to the store but with a different label.
*/
@Test
public void testAddSameKey() {
DeviceKeyId deviceKeyId = DeviceKeyId.deviceKeyId(deviceKeyIdValue);
DeviceKey deviceKey = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, deviceKeyLabel, deviceKeySnmpName);
// Add the first device key via the device key manager
manager.addKey(deviceKey);
// Test the getDeviceKeys method
Collection<DeviceKey> deviceKeys = manager.getDeviceKeys();
assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
// Now let's create a new device key with the same device key identifier as exists in the store.
DeviceKey deviceKey2 = DeviceKey.createDeviceKeyUsingCommunityName(deviceKeyId, deviceKeyLabel2, deviceKeySnmpName);
// Replace the new device key in the store
manager.addKey(deviceKey2);
// Test the getDeviceKeys method to ensure that only 1 device key exists in the store.
deviceKeys = manager.getDeviceKeys();
assertEquals("There should be one device key in the set.", deviceKeys.size(), 1);
// Test the getDeviceKey method using the device key unique identifier
deviceKey = manager.getDeviceKey(deviceKeyId);
assertNotNull("The device key should not be null.", deviceKey);
assertEquals("The device key label should match.", deviceKeyLabel2, deviceKey.label());
// Validate that the following events were received in order,
// DEVICE_KEY_ADDED, DEVICE_KEY_REMOVED, DEVICE_KEY_ADDED.
validateEvents(DeviceKeyEvent.Type.DEVICE_KEY_ADDED, DeviceKeyEvent.Type.DEVICE_KEY_UPDATED);
}
Aggregations