View Javadoc

1   package net.sf.provisioner.requests;
2   
3   import net.sf.provisioner.config.ConfigRequest;
4   import net.sf.provisioner.core.Consumer;
5   import net.sf.provisioner.core.Operation;
6   
7   import org.apache.log4j.Logger;
8   
9   /**
10   * Factory for {@link Request} objects.
11   * 
12   * @author g_pearson
13   * 
14   * @see    Request
15   * @see    Consumer
16   * @see    Operation
17   * @see    ConfigRequest
18   */
19  public class RequestFactory {
20  
21  	private static Logger logger = Logger.getLogger(RequestFactory.class);
22  	
23  	/**
24  	 * Builds a {@link Request} using the configuration information provided 
25  	 * by a {@linkplain Operation} and {@linkplain ConfigRequest}.
26  	 * 
27  	 * @param op the operation that's about to be transformed into a 
28  	 * {@linkplain request}. 
29  	 * @param request the {@linkplain ConfigRequest} that's about to be 
30  	 * transformed into a {@linkplain Request}.
31  	 *            
32  	 * @returns A {@linkplain Request} that's ready to be executed.  If either 
33  	 * parameter is badly configured or not supported a {@link FailedRequest} will be 
34  	 * returned.
35  	 */
36  	public static Request createRequest(Operation op, ConfigRequest configRequest) {
37  		Request request = validateRequest(op, configRequest);
38  		if (request != null) {
39  			return request;
40  		}
41  			
42  		String neType = configRequest.service.ne.type;
43  		if ("Intraway".equalsIgnoreCase(neType)) {		
44  			/* Send to Intraway */
45  			request = new IntrawayRequest(configRequest, op.getParameters());
46  			
47  		} else if ("Merak".equalsIgnoreCase(neType)) {
48  			/* Send to Merak */
49  			request = new MerakRequest(configRequest, op.getParameters());
50  			
51  		} else if ("SER".equalsIgnoreCase(neType)) {		
52  			/* Send to SER */
53  			request = new SERRequest(configRequest, op.getParameters());
54  			
55  		} else if ("Asterisk".equalsIgnoreCase(neType)) {
56  			request = new AsteriskRequest(configRequest, op.getParameters());
57  			
58  		} else if ("ldap_network_element_name".equalsIgnoreCase(neType)) {
59  			if ("LDAPC".equalsIgnoreCase(op.getType())) {
60  				request = new LDAPCreateRequest(configRequest, op.getParameters());	
61  			} else if ("LDAPD".equalsIgnoreCase(op.getType())) {
62  				request = new LDAPDeleteRequest(configRequest, op.getParameters());				
63  			} else if ("LDAPM".equalsIgnoreCase(op.getType())) {
64  				request = new LDAPModifyRequest(configRequest, op.getParameters());
65  			} else {			
66  				logger.warn("No LDAP support for operation type '" + op.getType() + "'");
67  			}
68  		} else if ("Generic".equalsIgnoreCase(neType)) {
69  			/* Create a Generic request type */			
70  			request = new GenericRequest(configRequest, op.getParameters());
71  		}
72  
73  		if (request == null) {
74  			/* TODO: decide if we need to be less lenient with bad configurations.
75  			 * Should this case throw an exception?  
76  			 */
77  			String message = "No request found for networkElement type '" + neType + "'";
78  			logger.warn(message);
79  			request = new FailedRequest(message);
80  		}
81  		
82  		return request;
83  	}
84  	
85  	/**
86  	 * Validates the content of <code>op</code> and <code>request</code>. 
87  	 *  
88  	 * @param op the operation that's about to be transformed into a 
89  	 * {@linkplain request}.  
90  	 * @param request the {@linkplain ConfigRequest} that's about to be 
91  	 * transformed into a {@linkplain Request}.
92  	 * 
93  	 * @returns A {@link FailedRequest} is returned if <code>op</code> or 
94  	 * <code>request</code> have an invalid configuration. 
95  	 * configurations. <code>null</null> is returned is both are valid.
96  	 */
97  	// TODO: think of a less confusing name?
98  	private static Request validateRequest(Operation op, ConfigRequest request) {
99  		if (request.service.ne.name == null) {
100 			return new FailedRequest(
101 					String.format(
102 							  "Network element name is not valid ('%s'). "
103 							+ "Please review routing configuration.",
104 							request.service.ne.name
105 					)
106 			);						
107 		}
108 		
109 		if (request.service.ne.type == null) {	
110 			return new FailedRequest(
111 					String.format(
112 							  "Network element type is not valid ('%s'). " 
113 							+ "Please review routing configuration.",
114 							request.service.ne.type
115 					)
116 			);
117 		}
118 		return null;
119 	}
120 }