Search in sources :

Example 1 with PolicyType

use of org.onosproject.segmentrouting.policy.api.Policy.PolicyType in project trellis-control by opennetworkinglab.

the class PolicyManager method installTrafficMatchToDevice.

// Orchestrate traffic match installation according to the type
private void installTrafficMatchToDevice(DeviceId deviceId, TrafficMatch trafficMatch) {
    if (log.isDebugEnabled()) {
        log.debug("Installing traffic match {} associated to policy {}", trafficMatch.trafficMatchId(), trafficMatch.policyId());
    }
    TrafficMatchKey trafficMatchKey = new TrafficMatchKey(deviceId, trafficMatch.trafficMatchId());
    Operation oldTrafficOperation = Versioned.valueOrNull(operations.get(trafficMatchKey.toString()));
    if (oldTrafficOperation != null && oldTrafficOperation.isInstall()) {
        if (trafficMatch.equals(oldTrafficOperation.trafficMatch().orElse(null))) {
            if (log.isDebugEnabled()) {
                log.debug("There is already an install operation for traffic match {} associated to policy {} " + "for device {}", trafficMatch.trafficMatchId(), trafficMatch.policyId(), deviceId);
            }
            // If we add or submit a trafficMatch multiple times
            // We skip the installation and update the state directly
            updateTrafficMatch(trafficMatch, true);
            return;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Starts updating traffic match {} associated to policy {} " + "for device {}", trafficMatch.trafficMatchId(), trafficMatch.policyId(), deviceId);
            }
        }
    }
    // For the DROP policy we need to set an ACL drop in the fwd objective. The other
    // policies require to retrieve the next Id and sets the next step.
    PolicyKey policyKey = new PolicyKey(deviceId, trafficMatch.policyId());
    Operation policyOperation = Versioned.valueOrNull(operations.get(policyKey.toString()));
    if (policyOperation == null || !policyOperation.isDone() || !policyOperation.isInstall() || policyOperation.policy().isEmpty() || (policyOperation.policy().get().policyType() == PolicyType.REDIRECT && policyOperation.objectiveOperation() == null)) {
        log.info("Deferring traffic match {} installation on device {}. Policy {} not yet installed", trafficMatch.trafficMatchId(), deviceId, trafficMatch.policyId());
        return;
    }
    // Updates the store and then send the versatile fwd objective to the pipeliner
    Operation newTrafficOperation = Operation.builder().isInstall(true).trafficMatch(trafficMatch).build();
    operations.put(trafficMatchKey.toString(), newTrafficOperation);
    Policy policy = policyOperation.policy().get();
    ForwardingObjective.Builder builder = trafficMatchFwdObjective(trafficMatch, policy.policyType());
    if (policy.policyType() == PolicyType.DROP) {
        // Firstly builds the fwd objective with the wipeDeferred action.
        TrafficTreatment dropTreatment = DefaultTrafficTreatment.builder().wipeDeferred().build();
        builder.withTreatment(dropTreatment);
    } else if (policy.policyType() == PolicyType.REDIRECT) {
        // Here we need to set only the next step
        builder.nextStep(policyOperation.objectiveOperation().id());
    }
    // Once, the fwd objective has completed its execution, we update the policiesOps map
    CompletableFuture<Objective> addNewFuture = new CompletableFuture<>();
    CompletableFuture<Objective> removeOldFuture = new CompletableFuture<>();
    if (log.isDebugEnabled()) {
        log.debug("Installing forwarding objective for dev: {}", deviceId);
    }
    ObjectiveContext addNewContext = new DefaultObjectiveContext((objective) -> {
        if (log.isDebugEnabled()) {
            log.debug("Forwarding objective for policy {} installed", trafficMatch.policyId());
        }
        addNewFuture.complete(objective);
    }, (objective, error) -> {
        log.warn("Failed to install forwarding objective for policy {}: {}", trafficMatch.policyId(), error);
        addNewFuture.complete(null);
    });
    ObjectiveContext removeOldContext = new DefaultObjectiveContext((objective) -> {
        if (log.isDebugEnabled()) {
            log.debug("Old forwarding objective for policy {} removed, update finished", trafficMatch.policyId());
        }
        removeOldFuture.complete(objective);
    }, (objective, error) -> {
        log.warn("Failed to remove old forwarding objective for policy {}: {}", trafficMatch.policyId(), error);
        removeOldFuture.complete(null);
    });
    // Context is not serializable
    ForwardingObjective serializableObjective = builder.add();
    flowObjectiveService.forward(deviceId, builder.add(addNewContext));
    addNewFuture.whenComplete((objective, ex) -> {
        if (ex != null) {
            log.error("Exception installing forwarding objective", ex);
        } else if (objective != null) {
            // base on priority, selector and metadata change
            if (oldTrafficOperation != null && oldTrafficOperation.objectiveOperation() != null && oldTrafficOperation.isInstall() && (oldTrafficOperation.objectiveOperation().priority() != serializableObjective.priority() || !((ForwardingObjective) oldTrafficOperation.objectiveOperation()).selector().equals(serializableObjective.selector()) || !((ForwardingObjective) oldTrafficOperation.objectiveOperation()).meta().equals(serializableObjective.meta()))) {
                ForwardingObjective oldFwdObj = (ForwardingObjective) oldTrafficOperation.objectiveOperation();
                ForwardingObjective.Builder oldBuilder = DefaultForwardingObjective.builder(oldFwdObj);
                flowObjectiveService.forward(deviceId, oldBuilder.remove(removeOldContext));
            } else {
                operations.computeIfPresent(trafficMatchKey.toString(), (k, v) -> {
                    if (!v.isDone() && v.isInstall()) {
                        v.isDone(true);
                        v.objectiveOperation(serializableObjective);
                    }
                    return v;
                });
            }
        }
    });
    removeOldFuture.whenComplete((objective, ex) -> {
        if (ex != null) {
            log.error("Exception removing old forwarding objective", ex);
        } else if (objective != null) {
            operations.computeIfPresent(trafficMatchKey.toString(), (k, v) -> {
                if (!v.isDone() && v.isInstall()) {
                    v.isDone(true);
                    v.objectiveOperation(serializableObjective);
                }
                return v;
            });
        }
    });
}
Also used : DropPolicy(org.onosproject.segmentrouting.policy.api.DropPolicy) Policy(org.onosproject.segmentrouting.policy.api.Policy) RedirectPolicy(org.onosproject.segmentrouting.policy.api.RedirectPolicy) DeviceConfigNotFoundException(org.onosproject.segmentrouting.config.DeviceConfigNotFoundException) ConsistentMap(org.onosproject.store.service.ConsistentMap) NetworkConfigRegistry(org.onosproject.net.config.NetworkConfigRegistry) PredictableExecutor(org.onlab.util.PredictableExecutor) CoreService(org.onosproject.core.CoreService) DeviceService(org.onosproject.net.device.DeviceService) Tools.groupedThreads(org.onlab.util.Tools.groupedThreads) DefaultNextObjective(org.onosproject.net.flowobjective.DefaultNextObjective) DropPolicy(org.onosproject.segmentrouting.policy.api.DropPolicy) ForwardingObjective(org.onosproject.net.flowobjective.ForwardingObjective) Link(org.onosproject.net.Link) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) ConnectPoint(org.onosproject.net.ConnectPoint) Sets(org.glassfish.jersey.internal.guava.Sets) StorageService(org.onosproject.store.service.StorageService) Map(java.util.Map) ApplicationId(org.onosproject.core.ApplicationId) NextObjective(org.onosproject.net.flowobjective.NextObjective) KryoNamespaces(org.onosproject.store.serializers.KryoNamespaces) EDGE_PORT(org.onosproject.segmentrouting.metadata.SRObjectiveMetadata.EDGE_PORT) WorkPartitionService(org.onosproject.net.intent.WorkPartitionService) NodeId(org.onosproject.cluster.NodeId) Serializer(org.onosproject.store.service.Serializer) ImmutableSet(com.google.common.collect.ImmutableSet) Deactivate(org.osgi.service.component.annotations.Deactivate) Set(java.util.Set) DefaultForwardingObjective(org.onosproject.net.flowobjective.DefaultForwardingObjective) Collectors(java.util.stream.Collectors) TrafficMatchData(org.onosproject.segmentrouting.policy.api.TrafficMatchData) PolicyType(org.onosproject.segmentrouting.policy.api.Policy.PolicyType) Executors(java.util.concurrent.Executors) Versioned(org.onosproject.store.service.Versioned) List(java.util.List) ObjectiveContext(org.onosproject.net.flowobjective.ObjectiveContext) Policy(org.onosproject.segmentrouting.policy.api.Policy) TrafficMatchState(org.onosproject.segmentrouting.policy.api.TrafficMatchState) RedirectPolicy(org.onosproject.segmentrouting.policy.api.RedirectPolicy) LinkService(org.onosproject.net.link.LinkService) Optional(java.util.Optional) ClusterService(org.onosproject.cluster.ClusterService) HashFunction(com.google.common.hash.HashFunction) DeviceId(org.onosproject.net.DeviceId) PolicyService(org.onosproject.segmentrouting.policy.api.PolicyService) StorageException(org.onosproject.store.service.StorageException) NetworkConfigEvent(org.onosproject.net.config.NetworkConfigEvent) Hashing(com.google.common.hash.Hashing) SegmentRoutingDeviceConfig(org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig) CompletableFuture(java.util.concurrent.CompletableFuture) PolicyState(org.onosproject.segmentrouting.policy.api.PolicyState) KryoNamespace(org.onlab.util.KryoNamespace) MapEventListener(org.onosproject.store.service.MapEventListener) ArrayList(java.util.ArrayList) FlowObjectiveService(org.onosproject.net.flowobjective.FlowObjectiveService) TrafficMatch(org.onosproject.segmentrouting.policy.api.TrafficMatch) Component(org.osgi.service.component.annotations.Component) Lists(com.google.common.collect.Lists) TrafficSelector(org.onosproject.net.flow.TrafficSelector) Activate(org.osgi.service.component.annotations.Activate) DefaultTrafficSelector(org.onosproject.net.flow.DefaultTrafficSelector) DefaultObjectiveContext(org.onosproject.net.flowobjective.DefaultObjectiveContext) ExecutorService(java.util.concurrent.ExecutorService) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) Logger(org.slf4j.Logger) TrafficMatchPriority(org.onosproject.segmentrouting.policy.api.TrafficMatchPriority) Maps(com.google.common.collect.Maps) CodecService(org.onosproject.codec.CodecService) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) PolicyId(org.onosproject.segmentrouting.policy.api.PolicyId) TrafficMatchId(org.onosproject.segmentrouting.policy.api.TrafficMatchId) MapEvent(org.onosproject.store.service.MapEvent) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Objective(org.onosproject.net.flowobjective.Objective) MacAddress(org.onlab.packet.MacAddress) Reference(org.osgi.service.component.annotations.Reference) NetworkConfigListener(org.onosproject.net.config.NetworkConfigListener) PolicyData(org.onosproject.segmentrouting.policy.api.PolicyData) ForwardingObjective(org.onosproject.net.flowobjective.ForwardingObjective) DefaultForwardingObjective(org.onosproject.net.flowobjective.DefaultForwardingObjective) DefaultTrafficTreatment(org.onosproject.net.flow.DefaultTrafficTreatment) TrafficTreatment(org.onosproject.net.flow.TrafficTreatment) DefaultNextObjective(org.onosproject.net.flowobjective.DefaultNextObjective) ForwardingObjective(org.onosproject.net.flowobjective.ForwardingObjective) NextObjective(org.onosproject.net.flowobjective.NextObjective) DefaultForwardingObjective(org.onosproject.net.flowobjective.DefaultForwardingObjective) Objective(org.onosproject.net.flowobjective.Objective) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultObjectiveContext(org.onosproject.net.flowobjective.DefaultObjectiveContext) ObjectiveContext(org.onosproject.net.flowobjective.ObjectiveContext) DefaultObjectiveContext(org.onosproject.net.flowobjective.DefaultObjectiveContext)

