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()]));
}
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;
}
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;
}
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));
}
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();
}
Aggregations