Search in sources :

Example 11 with SegmentRoutingService

use of org.onosproject.segmentrouting.SegmentRoutingService in project trellis-control by opennetworkinglab.

the class PseudowireWebResource method createPseudowiresBulk.

/**
 * Create a bulk of pseudowires.
 *
 * @param input JSON stream for pseudowires to create
 * @return Response with appropriate status
 * @throws IOException Throws IO exception.
 * @onos.rsModel PseudowireCreateBulk
 */
@POST
@Path("/bulk")
@Consumes(MediaType.APPLICATION_JSON)
public Response createPseudowiresBulk(InputStream input) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode pseudowireJson = readTreeFromStream(mapper, input);
    SegmentRoutingService srService = get(SegmentRoutingService.class);
    Pair<List<Pair<JsonNode, String>>, List<L2TunnelDescription>> pseudowires;
    try {
        ArrayNode pseudowiresArray = nullIsIllegal((ArrayNode) pseudowireJson.get(PWS), PWS_KEY_ERROR);
        // get two lists, first one contains pseudowires that we were unable to decode
        // that have faulty arguments, second one contains pseudowires that we decoded
        // succesfully
        pseudowires = PSEUDOWIRE_CODEC.decodePws(pseudowiresArray, this);
    } catch (ItemNotFoundException e) {
        return Response.serverError().status(Response.Status.BAD_REQUEST).build();
    }
    log.debug("Creating pseudowires {} from rest api!", pseudowires);
    List<Pair<DefaultL2TunnelDescription, String>> failed = new ArrayList<>();
    for (L2TunnelDescription pw : pseudowires.getRight()) {
        L2TunnelHandler.Result res = srService.addPseudowire(pw);
        if (res != L2TunnelHandler.Result.SUCCESS) {
            log.error("Could not create pseudowire {} : {}", pw.l2Tunnel().tunnelId(), res.getSpecificError());
            failed.add(Pair.of((DefaultL2TunnelDescription) pw, res.getSpecificError()));
        }
    }
    List<Pair<JsonNode, String>> undecodedPws = pseudowires.getLeft();
    if ((failed.size() == 0) && (undecodedPws.size() == 0)) {
        // all pseudowires were decoded and instantiated succesfully
        return Response.ok().build();
    } else {
        PseudowireCodec pwCodec = new PseudowireCodec();
        // some failed, need to report them to user
        ObjectNode result = pwCodec.encodeFailedPseudowires(failed, undecodedPws, this);
        return Response.serverError().entity(result).build();
    }
}
Also used : SegmentRoutingService(org.onosproject.segmentrouting.SegmentRoutingService) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) L2TunnelHandler(org.onosproject.segmentrouting.pwaas.L2TunnelHandler) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) DefaultL2TunnelDescription(org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription) ArrayList(java.util.ArrayList) List(java.util.List) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ItemNotFoundException(org.onlab.util.ItemNotFoundException) Pair(org.apache.commons.lang3.tuple.Pair) DefaultL2TunnelDescription(org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription) L2TunnelDescription(org.onosproject.segmentrouting.pwaas.L2TunnelDescription) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 12 with SegmentRoutingService

use of org.onosproject.segmentrouting.SegmentRoutingService in project trellis-control by opennetworkinglab.

the class PseudowireWebResource method createPseudowire.

