1 package org.opendaylight.nemo.tool.eclipse.plugin.rest.actions;
3 import java.util.ArrayList;
6 import org.opendaylight.nemo.tool.eclipse.plugin.rest.actions.model.*;
9 * Created by hj on 10/29/15.
11 public class NemoParserUtils {
12 public static String getIpv4(String engine) {
13 engine = engine.replace("Engines:", "").trim();
14 engine = engine.replace(";", "").trim();
15 String[] ipv4s = engine.split(",");
16 String[] ipv4_sub = ipv4s[0].split("\\.");
17 if (ipv4_sub.length == 4) {
18 return ipv4_sub[0].trim() + "." + ipv4_sub[1].trim() + "."
19 + ipv4_sub[2].trim() + "." + ipv4_sub[3].trim();
24 public static User getUser(String user_create) {
25 user_create = user_create.replace("CREATE", "").trim();
26 String[] param = user_create.split(" ");
27 if (param.length == 3) {
28 User user = new User(param[0], param[1], param[2]);
29 user.setUser_id(UUIDUtils.getUUId(user.getUser_name()));
35 public static Node getNode(String node) {
36 String nodeName = getNodeName(node);
37 String nodeId = UUIDUtils.getUUId(nodeName);
38 String nodeType = getType(node);
39 List<String> contains = getContains(node);
40 List<Property> properties = getProperties(node);
41 return new Node(nodeId, nodeName, nodeType, properties, contains);
44 public static Connection getConnection(String connection) {
45 String conName = getConnectionName(connection);
46 String conId = UUIDUtils.getUUId(conName);
47 String conType = getType(connection);
48 List<String> endNodes = getEndNodes(connection);
49 List<Property> properties = getProperties(connection);
50 return new Connection(conId, conName, conType, endNodes, properties);
53 public static Flow getFlow(String flow) {
54 String flowName = getFlowName(flow);
55 String flowId = UUIDUtils.getUUId(flowName);
56 List<Match> matches = getMatches(flow);
57 return new Flow(flowId, flowName, matches);
60 public static Operation getOperation(String operation) {
61 String opName = getOpName(operation);
62 String opId = UUIDUtils.getUUId(opName);
63 String targetObject = getTargetObj(operation);
64 String priority = getPriority(operation);
65 List<Action> actions = getActions(operation);
66 return new Operation(opId, opName, targetObject, priority, actions);
69 private static String getNodeName(String node) {
70 return getParam("Node", node);
73 private static String getConnectionName(String node) {
74 return getParam("Connection", node);
77 private static String getFlowName(String node) {
78 return getParam("Flow", node);
81 private static String getOpName(String node) {
82 return getParam("Operation", node);
85 private static String getTargetObj(String node) {
86 return getParam("Target", node);
89 private static String getPriority(String node) {
90 return getParam("Priority", node);
93 private static String getType(String node) {
94 return getParam("Type", node);
97 private static List<String> getContains(String node) {
98 if (node.indexOf("Contain") < 0)
100 String str = node.substring(node.indexOf("Contain"));
102 if (str.indexOf("Property") > 0) {
103 sub = str.substring(0, str.indexOf("Property"));
105 sub = str.replaceAll(";", "").trim();
107 String[] params = sub.split(" ");
108 String[] nodes = params[1].split(",");
109 List<String> nodeList = new ArrayList<String>();
110 for (String nodeName : nodes) {
111 String uuId = UUIDUtils.getUUId(nodeName.trim());
117 private static List<String> getEndNodes(String node) {
118 if (node.indexOf("Endnodes") < 0)
120 String str = node.substring(node.indexOf("Endnodes"));
122 if (str.indexOf("Property") > 0) {
123 sub = str.substring(0, str.indexOf("Property"));
125 sub = str.replaceAll(";", "").trim();
127 String[] params = sub.split(" ");
128 String[] nodes = params[1].split(",");
129 List<String> nodeList = new ArrayList<String>();
130 for (String nodeName : nodes) {
131 String uuId = UUIDUtils.getUUId(nodeName.trim());
137 private static List<Property> getProperties(String node) {
138 if (node.indexOf("Property") < 0)
140 String str = node.substring(node.indexOf("Property"))
141 .replaceAll(";", "").trim();
144 String[] properties = str.replaceAll("Property","").trim().split(",");
145 List<Property> propertyList = new ArrayList<Property>();
146 for (String property : properties) {
147 String pName = property.substring(0, property.indexOf(":"));
148 List<String> pValues = new ArrayList<String>();
149 pValues.add(property.substring(property.indexOf(":") + 1).trim());
150 propertyList.add(new Property(pName, pValues));
156 private static List<Action> getActions(String node) {
157 if (node.indexOf("Action") < 0)
159 String str = node.substring(node.indexOf("Action")).replaceAll(";", "")
163 String[] actions_str = str.replaceAll("Action","").trim().split(",");
164 List<Action> actionList = new ArrayList<Action>();
165 for (String action_s : actions_str) {
166 String[] ss = action_s.split(":");
167 if (ss.length == 1) {
168 actionList.add(new Action(ss[0].trim(), null));
170 if (ss.length == 2) {
171 String aName = ss[0].trim();
172 List<String> aValues = new ArrayList<String>();
173 String[] vss = ss[1].trim().split(",");
174 for (String v : vss) {
175 aValues.add(v.trim());
177 ActionValue actionValue = new ActionValue(aValues);
178 actionList.add(new Action(aName, actionValue));
184 private static List<Match> getMatches(String flow) {
185 if (flow.indexOf("Match") < 0)
187 String str = flow.substring(flow.indexOf("Match")).replaceAll(";", "")
190 String[] matches_str = str.replaceAll("Match","").trim().split(",");
191 List<Match> matches = new ArrayList<Match>();
192 for (String match : matches_str) {
193 String[] ss = match.split(":");
194 if (ss.length == 2) {
195 String matchName = ss[0].trim();
196 MatchValue matchValue = new MatchValue(ss[1].trim());
197 matches.add(new Match(matchName, matchValue));
203 private static String getParam(String paramName, String nemo) {
204 if (nemo.indexOf(paramName) < 0) {
207 String str = nemo.substring(nemo.indexOf(paramName));
208 String[] params = str.split(" ");
209 if (params.length < 2)
211 return params[1].trim();
214 public static void main(String[] args) {
215 String s = "IMPORT Node branch Type ext-group Property location:openflow:3:4,ac-info-network:layer3,ip-prifix:192.168.12.0/24,ac-info-protocol:static ;";
216 List<Property> pL = getProperties(s);
217 System.out.println(pL);