Skip to content

Commit ec19fac

Browse files
Devdeep SinghVijayendra Bhamidipati
authored andcommitted
CS-9919: Support for Nexus Swiches (Cisco Vswitches)
Description: Adding apis to create service policies and associate them with a port profile.
1 parent aa1f26c commit ec19fac

3 files changed

Lines changed: 281 additions & 8 deletions

File tree

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

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ public void queryStatus() throws CloudRuntimeException {
6060
+ " </xml>" + " </show>" + " </nc:filter>" + " </nc:get>"
6161
+ "</nc:rpc>" + SSH_NETCONF_TERMINATOR;
6262
send(status);
63-
// parse the rpc reply and the return success or failure.
63+
// parse the rpc reply.
6464
parseReply(receive());
6565
}
6666

6767
public void addPortProfile(String name, PortProfileType type, BindingType binding,
68-
SwitchPortMode mode, int vlanid, int networkRate) throws CloudRuntimeException {
69-
String command = VsmCommand.getAddPortProfile(name, type, binding, mode, vlanid, networkRate);
68+
SwitchPortMode mode, int vlanid) throws CloudRuntimeException {
69+
String command = VsmCommand.getAddPortProfile(name, type, binding, mode, vlanid);
7070
if (command != null) {
7171
command = command.concat(SSH_NETCONF_TERMINATOR);
7272
send(command);
73-
// parse the rpc reply and the return success or failure.
73+
// parse the rpc reply.
7474
parseReply(receive());
7575
} else {
7676
throw new CloudRuntimeException("Error generating rpc request for adding port profile.");
@@ -83,7 +83,7 @@ public void updatePortProfile(String name, SwitchPortMode mode,
8383
if (command != null) {
8484
command = command.concat(SSH_NETCONF_TERMINATOR);
8585
send(command);
86-
// parse the rpc reply and the return success or failure.
86+
// parse the rpc reply.
8787
parseReply(receive());
8888
} else {
8989
throw new CloudRuntimeException("Error generating rpc request for updating port profile.");
@@ -95,13 +95,57 @@ public void deletePortProfile(String name) throws CloudRuntimeException {
9595
if (command != null) {
9696
command = command.concat(SSH_NETCONF_TERMINATOR);
9797
send(command);
98-
// parse the rpc reply and the return success or failure.
98+
// parse the rpc reply.
9999
parseReply(receive());
100100
} else {
101101
throw new CloudRuntimeException("Error generating rpc request for deleting port profile.");
102102
}
103103
}
104104

105+
public void addPolicyMap(String name, int averageRate, int maxRate, int burstRate)
106+
throws CloudRuntimeException {
107+
String command = VsmCommand.getPolicyMap(name, averageRate, maxRate, burstRate);
108+
if (command != null) {
109+
command = command.concat(SSH_NETCONF_TERMINATOR);
110+
send(command);
111+
// parse the rpc reply.
112+
parseReply(receive());
113+
} else {
114+
throw new CloudRuntimeException("Error generating rpc request for adding/updating policy map.");
115+
}
116+
}
117+
118+
public void deletePolicyMap(String name) throws CloudRuntimeException {
119+
String command = VsmCommand.getDeletePolicyMap(name);
120+
if (command != null) {
121+
command = command.concat(SSH_NETCONF_TERMINATOR);
122+
send(command);
123+
// parse the rpc reply.
124+
parseReply(receive());
125+
} else {
126+
throw new CloudRuntimeException("Error generating rpc request for deleting policy map.");
127+
}
128+
}
129+
130+
public void updatePolicyMap(String name, int averageRate, int maxRate, int burstRate)
131+
throws CloudRuntimeException {
132+
// Add and update of policy map work in the exact same way.
133+
addPolicyMap(name, averageRate, maxRate, burstRate);
134+
}
135+
136+
public void attachServicePolicy(String policyMap, String portProfile)
137+
throws CloudRuntimeException {
138+
String command = VsmCommand.getAttachServicePolicy(policyMap, portProfile);
139+
if (command != null) {
140+
command = command.concat(SSH_NETCONF_TERMINATOR);
141+
send(command);
142+
// parse the rpc reply.
143+
parseReply(receive());
144+
} else {
145+
throw new CloudRuntimeException("Error generating rpc request for adding policy map.");
146+
}
147+
}
148+
105149
private void exchangeHello() {
106150
String ack = receive();
107151
String hello = VsmCommand.getHello() + SSH_NETCONF_TERMINATOR;

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

Lines changed: 230 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public class VsmCommand {
2323
private static final String s_ciscons = "http://www.cisco.com/nxos:1.0:ppm";
2424
private static final String s_configuremode = "__XML__MODE__exec_configure";
2525
private static final String s_portprofmode = "__XML__MODE_port-prof";
26+
private static final String s_policymapmode = "__XML__MODE_policy-map";
27+
private static final String s_classtypemode = "__XML__MODE_policy-map_class_type";
2628
private static final String s_paramvalue = "__XML__PARAM_value";
2729

2830
public enum PortProfileType {
@@ -53,7 +55,7 @@ public enum OperationType {
5355
}
5456

5557
public static String getAddPortProfile(String name, PortProfileType type,
56-
BindingType binding, SwitchPortMode mode, int vlanid, int networkRate) {
58+
BindingType binding, SwitchPortMode mode, int vlanid) {
5759
try {
5860
// Create the document and root element.
5961
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
@@ -153,6 +155,105 @@ public static String getDeletePortProfile(String portName) {
153155
}
154156
}
155157

158+
public static String getPolicyMap(String name, int averageRate, int maxRate, int burstRate) {
159+
try {
160+
// Create the document and root element.
161+
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
162+
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
163+
DOMImplementation domImpl = docBuilder.getDOMImplementation();
164+
Document doc = createDocument(domImpl);
165+
166+
// Edit configuration command.
167+
Element editConfig = doc.createElement("nf:edit-config");
168+
doc.getDocumentElement().appendChild(editConfig);
169+
170+
// Command to get into exec configure mode.
171+
Element target = doc.createElement("nf:target");
172+
Element running = doc.createElement("nf:running");
173+
target.appendChild(running);
174+
editConfig.appendChild(target);
175+
176+
// Command to create the port profile with the desired configuration.
177+
Element config = doc.createElement("nf:config");
178+
config.appendChild(policyMapDetails(doc, name, averageRate, maxRate, burstRate));
179+
editConfig.appendChild(config);
180+
181+
return serialize(domImpl, doc);
182+
} catch (ParserConfigurationException e) {
183+
s_logger.error("Error while creating delete message : " + e.getMessage());
184+
return null;
185+
} catch (DOMException e) {
186+
s_logger.error("Error while creating delete message : " + e.getMessage());
187+
return null;
188+
}
189+
}
190+
191+
public static String getDeletePolicyMap(String name) {
192+
try {
193+
// Create the document and root element.
194+
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
195+
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
196+
DOMImplementation domImpl = docBuilder.getDOMImplementation();
197+
Document doc = createDocument(domImpl);
198+
199+
// Edit configuration command.
200+
Element editConfig = doc.createElement("nf:edit-config");
201+
doc.getDocumentElement().appendChild(editConfig);
202+
203+
// Command to get into exec configure mode.
204+
Element target = doc.createElement("nf:target");
205+
Element running = doc.createElement("nf:running");
206+
target.appendChild(running);
207+
editConfig.appendChild(target);
208+
209+
// Command to create the port profile with the desired configuration.
210+
Element config = doc.createElement("nf:config");
211+
config.appendChild(deletePolicyMapDetails(doc, name));
212+
editConfig.appendChild(config);
213+
214+
return serialize(domImpl, doc);
215+
} catch (ParserConfigurationException e) {
216+
s_logger.error("Error while creating delete message : " + e.getMessage());
217+
return null;
218+
} catch (DOMException e) {
219+
s_logger.error("Error while creating delete message : " + e.getMessage());
220+
return null;
221+
}
222+
}
223+
224+
public static String getAttachServicePolicy(String policyMap, String portProfile) {
225+
try {
226+
// Create the document and root element.
227+
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
228+
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
229+
DOMImplementation domImpl = docBuilder.getDOMImplementation();
230+
Document doc = createDocument(domImpl);
231+
232+
// Edit configuration command.
233+
Element editConfig = doc.createElement("nf:edit-config");
234+
doc.getDocumentElement().appendChild(editConfig);
235+
236+
// Command to get into exec configure mode.
237+
Element target = doc.createElement("nf:target");
238+
Element running = doc.createElement("nf:running");
239+
target.appendChild(running);
240+
editConfig.appendChild(target);
241+
242+
// Command to create the port profile with the desired configuration.
243+
Element config = doc.createElement("nf:config");
244+
config.appendChild(attachServiceDetails(doc, policyMap, portProfile));
245+
editConfig.appendChild(config);
246+
247+
return serialize(domImpl, doc);
248+
} catch (ParserConfigurationException e) {
249+
s_logger.error("Error while creating delete message : " + e.getMessage());
250+
return null;
251+
} catch (DOMException e) {
252+
s_logger.error("Error while creating delete message : " + e.getMessage());
253+
return null;
254+
}
255+
}
256+
156257
public static String getHello() {
157258
try {
158259
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
@@ -333,6 +434,119 @@ private static Element deletePortProfileDetails(Document doc, String name) {
333434
return configure;
334435
}
335436

437+
private static Element policyMapDetails(Document doc, String name,
438+
int averageRate, int maxRate, int burstRate) {
439+
Element configure = doc.createElementNS(s_ciscons, "nxos:configure");
440+
Element modeConfigure = doc.createElement("nxos:" + s_configuremode);
441+
configure.appendChild(modeConfigure);
442+
443+
// Policy map details
444+
Element policyMap = doc.createElement("policy-map");
445+
modeConfigure.appendChild(policyMap);
446+
447+
Element policyDetails = doc.createElement("name");
448+
policyMap.appendChild(policyDetails);
449+
450+
// Name of the policy to create/update.
451+
Element value = doc.createElement(s_paramvalue);
452+
value.setAttribute("isKey", "true");
453+
value.setTextContent(name);
454+
policyDetails.appendChild(value);
455+
456+
Element policyMapMode = doc.createElement(s_policymapmode);
457+
policyDetails.appendChild(policyMapMode);
458+
459+
// Create the default class to match all trafic.
460+
Element classRoot = doc.createElement("class");
461+
Element classDefault = doc.createElement("class-default");
462+
policyMapMode.appendChild(classRoot);
463+
classRoot.appendChild(classDefault);
464+
465+
Element classMode = doc.createElement(s_classtypemode);
466+
classDefault.appendChild(classMode);
467+
468+
// Set the average, max and burst rate.
469+
// TODO: Add handling for max and burst.
470+
Element police = doc.createElement("police");
471+
classMode.appendChild(police);
472+
473+
// Set the committed information rate and its value in mbps.
474+
Element cir = doc.createElement("cir");
475+
police.appendChild(cir);
476+
Element cirValue = doc.createElement(s_paramvalue);
477+
Element mbps = doc.createElement("mbps");
478+
cirValue.setTextContent(Integer.toString(averageRate));
479+
cir.appendChild(cirValue);
480+
cir.appendChild(mbps);
481+
482+
// Persist the configuration across reboots.
483+
modeConfigure.appendChild(persistConfiguration(doc));
484+
485+
return configure;
486+
}
487+
488+
private static Element deletePolicyMapDetails(Document doc, String name) {
489+
Element configure = doc.createElementNS(s_ciscons, "nxos:configure");
490+
Element modeConfigure = doc.createElement("nxos:" + s_configuremode);
491+
configure.appendChild(modeConfigure);
492+
493+
// Delete Policy map details
494+
Element deletePolicyMap = doc.createElement("no");
495+
Element policyMap = doc.createElement("policy-map");
496+
deletePolicyMap.appendChild(policyMap);
497+
modeConfigure.appendChild(deletePolicyMap);
498+
499+
Element policyDetails = doc.createElement("name");
500+
policyMap.appendChild(policyDetails);
501+
502+
// Name of the policy to create/update.
503+
Element value = doc.createElement(s_paramvalue);
504+
value.setAttribute("isKey", "true");
505+
value.setTextContent(name);
506+
policyDetails.appendChild(value);
507+
508+
// Persist the configuration across reboots.
509+
modeConfigure.appendChild(persistConfiguration(doc));
510+
511+
return configure;
512+
}
513+
514+
private static Element attachServiceDetails(Document doc, String policyMap, String portProfile) {
515+
// In mode, exec_configure.
516+
Element configure = doc.createElementNS(s_ciscons, "nxos:configure");
517+
Element modeConfigure = doc.createElement("nxos:" + s_configuremode);
518+
configure.appendChild(modeConfigure);
519+
520+
// Port profile name and type configuration.
521+
Element profile = doc.createElement("port-profile");
522+
modeConfigure.appendChild(profile);
523+
524+
// Port profile type.
525+
Element portDetails = doc.createElement("name");
526+
profile.appendChild(portDetails);
527+
528+
// Name of the profile to update.
529+
Element value = doc.createElement(s_paramvalue);
530+
value.setAttribute("isKey", "true");
531+
value.setTextContent(portProfile);
532+
portDetails.appendChild(value);
533+
534+
// element for port prof mode.
535+
Element portProfMode = doc.createElement(s_portprofmode);
536+
portDetails.appendChild(portProfMode);
537+
538+
// Associate the policy for input.
539+
portProfMode.appendChild(getServicePolicyCmd(doc, policyMap, "input"));
540+
541+
// Associate the policy for output.
542+
portProfMode.appendChild(getServicePolicyCmd(doc, policyMap, "output"));
543+
544+
// Persist the configuration across reboots.
545+
modeConfigure.appendChild(persistConfiguration(doc));
546+
547+
return configure;
548+
}
549+
336550
private static Element persistConfiguration(Document doc) {
337551
Element copy = doc.createElement("copy");
338552
Element running = doc.createElement("running-config");
@@ -458,6 +672,21 @@ private static Element getSwitchPortMode(Document doc, SwitchPortMode mode) {
458672
return switchport;
459673
}
460674

675+
private static Element getServicePolicyCmd(Document doc, String policyMap, String type) {
676+
Element service = doc.createElement("service-policy");
677+
Element input = doc.createElement(type);
678+
service.appendChild(input);
679+
680+
Element name = doc.createElement("name");
681+
input.appendChild(name);
682+
683+
Element policyValue = doc.createElement(s_paramvalue);
684+
policyValue.setTextContent(policyMap);
685+
name.appendChild(policyValue);
686+
687+
return service;
688+
}
689+
461690
private static Document createDocument(DOMImplementation dom) {
462691
Document doc = dom.createDocument(s_namespace, "nf:rpc", null);
463692
doc.getDocumentElement().setAttribute( "message-id", "101" );

vmware-base/src/com/cloud/hypervisor/vmware/mo/HypervisorHostHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public static void createPortProfile(VmwareContext context, String ethPortProfil
179179
}
180180

181181
try {
182-
netconfClient.addPortProfile(networkName, PortProfileType.vethernet, BindingType.portbindingstatic, SwitchPortMode.access, vid, networkRateMbps);
182+
netconfClient.addPortProfile(networkName, PortProfileType.vethernet, BindingType.portbindingstatic, SwitchPortMode.access, vid);
183183
} catch(CloudRuntimeException e) {
184184
msg = "Failed to add vethernet port profile " + networkName + " with parameters " + params.toString();
185185
s_logger.error(msg);

0 commit comments

Comments
 (0)