Skip to content

Commit 4ff2893

Browse files
Devdeep SinghVijayendra Bhamidipati
authored andcommitted
CS-9919: Support for Nexus Swiches (Cisco Vswitches)
Description: Updated the add port profile routine to support more options (bindingtype, switchport mode, port profile types).
1 parent 0f28222 commit 4ff2893

3 files changed

Lines changed: 367 additions & 232 deletions

File tree

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

Lines changed: 32 additions & 227 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,13 @@
55
import java.io.InputStream;
66
import java.io.OutputStream;
77

8-
import javax.xml.parsers.DocumentBuilder;
9-
import javax.xml.parsers.DocumentBuilderFactory;
10-
import javax.xml.parsers.ParserConfigurationException;
11-
12-
import org.w3c.dom.DOMImplementation;
13-
import org.w3c.dom.Document;
14-
import org.w3c.dom.Element;
15-
import org.w3c.dom.ls.DOMImplementationLS;
16-
import org.w3c.dom.ls.LSSerializer;
17-
import org.w3c.dom.DOMException;
18-
198
import com.cloud.utils.ssh.*;
209
import com.trilead.ssh2.Session;
2110
import com.trilead.ssh2.Connection;
2211
import com.trilead.ssh2.ChannelCondition;
12+
import com.cloud.utils.cisco.n1kv.vsm.VsmCommand.BindingType;
13+
import com.cloud.utils.cisco.n1kv.vsm.VsmCommand.PortProfileType;
14+
import com.cloud.utils.cisco.n1kv.vsm.VsmCommand.SwitchPortMode;
2315
import com.cloud.utils.exception.CloudRuntimeException;
2416

