use of org.onosproject.net.flow.TrafficSelector in project onos by opennetworkinglab.
the class FibInstallerTest method createForwardingObjective.
/**
* Creates a new forwarding objective with the given parameters.
*
* @param prefix IP prefix
* @param add whether to create an add objective or a remove objective
* @return new forwarding objective
*/
private ForwardingObjective createForwardingObjective(IpPrefix prefix, boolean add) {
TrafficSelector selector = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPDst(prefix).build();
int priority = prefix.prefixLength() * 5 + 100;
ForwardingObjective.Builder fwdBuilder = DefaultForwardingObjective.builder().fromApp(APPID).makePermanent().withSelector(selector).withPriority(priority).withFlag(ForwardingObjective.Flag.SPECIFIC);
if (add) {
fwdBuilder.nextStep(NEXT_ID);
} else {
fwdBuilder.withTreatment(DefaultTrafficTreatment.builder().build());
}
return add ? fwdBuilder.add() : fwdBuilder.remove();
}
use of org.onosproject.net.flow.TrafficSelector in project onos by opennetworkinglab.
the class ConnectivityIntentCodec method intentAttributes.
/**
* Extracts connectivity intent specific attributes from a JSON object
* and adds them to a builder.
*
* @param json root JSON object
* @param context code context
* @param builder builder to use for storing the attributes. Constraints,
* selector and treatment are modified by this call.
*/
public static void intentAttributes(ObjectNode json, CodecContext context, ConnectivityIntent.Builder builder) {
JsonNode constraintsJson = json.get(CONSTRAINTS);
if (constraintsJson != null) {
JsonCodec<Constraint> constraintsCodec = context.codec(Constraint.class);
ArrayList<Constraint> constraints = new ArrayList<>(constraintsJson.size());
IntStream.range(0, constraintsJson.size()).forEach(i -> constraints.add(constraintsCodec.decode(get(constraintsJson, i), context)));
builder.constraints(constraints);
}
ObjectNode selectorJson = get(json, SELECTOR);
if (selectorJson != null) {
JsonCodec<TrafficSelector> selectorCodec = context.codec(TrafficSelector.class);
TrafficSelector selector = selectorCodec.decode(selectorJson, context);
builder.selector(selector);
}
ObjectNode treatmentJson = get(json, TREATMENT);
if (treatmentJson != null) {
JsonCodec<TrafficTreatment> treatmentCodec = context.codec(TrafficTreatment.class);
TrafficTreatment treatment = treatmentCodec.decode(treatmentJson, context);
builder.treatment(treatment);
}
}
use of org.onosproject.net.flow.TrafficSelector in project onos by opennetworkinglab.
the class ConnectivityIntentCodec method encode.
@Override
public ObjectNode encode(ConnectivityIntent intent, CodecContext context) {
checkNotNull(intent, "Connectivity intent cannot be null");
final JsonCodec<Intent> intentCodec = context.codec(Intent.class);
final ObjectNode result = intentCodec.encode(intent, context);
if (intent.selector() != null) {
final JsonCodec<TrafficSelector> selectorCodec = context.codec(TrafficSelector.class);
result.set(SELECTOR, selectorCodec.encode(intent.selector(), context));
}
if (intent.treatment() != null) {
final JsonCodec<TrafficTreatment> treatmentCodec = context.codec(TrafficTreatment.class);
result.set(TREATMENT, treatmentCodec.encode(intent.treatment(), context));
}
result.put(IntentCodec.PRIORITY, intent.priority());
if (intent.constraints() != null) {
final ArrayNode jsonConstraints = result.putArray(CONSTRAINTS);
if (intent.constraints() != null) {
final JsonCodec<Constraint> constraintCodec = context.codec(Constraint.class);
for (final Constraint constraint : intent.constraints()) {
final ObjectNode constraintNode = constraintCodec.encode(constraint, context);
jsonConstraints.add(constraintNode);
}
}
}
return result;
}
use of org.onosproject.net.flow.TrafficSelector in project onos by opennetworkinglab.
the class ClientLineTerminalDeviceFlowRuleProgrammable method fetchLineConnectionFromDevice.
private List<FlowRule> fetchLineConnectionFromDevice(String channel, Frequency centralFreq) {
List<FlowRule> confirmedRules = new ArrayList<>();
FlowRule cacheAddRule;
FlowRule cacheDropRule;
NetconfSession session = getNetconfSession();
log.debug("fetchOpticalConnectionsFromDevice {} frequency {}", did(), centralFreq);
// Build the corresponding flow rule as expected
// Selector including port and ochSignal
// Treatment including port
PortNumber inputPortNumber = PortNumber.portNumber(channel);
PortNumber outputPortNumber = PortNumber.portNumber(channel);
log.debug("fetchOpticalConnectionsFromDevice {} port {}-{}", did(), inputPortNumber, outputPortNumber);
TrafficSelector selectorDrop = DefaultTrafficSelector.builder().matchInPort(inputPortNumber).add(Criteria.matchLambda(toOchSignal(centralFreq, 50.0))).add(Criteria.matchOchSignalType(OchSignalType.FIXED_GRID)).build();
TrafficTreatment treatmentDrop = DefaultTrafficTreatment.builder().setOutput(outputPortNumber).build();
TrafficSelector selectorAdd = DefaultTrafficSelector.builder().matchInPort(inputPortNumber).build();
TrafficTreatment treatmentAdd = DefaultTrafficTreatment.builder().add(Instructions.modL0Lambda(toOchSignal(centralFreq, 50.0))).setOutput(outputPortNumber).build();
// Retrieved rules and cached rules are considered equal if both selector and treatment are equal
cacheAddRule = null;
cacheDropRule = null;
if (getConnectionCache().size(did()) != 0) {
cacheDropRule = getConnectionCache().get(did()).stream().filter(r -> (r.selector().equals(selectorDrop) && r.treatment().equals(treatmentDrop))).findFirst().orElse(null);
cacheAddRule = getConnectionCache().get(did()).stream().filter(r -> (r.selector().equals(selectorAdd) && r.treatment().equals(treatmentAdd))).findFirst().orElse(null);
}
// Include the DROP rule to the retrieved rules if found in cache
if ((cacheDropRule != null)) {
confirmedRules.add(cacheDropRule);
log.debug("fetchOpticalConnectionsFromDevice {} DROP LINE rule included in the cache {}", did(), cacheDropRule);
} else {
log.warn("fetchOpticalConnectionsFromDevice {} DROP LINE rule not included in cache", did());
}
// Include the ADD rule to the retrieved rules if found in cache
if ((cacheAddRule != null)) {
confirmedRules.add(cacheAddRule);
log.debug("fetchOpticalConnectionsFromDevice {} ADD LINE rule included in the cache {}", did(), cacheAddRule.selector());
} else {
log.warn("fetchOpticalConnectionsFromDevice {} ADD LINE rule not included in cache", did());
}
// If neither Add or Drop rules are present in the cache, remove configuration from the device
if ((cacheDropRule == null) && (cacheAddRule == null)) {
log.warn("fetchOpticalConnectionsFromDevice {} ADD and DROP rule not included in the cache", did());
FlowRule deviceDropRule = DefaultFlowRule.builder().forDevice(data().deviceId()).makePermanent().withSelector(selectorDrop).withTreatment(treatmentDrop).withCookie(DEFAULT_RULE_COOKIE).withPriority(DEFAULT_RULE_PRIORITY).build();
FlowRule deviceAddRule = DefaultFlowRule.builder().forDevice(data().deviceId()).makePermanent().withSelector(selectorAdd).withTreatment(treatmentAdd).withCookie(DEFAULT_RULE_COOKIE).withPriority(DEFAULT_RULE_PRIORITY).build();
try {
// TODO this is not required if allowExternalFlowRules
TerminalDeviceFlowRule addRule = new TerminalDeviceFlowRule(deviceAddRule, getLinePorts());
removeFlowRule(session, addRule);
TerminalDeviceFlowRule dropRule = new TerminalDeviceFlowRule(deviceDropRule, getLinePorts());
removeFlowRule(session, dropRule);
} catch (NetconfException e) {
openConfigError("Error removing LINE rule from device", e);
}
}
return confirmedRules;
}
use of org.onosproject.net.flow.TrafficSelector in project onos by opennetworkinglab.
the class ClientLineTerminalDeviceFlowRuleProgrammable method fetchClientConnectionFromDevice.
private List<FlowRule> fetchClientConnectionFromDevice(PortNumber clientPortNumber, PortNumber linePortNumber) {
List<FlowRule> confirmedRules = new ArrayList<>();
FlowRule cacheAddRule;
FlowRule cacheDropRule;
NetconfSession session = getNetconfSession();
// Build the corresponding flow rule as expected
// Selector including port
// Treatment including port
log.debug("fetchClientConnectionsFromDevice {} client {} line {}", did(), clientPortNumber, linePortNumber);
TrafficSelector selectorDrop = DefaultTrafficSelector.builder().matchInPort(linePortNumber).build();
TrafficTreatment treatmentDrop = DefaultTrafficTreatment.builder().setOutput(clientPortNumber).build();
TrafficSelector selectorAdd = DefaultTrafficSelector.builder().matchInPort(clientPortNumber).build();
TrafficTreatment treatmentAdd = DefaultTrafficTreatment.builder().setOutput(linePortNumber).build();
// Retrieved rules and cached rules are considered equal if both selector and treatment are equal
cacheAddRule = null;
cacheDropRule = null;
if (getConnectionCache().size(did()) != 0) {
cacheDropRule = getConnectionCache().get(did()).stream().filter(r -> (r.selector().equals(selectorDrop) && r.treatment().equals(treatmentDrop))).findFirst().orElse(null);
cacheAddRule = getConnectionCache().get(did()).stream().filter(r -> (r.selector().equals(selectorAdd) && r.treatment().equals(treatmentAdd))).findFirst().orElse(null);
}
// Include the DROP rule to the retrieved rules if found in cache
if ((cacheDropRule != null)) {
confirmedRules.add(cacheDropRule);
log.debug("fetchClientConnectionsFromDevice {} DROP CLIENT rule in the cache {}", did(), cacheDropRule);
} else {
log.warn("fetchClientConnectionsFromDevice {} DROP CLIENT rule not found in cache", did());
}
// Include the ADD rule to the retrieved rules if found in cache
if ((cacheAddRule != null)) {
confirmedRules.add(cacheAddRule);
log.debug("fetchClientConnectionsFromDevice {} ADD CLIENT rule in the cache {}", did(), cacheAddRule);
} else {
log.warn("fetchClientConnectionsFromDevice {} ADD CLIENT rule not found in cache", did());
}
if ((cacheDropRule == null) && (cacheAddRule == null)) {
log.warn("fetchClientConnectionsFromDevice {} ADD and DROP rule not included in the cache", did());
FlowRule deviceDropRule = DefaultFlowRule.builder().forDevice(data().deviceId()).makePermanent().withSelector(selectorDrop).withTreatment(treatmentDrop).withCookie(DEFAULT_RULE_COOKIE).withPriority(DEFAULT_RULE_PRIORITY).build();
FlowRule deviceAddRule = DefaultFlowRule.builder().forDevice(data().deviceId()).makePermanent().withSelector(selectorAdd).withTreatment(treatmentAdd).withCookie(DEFAULT_RULE_COOKIE).withPriority(DEFAULT_RULE_PRIORITY).build();
try {
// TODO this is not required if allowExternalFlowRules
TerminalDeviceFlowRule addRule = new TerminalDeviceFlowRule(deviceAddRule, getLinePorts());
removeFlowRule(session, addRule);
TerminalDeviceFlowRule dropRule = new TerminalDeviceFlowRule(deviceDropRule, getLinePorts());
removeFlowRule(session, dropRule);
} catch (NetconfException e) {
openConfigError("Error removing CLIENT rule from device", e);
}
}
return confirmedRules;
}
Aggregations