BGP Route policy distribution and flow spec

Change-Id: I8903efd225a24db6ccc85a4a3148a4dd6076e042
diff --git a/apps/bgpflowspec/flowmgr/BUCK b/apps/bgpflowspec/flowmgr/BUCK
new file mode 100755
index 0000000..dd7ae7e
--- /dev/null
+++ b/apps/bgpflowspec/flowmgr/BUCK
@@ -0,0 +1,10 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//core/store/serializers:onos-core-serializers',
+    '//apps/bgpflowspec/flowapi:onos-apps-bgpflowspec-flowapi',
+]
+
+osgi_jar_with_tests (
+    name = 'onos-apps-bgpflowspec-flowmgr',
+    deps = COMPILE_DEPS,
+)
diff --git a/apps/bgpflowspec/flowmgr/pom.xml b/apps/bgpflowspec/flowmgr/pom.xml
new file mode 100755
index 0000000..5d9afbe
--- /dev/null
+++ b/apps/bgpflowspec/flowmgr/pom.xml
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2015 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.
+  -->
+<project
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-app-bgpflow</artifactId>
+        <version>1.6.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>onos-app-bgp-flowmgr</artifactId>
+    <packaging>bundle</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>javax.ws.rs</groupId>
+            <artifactId>javax.ws.rs-api</artifactId>
+            <version>2.0.1</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.shell</groupId>
+            <artifactId>org.apache.karaf.shell.console</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-app-bgp-flowapi</artifactId>
+            <version>1.6.0-SNAPSHOT</version>
+        </dependency>
+    </dependencies>
+</project>
diff --git a/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/BgpFlowForwarderService.java b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/BgpFlowForwarderService.java
new file mode 100755
index 0000000..e2527b9
--- /dev/null
+++ b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/BgpFlowForwarderService.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2016-present 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.flow.forwarder;
+
+import org.onosproject.flowapi.ExtFlowContainer;
+
+/**
+ * Abstraction of an entity which provides Bgp Flow Service.
+ */
+public interface BgpFlowForwarderService {
+
+    /**
+     * Install forwarding rule.
+     *
+     * @param container BgpFlow container
+     * @return a true if success else false
+     */
+    boolean installForwardingRule(ExtFlowContainer container);
+
+    /**
+     * Uninstall forwarding rule.
+     *
+     * @param container BgpFlow container
+     * @return a true if success else false
+     */
+    boolean unInstallForwardingRule(ExtFlowContainer container);
+}
diff --git a/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/impl/BgpFlowForwarderImpl.java b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/impl/BgpFlowForwarderImpl.java
new file mode 100755
index 0000000..bc5170d
--- /dev/null
+++ b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/impl/BgpFlowForwarderImpl.java
@@ -0,0 +1,208 @@
+/*
+ * Copyright 2016-present 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.flow.forwarder.impl;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.flow.forwarder.BgpFlowForwarderService;
+
+import org.onosproject.flowapi.ExtFlowContainer;
+import org.onosproject.net.Annotations;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.behaviour.ExtensionSelectorResolver;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.DriverHandler;
+import org.onosproject.net.driver.DriverService;
+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.flow.criteria.ExtensionSelector;
+import org.onosproject.net.flowobjective.DefaultForwardingObjective;
+import org.onosproject.net.flowobjective.FlowObjectiveService;
+import org.onosproject.net.flowobjective.ForwardingObjective;
+import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
+import org.onosproject.net.flowobjective.Objective;
+import org.slf4j.Logger;
+
+import java.util.Iterator;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.net.flow.criteria.ExtensionSelectorType.ExtensionSelectorTypes.EXT_MATCH_FLOW_TYPE;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provides Bgp flow forwarder implementation.
+ */
+public class BgpFlowForwarderImpl implements BgpFlowForwarderService {
+
+    protected DriverService driverService;
+    protected DeviceService deviceService;
+    protected FlowObjectiveService flowObjectiveService;
+    private final Logger log = getLogger(getClass());
+    protected ApplicationId appId;
+
+    private static final String BGP_FLOW_CONTAINER_NOT_NULL = "Bgp flow container cannot be null";
+    private static final String APP_ID_NOT_NULL = "Application-Id cannot be null";
+    public static final String FLOW_PEER = "flowPeer";
+
+    /**
+     * Default constructor.
+     */
+    public BgpFlowForwarderImpl() {
+    }
+
+    /**
+     * Explicit constructor.
+     *
+     * @param appId Application id
+     * @param flowObjectiveService flow service
+     * @param deviceService device service
+     * @param driverService driver service
+     */
+    public BgpFlowForwarderImpl(ApplicationId appId, FlowObjectiveService flowObjectiveService,
+                                DeviceService deviceService, DriverService driverService) {
+        this.appId = checkNotNull(appId, APP_ID_NOT_NULL);
+        this.flowObjectiveService = flowObjectiveService;
+        this.deviceService = deviceService;
+        this.driverService = driverService;
+    }
+
+    @Override
+    public boolean installForwardingRule(ExtFlowContainer container) {
+        checkNotNull(container, BGP_FLOW_CONTAINER_NOT_NULL);
+        return pushBgpFlowRuleForwarder(container, Objective.Operation.ADD);
+    }
+
+    @Override
+    public boolean unInstallForwardingRule(ExtFlowContainer container) {
+        checkNotNull(container, BGP_FLOW_CONTAINER_NOT_NULL);
+        return pushBgpFlowRuleForwarder(container, Objective.Operation.REMOVE);
+    }
+
+    /**
+     * Find the bgp device and push the rule.
+     *
+     * @param container is a flow rule container
+     * @param type either add or remove the service rule
+     * @return a true if success else false
+     */
+    public boolean pushBgpFlowRuleForwarder(ExtFlowContainer container,
+                                             Objective.Operation type) {
+        DeviceId deviceId = null;
+
+        Iterable<Device> devices = deviceService.getAvailableDevices();
+        Iterator<Device> itr = devices.iterator();
+        while (itr.hasNext()) {
+            DeviceId tmp = itr.next().id();
+            if (tmp.toString().equals(container.deviceId())) {
+                if (validatePeer(tmp)) {
+                    deviceId = tmp;
+                    break;
+                }
+            }
+        }
+
+        if (deviceId != null) {
+            // pack traffic selector
+            TrafficSelector.Builder selector = packTrafficSelector(deviceId, container);
+
+            // pack traffic treatment
+            TrafficTreatment.Builder treatment = packTrafficTreatment(container);
+
+            sendBgpFlowRuleForwarder(selector, treatment, deviceId, type);
+        } else {
+            log.error("Bgp devices are not available..");
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Validates the device id is a flow peer or not.
+     *
+     * @param deviceId device to which the flow needed to be pushed.
+     * @return true if success else false
+     */
+    boolean validatePeer(DeviceId deviceId) {
+        boolean ret = false;
+        Device d = deviceService.getDevice(deviceId);
+        Annotations a = d != null ? d.annotations() : null;
+        String ipAddress = a.value(FLOW_PEER);
+        if (ipAddress != null) {
+            ret = true;
+        }
+        return ret;
+    }
+
+    /**
+     * Traffic selector builder function.
+     *
+     * @param deviceId device id.
+     * @param container container need to be pushed.
+     * @return the traffic selector builder
+     */
+    public TrafficSelector.Builder packTrafficSelector(DeviceId deviceId, ExtFlowContainer container) {
+        TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+
+        DriverHandler handler = driverService.createHandler(deviceId);
+        ExtensionSelectorResolver resolver = handler.behaviour(ExtensionSelectorResolver.class);
+        ExtensionSelector bgpExtSelector = resolver.getExtensionSelector(EXT_MATCH_FLOW_TYPE.type());
+
+        try {
+            bgpExtSelector.setPropertyValue("container", container);
+        } catch (Exception e) {
+            log.error("Failed to get extension instruction for bgp flow {}", deviceId);
+        }
+
+        selector.extension(bgpExtSelector, deviceId);
+        return selector;
+    }
+
+    /**
+     * Traffic treatment builder function.
+     *
+     * @param container container need to be pushed.
+     * @return the traffic treatment builder
+     */
+    public TrafficTreatment.Builder packTrafficTreatment(ExtFlowContainer container) {
+        TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
+
+        return treatment;
+    }
+
+    /**
+     * Send bgp flow forwarder to bgp provider.
+     *
+     * @param selector traffic selector
+     * @param treatment traffic treatment
+     * @param deviceId device id
+     * @param type operation type
+     */
+    public void sendBgpFlowRuleForwarder(TrafficSelector.Builder selector, TrafficTreatment.Builder treatment,
+            DeviceId deviceId, Objective.Operation type) {
+        ForwardingObjective.Builder objective = DefaultForwardingObjective.builder().withTreatment(treatment.build())
+                .withSelector(selector.build()).fromApp(appId).makePermanent().withFlag(Flag.VERSATILE);
+        if (type.equals(Objective.Operation.ADD)) {
+            log.debug("ADD");
+            flowObjectiveService.forward(deviceId, objective.add());
+        } else {
+            log.debug("REMOVE");
+            flowObjectiveService.forward(deviceId, objective.remove());
+        }
+    }
+}
diff --git a/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/impl/package-info.java b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/impl/package-info.java
new file mode 100755
index 0000000..4ae602f
--- /dev/null
+++ b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present 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.
+ */
+
+/**
+ * Bgp service manager for interacting with bgp provider.
+ */
+package org.onosproject.flow.forwarder.impl;
diff --git a/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/package-info.java b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/package-info.java
new file mode 100755
index 0000000..0000150
--- /dev/null
+++ b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/forwarder/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present 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.
+ */
+
+/**
+ * Service forwarder for bgp flow.
+ */
+package org.onosproject.flow.forwarder;
diff --git a/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/BgpFlowService.java b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/BgpFlowService.java
new file mode 100755
index 0000000..cf7aada
--- /dev/null
+++ b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/BgpFlowService.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2016-present 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.flow.manager;
+
+import org.onosproject.flowapi.ExtFlowContainer;
+
+/**
+ * Bgp flow service application.
+ */
+public interface BgpFlowService {
+
+    /**
+     * Bgp flow rule create interface.
+     *
+     * @param container flow container class
+     * @return true if success else false
+     */
+    boolean onBgpFlowCreated(ExtFlowContainer container);
+
+    /**
+     * Bgp flow rule delete interface.
+     *
+     * @param container flow container class
+     * @return true if success else false
+     */
+    boolean onBgpFlowDeleted(ExtFlowContainer container);
+}
diff --git a/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/impl/BgpFlowManager.java b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/impl/BgpFlowManager.java
new file mode 100755
index 0000000..deccf38
--- /dev/null
+++ b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/impl/BgpFlowManager.java
@@ -0,0 +1,83 @@
+/*
+ * 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.flow.manager.impl;
+
+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.apache.felix.scr.annotations.Service;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.flow.forwarder.BgpFlowForwarderService;
+import org.onosproject.flow.forwarder.impl.BgpFlowForwarderImpl;
+import org.onosproject.flow.manager.BgpFlowService;
+import org.onosproject.flowapi.ExtFlowContainer;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.DriverService;
+import org.onosproject.net.flowobjective.FlowObjectiveService;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Provides implementation of Bgp flow Service.
+ */
+@Component(immediate = true)
+@Service
+public class BgpFlowManager implements BgpFlowService {
+
+    private final Logger log = getLogger(getClass());
+    private static final String APP_ID = "org.onosproject.app.bgpflow";
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    protected ApplicationId appId;
+    private BgpFlowForwarderService bgpFlowForwarderService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DriverService driverService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected FlowObjectiveService flowObjectiveService;
+
+    @Activate
+    public void activate() {
+        appId = coreService.registerApplication(APP_ID);
+        bgpFlowForwarderService = new BgpFlowForwarderImpl(appId, flowObjectiveService, deviceService, driverService);
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        log.info("Stopped");
+    }
+
+    @Override
+    public boolean onBgpFlowCreated(ExtFlowContainer container) {
+        return bgpFlowForwarderService.installForwardingRule(container);
+    }
+
+    @Override
+    public boolean onBgpFlowDeleted(ExtFlowContainer container) {
+        return bgpFlowForwarderService.unInstallForwardingRule(container);
+    }
+}
diff --git a/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/impl/package-info.java b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/impl/package-info.java
new file mode 100755
index 0000000..e686bb8
--- /dev/null
+++ b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present 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.
+ */
+
+/**
+ * Bgp service manager for interacting provider.
+ */
+package org.onosproject.flow.manager.impl;
diff --git a/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/package-info.java b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/package-info.java
new file mode 100755
index 0000000..b3ba1cb
--- /dev/null
+++ b/apps/bgpflowspec/flowmgr/src/main/java/org/onosproject/flow/manager/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present 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.
+ */
+
+/**
+ * Bgp flow manager.
+ */
+package org.onosproject.flow.manager;