1 package net.sf.provisioner.requests;
2
3 import java.util.Enumeration;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
8
9 import net.sf.provisioner.adapters.TelnetAdapter;
10 import net.sf.provisioner.commands.MerakCommand;
11 import net.sf.provisioner.responses.Response;
12
13 import org.jdom.Element;
14 import org.jdom.Document;
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public class MerakRequest extends Request{
29
30
31 MerakCommand command = new MerakCommand();
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public MerakRequest(net.sf.provisioner.config.ConfigRequest request, Document parameters) {
49
50 _initRequest(parameters);
51 this.ne = request.service.ne;
52 this.ne.loginPrompt = "ogin:";
53 this.ne.port = "23";
54 this.ne.sendInitialCRLF = false;
55
56 this.command.bin = this.ne.bin;
57 this.command.operation = request.operationType;
58
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 void _initRequest(Document parameters) {
77
78 String paramName;
79
80 List children = parameters.getContent();
81 Iterator iterator = children.iterator();
82 while (iterator.hasNext()) {
83 Element child = (Element) iterator.next();
84 if (child.getName().equalsIgnoreCase("operation")) {
85 List children1 = child.getChildren();
86 Iterator iterator1 = children1.iterator();
87 while (iterator1.hasNext()) {
88 Element child1 = (Element) iterator1.next();
89 if (child1.getName().equalsIgnoreCase("parameter")) {
90 paramName = child1.getAttributeValue("name");
91 if (paramName.equalsIgnoreCase("account")) this.command.account = child1.getAttributeValue("value");
92 if (paramName.equalsIgnoreCase("u_name")) this.command.user = child1.getAttributeValue("value");
93 if (paramName.equalsIgnoreCase("u_password")) this.command.password = child1.getAttributeValue("value");
94 if (paramName.equalsIgnoreCase("u_accounttype")) this.command.accountType = child1.getAttributeValue("value");
95 }
96 }
97 }
98 }
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 public Response sendRequest() throws Exception{
117
118 Response response = new Response();
119
120 String operation = new String();
121
122 TelnetAdapter merak = new TelnetAdapter(this.ne);
123
124 if (this.command.operation.equalsIgnoreCase("create")) operation = this.ne.create;
125 if (this.command.operation.equalsIgnoreCase("delete")) operation = this.ne.delete;
126
127
128 try {
129
130
131 String respuesta = merak.ExecuteCommand(this.command.bin + " " +
132 operation + " account " +
133 this.command.account + " u_name \"" +
134 this.command.user + "\" u_password \"" +
135 this.command.password + "\" u_accounttype " +
136 this.command.accountType);
137
138 logger.debug(respuesta);
139
140
141 response = interpretaRespuesta(respuesta);
142
143 } catch (Exception e) { throw e; }
144
145 return response;
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 Response interpretaRespuesta (String respuesta) {
164
165 Response posibleRespuesta = new Response();
166
167
168 Enumeration respuestas = this.ne.responses.elements();
169 while (respuestas.hasMoreElements()) {
170 posibleRespuesta = (Response)respuestas.nextElement();
171 Pattern pattern = Pattern.compile(posibleRespuesta.result, Pattern.CASE_INSENSITIVE);
172 Matcher matcher = pattern.matcher(respuesta);
173 if (matcher.find() && this.command.operation.equalsIgnoreCase(posibleRespuesta.tipoOperacion))
174 return posibleRespuesta;
175 }
176 posibleRespuesta.errorStr = "No se encontro ningun match en el archivo de respuestas, respuesta desconocida";
177 posibleRespuesta.successfull = false;
178 posibleRespuesta.retry = false;
179 return posibleRespuesta;
180 }
181
182 }