Search in sources :

Example 1 with LinkTypeConstraint

use of org.onosproject.net.intent.constraint.LinkTypeConstraint in project onos by opennetworkinglab.

the class DecodeConstraintCodecHelper method decodeLinkTypeConstraint.

/**
 * Decodes a link type constraint.
 *
 * @return link type constraint object.
 */
private Constraint decodeLinkTypeConstraint() {
    boolean inclusive = nullIsIllegal(json.get(ConstraintCodec.INCLUSIVE), ConstraintCodec.INCLUSIVE + ConstraintCodec.MISSING_MEMBER_MESSAGE).asBoolean();
    JsonNode types = nullIsIllegal(json.get(ConstraintCodec.TYPES), ConstraintCodec.TYPES + ConstraintCodec.MISSING_MEMBER_MESSAGE);
    if (types.size() < 1) {
        throw new IllegalArgumentException("types array in link constraint must have at least one value");
    }
    ArrayList<Link.Type> typesEntries = new ArrayList<>(types.size());
    IntStream.range(0, types.size()).forEach(index -> typesEntries.add(Link.Type.valueOf(types.get(index).asText())));
    return new LinkTypeConstraint(inclusive, typesEntries.toArray(new Link.Type[types.size()]));
}
Also used : ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) LinkTypeConstraint(org.onosproject.net.intent.constraint.LinkTypeConstraint)

Example 2 with LinkTypeConstraint

use of org.onosproject.net.intent.constraint.LinkTypeConstraint in project onos by opennetworkinglab.

the class EncodeConstraintCodecHelper method encode.

/**
 * Encodes the constraint in JSON.
 *
 * @return JSON node
 */
