Skip to content

Commit 7319a12

Browse files
Refactoring the LibvirtComputingResource
- Adding LibvirtCheckConsoleProxyLoadCommandWrapper, LibvirtConsoleProxyLoadCommandWrapper, LibvirtWatchConsoleProxyLoadCommandWrapperand CitrixConsoleProxyLoadCommandWrapper - 2 unit tests added - KVM hypervisor plugin with 12% coverage Refactored the CommandWrapper interface in order to remove the esecuteProxyLoadScan, which is now implemented bu subclasses.
1 parent 08a9523 commit 7319a12

10 files changed

Lines changed: 303 additions & 137 deletions

File tree

core/src/com/cloud/resource/CommandWrapper.java

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -19,81 +19,16 @@
1919

2020
package com.cloud.resource;
2121

22-
import java.io.BufferedReader;
23-
import java.io.IOException;
24-
import java.io.InputStream;
25-
import java.io.InputStreamReader;
26-
import java.net.URL;
27-
import java.net.URLConnection;
28-
import java.nio.charset.Charset;
29-
30-
import org.apache.log4j.Logger;
31-
3222
import com.cloud.agent.api.Answer;
3323
import com.cloud.agent.api.Command;
34-
import com.cloud.agent.api.proxy.ConsoleProxyLoadAnswer;
3524

3625

3726
public abstract class CommandWrapper<T extends Command, A extends Answer, R extends ServerResource> {
3827

39-
private static final Logger s_logger = Logger.getLogger(CommandWrapper.class);
40-
4128
/**
4229
* @param T is the command to be used.
4330
* @param R is the resource base to be used.
4431
* @return A and the Answer from the command.
4532
*/
4633
public abstract A execute(T command, R serverResource);
47-
48-
/**
49-
* Common method so we added it here.
50-
*
51-
* @param cmd
52-
* @param proxyVmId
53-
* @param proxyVmName
54-
* @param proxyManagementIp
55-
* @param cmdPort
56-
* @return
57-
*/
58-
protected Answer executeProxyLoadScan(final Command cmd, final long proxyVmId, final String proxyVmName, final String proxyManagementIp, final int cmdPort) {
59-
String result = null;
60-
61-
final StringBuffer sb = new StringBuffer();
62-
sb.append("http://").append(proxyManagementIp).append(":" + cmdPort).append("/cmd/getstatus");
63-
64-
boolean success = true;
65-
try {
66-
final URL url = new URL(sb.toString());
67-
final URLConnection conn = url.openConnection();
68-
69-
// setting TIMEOUTs to avoid possible waiting until death situations
70-
conn.setConnectTimeout(5000);
71-
conn.setReadTimeout(5000);
72-
73-
final InputStream is = conn.getInputStream();
74-
final BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.defaultCharset()));
75-
final StringBuilder sb2 = new StringBuilder();
76-
String line = null;
77-
try {
78-
while ((line = reader.readLine()) != null) {
79-
sb2.append(line + "\n");
80-
}
81-
result = sb2.toString();
82-
} catch (final IOException e) {
83-
success = false;
84-
} finally {
85-
try {
86-
is.close();
87-
} catch (final IOException e) {
88-
s_logger.warn("Exception when closing , console proxy address : " + proxyManagementIp);
89-
success = false;
90-
}
91-
}
92-
} catch (final IOException e) {
93-
s_logger.warn("Unable to open console proxy command port url, console proxy address : " + proxyManagementIp);
94-
success = false;
95-
}
96-
97-
return new ConsoleProxyLoadAnswer(cmd, proxyVmId, proxyVmName, success, result);
98-
}
9934
}

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,10 @@
2323
import java.io.FileOutputStream;
2424
import java.io.FileReader;
2525
import java.io.IOException;
26-
import java.io.InputStream;
27-
import java.io.InputStreamReader;
2826
import java.io.Reader;
2927
import java.net.InetAddress;
3028
import java.net.URI;
3129
import java.net.URISyntaxException;
32-
import java.net.URL;
33-
import java.net.URLConnection;
3430
import java.text.DateFormat;
3531
import java.text.MessageFormat;
3632
import java.text.SimpleDateFormat;
@@ -148,9 +144,6 @@
148144
import com.cloud.agent.api.VmStatsEntry;
149145
import com.cloud.agent.api.check.CheckSshAnswer;
150146
import com.cloud.agent.api.check.CheckSshCommand;
151-
import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand;
152-
import com.cloud.agent.api.proxy.ConsoleProxyLoadAnswer;
153-
import com.cloud.agent.api.proxy.WatchConsoleProxyLoadCommand;
154147
import com.cloud.agent.api.routing.IpAssocCommand;
155148
import com.cloud.agent.api.routing.IpAssocVpcCommand;
156149
import com.cloud.agent.api.routing.NetworkElementCommand;
@@ -1307,11 +1300,7 @@ public Answer executeRequest(final Command cmd) {
13071300
}
13081301

