View Javadoc

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   * Esta clase .
28   * <p>
29   * 
30   * .
31   * <p>
32   * 
33   *             
34   * @version $Revision: 1.1.2.1 $, $Date: 2007/11/12 01:57:27 $
35   * @author Gonzalo Espert
36   */
37  public class Operation {
38  
39  	/** Logger for this class and subclasses */
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       * <p>
51       * 
52       * .
53       * <p>
54       * 
55       * 
56       * 
57       * @param 
58       *            
59       * @throws 
60       *            
61       *           
62       */
63  	public Operation() { this.id = "0";}
64  	
65  	/**
66       * Gets the id value for this operation.
67       * 
68       * @return id
69       */
70      public String getId() {
71          return id;
72      }
73  
74      /**
75       * Sets the id value for this operation.
76       * 
77       * @param id
78       */
79      public void setId(String id) {
80          this.id = id;
81      }
82  	
83      /**
84       * Gets the type value for this operation.
85       * 
86       * @return type
87       */
88      public String getType() {
89          return type;
90      }
91  
92      /**
93       * Sets the type value for this operation.
94       * 
95       * @param type
96       */
97      public void setType(String type) {
98          this.type = type;
99      }
100             
101     /**
102      * Gets the parameters value for this operation.
103      * 
104      * @return parameters
105      */
106     public Document getParameters() {
107         return parameters;
108     }
109 
110     /**
111      * Sets the parameters value for this operation.
112      * 
113      * @param parameters
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      * <p>
128      * 
129      * .
130      * <p>
131      * 
132      * 
133      * 
134      * @param 
135      *            
136      * @throws 
137      *            
138      *           
139      */
140 	public void applyRules(Enumeration rules, Hashtable services) {
141     	
142     	logger.info("Aplying provisioning rules......");
143     	applyProvisioningRules(rules);
144     	
145     	// Apply routing rationale for each request
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     		/* TODO: find out if all this looping is necessary.  This method 
153     		 * loops through every request, then applyRoutingCriteria does the same.
154     		 */
155     		applyRoutingCriteria(services, request.service.name, request.operationType); 	    
156     	}
157     }
158 	
159 	/**
160      * .
161      * <p>
162      * 
163      * .
164      * <p>
165      * 
166      * 
167      * 
168      * @param 
169      *            
170      * @throws 
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 					//	build requests
190 					this.requests.put((Integer) i++, request);
191 						
192 				}
193 				//end loop
194 				doContinue = false;
195 			}
196 		}
197 	}
198 	
199 	/**
200      * .
201      * <p>
202      * 
203      * .
204      * <p>
205      * 
206      * 
207      * 
208      * @param services
209      * @param serviceName
210      *                       
211      *           
212      */
213 	void applyRoutingCriteria(Hashtable services, String serviceName, String opType) {
214 		
215 		Enumeration servicesEnum = services.elements();
216 		
217 		// look for the correct operation request
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     			// look for service routing criteria in configuration
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     					// Apply configuration of routing criteria by replacing 
229     					// the composite object
230     					this.requests.remove(request);
231     					/* Build network element object based on routing criteria */
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      * <p>
245      * 
246      * .
247      * <p>
248      * 
249      * 
250      * 
251      * @param 
252      *            
253      * @throws 
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      * <p>
276      * 
277      * .
278      * <p>
279      * 
280      * 
281      * 
282      * @param 
283      *            
284      * @throws 
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 }