@@ -20,9 +20,6 @@ public class NetconfHelper {
20
20
21
21
private static final String SSH_NETCONF_TERMINATOR = "]]>]]>" ;
22
22
23
- // Number of times to retry the command on failure.
24
- private static final int s_retryCount = 3 ;
25
-
26
23
private Connection _connection ;
27
24
28
25
private Session _session ;
@@ -71,28 +68,7 @@ public void addPortProfile(String name, PortProfileType type, BindingType bindin
71
68
String command = VsmCommand .getAddPortProfile (name , type , binding , mode , vlanid );
72
69
if (command != null ) {
73
70
command = command .concat (SSH_NETCONF_TERMINATOR );
74
-
75
- // This command occasionally fails. On retry it succeeds. Putting in
76
- // retry to handle failures.
77
- for (int i = 0 ; i < s_retryCount ; ++i ) {
78
- send (command );
79
- // parse the rpc reply.
80
- // parseOkReply(receive());
81
- VsmOkResponse response = new VsmOkResponse (receive ().trim ());
82
- if (!response .isResponseOk ()) {
83
- if (i >= s_retryCount ) {
84
- throw new CloudRuntimeException (response .toString ());
85
- }
86
-
87
- try {
88
- Thread .sleep (1000 );
89
- } catch (final InterruptedException e ) {
90
- s_logger .debug ("Got interrupted while waiting." );
91
- }
92
- } else {
93
- break ;
94
- }
95
- }
71
+ parseOkReply (sendAndReceive (command ));
96
72
} else {
97
73
throw new CloudRuntimeException ("Error generating rpc request for adding port profile." );
98
74
}
@@ -103,9 +79,7 @@ public void updatePortProfile(String name, SwitchPortMode mode,
103
79
String command = VsmCommand .getUpdatePortProfile (name , mode , params );
104
80
if (command != null ) {
105
81
command = command .concat (SSH_NETCONF_TERMINATOR );
106
- send (command );
107
- // parse the rpc reply.
108
- parseOkReply (receive ());
82
+ parseOkReply (sendAndReceive (command ));
109
83
} else {
110
84
throw new CloudRuntimeException ("Error generating rpc request for updating port profile." );
111
85
}
@@ -115,9 +89,7 @@ public void deletePortProfile(String name) throws CloudRuntimeException {
115
89
String command = VsmCommand .getDeletePortProfile (name );
116
90
if (command != null ) {
117
91
command = command .concat (SSH_NETCONF_TERMINATOR );
118
- send (command );
119
- // parse the rpc reply.
120
- parseOkReply (receive ());
92
+ parseOkReply (sendAndReceive (command ));
121
93
} else {
122
94
throw new CloudRuntimeException ("Error generating rpc request for deleting port profile." );
123
95
}
@@ -128,9 +100,7 @@ public void addPolicyMap(String name, int averageRate, int maxRate, int burstRat
128
100
String command = VsmCommand .getAddPolicyMap (name , averageRate , maxRate , burstRate );
129
101
if (command != null ) {
130
102
command = command .concat (SSH_NETCONF_TERMINATOR );
131
- send (command );
132
- // parse the rpc reply.
133
- parseOkReply (receive ());
103
+ parseOkReply (sendAndReceive (command ));
134
104
} else {
135
105
throw new CloudRuntimeException ("Error generating rpc request for adding/updating policy map." );
136
106
}
@@ -140,9 +110,7 @@ public void deletePolicyMap(String name) throws CloudRuntimeException {
140
110
String command = VsmCommand .getDeletePolicyMap (name );
141
111
if (command != null ) {
142
112
command = command .concat (SSH_NETCONF_TERMINATOR );
143
- send (command );
144
- // parse the rpc reply.
145
- parseOkReply (receive ());
113
+ parseOkReply (sendAndReceive (command ));
146
114
} else {
147
115
throw new CloudRuntimeException ("Error generating rpc request for deleting policy map." );
148
116
}
@@ -159,9 +127,7 @@ public void attachServicePolicy(String policyMap, String portProfile)
159
127
String command = VsmCommand .getServicePolicy (policyMap , portProfile , true );
160
128
if (command != null ) {
161
129
command = command .concat (SSH_NETCONF_TERMINATOR );
162
- send (command );
163
- // parse the rpc reply.
164
- parseOkReply (receive ());
130
+ parseOkReply (sendAndReceive (command ));
165
131
} else {
166
132
throw new CloudRuntimeException ("Error generating rpc request for adding policy map." );
167
133
}
@@ -172,9 +138,7 @@ public void detachServicePolicy(String policyMap, String portProfile)
172
138
String command = VsmCommand .getServicePolicy (policyMap , portProfile , false );
173
139
if (command != null ) {
174
140
command = command .concat (SSH_NETCONF_TERMINATOR );
175
- send (command );
176
- // parse the rpc reply.
177
- parseOkReply (receive ());
141
+ parseOkReply (sendAndReceive (command ));
178
142
} else {
179
143
throw new CloudRuntimeException ("Error generating rpc request for removing policy map." );
180
144
}
@@ -184,12 +148,10 @@ public PortProfile getPortProfileByName(String name) throws CloudRuntimeExceptio
184
148
String command = VsmCommand .getPortProfile (name );
185
149
if (command != null ) {
186
150
command = command .concat (SSH_NETCONF_TERMINATOR );
187
- send (command );
188
- // parse the rpc reply.
189
- String received = receive ();
151
+ String received = sendAndReceive (command );
190
152
VsmPortProfileResponse response = new VsmPortProfileResponse (received .trim ());
191
153
if (!response .isResponseOk ()) {
192
- throw new CloudRuntimeException ("Error response while getting the port profile details." );
154
+ throw new CloudRuntimeException (response . toString () );
193
155
} else {
194
156
return response .getPortProfile ();
195
157
}
@@ -202,12 +164,10 @@ public PolicyMap getPolicyMapByName(String name) throws CloudRuntimeException {
202
164
String command = VsmCommand .getPolicyMap (name );
203
165
if (command != null ) {
204
166
command = command .concat (SSH_NETCONF_TERMINATOR );
205
- send (command );
206
- // parse the rpc reply.
207
- String received = receive ();
167
+ String received = sendAndReceive (command );
208
168
VsmPolicyMapResponse response = new VsmPolicyMapResponse (received .trim ());
209
169
if (!response .isResponseOk ()) {
210
- throw new CloudRuntimeException ("Error response while getting the port profile details." );
170
+ throw new CloudRuntimeException (response . toString () );
211
171
} else {
212
172
return response .getPolicyMap ();
213
173
}
@@ -222,6 +182,15 @@ private void exchangeHello() {
222
182
send (hello );
223
183
}
224
184
185
+ private String sendAndReceive (String command ) {
186
+ String received ;
187
+ synchronized (NetconfHelper .class ) {
188
+ send (command );
189
+ received = receive ();
190
+ }
191
+ return received ;
192
+ }
193
+
225
194
private void send (String message ) {
226
195
try {
227
196
OutputStream outputStream = _session .getStdin ();
0 commit comments