jcc
Committed by Gerrit Code Review

【ONOS-1871】Provide CLI support on flowrule subsystem extension for QA

purposes。
Create a new test application to apply a flow rule to a third-party
random device automatically.
and modfiy FlowsListCommand to show third-party flow rule.

Change-Id: I8902b1577c91c43a7755afd4c1c40fc5ba87d54f
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
36 <module>intent-perf</module> 36 <module>intent-perf</module>
37 <module>messaging-perf</module> 37 <module>messaging-perf</module>
38 <module>demo</module> 38 <module>demo</module>
39 + <module>samples</module>
39 <module>distributed-primitives</module> 40 <module>distributed-primitives</module>
40 </modules> 41 </modules>
41 42
......
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2015 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +<project xmlns="http://maven.apache.org/POM/4.0.0"
18 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
20 + <modelVersion>4.0.0</modelVersion>
21 +
22 + <parent>
23 + <groupId>org.onosproject</groupId>
24 + <artifactId>onos-apps-test</artifactId>
25 + <version>1.2.0-SNAPSHOT</version>
26 + <relativePath>../pom.xml</relativePath>
27 + </parent>
28 +
29 + <artifactId>onos-app-samples</artifactId>
30 + <packaging>bundle</packaging>
31 +
32 + <description>Flow throughput test application</description>
33 +
34 + <properties>
35 + <onos.app.name>org.onosproject.flowrule</onos.app.name>
36 + </properties>
37 +
38 + <dependencies>
39 + <dependency>
40 + <groupId>org.osgi</groupId>
41 + <artifactId>org.osgi.compendium</artifactId>
42 + </dependency>
43 + <dependency>
44 + <groupId>org.apache.karaf.shell</groupId>
45 + <artifactId>org.apache.karaf.shell.console</artifactId>
46 + </dependency>
47 + <!-- Required for javadoc generation -->
48 + <dependency>
49 + <groupId>org.osgi</groupId>
50 + <artifactId>org.osgi.core</artifactId>
51 + </dependency>
52 + </dependencies>
53 +
54 +
55 +
56 +</project>
1 +package org.onosproject.flowrule;
2 +
3 +/**
4 + * Applications test service.
5 + */
6 +public interface AppTestService {
7 +
8 +}
1 +package org.onosproject.flowrule.dispatch;
2 +
3 +import static org.slf4j.LoggerFactory.getLogger;
4 +
5 +import java.io.BufferedReader;
6 +import java.io.File;
7 +import java.io.FileReader;
8 +import java.io.IOException;
9 +
10 +import org.onosproject.core.ApplicationId;
11 +import org.onosproject.net.DeviceId;
12 +import org.onosproject.net.device.DeviceService;
13 +import org.onosproject.net.flow.DefaultFlowRule;
14 +import org.onosproject.net.flow.FlowRule;
15 +import org.onosproject.net.flow.FlowRuleExtPayLoad;
16 +import org.onosproject.net.flow.FlowRuleService;
17 +import org.slf4j.Logger;
18 +
19 +/**
20 + * third party flow rule test code.
21 + */
22 +public class FlowRuleTest {
23 +
24 + private final Logger log = getLogger(getClass());
25 + protected FlowRuleService flowRuleService;
26 + protected DeviceService deviceService;
27 + private ApplicationId appId;
28 + private FlowRule[] flowSet = new DefaultFlowRule[10];
29 + private DeviceId deviceId;
30 + private static final String FILE_NAME = "/src/main/resource/org/onosproject/flowrule/resource/flowrule.txt";
31 +
32 + /**
33 + * Creates a flow rule test object.
34 + * @param flowRuleService service for injecting flow rules into the environment
35 + * @param deviceService service for interacting with the inventory of infrastructure devices
36 + * @param appId application identifier
37 + */
38 + public FlowRuleTest(FlowRuleService flowRuleService,
39 + DeviceService deviceService, ApplicationId appId) {
40 + this.flowRuleService = flowRuleService;
41 + this.deviceService = deviceService;
42 + this.deviceId = deviceService.getAvailableDevices().iterator().next().id();
43 + this.appId = appId;
44 + loadFile();
45 + }
46 +
47 + private void loadFile() {
48 + String relativelyPath = System.getProperty("user.dir");
49 + File flowFile = new File(relativelyPath + FILE_NAME);
50 + BufferedReader br = null;
51 + try {
52 + FileReader in = new FileReader(flowFile);
53 + br = new BufferedReader(in);
54 + FlowRule rule = null;
55 + int i = 0;
56 + String flow = "";
57 + while ((flow = br.readLine()) != null) {
58 + rule = buildFlowRule(flow);
59 + flowSet[i++] = rule;
60 + }
61 + } catch (IOException e) {
62 + log.info("file does not exist.");
63 + } finally {
64 + if (br != null) {
65 + try {
66 + br.close();
67 + } catch (IOException e) {
68 + log.info("nothing");
69 + }
70 + }
71 + }
72 + }
73 +
74 + private FlowRule buildFlowRule(String flow) {
75 + FlowRuleExtPayLoad payLoad = FlowRuleExtPayLoad.flowRuleExtPayLoad(flow
76 + .getBytes());
77 + FlowRule flowRule = new DefaultFlowRule(deviceId, null, null, 0, appId,
78 + 0, false, payLoad);
79 + return flowRule;
80 + }
81 +
82 + /**
83 + * Apply flow rules to specific devices.
84 + */
85 + public void applyFlowRules() {
86 + flowRuleService.applyFlowRules(flowSet);
87 + }
88 +
89 + /**
90 + * Remove flow rules from specific devices.
91 + */
92 + public void removeFlowRules() {
93 + flowRuleService.removeFlowRules(flowSet);
94 + }
95 +
96 +}
1 +/*
2 + * Copyright 2014-2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Provide for specific functional test script. e.g. flow rule subsystem extension.
19 + * You can create a new class to test other functions.
20 + */
21 +package org.onosproject.flowrule.dispatch;
...\ No newline at end of file ...\ No newline at end of file
1 +package org.onosproject.flowrule.impl;
2 +
3 +import static org.slf4j.LoggerFactory.getLogger;
4 +
5 +import org.apache.felix.scr.annotations.Activate;
6 +import org.apache.felix.scr.annotations.Component;
7 +import org.apache.felix.scr.annotations.Deactivate;
8 +import org.apache.felix.scr.annotations.Reference;
9 +import org.apache.felix.scr.annotations.ReferenceCardinality;
10 +import org.apache.felix.scr.annotations.Service;
11 +import org.onosproject.core.ApplicationId;
12 +import org.onosproject.core.CoreService;
13 +import org.onosproject.flowrule.AppTestService;
14 +import org.onosproject.flowrule.dispatch.FlowRuleTest;
15 +import org.onosproject.net.device.DeviceService;
16 +import org.onosproject.net.flow.FlowRuleService;
17 +import org.slf4j.Logger;
18 +
19 +/**
20 + * Test for a application.
21 + */
22 +@Component(immediate = true)
23 +@Service
24 +public class AppTestManager implements AppTestService {
25 +
26 + private static final String APP_TEST = "org.onosproject.apptest";
27 + private final Logger log = getLogger(getClass());
28 +
29 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
30 + protected CoreService coreService;
31 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
32 + protected FlowRuleService flowRuleService;
33 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
34 + protected DeviceService deviceService;
35 + private ApplicationId appId;
36 + FlowRuleTest flowRule;
37 +
38 + @Activate
39 + protected void activate() {
40 + log.info("APP-TEST started");
41 + appId = coreService.registerApplication(APP_TEST);
42 + flowRule = new FlowRuleTest(flowRuleService, deviceService, appId);
43 + flowRule.applyFlowRules();
44 + }
45 +
46 + @Deactivate
47 + protected void deactivate() {
48 + flowRule.removeFlowRules();
49 + log.info("APP-TEST Stopped");
50 + }
51 +}
1 +/*
2 + * Copyright 2014-2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Test for flow rule subsystem extension.
19 + * Also used to test other functions, but it need be extended.
20 + */
21 +package org.onosproject.flowrule.impl;
1 +/*
2 + * Copyright 2014-2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Test for flow rule subsystem extension.
19 + * Now it has no APIs, Maybe it can add other methods in future.
20 + */
21 +package org.onosproject.flowrule;
1 +050e009000000000000000000000000000000000000000000701000000000000000000000000000000000000000000020001001080001702010180001902010200040030000000000000001000000006000000000000000000130008001300000019001080001a0101000000000000000002000000000000000000000000000100000000000000010001000003000000
...\ No newline at end of file ...\ No newline at end of file
1 +/*
2 + * Copyright 2014-2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * applications live here.
19 + */
20 +package org.onosproject.flowrule.resource;
...\ No newline at end of file ...\ No newline at end of file
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
15 */ 15 */
16 package org.onosproject.cli.net; 16 package org.onosproject.cli.net;
17 17
18 +import static com.google.common.collect.Lists.newArrayList;
19 +
18 import java.util.Collections; 20 import java.util.Collections;
19 import java.util.List; 21 import java.util.List;
20 import java.util.Map; 22 import java.util.Map;
...@@ -38,10 +40,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -38,10 +40,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.fasterxml.jackson.databind.node.ArrayNode; 40 import com.fasterxml.jackson.databind.node.ArrayNode;
39 import com.fasterxml.jackson.databind.node.ObjectNode; 41 import com.fasterxml.jackson.databind.node.ObjectNode;
40 42
41 -import static com.google.common.collect.Lists.newArrayList;
42 -
43 /** 43 /**
44 - * Lists all currently-known hosts. 44 + * Lists all currently-known flows.
45 */ 45 */
46 @Command(scope = "onos", name = "flows", 46 @Command(scope = "onos", name = "flows",
47 description = "Lists all currently-known flows.") 47 description = "Lists all currently-known flows.")
...@@ -50,7 +50,7 @@ public class FlowsListCommand extends AbstractShellCommand { ...@@ -50,7 +50,7 @@ public class FlowsListCommand extends AbstractShellCommand {
50 public static final String ANY = "any"; 50 public static final String ANY = "any";
51 51
52 private static final String FMT = 52 private static final String FMT =
53 - " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, tableId=%s, timeout=%s, appId=%s"; 53 + " id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, tableId=%s appId=%s, payLoad=%s";
54 private static final String TFMT = " treatment=%s"; 54 private static final String TFMT = " treatment=%s";
55 private static final String SFMT = " selector=%s"; 55 private static final String SFMT = " selector=%s";
56 56
...@@ -156,7 +156,8 @@ public class FlowsListCommand extends AbstractShellCommand { ...@@ -156,7 +156,8 @@ public class FlowsListCommand extends AbstractShellCommand {
156 for (FlowEntry f : flows) { 156 for (FlowEntry f : flows) {
157 print(FMT, Long.toHexString(f.id().value()), f.state(), 157 print(FMT, Long.toHexString(f.id().value()), f.state(),
158 f.bytes(), f.packets(), f.life(), f.priority(), f.tableId(), 158 f.bytes(), f.packets(), f.life(), f.priority(), f.tableId(),
159 - f.timeout(), coreService.getAppId(f.appId()).name()); 159 + coreService.getAppId(f.appId()).name(),
160 + f.payLoad() == null ? null : f.payLoad().payLoad().toString());
160 print(SFMT, f.selector().criteria()); 161 print(SFMT, f.selector().criteria());
161 print(TFMT, f.treatment()); 162 print(TFMT, f.treatment());
162 } 163 }
......