13091302
try {
1310-
if (cmd instanceof CheckConsoleProxyLoadCommand) {
1311-
return execute((CheckConsoleProxyLoadCommand)cmd);
1312-
} else if (cmd instanceof WatchConsoleProxyLoadCommand) {
1313-
return execute((WatchConsoleProxyLoadCommand)cmd);
1314-
} else if (cmd instanceof GetVncPortCommand) {
1303+
if (cmd instanceof GetVncPortCommand) {
13151304
return execute((GetVncPortCommand)cmd);
13161305
} else if (cmd instanceof ModifySshKeysCommand) {
13171306
return execute((ModifySshKeysCommand)cmd);
@@ -2886,56 +2875,10 @@ protected GetVncPortAnswer execute(final GetVncPortCommand cmd) {
28862875
}
28872876
}
28882877

2889-
protected Answer execute(final CheckConsoleProxyLoadCommand cmd) {
2890-
return executeProxyLoadScan(cmd, cmd.getProxyVmId(), cmd.getProxyVmName(), cmd.getProxyManagementIp(), cmd.getProxyCmdPort());
2891-
}
2892-
2893-
protected Answer execute(final WatchConsoleProxyLoadCommand cmd) {
2894-
return executeProxyLoadScan(cmd, cmd.getProxyVmId(), cmd.getProxyVmName(), cmd.getProxyManagementIp(), cmd.getProxyCmdPort());
2895-
}
2896-
28972878
protected MaintainAnswer execute(final MaintainCommand cmd) {
28982879
return new MaintainAnswer(cmd);
28992880
}
29002881

2901-
private Answer executeProxyLoadScan(final Command cmd, final long proxyVmId, final String proxyVmName, final String proxyManagementIp, final int cmdPort) {
2902-
String result = null;
2903-
2904-
final StringBuffer sb = new StringBuffer();
2905-
sb.append("http://").append(proxyManagementIp).append(":" + cmdPort).append("/cmd/getstatus");
2906-
2907-
boolean success = true;
2908-
try {
2909-
final URL url = new URL(sb.toString());
2910-
final URLConnection conn = url.openConnection();
2911-
2912-
final InputStream is = conn.getInputStream();
2913-
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
2914-
final StringBuilder sb2 = new StringBuilder();
2915-
String line = null;
2916-
try {
2917-
while ((line = reader.readLine()) != null) {
2918-
sb2.append(line + "\n");
2919-
}
2920-
result = sb2.toString();
2921-
} catch (final IOException e) {
2922-
success = false;
2923-
} finally {
2924-
try {
2925-
is.close();
2926-
} catch (final IOException e) {
2927-
s_logger.warn("Exception when closing , console proxy address : " + proxyManagementIp);
2928-
success = false;
2929-
}
2930-
}
2931-
} catch (final IOException e) {
2932-
s_logger.warn("Unable to open console proxy command port url, console proxy address : " + proxyManagementIp);
2933-
success = false;
2934-
}
2935-
2936-
return new ConsoleProxyLoadAnswer(cmd, proxyVmId, proxyVmName, success, result);
2937-
}
2938-
29392882
protected PowerState convertToPowerState(final DomainState ps) {
29402883
final PowerState state = s_powerStatesTable.get(ps);
29412884
return state == null ? PowerState.PowerUnknown : state;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.hypervisor.kvm.resource.wrapper;
21+
22+
import com.cloud.agent.api.Answer;
23+
import com.cloud.agent.api.Command;
24+
import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand;
25+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
26+
import com.cloud.resource.ServerResource;
27+
28+
public class LibvirtCheckConsoleProxyLoadCommandWrapper extends LibvirtConsoleProxyLoadCommandWrapper<CheckConsoleProxyLoadCommand, Answer, LibvirtComputingResource> {
29+
30+
@Override
31+
public Answer execute(final Command command, final ServerResource serverResource) {
32+
final CheckConsoleProxyLoadCommand cmd = (CheckConsoleProxyLoadCommand) command;
33+
34+
final long proxyVmId = cmd.getProxyVmId();
35+
final String proxyVmName = cmd.getProxyVmName();
36+
final String proxyManagementIp = cmd.getProxyManagementIp();
37+
final int proxyCmdPort = cmd.getProxyCmdPort();
38+
39+
return executeProxyLoadScan(cmd, proxyVmId, proxyVmName, proxyManagementIp, proxyCmdPort);
40+
}
41+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.hypervisor.kvm.resource.wrapper;
21+
22+
import java.io.BufferedReader;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.io.InputStreamReader;
26+
import java.net.URL;
27+
import java.net.URLConnection;
28+
29+
import org.apache.log4j.Logger;
30+
31+
import com.cloud.agent.api.Answer;
32+
import com.cloud.agent.api.Command;
33+
import com.cloud.agent.api.proxy.ConsoleProxyLoadAnswer;
34+
import com.cloud.resource.CommandWrapper;
35+
import com.cloud.resource.ServerResource;
36+
37+
public abstract class LibvirtConsoleProxyLoadCommandWrapper<T extends Command, A extends Answer, R extends ServerResource> extends CommandWrapper<Command, Answer, ServerResource> {
38+
39+
private static final Logger s_logger = Logger.getLogger(LibvirtConsoleProxyLoadCommandWrapper.class);
40+
41+
public Answer executeProxyLoadScan(final Command cmd, final long proxyVmId, final String proxyVmName, final String proxyManagementIp, final int cmdPort) {
42+
String result = null;
43+
44+
final StringBuffer sb = new StringBuffer();
45+
sb.append("http://").append(proxyManagementIp).append(":" + cmdPort).append("/cmd/getstatus");
46+
47+
boolean success = true;
48+
try {
49+
final URL url = new URL(sb.toString());
50+
final URLConnection conn = url.openConnection();
51+
52+
final InputStream is = conn.getInputStream();
53+
final BufferedReader reader = new BufferedReader(new InputStreamReader(is));
54+
final StringBuilder sb2 = new StringBuilder();
55+
String line = null;
56+
try {
57+
while ((line = reader.readLine()) != null) {
58+
sb2.append(line + "\n");
59+
}
60+
result = sb2.toString();
61+
} catch (final IOException e) {
62+
success = false;
63+
} finally {
64+
try {
65+
is.close();
66+
} catch (final IOException e) {
67+
s_logger.warn("Exception when closing , console proxy address : " + proxyManagementIp);
68+
success = false;
69+
}
70+
}
71+
} catch (final IOException e) {
72+
s_logger.warn("Unable to open console proxy command port url, console proxy address : " + proxyManagementIp);
73+
success = false;
74+
}
75+
76+
return new ConsoleProxyLoadAnswer(cmd, proxyVmId, proxyVmName, success, result);
77+
}
78+
}

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRequestWrapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import com.cloud.agent.api.RebootCommand;
3737
import com.cloud.agent.api.RebootRouterCommand;
3838
import com.cloud.agent.api.StopCommand;
39+
import com.cloud.agent.api.proxy.CheckConsoleProxyLoadCommand;
40+
import com.cloud.agent.api.proxy.WatchConsoleProxyLoadCommand;
3941
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
4042
import com.cloud.resource.CommandWrapper;
4143
import com.cloud.resource.RequestWrapper;
@@ -72,6 +74,8 @@ private void init() {
7274
linbvirtCommands.put(ReadyCommand.class, new LibvirtReadyCommandWrapper());
7375
linbvirtCommands.put(AttachIsoCommand.class, new LibvirtAttachIsoCommandWrapper());
7476
linbvirtCommands.put(AttachVolumeCommand.class, new LibvirtAttachVolumeCommandWrapper());
77+
linbvirtCommands.put(WatchConsoleProxyLoadCommand.class, new LibvirtWatchConsoleProxyLoadCommandWrapper());
78+
linbvirtCommands.put(CheckConsoleProxyLoadCommand.class, new LibvirtCheckConsoleProxyLoadCommandWrapper());
7579

7680
resources.put(LibvirtComputingResource.class, linbvirtCommands);
7781
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.hypervisor.kvm.resource.wrapper;
21+
22+
import com.cloud.agent.api.Answer;
23+
import com.cloud.agent.api.Command;
24+
import com.cloud.agent.api.proxy.WatchConsoleProxyLoadCommand;
25+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
26+
import com.cloud.resource.ServerResource;
27+
28+
public class LibvirtWatchConsoleProxyLoadCommandWrapper extends LibvirtConsoleProxyLoadCommandWrapper<WatchConsoleProxyLoadCommand, Answer, LibvirtComputingResource> {
29+
30+
@Override
31+
public Answer execute(final Command command, final ServerResource serverResource) {
32+
final WatchConsoleProxyLoadCommand cmd = (WatchConsoleProxyLoadCommand) command;
33+
34+
final long proxyVmId = cmd.getProxyVmId();
35+
final String proxyVmName = cmd.getProxyVmName();
36+
final String proxyManagementIp = cmd.getProxyManagementIp();
37+
final int proxyCmdPort = cmd.getProxyCmdPort();
38+
39+
return executeProxyLoadScan(cmd, proxyVmId, proxyVmName, proxyManagementIp, proxyCmdPort);
40+
}
41+
}

0 commit comments

Comments
 (0)