Skip to content

Commit 5fc5b7a

Browse files
Devdeep SinghVijayendra Bhamidipati
Devdeep Singh
authored and
Vijayendra Bhamidipati
committed
CS-9919: Adding helper routines to query details of a port profile and
associated policy maps. Also updating the error message logs.
1 parent 9fcdfe5 commit 5fc5b7a

8 files changed

+313
-23
lines changed

utils/src/com/cloud/utils/cisco/n1kv/vsm/NetconfHelper.java

+25-4
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void deletePortProfile(String name) throws CloudRuntimeException {
125125

126126
public void addPolicyMap(String name, int averageRate, int maxRate, int burstRate)
127127
throws CloudRuntimeException {
128-
String command = VsmCommand.getPolicyMap(name, averageRate, maxRate, burstRate);
128+
String command = VsmCommand.getAddPolicyMap(name, averageRate, maxRate, burstRate);
129129
if (command != null) {
130130
command = command.concat(SSH_NETCONF_TERMINATOR);
131131
send(command);
@@ -180,18 +180,39 @@ public void detachServicePolicy(String policyMap, String portProfile)
180180
}
181181
}
182182

183-
public void getPortProfileByName(String name) throws CloudRuntimeException {
183+
public PortProfile getPortProfileByName(String name) throws CloudRuntimeException {
184184
String command = VsmCommand.getPortProfile(name);
185185
if (command != null) {
186186
command = command.concat(SSH_NETCONF_TERMINATOR);
187187
send(command);
188188
// parse the rpc reply.
189-
VsmPortProfileResponse response = new VsmPortProfileResponse(receive().trim());
189+
String received = receive();
190+
VsmPortProfileResponse response = new VsmPortProfileResponse(received.trim());
190191
if (!response.isResponseOk()) {
191192
throw new CloudRuntimeException("Error response while getting the port profile details.");
193+
} else {
194+
return response.getPortProfile();
192195
}
193196
} else {
194-
throw new CloudRuntimeException("Error generating rpc request for removing policy map.");
197+
throw new CloudRuntimeException("Error generating rpc request for getting port profile.");
198+
}
199+
}
200+
201+
public PolicyMap getPolicyMapByName(String name) throws CloudRuntimeException {
202+
String command = VsmCommand.getPolicyMap(name);
203+
if (command != null) {
204+
command = command.concat(SSH_NETCONF_TERMINATOR);
205+
send(command);
206+
// parse the rpc reply.
207+
String received = receive();
208+
VsmPolicyMapResponse response = new VsmPolicyMapResponse(received.trim());
209+
if (!response.isResponseOk()) {
210+
throw new CloudRuntimeException("Error response while getting the port profile details.");
211+
} else {
212+
return response.getPolicyMap();
213+
}
214+
} else {
215+
throw new CloudRuntimeException("Error generating rpc request for getting policy map.");
195216
}
196217
}
197218

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.cloud.utils.cisco.n1kv.vsm;
2+
3+
public class PolicyMap {
4+
public String policyMapName;
5+
public int committedRate;
6+
public int burstRate;
7+
public int peakRate;
8+
9+
PolicyMap() {
10+
policyMapName = null;
11+
committedRate = 0;
12+
burstRate = 0;
13+
peakRate = 0;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.cloud.utils.cisco.n1kv.vsm;
2+
3+
import com.cloud.utils.cisco.n1kv.vsm.VsmCommand.BindingType;
4+
import com.cloud.utils.cisco.n1kv.vsm.VsmCommand.PortProfileType;
5+
import com.cloud.utils.cisco.n1kv.vsm.VsmCommand.SwitchPortMode;
6+
7+
public class PortProfile {
8+
public PortProfileType type;
9+
public SwitchPortMode mode;
10+
public BindingType binding;
11+
public String profileName;
12+
public String inputPolicyMap;
13+
public String outputPolicyMap;
14+
public String vlan;
15+
public boolean status;
16+
public int maxPorts;
17+
18+
PortProfile() {
19+
profileName = null;
20+
inputPolicyMap = null;
21+
outputPolicyMap = null;
22+
vlan = null;
23+
status = false;
24+
maxPorts = 32;
25+
type = PortProfileType.none;
26+
mode = SwitchPortMode.none;
27+
binding = BindingType.none;
28+
}
29+
}

utils/src/com/cloud/utils/cisco/n1kv/vsm/VsmCommand.java

+49-16
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ public static String getAddPortProfile(String name, PortProfileType type,
7979

8080
return serialize(domImpl, doc);
8181
} catch (ParserConfigurationException e) {
82-
s_logger.error("Error while creating delete message : " + e.getMessage());
82+
s_logger.error("Error while creating add port profile message : " + e.getMessage());
8383
return null;
8484
} catch (DOMException e) {
85-
s_logger.error("Error while creating delete message : " + e.getMessage());
85+
s_logger.error("Error while creating add port profile message : " + e.getMessage());
8686
return null;
8787
}
8888
}
@@ -113,10 +113,10 @@ public static String getUpdatePortProfile(String name, SwitchPortMode mode,
113113

114114
return serialize(domImpl, doc);
115115
} catch (ParserConfigurationException e) {
116-
s_logger.error("Error while creating update message : " + e.getMessage());
116+
s_logger.error("Error while creating update port profile message : " + e.getMessage());
117117
return null;
118118
} catch (DOMException e) {
119-
s_logger.error("Error while creating update message : " + e.getMessage());
119+
s_logger.error("Error while creating update port profile message : " + e.getMessage());
120120
return null;
121121
}
122122
}
@@ -146,15 +146,15 @@ public static String getDeletePortProfile(String portName) {
146146

147147
return serialize(domImpl, doc);
148148
} catch (ParserConfigurationException e) {
149-
s_logger.error("Error while creating delete message : " + e.getMessage());
149+
s_logger.error("Error while creating delete port profile message : " + e.getMessage());
150150
return null;
151151
} catch (DOMException e) {
152-
s_logger.error("Error while creating delete message : " + e.getMessage());
152+
s_logger.error("Error while creating delete port profile message : " + e.getMessage());
153153
return null;
154154
}
155155
}
156156

157-
public static String getPolicyMap(String name, int averageRate, int maxRate, int burstRate) {
157+
public static String getAddPolicyMap(String name, int averageRate, int maxRate, int burstRate) {
158158
try {
159159
// Create the document and root element.
160160
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
@@ -179,10 +179,10 @@ public static String getPolicyMap(String name, int averageRate, int maxRate, int
179179

180180
return serialize(domImpl, doc);
181181
} catch (ParserConfigurationException e) {
182-
s_logger.error("Error while creating delete message : " + e.getMessage());
182+
s_logger.error("Error while creating policy map message : " + e.getMessage());
183183
return null;
184184
} catch (DOMException e) {
185-
s_logger.error("Error while creating delete message : " + e.getMessage());
185+
s_logger.error("Error while creating policy map message : " + e.getMessage());
186186
return null;
187187
}
188188
}
@@ -212,10 +212,10 @@ public static String getDeletePolicyMap(String name) {
212212

213213
return serialize(domImpl, doc);
214214
} catch (ParserConfigurationException e) {
215-
s_logger.error("Error while creating delete message : " + e.getMessage());
215+
s_logger.error("Error while creating delete policy map message : " + e.getMessage());
216216
return null;
217217
} catch (DOMException e) {
218-
s_logger.error("Error while creating delete message : " + e.getMessage());
218+
s_logger.error("Error while creating delete policy map message : " + e.getMessage());
219219
return null;
220220
}
221221
}
@@ -245,10 +245,10 @@ public static String getServicePolicy(String policyMap, String portProfile, bool
245245

246246
return serialize(domImpl, doc);
247247
} catch (ParserConfigurationException e) {
248-
s_logger.error("Error while creating delete message : " + e.getMessage());
248+
s_logger.error("Error while creating attach/detach service policy message : " + e.getMessage());
249249
return null;
250250
} catch (DOMException e) {
251-
s_logger.error("Error while creating delete message : " + e.getMessage());
251+
s_logger.error("Error while creating attach/detach service policy message : " + e.getMessage());
252252
return null;
253253
}
254254
}
@@ -282,10 +282,43 @@ public static String getPortProfile(String name) {
282282

283283
return serialize(domImpl, doc);
284284
} catch (ParserConfigurationException e) {
285-
s_logger.error("Error while creating delete message : " + e.getMessage());
285+
s_logger.error("Error while creating the message to get port profile details: " + e.getMessage());
286286
return null;
287287
} catch (DOMException e) {
288-
s_logger.error("Error while creating delete message : " + e.getMessage());
288+
s_logger.error("Error while creating the message to get port profile details: " + e.getMessage());
289+
return null;
290+
}
291+
}
292+
293+
public static String getPolicyMap(String name) {
294+
try {
295+
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
296+
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
297+
DOMImplementation domImpl = docBuilder.getDOMImplementation();
298+
Document doc = createDocument(domImpl);
299+
300+
Element get = doc.createElement("nf:get");
301+
doc.getDocumentElement().appendChild(get);
302+
303+
Element filter = doc.createElement("nf:filter");
304+
filter.setAttribute("type", "subtree");
305+
get.appendChild(filter);
306+
307+
// Create the show port-profile name <profile-name> command.
308+
Element show = doc.createElement("show");
309+
filter.appendChild(show);
310+
Element policyMap = doc.createElement("policy-map");
311+
show.appendChild(policyMap);
312+
Element nameNode = doc.createElement("name");
313+
nameNode.setTextContent(name);
314+
policyMap.appendChild(nameNode);
315+
316+
return serialize(domImpl, doc);
317+
} catch (ParserConfigurationException e) {
318+
s_logger.error("Error while creating the message to get policy map details : " + e.getMessage());
319+
return null;
320+
} catch (DOMException e) {
321+
s_logger.error("Error while creating the message to get policy map details : " + e.getMessage());
289322
return null;
290323
}
291324
}
@@ -312,7 +345,7 @@ public static String getHello() {
312345
s_logger.error("Error while creating hello message : " + e.getMessage());
313346
return null;
314347
} catch (DOMException e) {
315-
s_logger.error("Error while creating delete message : " + e.getMessage());
348+
s_logger.error("Error while creating hello message : " + e.getMessage());
316349
return null;
317350
}
318351
}

