Add path intent compiler that generates flow objective intents

Change-Id: I11bee398d927f0e3f32b7cf81d98cfe5816db477
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentFlowObjectiveCompiler.java b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentFlowObjectiveCompiler.java
new file mode 100644
index 0000000..25cae18
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/compiler/PathIntentFlowObjectiveCompiler.java
@@ -0,0 +1,130 @@
+/*
+ * Copyright 2016 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.compiler;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+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.DeviceId;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.flowobjective.DefaultForwardingObjective;
+import org.onosproject.net.flowobjective.ForwardingObjective;
+import org.onosproject.net.flowobjective.Objective;
+import org.onosproject.net.intent.FlowObjectiveIntent;
+import org.onosproject.net.intent.Intent;
+import org.onosproject.net.intent.IntentCompiler;
+import org.onosproject.net.intent.IntentExtensionService;
+import org.onosproject.net.intent.PathIntent;
+import org.onosproject.net.newresource.ResourceService;
+import org.onosproject.net.resource.link.LinkResourceAllocations;
+import org.slf4j.Logger;
+
+import com.google.common.collect.ImmutableList;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+@Component(immediate = true)
+public class PathIntentFlowObjectiveCompiler
+        extends PathCompiler<Objective>
+        implements IntentCompiler<PathIntent>,
+                   PathCompiler.PathCompilerCreateFlow<Objective> {
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected IntentExtensionService intentManager;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ResourceService resourceService;
+
+    private ApplicationId appId;
+
+    @Activate
+    public void activate() {
+        appId = coreService.registerApplication("org.onosproject.net.intent");
+        //intentManager.registerCompiler(PathIntent.class, this);
+    }
+
+    @Deactivate
+    public void deactivate() {
+        //intentManager.unregisterCompiler(PathIntent.class);
+    }
+
+    @Override
+    public List<Intent> compile(PathIntent intent, List<Intent> installable,
+                                Set<LinkResourceAllocations> resources) {
+
+        List<Objective> objectives = new LinkedList<>();
+        List<DeviceId> devices = new LinkedList<>();
+        compile(this, intent, objectives, devices);
+
+        return ImmutableList.of(new FlowObjectiveIntent(appId, devices, objectives, intent.resources()));
+    }
+
+    @Override
+    public Logger log() {
+        return log;
+    }
+
+    @Override
+    public ResourceService resourceService() {
+        return resourceService;
+    }
+
+    @Override
+    public void createFlow(TrafficSelector originalSelector, TrafficTreatment originalTreatment,
+                                          ConnectPoint ingress, ConnectPoint egress,
+                                          int priority, boolean applyTreatment,
+                                          List<Objective> objectives,
+                                          List<DeviceId> devices) {
+        TrafficSelector selector = DefaultTrafficSelector.builder(originalSelector)
+                .matchInPort(ingress.port())
+                .build();
+
+        TrafficTreatment.Builder treatmentBuilder;
+        if (applyTreatment) {
+            treatmentBuilder = DefaultTrafficTreatment.builder(originalTreatment);
+        } else {
+            treatmentBuilder = DefaultTrafficTreatment.builder();
+        }
+        TrafficTreatment treatment = treatmentBuilder.setOutput(egress.port()).build();
+
+        objectives.add(DefaultForwardingObjective.builder()
+                  .withSelector(selector)
+                  .withTreatment(treatment)
+                  .withPriority(priority)
+                  .fromApp(appId)
+                  .makePermanent()
+                  .withFlag(ForwardingObjective.Flag.SPECIFIC)
+                  .add());
+        devices.add(ingress.deviceId());
+    }
+}