blob: 9468984a6ade9b346df5ac7430935b38b6193200 [file] [log] [blame]
Daniel Park577b69c2018-07-16 17:29:34 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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 */
16package org.onosproject.openstacknetworkingui;
17
18import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21
22/**
23 * Parser class for flow trace result string from OVS28.
24 */
25public final class Ovs28FlowTraceResultParser {
26 private static final String TRACE_NODE_NAME = "traceNodeName";
27 private static final String IS_SUCCESS = "isSuccess";
28 private static final String FLOW_RULES = "flowRules";
29 private static final String TABLE = "table";
30 private static final String PRIORITY = "priority";
31 private static final String SELECTOR = "selector";
32 private static final String ACTIONS = "actions";
33 private static final String BRIDGE = "bridge";
34 private static final String DATAPATH = "Datapath";
35 private static final String DROP = "drop";
36 private static final String COMMA = ",";
37 private static final String DOT = "\\.";
38 private static final String NEW_LINE = "\n";
39
40 private Ovs28FlowTraceResultParser() {
41 }
42
43 public static ObjectNode flowTraceResultInJson(String outputStream, String hostName) {
44 if (outputStream == null || hostName == null) {
45 return null;
46 }
47
48 ObjectMapper mapper = new ObjectMapper();
49 ObjectNode jsonNode = mapper.createObjectNode();
50
51 int flowRuleStartLineNum = 0;
52 int flowRuleEndLineNum = 0;
53
54 jsonNode.put(TRACE_NODE_NAME, hostName);
55
56 String[] lines = outputStream.split(NEW_LINE);
57
58 for (int i = 0; i < lines.length; i++) {
59 if (lines[i].startsWith(BRIDGE)) {
60 flowRuleStartLineNum = i + 2;
61 }
62
63 if (lines[i].startsWith(DATAPATH)) {
64 flowRuleEndLineNum = i;
65 break;
66 }
67 }
68
69 ArrayNode arrayNode = jsonNode.putArray(FLOW_RULES);
70
71 for (int i = flowRuleStartLineNum; i < flowRuleEndLineNum; i = i + 2) {
72 if (!isNewFlowTable(lines[i])) {
73 break;
74 }
75
76 ObjectNode flowRule = arrayNode.addObject();
77
78 flowRule.put(TABLE, tableNum(lines[i]));
79 flowRule.put(PRIORITY, priority(lines[i]));
80 flowRule.put(SELECTOR, selector(lines[i]));
81
82 String actions = action(lines[i + 1]);
83
84 if (!isNewFlowTable(lines[i + 2])) {
85 actions = actions + "\n" + action(lines[i + 2]);
86 i = i + 1;
87 }
88
89 flowRule.put(ACTIONS, actions);
90 }
91
92 if (lines[flowRuleEndLineNum].contains(DROP)) {
93 jsonNode.put(IS_SUCCESS, false);
94 } else {
95 jsonNode.put(IS_SUCCESS, true);
96 }
97
98 return jsonNode;
99 }
100
101 private static boolean isNewFlowTable(String line) {
102 return line.contains(PRIORITY);
103 }
104
105 private static String tableNum(String line) {
106 return line.split(DOT)[0];
107 }
108
109 private static String priority(String line) {
110 return line.split(PRIORITY)[1].trim().split(COMMA)[0];
111 }
112
113 private static String selector(String line) {
114 if (!hasSelector(line)) {
115 return "";
116 }
117
118 String selectorString = line.trim().split(PRIORITY)[0].split(" ")[1];
119
120 return selectorString.replaceAll(COMMA, NEW_LINE);
121 }
122
123 private static boolean hasSelector(String line) {
124 String tableNum = tableNum(line);
125 return !line.replaceFirst(tableNum + DOT + " ", "").startsWith(PRIORITY);
126 }
127
128 private static String action(String line) {
129 return line.trim();
130 }
131
132
133}