From 0e3bf8cabb845bf10005f5fe3320544f596344ff Mon Sep 17 00:00:00 2001 From: Devdeep Singh Date: Sat, 26 May 2012 04:38:31 +0530 Subject: [PATCH] CS-14956: Fixing an issue that surfaced while testing rate limiting policies. An error was getting reported during policy map creation that config operation was in progress, Added synchronization to make sure sending and receiving commands are seralized. Also removed the retry logic as after this change it is not needed. Reviewed-By: Vijay --- .../utils/cisco/n1kv/vsm/NetconfHelper.java | 71 ++++++------------- .../utils/cisco/n1kv/vsm/VsmCommand.java | 12 ++-- 2 files changed, 27 insertions(+), 56 deletions(-) diff --git a/utils/src/com/cloud/utils/cisco/n1kv/vsm/NetconfHelper.java b/utils/src/com/cloud/utils/cisco/n1kv/vsm/NetconfHelper.java index 0ad368a4dfe..756179646c6 100644 --- a/utils/src/com/cloud/utils/cisco/n1kv/vsm/NetconfHelper.java +++ b/utils/src/com/cloud/utils/cisco/n1kv/vsm/NetconfHelper.java @@ -20,9 +20,6 @@ public class NetconfHelper { private static final String SSH_NETCONF_TERMINATOR = "]]>]]>"; - // Number of times to retry the command on failure. - private static final int s_retryCount = 3; - private Connection _connection; private Session _session; @@ -71,28 +68,7 @@ public class NetconfHelper { String command = VsmCommand.getAddPortProfile(name, type, binding, mode, vlanid); if (command != null) { command = command.concat(SSH_NETCONF_TERMINATOR); - - // This command occasionally fails. On retry it succeeds. Putting in - // retry to handle failures. - for (int i = 0; i < s_retryCount; ++i) { - send(command); - // parse the rpc reply. - // parseOkReply(receive()); - VsmOkResponse response = new VsmOkResponse(receive().trim()); - if (!response.isResponseOk()) { - if (i >= s_retryCount) { - throw new CloudRuntimeException(response.toString()); - } - - try { - Thread.sleep(1000); - } catch (final InterruptedException e) { - s_logger.debug("Got interrupted while waiting."); - } - } else { - break; - } - } + parseOkReply(sendAndReceive(command)); } else { throw new CloudRuntimeException("Error generating rpc request for adding port profile."); } @@ -103,9 +79,7 @@ public class NetconfHelper { String command = VsmCommand.getUpdatePortProfile(name, mode, params); if (command != null) { command = command.concat(SSH_NETCONF_TERMINATOR); - send(command); - // parse the rpc reply. - parseOkReply(receive()); + parseOkReply(sendAndReceive(command)); } else { throw new CloudRuntimeException("Error generating rpc request for updating port profile."); } @@ -115,9 +89,7 @@ public class NetconfHelper { String command = VsmCommand.getDeletePortProfile(name); if (command != null) { command = command.concat(SSH_NETCONF_TERMINATOR); - send(command); - // parse the rpc reply. - parseOkReply(receive()); + parseOkReply(sendAndReceive(command)); } else { throw new CloudRuntimeException("Error generating rpc request for deleting port profile."); } @@ -128,9 +100,7 @@ public class NetconfHelper { String command = VsmCommand.getAddPolicyMap(name, averageRate, maxRate, burstRate); if (command != null) { command = command.concat(SSH_NETCONF_TERMINATOR); - send(command); - // parse the rpc reply. - parseOkReply(receive()); + parseOkReply(sendAndReceive(command)); } else { throw new CloudRuntimeException("Error generating rpc request for adding/updating policy map."); } @@ -140,9 +110,7 @@ public class NetconfHelper { String command = VsmCommand.getDeletePolicyMap(name); if (command != null) { command = command.concat(SSH_NETCONF_TERMINATOR); - send(command); - // parse the rpc reply. - parseOkReply(receive()); + parseOkReply(sendAndReceive(command)); } else { throw new CloudRuntimeException("Error generating rpc request for deleting policy map."); } @@ -159,9 +127,7 @@ public class NetconfHelper { String command = VsmCommand.getServicePolicy(policyMap, portProfile, true); if (command != null) { command = command.concat(SSH_NETCONF_TERMINATOR); - send(command); - // parse the rpc reply. - parseOkReply(receive()); + parseOkReply(sendAndReceive(command)); } else { throw new CloudRuntimeException("Error generating rpc request for adding policy map."); } @@ -172,9 +138,7 @@ public class NetconfHelper { String command = VsmCommand.getServicePolicy(policyMap, portProfile, false); if (command != null) { command = command.concat(SSH_NETCONF_TERMINATOR); - send(command); - // parse the rpc reply. - parseOkReply(receive()); + parseOkReply(sendAndReceive(command)); } else { throw new CloudRuntimeException("Error generating rpc request for removing policy map."); } @@ -184,12 +148,10 @@ public class NetconfHelper { String command = VsmCommand.getPortProfile(name); if (command != null) { command = command.concat(SSH_NETCONF_TERMINATOR); - send(command); - // parse the rpc reply. - String received = receive(); + String received = sendAndReceive(command); VsmPortProfileResponse response = new VsmPortProfileResponse(received.trim()); if (!response.isResponseOk()) { - throw new CloudRuntimeException("Error response while getting the port profile details."); + throw new CloudRuntimeException(response.toString()); } else { return response.getPortProfile(); } @@ -202,12 +164,10 @@ public class NetconfHelper { String command = VsmCommand.getPolicyMap(name); if (command != null) { command = command.concat(SSH_NETCONF_TERMINATOR); - send(command); - // parse the rpc reply. - String received = receive(); + String received = sendAndReceive(command); VsmPolicyMapResponse response = new VsmPolicyMapResponse(received.trim()); if (!response.isResponseOk()) { - throw new CloudRuntimeException("Error response while getting the port profile details."); + throw new CloudRuntimeException(response.toString()); } else { return response.getPolicyMap(); } @@ -222,6 +182,15 @@ public class NetconfHelper { send(hello); } + private String sendAndReceive(String command) { + String received; + synchronized (NetconfHelper.class) { + send(command); + received = receive(); + } + return received; + } + private void send(String message) { try { OutputStream outputStream = _session.getStdin(); diff --git a/utils/src/com/cloud/utils/cisco/n1kv/vsm/VsmCommand.java b/utils/src/com/cloud/utils/cisco/n1kv/vsm/VsmCommand.java index d510d6d0676..1136a0e0e7c 100644 --- a/utils/src/com/cloud/utils/cisco/n1kv/vsm/VsmCommand.java +++ b/utils/src/com/cloud/utils/cisco/n1kv/vsm/VsmCommand.java @@ -527,7 +527,7 @@ public class VsmCommand { Element policyMapMode = doc.createElement(s_policymapmode); policyDetails.appendChild(policyMapMode); - // Create the default class to match all trafic. + // Create the default class to match all traffic. Element classRoot = doc.createElement("class"); Element classDefault = doc.createElement("class-default"); policyMapMode.appendChild(classRoot); @@ -544,11 +544,13 @@ public class VsmCommand { // Set the committed information rate and its value in mbps. Element cir = doc.createElement("cir"); police.appendChild(cir); - Element cirValue = doc.createElement(s_paramvalue); - Element mbps = doc.createElement("mbps"); - cirValue.setTextContent(Integer.toString(averageRate)); + Element cirValue = doc.createElement("cir-val"); cir.appendChild(cirValue); - cir.appendChild(mbps); + Element value2 = doc.createElement(s_paramvalue); + Element mbps = doc.createElement("mbps"); + value2.setTextContent(Integer.toString(averageRate)); + cirValue.appendChild(value2); + cirValue.appendChild(mbps); // Persist the configuration across reboots. modeConfigure.appendChild(persistConfiguration(doc));