1
2
3
4 package net.sf.provisioner.core;
5
6 import java.io.StringReader;
7 import java.util.Enumeration;
8 import java.util.Hashtable;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.regex.Matcher;
12 import java.util.regex.Pattern;
13
14 import net.sf.provisioner.config.ConfigRequest;
15 import net.sf.provisioner.config.NetworkElement;
16 import net.sf.provisioner.config.NetworkElementFactory;
17 import net.sf.provisioner.config.Route;
18 import net.sf.provisioner.config.Rule;
19
20 import org.apache.log4j.Logger;
21 import org.jdom.Document;
22 import org.jdom.Element;
23 import org.jdom.input.SAXBuilder;
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class Operation {
38
39
40 Logger logger = Logger.getLogger(getClass());
41
42 private String id, type;
43
44 private Document parameters;
45
46 public Hashtable requests = new Hashtable();
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public Operation() { this.id = "0";}
64
65
66
67
68
69
70 public String getId() {
71 return id;
72 }
73
74
75
76
77
78
79 public void setId(String id) {
80 this.id = id;
81 }
82
83
84
85
86
87
88 public String getType() {
89 return type;
90 }
91
92
93
94
95
96
97 public void setType(String type) {
98 this.type = type;
99 }
100
101
102
103
104
105
106 public Document getParameters() {
107 return parameters;
108 }
109
110
111
112
113
114
115 public void setParameters(String xml_string) throws Exception {
116
117 try {
118 logger.debug("Operation xml string: " + xml_string);
119 this.parameters = new SAXBuilder().build( new StringReader(xml_string));
120 } catch (Exception e) {
121 throw e;
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 public void applyRules(Enumeration rules, Hashtable services) {
141
142 logger.info("Aplying provisioning rules......");
143 applyProvisioningRules(rules);
144
145
146 Enumeration requests = this.requests.elements();
147 while (requests.hasMoreElements()) {
148
149 ConfigRequest request = (ConfigRequest)requests.nextElement();
150
151 logger.info("Aplying routing rules for the service " + request.service.name + ".....");
152
153
154
155 applyRoutingCriteria(services, request.service.name, request.operationType);
156 }
157 }
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 void applyProvisioningRules(Enumeration rules) {
175
176 boolean doContinue = true;
177 int i = 0;
178
179 while (rules.hasMoreElements() && doContinue) {
180
181 Rule rule = (Rule)rules.nextElement();
182 if (this.type.equalsIgnoreCase(rule.operation)) {
183
184 Enumeration requests = rule.requests.elements();
185
186 while (requests.hasMoreElements()) {
187
188 ConfigRequest request = (ConfigRequest)requests.nextElement();
189
190 this.requests.put((Integer) i++, request);
191
192 }
193
194 doContinue = false;
195 }
196 }
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 void applyRoutingCriteria(Hashtable services, String serviceName, String opType) {
214
215 Enumeration servicesEnum = services.elements();
216
217
218 for (int i = 0; i < this.requests.size(); i++) {
219 ConfigRequest request = (ConfigRequest)this.requests.get(i);
220 if (request.service.name.equalsIgnoreCase(serviceName) && request.operationType.equalsIgnoreCase(opType)) {
221
222
223 while (servicesEnum.hasMoreElements()) {
224
225 net.sf.provisioner.config.ConfigService service = (net.sf.provisioner.config.ConfigService)servicesEnum.nextElement();
226 if (service.name.equalsIgnoreCase(serviceName)) {
227
228
229
230 this.requests.remove(request);
231
232 NetworkElement ne = getRoutingData(service.routes.elements(), getKey(service.routingData));
233 Service newService = new Service(serviceName, ne);
234 ConfigRequest newRequest = new ConfigRequest(newService, request.operationType);
235 this.requests.put((Integer) i, newRequest);
236 }
237 }
238 }
239 }
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257 NetworkElement getRoutingData(Enumeration routes, String key) {
258 if (key == null) throw new IllegalArgumentException("null key passed to Operation.getRoutingData");
259 logger.debug("getting routing data for key '" + key + "'");
260
261 while (routes.hasMoreElements()) {
262
263 Route route = (Route)routes.nextElement();
264 Pattern pattern = Pattern.compile(route.key, Pattern.CASE_INSENSITIVE);
265 Matcher matcher = pattern.matcher(key);
266 if (matcher.matches()) return route.ne;
267
268 }
269 logger.info("Couldn't find route for the request key: " + key);
270 return NetworkElementFactory.defaultElement();
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288 String getKey(String criteria) {
289 logger.trace("searching for request type using criteria '" + criteria + "'");
290
291 List children = parameters.getContent();
292 Iterator iterator = children.iterator();
293 while (iterator.hasNext()) {
294 Element child = (Element) iterator.next();
295 if (child.getName().equalsIgnoreCase("operation")) {
296 List children1 = child.getChildren();
297 Iterator iterator1 = children1.iterator();
298 while (iterator1.hasNext()) {
299 Element child1 = (Element) iterator1.next();
300 if (child1.getName().equalsIgnoreCase("parameter")) {
301 logger.trace("found operation parameter '" + child1.getAttributeValue("name") + "'");
302 if (child1.getAttributeValue("name").equalsIgnoreCase(criteria)) {
303 String value = child1.getAttributeValue("value");
304 logger.trace("returning '" + value + "' for criteria '" + criteria + "'");
305 return value;
306 }
307 }
308 }
309 }
310 }
311 logger.info("No key found for criteria '" + criteria + "'");
312 return null;
313 }
314
315 public String toString() {
316 return "id=" + id + ", type=" + type + ", requests=[" + requests + "]";
317 }
318 }