Example 2 with PolicyType

use of org.onosproject.segmentrouting.policy.api.Policy.PolicyType in project trellis-control by opennetworkinglab.

the class PolicyWebResource method getPolicies.

/**
 * Get all Policies.
 *
 * @return 200 OK will a collection of Policies
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getPolicies() {
    PolicyService policyService = get(PolicyService.class);
    ObjectNode root = mapper().createObjectNode();
    ArrayNode policiesArr = root.putArray(POLICY);
    // Create a filter set contains all PolicyType
    Set<PolicyType> policyTypes = Set.of(PolicyType.values());
    for (PolicyData policyData : policyService.policies(policyTypes)) {
        Policy policy = policyData.policy();
        switch(policy.policyType()) {
            case DROP:
                policiesArr.add(codec(DropPolicy.class).encode((DropPolicy) policy, this));
                break;
            case REDIRECT:
                policiesArr.add(codec(RedirectPolicy.class).encode((RedirectPolicy) policy, this));
                break;
            default:
                continue;
        }
    }
    return Response.ok(root).build();
}
Also used : DropPolicy(org.onosproject.segmentrouting.policy.api.DropPolicy) Policy(org.onosproject.segmentrouting.policy.api.Policy) RedirectPolicy(org.onosproject.segmentrouting.policy.api.RedirectPolicy) PolicyType(org.onosproject.segmentrouting.policy.api.Policy.PolicyType) RedirectPolicy(org.onosproject.segmentrouting.policy.api.RedirectPolicy) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PolicyService(org.onosproject.segmentrouting.policy.api.PolicyService) PolicyData(org.onosproject.segmentrouting.policy.api.PolicyData) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) DropPolicy(org.onosproject.segmentrouting.policy.api.DropPolicy) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with PolicyType

use of org.onosproject.segmentrouting.policy.api.Policy.PolicyType in project trellis-control by opennetworkinglab.

the class PolicyWebResource method getDropPolicies.

/**
 * Get all Drop Policies.
 *
 * @return 200 OK will a collection of Dop Policies
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("drop")
public Response getDropPolicies() {
    PolicyService policyService = get(PolicyService.class);
    ObjectNode root = mapper().createObjectNode();
    ArrayNode policiesArr = root.putArray(POLICY);
    Set<PolicyType> policyTypes = Set.of(PolicyType.DROP);
    for (PolicyData policyData : policyService.policies(policyTypes)) {
        Policy policy = policyData.policy();
        policiesArr.add(codec(DropPolicy.class).encode((DropPolicy) policy, this));
    }
    return Response.ok(root).build();
}
Also used : DropPolicy(org.onosproject.segmentrouting.policy.api.DropPolicy) Policy(org.onosproject.segmentrouting.policy.api.Policy) RedirectPolicy(org.onosproject.segmentrouting.policy.api.RedirectPolicy) PolicyType(org.onosproject.segmentrouting.policy.api.Policy.PolicyType) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PolicyService(org.onosproject.segmentrouting.policy.api.PolicyService) PolicyData(org.onosproject.segmentrouting.policy.api.PolicyData) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) DropPolicy(org.onosproject.segmentrouting.policy.api.DropPolicy) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with PolicyType

use of org.onosproject.segmentrouting.policy.api.Policy.PolicyType in project trellis-control by opennetworkinglab.

the class PolicyWebResource method getRedirectPolicies.

/**
 * Get all Redirect Policies.
 *
 * @return 200 OK will a collection of Redirect Policies
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("redirect")
public Response getRedirectPolicies() {
    PolicyService policyService = get(PolicyService.class);
    ObjectNode root = mapper().createObjectNode();
    ArrayNode policiesArr = root.putArray(POLICY);
    Set<PolicyType> policyTypes = Set.of(PolicyType.REDIRECT);
    for (PolicyData policyData : policyService.policies(policyTypes)) {
        Policy policy = policyData.policy();
        policiesArr.add(codec(RedirectPolicy.class).encode((RedirectPolicy) policy, this));
    }
    return Response.ok(root).build();
}
Also used : DropPolicy(org.onosproject.segmentrouting.policy.api.DropPolicy) Policy(org.onosproject.segmentrouting.policy.api.Policy) RedirectPolicy(org.onosproject.segmentrouting.policy.api.RedirectPolicy) PolicyType(org.onosproject.segmentrouting.policy.api.Policy.PolicyType) RedirectPolicy(org.onosproject.segmentrouting.policy.api.RedirectPolicy) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PolicyService(org.onosproject.segmentrouting.policy.api.PolicyService) PolicyData(org.onosproject.segmentrouting.policy.api.PolicyData) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 GET (javax.ws.rs.GET)3 Produces (javax.ws.rs.Produces)3 DropPolicy (org.onosproject.segmentrouting.policy.api.DropPolicy)3 Policy (org.onosproject.segmentrouting.policy.api.Policy)3 PolicyType (org.onosproject.segmentrouting.policy.api.Policy.PolicyType)3 PolicyData (org.onosproject.segmentrouting.policy.api.PolicyData)3 PolicyService (org.onosproject.segmentrouting.policy.api.PolicyService)3 RedirectPolicy (org.onosproject.segmentrouting.policy.api.RedirectPolicy)3 Path (javax.ws.rs.Path)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 HashFunction (com.google.common.hash.HashFunction)1 Hashing (com.google.common.hash.Hashing)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1