utils/src/com/cloud/utils/cisco/n1kv/vsm/VsmOkResponse.java

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public class VsmOkResponse extends VsmResponse {
77

88
VsmOkResponse(String response) {
99
super(response);
10+
initialize();
1011
}
1112

1213
protected void parse(Element root) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.cloud.utils.cisco.n1kv.vsm;
2+
3+
import org.apache.log4j.Logger;
4+
5+
import org.w3c.dom.DOMException;
6+
import org.w3c.dom.Element;
7+
import org.w3c.dom.Node;
8+
import org.w3c.dom.NodeList;
9+
10+
public class VsmPolicyMapResponse extends VsmResponse{
11+
private static final Logger s_logger = Logger.getLogger(VsmPolicyMapResponse.class);
12+
private static final String s_policyMapDetails = "__XML__OPT_Cmd_show_policy-map___readonly__";
13+
14+
private PolicyMap _policyMap = new PolicyMap();
15+
16+
VsmPolicyMapResponse(String response) {
17+
super(response);
18+
initialize();
19+
}
20+
21+
public PolicyMap getPolicyMap() {
22+
return _policyMap;
23+
}
24+
25+
protected void parse(Element root) {
26+
NodeList list = root.getElementsByTagName("nf:rpc-error");
27+
if (list.getLength() == 0) {
28+
// No rpc-error tag; means response was ok.
29+
NodeList dataList = root.getElementsByTagName("nf:data");
30+
if (dataList.getLength() > 0) {
31+
parseData(dataList.item(0));
32+
_responseOk = true;
33+
}
34+
} else {
35+
super.parseError(list.item(0));
36+
_responseOk = false;
37+
}
38+
}
39+
40+
protected void parseData(Node data) {
41+
try {
42+
NodeList list = ((Element)data).getElementsByTagName(s_policyMapDetails);
43+
if (list.getLength() > 0) {
44+
NodeList readOnlyList = ((Element)list.item(0)).getElementsByTagName("__readonly__");
45+
Element readOnly = (Element)readOnlyList.item(0);
46+
47+
for (Node node = readOnly.getFirstChild();
48+
node != null; node = node.getNextSibling()) {
49+
String currentNode = node.getNodeName();
50+
String value = node.getTextContent();
51+
if ("pmap-name-out".equalsIgnoreCase(currentNode)) {
52+
_policyMap.policyMapName = value;
53+
} else if ("cir".equalsIgnoreCase(currentNode)) {
54+
_policyMap.committedRate = Integer.parseInt(value.trim());
55+
} else if ("bc".equalsIgnoreCase(currentNode)) {
56+
_policyMap.burstRate = Integer.parseInt(value.trim());
57+
} else if ("pir".equalsIgnoreCase(currentNode)) {
58+
_policyMap.peakRate = Integer.parseInt(value.trim());
59+
}
60+
}
61+
}
62+
} catch (DOMException e) {
63+
s_logger.error("Error parsing the response : " + e.toString());
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)