Adding Flow Rule test to app-samples (from onos-core)

Change-Id: Ibc6971f2db73acc01a1eb71bcb3cd3b2237a099e
diff --git a/flowtest/pom.xml b/flowtest/pom.xml
new file mode 100644
index 0000000..9623bcb
--- /dev/null
+++ b/flowtest/pom.xml
@@ -0,0 +1,56 @@
+<?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 xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onosproject</groupId>
+        <artifactId>onos-apps-test</artifactId>
+        <version>1.2.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>onos-app-flowtest</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>Flow throughput test application</description>
+
+    <properties>
+        <onos.app.name>org.onosproject.flowtest</onos.app.name>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.karaf.shell</groupId>
+            <artifactId>org.apache.karaf.shell.console</artifactId>
+        </dependency>
+        <!-- Required for javadoc generation -->
+        <dependency>
+           <groupId>org.osgi</groupId>
+           <artifactId>org.osgi.core</artifactId>
+        </dependency>
+    </dependencies>
+
+    
+
+</project>
diff --git a/flowtest/src/main/java/org/onosproject/flowruletest/AppTestService.java b/flowtest/src/main/java/org/onosproject/flowruletest/AppTestService.java
new file mode 100644
index 0000000..67a9825
--- /dev/null
+++ b/flowtest/src/main/java/org/onosproject/flowruletest/AppTestService.java
@@ -0,0 +1,8 @@
+package org.onosproject.flowruletest;
+
+/**
+ * Applications test service.
+ */
+public interface AppTestService {
+
+}
diff --git a/flowtest/src/main/java/org/onosproject/flowruletest/dispatch/FlowRuleTest.java b/flowtest/src/main/java/org/onosproject/flowruletest/dispatch/FlowRuleTest.java
new file mode 100644
index 0000000..bc57d42
--- /dev/null
+++ b/flowtest/src/main/java/org/onosproject/flowruletest/dispatch/FlowRuleTest.java
@@ -0,0 +1,96 @@
+package org.onosproject.flowruletest.dispatch;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+
+import org.onosproject.core.ApplicationId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.DefaultFlowRule;
+import org.onosproject.net.flow.FlowRule;
+import org.onosproject.net.flow.FlowRuleExtPayLoad;
+import org.onosproject.net.flow.FlowRuleService;
+import org.slf4j.Logger;
+
+/**
+ * third party flow rule test code.
+ */
+public class FlowRuleTest {
+
+    private final Logger log = getLogger(getClass());
+    protected FlowRuleService flowRuleService;
+    protected DeviceService deviceService;
+    private ApplicationId appId;
+    private FlowRule[] flowSet = new DefaultFlowRule[10];
+    private DeviceId deviceId;
+    private static final String FILE_NAME = "/src/main/resource/org/onosproject/flowruletest/resource/flowruletest.txt";
+
+    /**
+     * Creates a flow rule test object.
+     * @param flowRuleService service for injecting flow rules into the environment
+     * @param deviceService service for interacting with the inventory of infrastructure devices
+     * @param appId application identifier
+     */
+    public FlowRuleTest(FlowRuleService flowRuleService,
+                        DeviceService deviceService, ApplicationId appId) {
+        this.flowRuleService = flowRuleService;
+        this.deviceService = deviceService;
+        this.deviceId = deviceService.getAvailableDevices().iterator().next().id();
+        this.appId = appId;
+        loadFile();
+    }
+
+    private void loadFile() {
+        String relativelyPath = System.getProperty("user.dir");
+        File flowFile = new File(relativelyPath + FILE_NAME);
+        BufferedReader br = null;
+        try {
+            FileReader in = new FileReader(flowFile);
+            br = new BufferedReader(in);
+            FlowRule rule = null;
+            int i = 0;
+            String flow = "";
+            while ((flow = br.readLine()) != null) {
+                rule = buildFlowRule(flow);
+                flowSet[i++] = rule;
+            }
+        } catch (IOException e) {
+            log.info("file does not exist.");
+        } finally {
+            if (br != null) {
+                try {
+                    br.close();
+                } catch (IOException e) {
+                    log.info("nothing");
+                }
+            }
+        }
+    }
+
+    private FlowRule buildFlowRule(String flow) {
+        FlowRuleExtPayLoad payLoad = FlowRuleExtPayLoad.flowRuleExtPayLoad(flow
+                .getBytes());
+        FlowRule flowRule = new DefaultFlowRule(deviceId, null, null, 0, appId,
+                                                0, false, payLoad);
+        return flowRule;
+    }
+
+    /**
+     * Apply flow rules to specific devices.
+     */
+    public void applyFlowRules() {
+        flowRuleService.applyFlowRules(flowSet);
+    }
+
+    /**
+     * Remove flow rules from specific devices.
+     */
+    public void removeFlowRules() {
+        flowRuleService.removeFlowRules(flowSet);
+    }
+
+}
diff --git a/flowtest/src/main/java/org/onosproject/flowruletest/dispatch/package-info.java b/flowtest/src/main/java/org/onosproject/flowruletest/dispatch/package-info.java
new file mode 100644
index 0000000..329140a
--- /dev/null
+++ b/flowtest/src/main/java/org/onosproject/flowruletest/dispatch/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2014-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.
+ */
+
+/**
+ * Provide for specific functional test script. e.g. flow rule subsystem extension.
+ * You can create a new class to test other functions.
+ */
+package org.onosproject.flowruletest.dispatch;
\ No newline at end of file
diff --git a/flowtest/src/main/java/org/onosproject/flowruletest/impl/AppTestManager.java b/flowtest/src/main/java/org/onosproject/flowruletest/impl/AppTestManager.java
new file mode 100644
index 0000000..d87d664
--- /dev/null
+++ b/flowtest/src/main/java/org/onosproject/flowruletest/impl/AppTestManager.java
@@ -0,0 +1,51 @@
+package org.onosproject.flowruletest.impl;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+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.flowruletest.AppTestService;
+import org.onosproject.flowruletest.dispatch.FlowRuleTest;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.flow.FlowRuleService;
+import org.slf4j.Logger;
+
+/**
+ * Test for a application.
+ */
+@Component(immediate = true)
+@Service
+public class AppTestManager implements AppTestService {
+
+    private static final String APP_TEST = "org.onosproject.apptest";
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected FlowRuleService flowRuleService;
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+    private ApplicationId appId;
+    FlowRuleTest flowRule;
+
+    @Activate
+    protected void activate() {
+        log.info("APP-TEST started");
+        appId = coreService.registerApplication(APP_TEST);
+        flowRule = new FlowRuleTest(flowRuleService, deviceService, appId);
+        flowRule.applyFlowRules();
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        flowRule.removeFlowRules();
+        log.info("APP-TEST Stopped");
+    }
+}
diff --git a/flowtest/src/main/java/org/onosproject/flowruletest/impl/package-info.java b/flowtest/src/main/java/org/onosproject/flowruletest/impl/package-info.java
new file mode 100644
index 0000000..2ecb23d
--- /dev/null
+++ b/flowtest/src/main/java/org/onosproject/flowruletest/impl/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2014-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.
+ */
+
+/**
+ *  Test for flow rule subsystem extension.
+ *  Also used to test other functions, but it need be extended.
+ */
+package org.onosproject.flowruletest.impl;
diff --git a/flowtest/src/main/java/org/onosproject/flowruletest/package-info.java b/flowtest/src/main/java/org/onosproject/flowruletest/package-info.java
new file mode 100644
index 0000000..ea0207b
--- /dev/null
+++ b/flowtest/src/main/java/org/onosproject/flowruletest/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2014-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.
+ */
+
+/**
+ *  Test for flow rule subsystem extension.
+ *  Now it has no APIs, Maybe it can add other methods in future.
+ */
+package org.onosproject.flowruletest;
diff --git a/flowtest/src/main/resource/org/onosproject/flowrule/resource/flowrule.txt b/flowtest/src/main/resource/org/onosproject/flowrule/resource/flowrule.txt
new file mode 100644
index 0000000..ec1bc01
--- /dev/null
+++ b/flowtest/src/main/resource/org/onosproject/flowrule/resource/flowrule.txt
@@ -0,0 +1 @@
+050e009000000000000000000000000000000000000000000701000000000000000000000000000000000000000000020001001080001702010180001902010200040030000000000000001000000006000000000000000000130008001300000019001080001a0101000000000000000002000000000000000000000000000100000000000000010001000003000000
\ No newline at end of file
diff --git a/flowtest/src/main/resource/org/onosproject/flowrule/resource/package-info.java b/flowtest/src/main/resource/org/onosproject/flowrule/resource/package-info.java
new file mode 100644
index 0000000..a1918c8
--- /dev/null
+++ b/flowtest/src/main/resource/org/onosproject/flowrule/resource/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2014-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.
+ */
+
+/**
+ *  applications live here.
+ */
+package org.onosproject.flowruletest.resource;
\ No newline at end of file