Search in sources :

Example 1 with DeviceKey

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();
}
Also used : DeviceKey(org.onosproject.net.key.DeviceKey) DeviceKeyAdminService(org.onosproject.net.key.DeviceKeyAdminService) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Example 2 with DeviceKey

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);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DeviceKey(org.onosproject.net.key.DeviceKey) IOException(java.io.IOException) UriBuilder(javax.ws.rs.core.UriBuilder) DeviceKeyAdminService(org.onosproject.net.key.DeviceKeyAdminService) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 3 with DeviceKey

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);
}
Also used : DeviceKeyId(org.onosproject.net.key.DeviceKeyId) DeviceKey(org.onosproject.net.key.DeviceKey) Test(org.junit.Test)

Example 4 with DeviceKey

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);
}
Also used : DeviceKeyId(org.onosproject.net.key.DeviceKeyId) DeviceKey(org.onosproject.net.key.DeviceKey) Test(org.junit.Test)

Example 5 with DeviceKey

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);
}
Also used : DeviceKeyId(org.onosproject.net.key.DeviceKeyId) DeviceKey(org.onosproject.net.key.DeviceKey) Test(org.junit.Test)

Aggregations

DeviceKey (org.onosproject.net.key.DeviceKey)11 DeviceKeyId (org.onosproject.net.key.DeviceKeyId)7 Test (org.junit.Test)6 DeviceKeyAdminService (org.onosproject.net.key.DeviceKeyAdminService)3 Produces (javax.ws.rs.Produces)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 Optional (java.util.Optional)1 Consumes (javax.ws.rs.Consumes)1 DELETE (javax.ws.rs.DELETE)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 Device (org.onosproject.net.Device)1 UsernamePassword (org.onosproject.net.key.UsernamePassword)1 NetconfDevice (org.onosproject.netconf.NetconfDevice)1 NetconfDeviceInfo (org.onosproject.netconf.NetconfDeviceInfo)1 NetconfException (org.onosproject.netconf.NetconfException)1