use of org.onosproject.net.intent.Constraint in project onos by opennetworkinglab.
the class ConstraintCodecTest method waypointConstraint.
/**
* Tests waypoint constaint.
*/
@Test
public void waypointConstraint() {
Constraint constraint = getConstraint("WaypointConstraint.json");
assertThat(constraint, instanceOf(WaypointConstraint.class));
WaypointConstraint waypointConstraint = (WaypointConstraint) constraint;
assertThat(waypointConstraint.waypoints(), hasItem(did("devA")));
assertThat(waypointConstraint.waypoints(), hasItem(did("devB")));
assertThat(waypointConstraint.waypoints(), hasItem(did("devC")));
}
use of org.onosproject.net.intent.Constraint in project onos by opennetworkinglab.
the class ConstraintCodecTest method getConstraint.
/**
* Reads in a constraint from the given resource and decodes it.
*
* @param resourceName resource to use to read the JSON for the constraint
* @return decoded constraint
*/
private Constraint getConstraint(String resourceName) {
InputStream jsonStream = ConstraintCodecTest.class.getResourceAsStream(resourceName);
try {
JsonNode json = context.mapper().readTree(jsonStream);
assertThat(json, notNullValue());
Constraint constraint = constraintCodec.decode((ObjectNode) json, context);
assertThat(constraint, notNullValue());
return checkNotNull(constraint);
} catch (IOException ioe) {
Assert.fail(ioe.getMessage());
throw new IllegalStateException("cannot happen");
}
}
use of org.onosproject.net.intent.Constraint in project onos by opennetworkinglab.
the class ConstraintCodecTest method domainConstraint.
/**
* Tests domain constraint.
*/
@Test
public void domainConstraint() {
Constraint constraint = getConstraint("DomainConstraint.json");
assertThat(constraint, instanceOf(DomainConstraint.class));
}
use of org.onosproject.net.intent.Constraint in project onos by opennetworkinglab.
the class ConstraintCodecTest method tierConstraint.
/**
* Tests tier constraint.
*/
@Test
public void tierConstraint() {
Constraint constraint = getConstraint("TierConstraint.json");
assertThat(constraint, instanceOf(TierConstraint.class));
TierConstraint tierConstraint = (TierConstraint) constraint;
assertThat(tierConstraint.isInclusive(), is(true));
assertThat(tierConstraint.costType(), is(TierConstraint.CostType.ORDER));
assertThat(tierConstraint.tiers().get(0), is(3));
assertThat(tierConstraint.tiers().get(1), is(2));
assertThat(tierConstraint.tiers().get(2), is(1));
}
use of org.onosproject.net.intent.Constraint in project onos by opennetworkinglab.
the class IntentJsonMatcher method matchConnectivityIntent.
/**
* Matches the JSON representation of a connectivity intent. Calls the
* matcher for the connectivity intent subtype.
*
* @param jsonIntent JSON representation of the intent
* @param description Description object used for recording errors
* @return true if the JSON matches the intent, false otherwise
*/
private boolean matchConnectivityIntent(JsonNode jsonIntent, Description description) {
final ConnectivityIntent connectivityIntent = (ConnectivityIntent) intent;
// check selector
final JsonNode jsonSelector = jsonIntent.get("selector");
final TrafficSelector selector = connectivityIntent.selector();
final Set<Criterion> criteria = selector.criteria();
final JsonNode jsonCriteria = jsonSelector.get("criteria");
if (jsonCriteria.size() != criteria.size()) {
description.appendText("size of criteria array is " + Integer.toString(jsonCriteria.size()));
return false;
}
for (Criterion criterion : criteria) {
boolean criterionFound = false;
for (int criterionIndex = 0; criterionIndex < jsonCriteria.size(); criterionIndex++) {
final CriterionJsonMatcher criterionMatcher = CriterionJsonMatcher.matchesCriterion(criterion);
if (criterionMatcher.matches(jsonCriteria.get(criterionIndex))) {
criterionFound = true;
break;
}
}
if (!criterionFound) {
description.appendText("criterion not found " + criterion.toString());
return false;
}
}
// check treatment
final JsonNode jsonTreatment = jsonIntent.get("treatment");
final TrafficTreatment treatment = connectivityIntent.treatment();
final List<Instruction> instructions = treatment.immediate();
final JsonNode jsonInstructions = jsonTreatment.get("instructions");
if (jsonInstructions.size() != instructions.size()) {
description.appendText("size of instructions array is " + Integer.toString(jsonInstructions.size()));
return false;
}
for (Instruction instruction : instructions) {
boolean instructionFound = false;
for (int instructionIndex = 0; instructionIndex < jsonInstructions.size(); instructionIndex++) {
final InstructionJsonMatcher instructionMatcher = InstructionJsonMatcher.matchesInstruction(instruction);
if (instructionMatcher.matches(jsonInstructions.get(instructionIndex))) {
instructionFound = true;
break;
}
}
if (!instructionFound) {
description.appendText("instruction not found " + instruction.toString());
return false;
}
}
// Check constraints
final JsonNode jsonConstraints = jsonIntent.get("constraints");
if (connectivityIntent.constraints() != null) {
if (connectivityIntent.constraints().size() != jsonConstraints.size()) {
description.appendText("constraints array size was " + Integer.toString(jsonConstraints.size()));
return false;
}
for (final Constraint constraint : connectivityIntent.constraints()) {
boolean constraintFound = false;
for (int constraintIndex = 0; constraintIndex < jsonConstraints.size(); constraintIndex++) {
final JsonNode value = jsonConstraints.get(constraintIndex);
if (matchConstraint(constraint, value)) {
constraintFound = true;
}
}
if (!constraintFound) {
final String constraintString = constraint.toString();
description.appendText("constraint missing " + constraintString);
return false;
}
}
} else if (jsonConstraints.size() != 0) {
description.appendText("constraint array not empty");
return false;
}
if (connectivityIntent instanceof HostToHostIntent) {
return matchHostToHostIntent(jsonIntent, description);
} else if (connectivityIntent instanceof PointToPointIntent) {
return matchPointToPointIntent(jsonIntent, description);
} else {
description.appendText("class of connectivity intent is unknown");
return false;
}
}
Aggregations