public ObjectNode encode() {
    final ObjectNode result;
    if (constraint instanceof BandwidthConstraint) {
        result = encodeBandwidthConstraint();
    } else if (constraint instanceof LinkTypeConstraint) {
        result = encodeLinkTypeConstraint();
    } else if (constraint instanceof AnnotationConstraint) {
        result = encodeAnnotationConstraint();
    } else if (constraint instanceof LatencyConstraint) {
        result = encodeLatencyConstraint();
    } else if (constraint instanceof ObstacleConstraint) {
        result = encodeObstacleConstraint();
    } else if (constraint instanceof WaypointConstraint) {
        result = encodeWaypointConstraint();
    } else if (constraint instanceof MeteredConstraint) {
        result = encodeMeteredConstraint();
    } else if (constraint instanceof TierConstraint) {
        result = encodeTierConstraint();
    } else {
        result = context.mapper().createObjectNode();
    }
    result.put(ConstraintCodec.TYPE, constraint.getClass().getSimpleName());
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ObstacleConstraint(org.onosproject.net.intent.constraint.ObstacleConstraint) TierConstraint(org.onosproject.net.intent.constraint.TierConstraint) WaypointConstraint(org.onosproject.net.intent.constraint.WaypointConstraint) LatencyConstraint(org.onosproject.net.intent.constraint.LatencyConstraint) MeteredConstraint(org.onosproject.net.intent.constraint.MeteredConstraint) LinkTypeConstraint(org.onosproject.net.intent.constraint.LinkTypeConstraint) AnnotationConstraint(org.onosproject.net.intent.constraint.AnnotationConstraint) BandwidthConstraint(org.onosproject.net.intent.constraint.BandwidthConstraint)

Example 3 with LinkTypeConstraint

use of org.onosproject.net.intent.constraint.LinkTypeConstraint in project onos by opennetworkinglab.

the class EncodeConstraintCodecHelper method encodeLinkTypeConstraint.

/**
 * Encodes a link type constraint.
 *
 * @return JSON ObjectNode representing the constraint
 */
private ObjectNode encodeLinkTypeConstraint() {
    checkNotNull(constraint, "Link type constraint cannot be null");
    final LinkTypeConstraint linkTypeConstraint = (LinkTypeConstraint) constraint;
    final ObjectNode result = context.mapper().createObjectNode().put(ConstraintCodec.INCLUSIVE, linkTypeConstraint.isInclusive());
    final ArrayNode jsonTypes = result.putArray(ConstraintCodec.TYPES);
    if (linkTypeConstraint.types() != null) {
        for (Link.Type type : linkTypeConstraint.types()) {
            jsonTypes.add(type.name());
        }
    }
    return result;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) LinkTypeConstraint(org.onosproject.net.intent.constraint.LinkTypeConstraint) Link(org.onosproject.net.Link)

Example 4 with LinkTypeConstraint

use of org.onosproject.net.intent.constraint.LinkTypeConstraint in project onos by opennetworkinglab.

the class ConstraintCodecTest method linkTypeConstraint.

/**
 * Tests link type constraint.
 */
@Test
public void linkTypeConstraint() {
    Constraint constraint = getConstraint("LinkTypeConstraint.json");
    assertThat(constraint, instanceOf(LinkTypeConstraint.class));
    LinkTypeConstraint linkTypeConstraint = (LinkTypeConstraint) constraint;
    assertThat(linkTypeConstraint.isInclusive(), is(false));
    assertThat(linkTypeConstraint.types(), hasSize(2));
    assertThat(linkTypeConstraint.types(), hasItem(Link.Type.OPTICAL));
    assertThat(linkTypeConstraint.types(), hasItem(Link.Type.DIRECT));
}
Also used : WaypointConstraint(org.onosproject.net.intent.constraint.WaypointConstraint) AsymmetricPathConstraint(org.onosproject.net.intent.constraint.AsymmetricPathConstraint) DomainConstraint(org.onosproject.net.intent.constraint.DomainConstraint) MeteredConstraint(org.onosproject.net.intent.constraint.MeteredConstraint) ObstacleConstraint(org.onosproject.net.intent.constraint.ObstacleConstraint) Constraint(org.onosproject.net.intent.Constraint) TierConstraint(org.onosproject.net.intent.constraint.TierConstraint) AnnotationConstraint(org.onosproject.net.intent.constraint.AnnotationConstraint) BandwidthConstraint(org.onosproject.net.intent.constraint.BandwidthConstraint) LatencyConstraint(org.onosproject.net.intent.constraint.LatencyConstraint) LinkTypeConstraint(org.onosproject.net.intent.constraint.LinkTypeConstraint) LinkTypeConstraint(org.onosproject.net.intent.constraint.LinkTypeConstraint) Test(org.junit.Test)

Example 5 with LinkTypeConstraint

use of org.onosproject.net.intent.constraint.LinkTypeConstraint in project onos by opennetworkinglab.

the class IntentJsonMatcher method matchLinkTypeConstraint.

/**
 * Matches a link type constraint against a JSON representation of the
 * constraint.
 *
 * @param linkTypeConstraint constraint object to match
 * @param constraintJson JSON representation of the constraint
 * @return true if the constraint and JSON match, false otherwise.
 */
private boolean matchLinkTypeConstraint(LinkTypeConstraint linkTypeConstraint, JsonNode constraintJson) {
    final JsonNode inclusiveJson = constraintJson.get("inclusive");
    final JsonNode typesJson = constraintJson.get("types");
    if (typesJson.size() != linkTypeConstraint.types().size()) {
        return false;
    }
    int foundType = 0;
    for (Link.Type type : linkTypeConstraint.types()) {
        for (int jsonIndex = 0; jsonIndex < typesJson.size(); jsonIndex++) {
            if (type.name().equals(typesJson.get(jsonIndex).asText())) {
                foundType++;
                break;
            }
        }
    }
    return (inclusiveJson != null && inclusiveJson.asBoolean() == linkTypeConstraint.isInclusive()) && foundType == typesJson.size();
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) WaypointConstraint(org.onosproject.net.intent.constraint.WaypointConstraint) ObstacleConstraint(org.onosproject.net.intent.constraint.ObstacleConstraint) Constraint(org.onosproject.net.intent.Constraint) ConnectPoint(org.onosproject.net.ConnectPoint) AnnotationConstraint(org.onosproject.net.intent.constraint.AnnotationConstraint) BandwidthConstraint(org.onosproject.net.intent.constraint.BandwidthConstraint) LatencyConstraint(org.onosproject.net.intent.constraint.LatencyConstraint) LinkTypeConstraint(org.onosproject.net.intent.constraint.LinkTypeConstraint) Link(org.onosproject.net.Link)

Aggregations

LinkTypeConstraint (org.onosproject.net.intent.constraint.LinkTypeConstraint)5 AnnotationConstraint (org.onosproject.net.intent.constraint.AnnotationConstraint)3 BandwidthConstraint (org.onosproject.net.intent.constraint.BandwidthConstraint)3 LatencyConstraint (org.onosproject.net.intent.constraint.LatencyConstraint)3 ObstacleConstraint (org.onosproject.net.intent.constraint.ObstacleConstraint)3 WaypointConstraint (org.onosproject.net.intent.constraint.WaypointConstraint)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Link (org.onosproject.net.Link)2 Constraint (org.onosproject.net.intent.Constraint)2 MeteredConstraint (org.onosproject.net.intent.constraint.MeteredConstraint)2 TierConstraint (org.onosproject.net.intent.constraint.TierConstraint)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 ConnectPoint (org.onosproject.net.ConnectPoint)1 AsymmetricPathConstraint (org.onosproject.net.intent.constraint.AsymmetricPathConstraint)1 DomainConstraint (org.onosproject.net.intent.constraint.DomainConstraint)1