/**
 * Create a new pseudowire.
 *
 * @param input JSON stream for pseudowire to create
 * @return Response with appropriate status
 * @throws IOException Throws IO exception.
 * @onos.rsModel PseudowireCreate
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createPseudowire(InputStream input) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode pseudowireJson = readTreeFromStream(mapper, input);
    SegmentRoutingService srService = get(SegmentRoutingService.class);
    List<Pair<DefaultL2TunnelDescription, String>> failed = new ArrayList<>();
    List<Pair<JsonNode, String>> undecoded = new ArrayList<>();
    DefaultL2TunnelDescription pseudowire;
    try {
        pseudowire = PSEUDOWIRE_CODEC.decode(pseudowireJson, this);
        // pseudowire decoded, try to instantiate it, if we fail add it to failed list
        long tunId = pseudowire.l2Tunnel().tunnelId();
        log.debug("Creating pseudowire {} from rest api!", tunId);
        L2TunnelHandler.Result res = srService.addPseudowire(pseudowire);
        if (res != L2TunnelHandler.Result.SUCCESS) {
            log.error("Could not create pseudowire {} : {}", pseudowire.l2Tunnel().tunnelId(), res.getSpecificError());
            failed.add(Pair.of(pseudowire, res.getSpecificError()));
        }
    } catch (IllegalArgumentException e) {
        log.debug("Pseudowire could not be decoded : {}", e.getMessage());
        undecoded.add(Pair.of(pseudowireJson, e.getMessage()));
    }
    if ((failed.size() == 0) && (undecoded.size() == 0)) {
        // pseudowire instantiated correctly
        return Response.ok().build();
    } else {
        // failed to decode or instantiate pseudowire, return the reason
        PseudowireCodec pwCodec = new PseudowireCodec();
        ObjectNode result = pwCodec.encodeFailedPseudowires(failed, undecoded, this);
        return Response.serverError().entity(result).build();
    }
}
Also used : SegmentRoutingService(org.onosproject.segmentrouting.SegmentRoutingService) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) L2TunnelHandler(org.onosproject.segmentrouting.pwaas.L2TunnelHandler) ArrayList(java.util.ArrayList) DefaultL2TunnelDescription(org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pair(org.apache.commons.lang3.tuple.Pair) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 13 with SegmentRoutingService

use of org.onosproject.segmentrouting.SegmentRoutingService in project trellis-control by opennetworkinglab.

the class PseudowireWebResource method removePseudowire.

/**
 * Delete a pseudowire.
 *
 * @param input JSON stream for pseudowire to delete
 * @return Response with appropriate status
 * @throws IOException Throws IO exception.
 * @onos.rsModel PseudowireDelete
 */
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
public Response removePseudowire(InputStream input) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode pseudowireJson = readTreeFromStream(mapper, input);
    SegmentRoutingService srService = get(SegmentRoutingService.class);
    Integer pseudowireId = PSEUDOWIRE_CODEC.decodeId(pseudowireJson);
    if (pseudowireId == null) {
        return Response.serverError().status(Response.Status.BAD_REQUEST).build();
    }
    log.debug("Deleting pseudowire {} from rest api!", pseudowireId);
    L2TunnelHandler.Result res = srService.removePseudowire(pseudowireId);
    switch(res) {
        case WRONG_PARAMETERS:
        case INTERNAL_ERROR:
            log.error("Pseudowire {} could not be removed : {}", pseudowireId, res.getSpecificError());
            return Response.noContent().build();
        case SUCCESS:
            log.debug("Pseudowire {} was removed succesfully!", pseudowireId);
            return Response.noContent().build();
        default:
            return Response.noContent().build();
    }
}
Also used : SegmentRoutingService(org.onosproject.segmentrouting.SegmentRoutingService) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) L2TunnelHandler(org.onosproject.segmentrouting.pwaas.L2TunnelHandler) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DELETE(javax.ws.rs.DELETE) Consumes(javax.ws.rs.Consumes)

Example 14 with SegmentRoutingService

use of org.onosproject.segmentrouting.SegmentRoutingService in project trellis-control by opennetworkinglab.

the class PseudowireWebResource method getPseudowire.

