Modify pom file
[nemo.git] / nemo-tools / eclipse-plugin / nemo-rest / org.opendaylight.nemo.tool.eclipse.plugin.rest / src / org / opendaylight / nemo / tool / eclipse / plugin / rest / actions / NemoParserUtils.java
1 package org.opendaylight.nemo.tool.eclipse.plugin.rest.actions;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.opendaylight.nemo.tool.eclipse.plugin.rest.actions.model.*;
7
8 /**
9  * Created by hj on 10/29/15.
10  */
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();
20                 }
21                 return null;
22         }
23
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()));
30                         return user;
31                 }
32                 return null;
33         }
34
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);
42         }
43
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);
51         }
52
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);
58         }
59
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);
67         }
68
69         private static String getNodeName(String node) {
70                 return getParam("Node", node);
71         }
72
73         private static String getConnectionName(String node) {
74                 return getParam("Connection", node);
75         }
76
77         private static String getFlowName(String node) {
78                 return getParam("Flow", node);
79         }
80
81         private static String getOpName(String node) {
82                 return getParam("Operation", node);
83         }
84
85         private static String getTargetObj(String node) {
86                 return getParam("Target", node);
87         }
88
89         private static String getPriority(String node) {
90                 return getParam("Priority", node);
91         }
92
93         private static String getType(String node) {
94                 return getParam("Type", node);
95         }
96
97         private static List<String> getContains(String node) {
98                 if (node.indexOf("Contain") < 0)
99                         return null;
100                 String str = node.substring(node.indexOf("Contain"));
101                 String sub = null;
102                 if (str.indexOf("Property") > 0) {
103                         sub = str.substring(0, str.indexOf("Property"));
104                 } else {
105                         sub = str.replaceAll(";", "").trim();
106                 }
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());
112                         nodeList.add(uuId);
113                 }
114                 return nodeList;
115         }
116
117         private static List<String> getEndNodes(String node) {
118                 if (node.indexOf("Endnodes") < 0)
119                         return null;
120                 String str = node.substring(node.indexOf("Endnodes"));
121                 String sub = null;
122                 if (str.indexOf("Property") > 0) {
123                         sub = str.substring(0, str.indexOf("Property"));
124                 } else {
125                         sub = str.replaceAll(";", "").trim();
126                 }
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());
132                         nodeList.add(uuId);
133                 }
134                 return nodeList;
135         }
136
137         private static List<Property> getProperties(String node) {
138                 if (node.indexOf("Property") < 0)
139                         return null;
140                 String str = node.substring(node.indexOf("Property"))
141                                 .replaceAll(";", "").trim();
142
143
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));
151
152                 }
153                 return propertyList;
154         }
155
156         private static List<Action> getActions(String node) {
157                 if (node.indexOf("Action") < 0)
158                         return null;
159                 String str = node.substring(node.indexOf("Action")).replaceAll(";", "")
160                                 .trim();
161
162
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));
169                         }
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());
176                                 }
177                                 ActionValue actionValue = new ActionValue(aValues);
178                                 actionList.add(new Action(aName, actionValue));
179                         }
180                 }
181                 return actionList;
182         }
183
184         private static List<Match> getMatches(String flow) {
185                 if (flow.indexOf("Match") < 0)
186                         return null;
187                 String str = flow.substring(flow.indexOf("Match")).replaceAll(";", "")
188                                 .trim();
189
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));
198                         }
199                 }
200                 return matches;
201         }
202
203         private static String getParam(String paramName, String nemo) {
204                 if (nemo.indexOf(paramName) < 0) {
205                         return null;
206                 }
207                 String str = nemo.substring(nemo.indexOf(paramName));
208                 String[] params = str.split(" ");
209                 if (params.length < 2)
210                         return null;
211                 return params[1].trim();
212         }
213
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);
218         }
219 }