Move IntentCompilers to the dedicated package
Change-Id: I9ec09e5e5fce45acfc6417dd640f57fd659f7f1f
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/installer/LinkCollectionIntentInstaller.java b/core/net/src/main/java/org/onosproject/net/intent/impl/installer/LinkCollectionIntentInstaller.java
new file mode 100644
index 0000000..54ff5fc
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/installer/LinkCollectionIntentInstaller.java
@@ -0,0 +1,197 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.intent.impl.installer;
+
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import com.google.common.collect.SetMultimap;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.core.DefaultGroupId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleOperation;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.intent.IntentExtensionService;
+import org.onosproject.net.intent.IntentInstaller;
+import org.onosproject.net.intent.LinkCollectionIntent;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Installer for {@link org.onosproject.net.intent.LinkCollectionIntent} path
+ * segment intents.
+ */
+@Component(immediate = true)
+public class LinkCollectionIntentInstaller
+ implements IntentInstaller<LinkCollectionIntent> {
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected IntentExtensionService intentManager;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ private ApplicationId appId;
+
+ @Activate
+ public void activate() {
+ appId = coreService.registerApplication("org.onosproject.net.intent");
+ intentManager.registerInstaller(LinkCollectionIntent.class, this);
+ }
+
+ @Deactivate
+ public void deactivate() {
+ intentManager.unregisterInstaller(LinkCollectionIntent.class);
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> install(LinkCollectionIntent intent) {
+ return generateBatchOperations(intent, FlowRuleOperation.Type.ADD);
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> uninstall(LinkCollectionIntent intent) {
+ return generateBatchOperations(intent, FlowRuleOperation.Type.REMOVE);
+ }
+
+ private List<Collection<FlowRuleOperation>> generateBatchOperations(
+ LinkCollectionIntent intent, FlowRuleOperation.Type operation) {
+
+ //TODO do we need a set here?
+ SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create();
+ SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create();
+
+ for (Link link : intent.links()) {
+ inputPorts.put(link.dst().deviceId(), link.dst().port());
+ outputPorts.put(link.src().deviceId(), link.src().port());
+ }
+
+ for (ConnectPoint ingressPoint : intent.ingressPoints()) {
+ inputPorts.put(ingressPoint.deviceId(), ingressPoint.port());
+ }
+
+ for (ConnectPoint egressPoint : intent.egressPoints()) {
+ outputPorts.put(egressPoint.deviceId(), egressPoint.port());
+ }
+
+ List<FlowRuleOperation> rules = Lists.newArrayList();
+ outputPorts.keys().stream()
+ .map(deviceId -> createBatchEntries(operation,
+ intent, deviceId,
+ inputPorts.get(deviceId),
+ outputPorts.get(deviceId)))
+ .forEach(rules::addAll);
+
+ return Lists.newArrayList(ImmutableSet.of(rules));
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> replace(LinkCollectionIntent oldIntent,
+ LinkCollectionIntent newIntent) {
+ // FIXME: implement this in a more intelligent/less brute force way
+ List<Collection<FlowRuleOperation>> batches = Lists.newArrayList();
+ batches.addAll(uninstall(oldIntent));
+ batches.addAll(install(newIntent));
+ return batches;
+ }
+
+ /**
+ * Creates a collection of FlowRuleOperation based on the provided
+ * parameters.
+ *
+ * @param operation the FlowRuleOperation type to use
+ * @param intent the link collection intent
+ * @param deviceId the device ID for the flow rule
+ * @param inPorts the logical input ports of the flow rule
+ * @param outPorts the set of output ports for the flow rule
+ * @return a collection with the new flow rule batch entries
+ */
+ private Collection<FlowRuleOperation> createBatchEntries(
+ FlowRuleOperation.Type operation,
+ LinkCollectionIntent intent,
+ DeviceId deviceId,
+ Set<PortNumber> inPorts,
+ Set<PortNumber> outPorts) {
+ Collection<FlowRuleOperation> result = Lists.newLinkedList();
+ Set<PortNumber> ingressPorts = new HashSet<PortNumber>();
+
+ //
+ // Collect all ingress ports for this device.
+ // The intent treatment is applied only on those ports.
+ //
+ for (ConnectPoint cp : intent.ingressPoints()) {
+ if (cp.deviceId().equals(deviceId)) {
+ ingressPorts.add(cp.port());
+ }
+ }
+
+ //
+ // Create two treatments: one for setting the output ports,
+ // and a second one that applies the intent treatment and sets the
+ // output ports.
+ // NOTE: The second one is created only if there are ingress ports.
+ //
+ TrafficTreatment.Builder defaultTreatmentBuilder =
+ DefaultTrafficTreatment.builder();
+ for (PortNumber outPort : outPorts) {
+ defaultTreatmentBuilder.setOutput(outPort);
+ }
+ TrafficTreatment defaultTreatment = defaultTreatmentBuilder.build();
+ TrafficTreatment intentTreatment = null;
+ if (!ingressPorts.isEmpty()) {
+ TrafficTreatment.Builder intentTreatmentBuilder =
+ DefaultTrafficTreatment.builder(intent.treatment());
+ for (PortNumber outPort : outPorts) {
+ intentTreatmentBuilder.setOutput(outPort);
+ }
+ intentTreatment = intentTreatmentBuilder.build();
+ }
+
+ for (PortNumber inPort : inPorts) {
+ TrafficSelector selector = DefaultTrafficSelector
+ .builder(intent.selector()).matchInPort(inPort).build();
+ TrafficTreatment treatment = defaultTreatment;
+ if (ingressPorts.contains(inPort)) {
+ // Use the intent treatment if this is ingress port
+ treatment = intentTreatment;
+ }
+ FlowRule rule = new DefaultFlowRule(deviceId,
+ selector, treatment, 123, appId,
+ new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
+ 0, true);
+ result.add(new FlowRuleOperation(rule, operation));
+ }
+
+ return result;
+ }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/installer/MplsPathIntentInstaller.java b/core/net/src/main/java/org/onosproject/net/intent/impl/installer/MplsPathIntentInstaller.java
new file mode 100644
index 0000000..dbe9233
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/installer/MplsPathIntentInstaller.java
@@ -0,0 +1,290 @@
+package org.onosproject.net.intent.impl.installer;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.packet.Ethernet;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Link;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleOperation;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flow.criteria.Criteria.EthTypeCriterion;
+import org.onosproject.net.flow.criteria.Criterion;
+import org.onosproject.net.flow.criteria.Criterion.Type;
+import org.onosproject.net.intent.IntentExtensionService;
+import org.onosproject.net.intent.IntentInstaller;
+import org.onosproject.net.intent.MplsPathIntent;
+import org.onosproject.net.link.LinkStore;
+import org.onosproject.net.resource.DefaultLinkResourceRequest;
+import org.onosproject.net.resource.LinkResourceAllocations;
+import org.onosproject.net.resource.LinkResourceRequest;
+import org.onosproject.net.resource.LinkResourceService;
+import org.onosproject.net.resource.MplsLabel;
+import org.onosproject.net.resource.MplsLabelResourceAllocation;
+import org.onosproject.net.resource.ResourceAllocation;
+import org.onosproject.net.resource.ResourceType;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Installer for {@link MplsPathIntent packet path connectivity intents}.
+ */
+@Component(immediate = true)
+public class MplsPathIntentInstaller implements IntentInstaller<MplsPathIntent> {
+
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected IntentExtensionService intentManager;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LinkResourceService resourceService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LinkStore linkStore;
+
+ protected ApplicationId appId;
+
+ @Activate
+ public void activate() {
+ appId = coreService.registerApplication("org.onosproject.net.intent");
+ intentManager.registerInstaller(MplsPathIntent.class, this);
+ }
+
+ @Deactivate
+ public void deactivate() {
+ intentManager.unregisterInstaller(MplsPathIntent.class);
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> install(MplsPathIntent intent) {
+ LinkResourceAllocations allocations = assignMplsLabel(intent);
+ return generateRules(intent, allocations, FlowRuleOperation.Type.ADD);
+
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> uninstall(MplsPathIntent intent) {
+ LinkResourceAllocations allocations = resourceService
+ .getAllocations(intent.id());
+ resourceService.releaseResources(allocations);
+
+ return generateRules(intent, allocations, FlowRuleOperation.Type.REMOVE);
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> replace(MplsPathIntent oldIntent,
+ MplsPathIntent newIntent) {
+ //FIXME this is brute force
+ List<Collection<FlowRuleOperation>> batches = Lists.newArrayList();
+ batches.addAll(uninstall(oldIntent));
+ batches.addAll(install(newIntent));
+ return batches;
+ }
+
+ private LinkResourceAllocations assignMplsLabel(MplsPathIntent intent) {
+
+ // TODO: do it better... Suggestions?
+ Set<Link> linkRequest = Sets.newHashSetWithExpectedSize(intent.path()
+ .links().size() - 2);
+ for (int i = 1; i <= intent.path().links().size() - 2; i++) {
+ Link link = intent.path().links().get(i);
+ linkRequest.add(link);
+ // add the inverse link. I want that the label is reserved both for
+ // the direct and inverse link
+ linkRequest.add(linkStore.getLink(link.dst(), link.src()));
+ }
+
+ LinkResourceRequest.Builder request = DefaultLinkResourceRequest
+ .builder(intent.id(), linkRequest).addMplsRequest();
+ LinkResourceAllocations reqMpls = resourceService
+ .requestResources(request.build());
+ return reqMpls;
+ }
+
+ private MplsLabel getMplsLabel(LinkResourceAllocations allocations,
+ Link link) {
+
+ for (ResourceAllocation allocation : allocations
+ .getResourceAllocation(link)) {
+ if (allocation.type() == ResourceType.MPLS_LABEL) {
+ return ((MplsLabelResourceAllocation) allocation).mplsLabel();
+
+ }
+ }
+ log.warn("MPLS label was not assigned successfully");
+ return null;
+ }
+
+ private List<Collection<FlowRuleOperation>> generateRules(MplsPathIntent intent,
+ LinkResourceAllocations allocations,
+ FlowRuleOperation.Type operation) {
+
+ Iterator<Link> links = intent.path().links().iterator();
+ Link srcLink = links.next();
+ ConnectPoint prev = srcLink.dst();
+
+ Link link = links.next();
+ // List of flow rules to be installed
+ List<FlowRuleOperation> rules = Lists.newLinkedList();
+
+ // Ingress traffic
+ // Get the new MPLS label
+ MplsLabel mpls = getMplsLabel(allocations, link);
+ checkNotNull(mpls);
+ MplsLabel prevLabel = mpls;
+ rules.add(ingressFlow(prev.port(), link, intent, mpls, operation));
+
+ prev = link.dst();
+
+ while (links.hasNext()) {
+
+ link = links.next();
+
+ if (links.hasNext()) {
+ // Transit traffic
+ // Get the new MPLS label
+ mpls = getMplsLabel(allocations, link);
+ checkNotNull(mpls);
+ rules.add(transitFlow(prev.port(), link, intent,
+ prevLabel, mpls, operation));
+ prevLabel = mpls;
+
+ } else {
+ // Egress traffic
+ rules.add(egressFlow(prev.port(), link, intent,
+ prevLabel, operation));
+ }
+
+ prev = link.dst();
+ }
+ return Lists.newArrayList(ImmutableSet.of(rules));
+ }
+
+ private FlowRuleOperation ingressFlow(PortNumber inPort, Link link,
+ MplsPathIntent intent,
+ MplsLabel label,
+ FlowRuleOperation.Type operation) {
+
+ TrafficSelector.Builder ingressSelector = DefaultTrafficSelector
+ .builder(intent.selector());
+ TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
+ ingressSelector.matchInPort(inPort);
+
+ if (intent.ingressLabel().isPresent()) {
+ ingressSelector.matchEthType(Ethernet.MPLS_UNICAST)
+ .matchMplsLabel(intent.ingressLabel().get());
+
+ // Swap the MPLS label
+ treat.setMpls(label.label());
+ } else {
+ // Push and set the MPLS label
+ treat.pushMpls().setMpls(label.label());
+ }
+ // Add the output action
+ treat.setOutput(link.src().port());
+
+ return flowRuleOperation(intent, link.src().deviceId(),
+ ingressSelector.build(), treat.build(),
+ operation);
+ }
+
+ private FlowRuleOperation transitFlow(PortNumber inPort, Link link,
+ MplsPathIntent intent,
+ MplsLabel prevLabel,
+ MplsLabel outLabel,
+ FlowRuleOperation.Type operation) {
+
+ // Ignore the ingress Traffic Selector and use only the MPLS label
+ // assigned in the previous link
+ TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+ selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
+ .matchMplsLabel(prevLabel.label());
+ TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder();
+
+ // Set the new label only if the label on the packet is
+ // different
+ if (prevLabel.equals(outLabel)) {
+ treat.setMpls(outLabel.label());
+ }
+
+ treat.setOutput(link.src().port());
+ return flowRuleOperation(intent, link.src().deviceId(),
+ selector.build(), treat.build(), operation);
+ }
+
+ private FlowRuleOperation egressFlow(PortNumber inPort, Link link,
+ MplsPathIntent intent,
+ MplsLabel prevLabel,
+ FlowRuleOperation.Type operation) {
+ // egress point: either set the egress MPLS label or pop the
+ // MPLS label based on the intent annotations
+
+ TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+ selector.matchInPort(inPort).matchEthType(Ethernet.MPLS_UNICAST)
+ .matchMplsLabel(prevLabel.label());
+
+ // apply the intent's treatments
+ TrafficTreatment.Builder treat = DefaultTrafficTreatment.builder(intent
+ .treatment());
+
+ if (intent.egressLabel().isPresent()) {
+ treat.setMpls(intent.egressLabel().get());
+ } else {
+ // if the ingress ethertype is defined, the egress traffic
+ // will be use that value, otherwise the IPv4 ethertype is used.
+ Criterion c = intent.selector().getCriterion(Type.ETH_TYPE);
+ if (c != null && c instanceof EthTypeCriterion) {
+ EthTypeCriterion ethertype = (EthTypeCriterion) c;
+ treat.popMpls((short) ethertype.ethType());
+ } else {
+ treat.popMpls(Ethernet.TYPE_IPV4);
+ }
+
+ }
+ treat.setOutput(link.src().port());
+ return flowRuleOperation(intent, link.src().deviceId(),
+ selector.build(), treat.build(), operation);
+ }
+
+ protected FlowRuleOperation flowRuleOperation(MplsPathIntent intent,
+ DeviceId deviceId,
+ TrafficSelector selector,
+ TrafficTreatment treat,
+ FlowRuleOperation.Type operation) {
+ FlowRule rule = new DefaultFlowRule(
+ deviceId,
+ selector,
+ treat,
+ 123, // FIXME 123
+ appId,
+ 0,
+ true);
+ return new FlowRuleOperation(rule, operation);
+
+ }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/installer/OpticalPathIntentInstaller.java b/core/net/src/main/java/org/onosproject/net/intent/impl/installer/OpticalPathIntentInstaller.java
new file mode 100644
index 0000000..5fa4d49
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/installer/OpticalPathIntentInstaller.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.intent.impl.installer;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Link;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleOperation;
+import org.onosproject.net.flow.FlowRuleService;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.intent.IntentExtensionService;
+import org.onosproject.net.intent.IntentInstaller;
+import org.onosproject.net.intent.OpticalPathIntent;
+import org.onosproject.net.resource.DefaultLinkResourceRequest;
+import org.onosproject.net.resource.Lambda;
+import org.onosproject.net.resource.LambdaResourceAllocation;
+import org.onosproject.net.resource.LinkResourceAllocations;
+import org.onosproject.net.resource.LinkResourceRequest;
+import org.onosproject.net.resource.LinkResourceService;
+import org.onosproject.net.resource.ResourceAllocation;
+import org.onosproject.net.resource.ResourceType;
+import org.onosproject.net.topology.TopologyService;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.List;
+
+import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Installer for {@link org.onosproject.net.intent.OpticalPathIntent optical path connectivity intents}.
+ */
+@Component(immediate = true)
+public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIntent> {
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected IntentExtensionService intentManager;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected FlowRuleService flowRuleService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected TopologyService topologyService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LinkResourceService resourceService;
+
+ private ApplicationId appId;
+
+ static final short SIGNAL_TYPE = (short) 1;
+
+ @Activate
+ public void activate() {
+ appId = coreService.registerApplication("org.onosproject.net.intent");
+ intentManager.registerInstaller(OpticalPathIntent.class, this);
+ }
+
+ @Deactivate
+ public void deactivate() {
+ intentManager.unregisterInstaller(OpticalPathIntent.class);
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> install(OpticalPathIntent intent) {
+ LinkResourceAllocations allocations = assignWavelength(intent);
+ return generateRules(intent, allocations, FlowRuleOperation.Type.ADD);
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> uninstall(OpticalPathIntent intent) {
+ LinkResourceAllocations allocations = resourceService.getAllocations(intent.id());
+ List<Collection<FlowRuleOperation>> rules = generateRules(intent, allocations, FlowRuleOperation.Type.REMOVE);
+ log.info("uninstall rules: {}", rules);
+ return rules;
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> replace(OpticalPathIntent oldIntent,
+ OpticalPathIntent newIntent) {
+ // FIXME: implement this
+ List<Collection<FlowRuleOperation>> batches = Lists.newArrayList();
+ batches.addAll(uninstall(oldIntent));
+ batches.addAll(install(newIntent));
+ return batches;
+ }
+
+ private LinkResourceAllocations assignWavelength(OpticalPathIntent intent) {
+ LinkResourceRequest.Builder request = DefaultLinkResourceRequest.builder(intent.id(),
+ intent.path().links())
+ .addLambdaRequest();
+ LinkResourceAllocations retLambda = resourceService.requestResources(request.build());
+ return retLambda;
+ }
+
+ private List<Collection<FlowRuleOperation>> generateRules(OpticalPathIntent intent,
+ LinkResourceAllocations allocations,
+ FlowRuleOperation.Type operation) {
+ TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
+ selectorBuilder.matchInPort(intent.src().port());
+
+ List<FlowRuleOperation> rules = Lists.newLinkedList();
+ ConnectPoint prev = intent.src();
+
+ //FIXME check for null allocations
+ //TODO throw exception if the lambda was not assigned successfully
+ for (Link link : intent.path().links()) {
+ Lambda la = null;
+ for (ResourceAllocation allocation : allocations.getResourceAllocation(link)) {
+ if (allocation.type() == ResourceType.LAMBDA) {
+ la = ((LambdaResourceAllocation) allocation).lambda();
+ break;
+ }
+ }
+
+ if (la == null) {
+ log.info("Lambda was not assigned successfully");
+ return null;
+ }
+
+ TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
+ treatmentBuilder.setOutput(link.src().port());
+ treatmentBuilder.setLambda((short) la.toInt());
+
+ FlowRule rule = new DefaultFlowRule(prev.deviceId(),
+ selectorBuilder.build(),
+ treatmentBuilder.build(),
+ 100,
+ appId,
+ 100,
+ true);
+
+ rules.add(new FlowRuleOperation(rule, operation));
+
+ prev = link.dst();
+ selectorBuilder.matchInPort(link.dst().port());
+ selectorBuilder.matchOpticalSignalType(SIGNAL_TYPE); //todo
+ selectorBuilder.matchLambda((short) la.toInt());
+
+ }
+
+ // build the last T port rule
+ TrafficTreatment.Builder treatmentLast = builder();
+ treatmentLast.setOutput(intent.dst().port());
+ FlowRule rule = new DefaultFlowRule(intent.dst().deviceId(),
+ selectorBuilder.build(),
+ treatmentLast.build(),
+ 100,
+ appId,
+ 100,
+ true);
+ rules.add(new FlowRuleOperation(rule, operation));
+
+ //FIXME change to new api
+ return Lists.newArrayList(ImmutableSet.of(rules));
+ }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/installer/PathIntentInstaller.java b/core/net/src/main/java/org/onosproject/net/intent/impl/installer/PathIntentInstaller.java
new file mode 100644
index 0000000..5230736
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/installer/PathIntentInstaller.java
@@ -0,0 +1,180 @@
+/*
+ * Copyright 2014 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.net.intent.impl.installer;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Lists;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.core.DefaultGroupId;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.Link;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleOperation;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.intent.Constraint;
+import org.onosproject.net.intent.IntentExtensionService;
+import org.onosproject.net.intent.IntentInstaller;
+import org.onosproject.net.intent.PathIntent;
+import org.onosproject.net.resource.DefaultLinkResourceRequest;
+import org.onosproject.net.resource.LinkResourceAllocations;
+import org.onosproject.net.resource.LinkResourceRequest;
+import org.onosproject.net.resource.LinkResourceService;
+import org.slf4j.Logger;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Installer for {@link PathIntent packet path connectivity intents}.
+ */
+@Component(immediate = true)
+public class PathIntentInstaller implements IntentInstaller<PathIntent> {
+
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected IntentExtensionService intentManager;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected CoreService coreService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LinkResourceService resourceService;
+
+ protected ApplicationId appId;
+
+ @Activate
+ public void activate() {
+ appId = coreService.registerApplication("org.onosproject.net.intent");
+ intentManager.registerInstaller(PathIntent.class, this);
+ }
+
+ @Deactivate
+ public void deactivate() {
+ intentManager.unregisterInstaller(PathIntent.class);
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> install(PathIntent intent) {
+ LinkResourceAllocations allocations = allocateResources(intent);
+
+ TrafficSelector.Builder builder =
+ DefaultTrafficSelector.builder(intent.selector());
+ Iterator<Link> links = intent.path().links().iterator();
+ ConnectPoint prev = links.next().dst();
+ List<FlowRuleOperation> rules = Lists.newLinkedList();
+ // TODO Generate multiple batches
+ while (links.hasNext()) {
+ builder.matchInPort(prev.port());
+ Link link = links.next();
+ // if this is the last flow rule, apply the intent's treatments
+ TrafficTreatment treatment =
+ (links.hasNext() ? builder() : builder(intent.treatment()))
+ .setOutput(link.src().port()).build();
+
+ FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
+ builder.build(), treatment, 123, //FIXME 123
+ appId,
+ new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
+ 0, true);
+ rules.add(new FlowRuleOperation(rule, FlowRuleOperation.Type.ADD));
+ prev = link.dst();
+ }
+
+ return Lists.newArrayList(ImmutableSet.of(rules));
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> uninstall(PathIntent intent) {
+ deallocateResources(intent);
+ TrafficSelector.Builder builder =
+ DefaultTrafficSelector.builder(intent.selector());
+ Iterator<Link> links = intent.path().links().iterator();
+ ConnectPoint prev = links.next().dst();
+ List<FlowRuleOperation> rules = Lists.newLinkedList();
+ // TODO Generate multiple batches
+ while (links.hasNext()) {
+ builder.matchInPort(prev.port());
+ Link link = links.next();
+ // if this is the last flow rule, apply the intent's treatments
+ TrafficTreatment treatment =
+ (links.hasNext() ? builder() : builder(intent.treatment()))
+ .setOutput(link.src().port()).build();
+ FlowRule rule = new DefaultFlowRule(link.src().deviceId(),
+ builder.build(), treatment, 123, appId,
+ new DefaultGroupId((short) (intent.id().fingerprint() & 0xffff)),
+ 0, true);
+ rules.add(new FlowRuleOperation(rule, FlowRuleOperation.Type.REMOVE));
+ prev = link.dst();
+ }
+ // FIXME this should change to new api
+ return Lists.newArrayList(ImmutableSet.of(rules));
+ }
+
+ @Override
+ public List<Collection<FlowRuleOperation>> replace(PathIntent oldIntent, PathIntent newIntent) {
+ // FIXME: implement this
+ List<Collection<FlowRuleOperation>> batches = Lists.newArrayList();
+ batches.addAll(uninstall(oldIntent));
+ batches.addAll(install(newIntent));
+ return batches;
+ }
+
+ /**
+ * Allocate resources required for an intent.
+ *
+ * @param intent intent to allocate resource for
+ * @return allocated resources if any are required, null otherwise
+ */
+ private LinkResourceAllocations allocateResources(PathIntent intent) {
+ LinkResourceRequest.Builder builder =
+ DefaultLinkResourceRequest.builder(intent.id(), intent.path().links());
+ for (Constraint constraint : intent.constraints()) {
+ builder.addConstraint(constraint);
+ }
+ LinkResourceRequest request = builder.build();
+ return request.resources().isEmpty() ? null : resourceService.requestResources(request);
+ }
+
+ /**
+ * Deallocate resources held by an intent.
+ *
+ * @param intent intent to deallocate resources for
+ */
+ private void deallocateResources(PathIntent intent) {
+ if (intent.constraints().isEmpty()) {
+ return;
+ }
+
+ LinkResourceAllocations allocatedResources = resourceService.getAllocations(intent.id());
+ if (allocatedResources != null) {
+ resourceService.releaseResources(allocatedResources);
+ }
+ }
+}