2517
public class NetconfHelper {
@@ -56,8 +48,8 @@ public void disconnect() {
5648
SSHCmdHelper.releaseSshConnection(_connection);
5749
}
5850

59-
public void queryStatus() {
60-
// This command is used query the server status.
51+
public void queryStatus() throws CloudRuntimeException {
52+
// This command is used to query the server status.
6153
String status = "<?xml version=\"1.0\"?>"
6254
+ "<nc:rpc message-id=\"1\" xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0"
6355
+ "\"xmlns=\"http://www.cisco.com/nxos:1.0:xml\">" + " <nc:get>"
@@ -66,231 +58,29 @@ public void queryStatus() {
6658
+ " </xml>" + " </show>" + " </nc:filter>" + " </nc:get>"
6759
+ "</nc:rpc>" + SSH_NETCONF_TERMINATOR;
6860
send(status);
69-
String reply = receive();
70-
}
71-
72-
private String getAddPortProfile(String portName, int vlanid) {
73-
try {
74-
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
75-
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
76-
DOMImplementation domImpl = docBuilder.getDOMImplementation();
77-
78-
// Root elements.
79-
Document doc = domImpl.createDocument("urn:ietf:params:xml:ns:netconf:base:1.0",
80-
"nf:rpc", null);
81-
doc.getDocumentElement().setAttribute( "message-id", "101" );
82-
doc.getDocumentElement().setAttributeNS("http://www.cisco.com/nxos:1.0:ppm",
83-
"addportprofile", "true");
84-
85-
// Edit configuration command.
86-
Element editConfig = doc.createElement("nf:edit-config");
87-
Element target = doc.createElement("nf:target");
88-
Element running = doc.createElement("nf:running");
89-
target.appendChild(running);
90-
Element config = doc.createElement("nf:config");
91-
Element configure = doc.createElementNS("http://www.cisco.com/nxos:1.0:ppm", "nxos:configure");
92-
Element execConfigure = doc.createElement("nxos:__XML__MODE__exec_configure");
93-
Element portProfile = doc.createElement("port-profile");
94-
Element type = doc.createElement("type");
95-
Element ethernettype = doc.createElement("vethernet");
96-
Element name = doc.createElement("name");
97-
Element value = doc.createElement("__XML__PARAM_value");
98-
value.setAttribute("isKey", "true");
99-
value.setTextContent(portName);
100-
101-
// Port profile details start here.
102-
Element portProf = doc.createElement("__XML__MODE__port-prof");
103-
104-
// Command : switchport mode access
105-
Element switchport1 = doc.createElement("switchport");
106-
Element mode1 = doc.createElement("mode");
107-
Element access1 = doc.createElement("access");
108-
mode1.appendChild(access1);
109-
switchport1.appendChild(mode1);
110-
111-
// Command : switchport access vlan <vlanid>
112-
Element switchport2 = doc.createElement("switchport");
113-
Element access2 = doc.createElement("access");
114-
Element vlan = doc.createElement("vlan");
115-
Element vlancreate = doc.createElement("vlan-id-create-delete");
116-
Element value2 = doc.createElement("__XML__PARAM_value");
117-
value2.setTextContent(Integer.toString(vlanid));
118-
vlancreate.appendChild(value2);
119-
vlan.appendChild(vlancreate);
120-
access2.appendChild(vlan);
121-
switchport2.appendChild(access2);
122-
123-
// Command : vmware port-group
124-
Element vmware = doc.createElement("vmware");
125-
Element portgroup = doc.createElement("port-group");
126-
vmware.appendChild(portgroup);
127-
128-
// Command : state enabled.
129-
Element state = doc.createElement("state");
130-
Element enabled = doc.createElement("enabled");
131-
state.appendChild(enabled);
132-
133-
// Command : no shutdown.
134-
Element no = doc.createElement("no");
135-
Element shutdown = doc.createElement("shutdown");
136-
no.appendChild(shutdown);
137-
138-
// Put the port profile details together.
139-
portProf.appendChild(switchport1);
140-
portProf.appendChild(switchport2);
141-
portProf.appendChild(vmware);
142-
portProf.appendChild(state);
143-
portProf.appendChild(no);
144-
145-
// Put the xml-rpc together.
146-
name.appendChild(value);
147-
name.appendChild(portProf);
148-
ethernettype.appendChild(name);
149-
type.appendChild(ethernettype);
150-
portProfile.appendChild(type);
151-
execConfigure.appendChild(portProfile);
152-
configure.appendChild(execConfigure);
153-
config.appendChild(configure);
154-
editConfig.appendChild(target);
155-
editConfig.appendChild(config);
156-
doc.getDocumentElement().appendChild(editConfig);
157-
158-
return serialize(domImpl, doc);
159-
} catch (ParserConfigurationException e) {
160-
s_logger.error("Error while creating add port profile message : " + e.getMessage());
161-
return null;
162-
} catch (DOMException e) {
163-
s_logger.error("Error while creating add port profile message : " + e.getMessage());
164-
return null;
165-
}
61+
// parse the rpc reply and the return success or failure.
62+
parseReply(receive());
16663
}
16764

168-
public void addPortProfile(String name, int vlan) {
169-
String command = getAddPortProfile(name, vlan) + SSH_NETCONF_TERMINATOR;
65+
public void addPortProfile(String name, PortProfileType type, BindingType binding,
66+
SwitchPortMode mode, int vlanid) throws CloudRuntimeException {
67+
String command = VsmCommand.getAddPortProfile(name, type, binding, mode, vlanid);
68+
command = command.concat(SSH_NETCONF_TERMINATOR);
17069
send(command);
171-
17270
// parse the rpc reply and the return success or failure.
173-
String reply = receive().trim();
174-
if (reply.endsWith(SSH_NETCONF_TERMINATOR)) {
175-
reply = reply.substring(0, reply.length() - (new String(SSH_NETCONF_TERMINATOR).length()));
176-
}
177-
else {
178-
throw new CloudRuntimeException("Malformed repsonse from vsm for add " +
179-
"port profile request: " + reply);
180-
}
181-
182-
VsmResponse response = new VsmResponse(reply);
183-
if (!response.isResponseOk()) {
184-
throw new CloudRuntimeException(response.toString());
185-
}
186-
}
187-
188-
private String getDeletePortProfile(String portName) {
189-
try {
190-
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
191-
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
192-
DOMImplementation domImpl = docBuilder.getDOMImplementation();
193-
194-
// Root elements.
195-
Document doc = domImpl.createDocument("urn:ietf:params:xml:ns:netconf:base:1.0",
196-
"nf:rpc", null);
197-
doc.getDocumentElement().setAttribute( "message-id", "101" );
198-
doc.getDocumentElement().setAttributeNS("http://www.cisco.com/nxos:1.0:ppm",
199-
"deleteportprofile", "true");
200-
201-
// Edit configuration command.
202-
Element editConfig = doc.createElement("nf:edit-config");
203-
204-
// Details of the port profile to delete.
205-
Element target = doc.createElement("nf:target");
206-
Element running = doc.createElement("nf:running");
207-
target.appendChild(running);
208-
209-
Element config = doc.createElement("nf:config");
210-
Element configure = doc.createElementNS("http://www.cisco.com/nxos:1.0:ppm", "nxos:configure");
211-
Element execConfigure = doc.createElement("nxos:__XML__MODE__exec_configure");
212-
Element delete = doc.createElement("no");
213-
Element portProfile = doc.createElement("port-profile");
214-
Element name = doc.createElement("name");
215-
Element value = doc.createElement("__XML__PARAM_value");
216-
value.setAttribute("isKey", "true");
217-
value.setTextContent(portName);
218-
219-
// Put the xml-rpc together.
220-
name.appendChild(value);
221-
portProfile.appendChild(name);
222-
delete.appendChild(portProfile);
223-
execConfigure.appendChild(delete);
224-
configure.appendChild(execConfigure);
225-
config.appendChild(configure);
226-
editConfig.appendChild(target);
227-
editConfig.appendChild(config);
228-
doc.getDocumentElement().appendChild(editConfig);
229-
230-
return serialize(domImpl, doc);
231-
} catch (ParserConfigurationException e) {
232-
s_logger.error("Error while creating delete message : " + e.getMessage());
233-
return null;
234-
} catch (DOMException e) {
235-
s_logger.error("Error while creating delete message : " + e.getMessage());
236-
return null;
237-
}
71+
parseReply(receive());
23872
}
23973

240-
public void deletePortProfile(String name) {
241-
String command = getDeletePortProfile(name) + SSH_NETCONF_TERMINATOR;
74+
public void deletePortProfile(String name) throws CloudRuntimeException {
75+
String command = VsmCommand.getDeletePortProfile(name) + SSH_NETCONF_TERMINATOR;
24276
send(command);
243-
24477
// parse the rpc reply and the return success or failure.
245-
String reply = receive().trim();
246-
if (reply.endsWith(SSH_NETCONF_TERMINATOR)) {
247-
reply = reply.substring(0, reply.length() - (new String(SSH_NETCONF_TERMINATOR).length()));
248-
}
249-
else {
250-
throw new CloudRuntimeException("Malformed repsonse from vsm for delete " +
251-
"port profile request :" + reply);
252-
}
253-
254-
VsmResponse response = new VsmResponse(reply);
255-
if (!response.isResponseOk()) {
256-
throw new CloudRuntimeException(response.toString());
257-
}
258-
}
259-
260-
private String getHello() {
261-
try {
262-
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
263-
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
264-
DOMImplementation domImpl = docBuilder.getDOMImplementation();
265-
266-
// Root elements.
267-
Document doc = domImpl.createDocument("urn:ietf:params:xml:ns:netconf:base:1.0",
268-
"nc:hello", null);
269-
270-
// Client capacity. We are only supporting basic capacity.
271-
Element capabilities = doc.createElement("nc:capabilities");
272-
Element capability = doc.createElement("nc:capability");
273-
capability.setTextContent("urn:ietf:params:xml:ns:netconf:base:1.0");
274-
275-
capabilities.appendChild(capability);
276-
doc.getDocumentElement().appendChild(capabilities);
277-
278-
return serialize(domImpl, doc);
279-
} catch (ParserConfigurationException e) {
280-
s_logger.error("Error while creating hello message : " + e.getMessage());
281-
return null;
282-
}
283-
}
284-
285-
private String serialize(DOMImplementation domImpl, Document document) {
286-
DOMImplementationLS ls = (DOMImplementationLS) domImpl;
287-
LSSerializer lss = ls.createLSSerializer();
288-
return lss.writeToString(document);
78+
parseReply(receive());
28979
}
29080

29181
private void exchangeHello() {
29282
String ack = receive();
293-
String hello = getHello() + SSH_NETCONF_TERMINATOR;
83+
String hello = VsmCommand.getHello() + SSH_NETCONF_TERMINATOR;
29484
send(hello);
29585
}
29686

@@ -312,7 +102,7 @@ private String receive() {
312102
while (true) {
313103
if (inputStream.available() == 0) {
314104
int conditions = _session.waitForCondition(ChannelCondition.STDOUT_DATA
315-
| ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 100);
105+
| ChannelCondition.STDERR_DATA | ChannelCondition.EOF, 500);
316106

317107
if ((conditions & ChannelCondition.TIMEOUT) != 0) {
318108
break;
@@ -336,4 +126,19 @@ private String receive() {
336126

337127
return new String(buffer);
338128
}
129+
130+
private void parseReply(String reply) throws CloudRuntimeException {
131+
reply = reply.trim();
132+
if (reply.endsWith(SSH_NETCONF_TERMINATOR)) {
133+
reply = reply.substring(0, reply.length() - (new String(SSH_NETCONF_TERMINATOR).length()));
134+
}
135+
else {
136+
throw new CloudRuntimeException("Malformed response from vsm" + reply);
137+
}
138+
139+
VsmResponse response = new VsmResponse(reply);
140+
if (!response.isResponseOk()) {
141+
throw new CloudRuntimeException(response.toString());
142+
}
143+
}
339144
}

0 commit comments

Comments
 (0)