/**
 * Get all pseudowires.
 * Returns an array of pseudowires.
 *
 * @return status of OK
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getPseudowire() {
    SegmentRoutingService srService = get(SegmentRoutingService.class);
    log.debug("Fetching pseudowires form rest api!");
    List<L2TunnelPolicy> policies = srService.getL2Policies();
    List<L2Tunnel> tunnels = srService.getL2Tunnels();
    List<DefaultL2TunnelDescription> pseudowires = tunnels.stream().map(l2Tunnel -> {
        L2TunnelPolicy policy = null;
        for (L2TunnelPolicy l2Policy : policies) {
            if (l2Policy.tunnelId() == l2Tunnel.tunnelId()) {
                policy = l2Policy;
                break;
            }
        }
        // return a copy
        return new DefaultL2TunnelDescription(l2Tunnel, policy);
    }).collect(Collectors.toList());
    ObjectNode result = new ObjectMapper().createObjectNode();
    result.set("pseudowires", new PseudowireCodec().encode(pseudowires, this));
    return ok(result.toString()).build();
}
Also used : SegmentRoutingService(org.onosproject.segmentrouting.SegmentRoutingService) AbstractWebResource(org.onosproject.rest.AbstractWebResource) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) L2Tunnel(org.onosproject.segmentrouting.pwaas.L2Tunnel) LoggerFactory(org.slf4j.LoggerFactory) Path(javax.ws.rs.Path) DefaultL2TunnelDescription(org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription) L2TunnelDescription(org.onosproject.segmentrouting.pwaas.L2TunnelDescription) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) SegmentRoutingService(org.onosproject.segmentrouting.SegmentRoutingService) L2TunnelHandler(org.onosproject.segmentrouting.pwaas.L2TunnelHandler) MediaType(javax.ws.rs.core.MediaType) Pair(org.apache.commons.lang3.tuple.Pair) Consumes(javax.ws.rs.Consumes) JsonNode(com.fasterxml.jackson.databind.JsonNode) Tools.nullIsIllegal(org.onlab.util.Tools.nullIsIllegal) DELETE(javax.ws.rs.DELETE) Logger(org.slf4j.Logger) POST(javax.ws.rs.POST) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) ItemNotFoundException(org.onlab.util.ItemNotFoundException) Collectors(java.util.stream.Collectors) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) L2TunnelPolicy(org.onosproject.segmentrouting.pwaas.L2TunnelPolicy) List(java.util.List) Response(javax.ws.rs.core.Response) Tools.readTreeFromStream(org.onlab.util.Tools.readTreeFromStream) InputStream(java.io.InputStream) L2Tunnel(org.onosproject.segmentrouting.pwaas.L2Tunnel) DefaultL2TunnelDescription(org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) L2TunnelPolicy(org.onosproject.segmentrouting.pwaas.L2TunnelPolicy) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 15 with SegmentRoutingService

use of org.onosproject.segmentrouting.SegmentRoutingService in project trellis-control by opennetworkinglab.

the class TunnelWebResource method getTunnel.

/**
 * Get all segment routing tunnels.
 * Returns an array of segment routing tunnels.
 *
 * @return status of OK
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getTunnel() {
    SegmentRoutingService srService = get(SegmentRoutingService.class);
    List<Tunnel> tunnels = srService.getTunnels();
    ObjectNode result = new ObjectMapper().createObjectNode();
    result.set("tunnel", new TunnelCodec().encode(tunnels, this));
    return ok(result.toString()).build();
}
Also used : SegmentRoutingService(org.onosproject.segmentrouting.SegmentRoutingService) Tunnel(org.onosproject.segmentrouting.Tunnel) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

SegmentRoutingService (org.onosproject.segmentrouting.SegmentRoutingService)31 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)10 Consumes (javax.ws.rs.Consumes)7 L2TunnelHandler (org.onosproject.segmentrouting.pwaas.L2TunnelHandler)7 List (java.util.List)5 IpAddress (org.onlab.packet.IpAddress)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 ArrayList (java.util.ArrayList)4 DELETE (javax.ws.rs.DELETE)4 POST (javax.ws.rs.POST)4 Pair (org.apache.commons.lang3.tuple.Pair)4 DeviceId (org.onosproject.net.DeviceId)4 DefaultL2TunnelDescription (org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription)4 Collectors (java.util.stream.Collectors)3 Path (javax.ws.rs.Path)3 Tunnel (org.onosproject.segmentrouting.Tunnel)3 L2Tunnel (org.onosproject.segmentrouting.pwaas.L2Tunnel)3 L2TunnelDescription (org.onosproject.segmentrouting.pwaas.L2TunnelDescription)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2