Search in sources :

Example 1 with HostProviderRegistry

use of org.onosproject.net.host.HostProviderRegistry in project onos by opennetworkinglab.

the class HostsWebResource method createAndAddHost.

/**
 * Creates a new host based on JSON input and adds it to the current
 * host inventory.
 *
 * @param stream input JSON
 * @return status of the request - CREATED if the JSON is correct,
 * BAD_REQUEST if the JSON is invalid
 * @onos.rsModel HostPut
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createAndAddHost(InputStream stream) {
    URI location;
    HostProviderRegistry hostProviderRegistry = get(HostProviderRegistry.class);
    InternalHostProvider hostProvider = new InternalHostProvider();
    try {
        // Parse the input stream
        ObjectNode root = readTreeFromStream(mapper(), stream);
        HostProviderService hostProviderService = hostProviderRegistry.register(hostProvider);
        hostProvider.setHostProviderService(hostProviderService);
        HostId hostId = hostProvider.parseHost(root);
        UriBuilder locationBuilder = uriInfo.getBaseUriBuilder().path("hosts").path(hostId.mac().toString()).path(hostId.vlanId().toString());
        location = locationBuilder.build();
    } catch (IOException ex) {
        throw new IllegalArgumentException(ex);
    } finally {
        hostProviderRegistry.unregister(hostProvider);
    }
    return Response.created(location).build();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HostProviderService(org.onosproject.net.host.HostProviderService) HostProviderRegistry(org.onosproject.net.host.HostProviderRegistry) IOException(java.io.IOException) HostId(org.onosproject.net.HostId) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 HostId (org.onosproject.net.HostId)1 HostProviderRegistry (org.onosproject.net.host.HostProviderRegistry)1 HostProviderService (org.onosproject.net.host.HostProviderService)1