From 05df0a1965ef4fc9f8646fa3bd4fa47d63e31f4f Mon Sep 17 00:00:00 2001 From: Edison Su Date: Wed, 1 Aug 2012 13:59:11 -0700 Subject: [PATCH] Another clean up for unnecessary parameter conn Clean up unused parameter conn. Add test for LibvirtVMDef Add support for ethernet type RB: https://reviews.apache.org/r/6284/ Send-by: tomoe@midokura.com --- .../resource/LibvirtComputingResource.java | 26 ++++++------- .../hypervisor/kvm/resource/LibvirtVMDef.java | 7 ++++ .../kvm/resource/LibvirtVMDefTest.java | 38 +++++++++++++++++++ 3 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtVMDefTest.java diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index ea12b986b0a..73101a9896c 100755 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -698,7 +698,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements } try { - createControlNetwork(conn); + createControlNetwork(); } catch (LibvirtException e) { throw new ConfigurationException(e.getMessage()); } @@ -1220,7 +1220,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements nicTO.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vlanId)); } - InterfaceDef nic = createVif(conn, nicTO, InterfaceDef.nicModel.VIRTIO); + InterfaceDef nic = createVif(nicTO, InterfaceDef.nicModel.VIRTIO); Domain vm = getDomain(conn, vmName); vm.attachDevice(nic.toString()); } @@ -2122,7 +2122,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements } } else if (nic.getType() == TrafficType.Control) { /* Make sure the network is still there */ - createControlNetwork(conn); + createControlNetwork(); } else if (nic.getType() == TrafficType.Public) { if (nic.getBroadcastType() == BroadcastDomainType.Vlan && !vlanId.equalsIgnoreCase("untagged")) { @@ -2516,13 +2516,13 @@ public class LibvirtComputingResource extends ServerResourceBase implements return vm; } - protected void createVifs(Connect conn, VirtualMachineTO vmSpec, - LibvirtVMDef vm) throws InternalErrorException, LibvirtException { + protected void createVifs(VirtualMachineTO vmSpec, + LibvirtVMDef vm) throws InternalErrorException, LibvirtException { NicTO[] nics = vmSpec.getNics(); for (int i = 0; i < nics.length; i++) { for (NicTO nic : vmSpec.getNics()) { if (nic.getDeviceId() == i) { - createVif(conn, vm, nic); + createVif(vm, nic); } } } @@ -2546,7 +2546,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements createVbd(conn, vmSpec, vmName, vm); - createVifs(conn, vmSpec, vm); + createVifs(vmSpec, vm); s_logger.debug("starting " + vmName + ": " + vm.toString()); startDomain(conn, vmName, vm.toString()); @@ -2734,8 +2734,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements return brName; } - private InterfaceDef createVif(Connect conn, NicTO nic, - InterfaceDef.nicModel model) throws InternalErrorException, + private InterfaceDef createVif(NicTO nic, + InterfaceDef.nicModel model) throws InternalErrorException, LibvirtException { InterfaceDef intf = new InterfaceDef(); @@ -2755,7 +2755,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements } } else if (nic.getType() == TrafficType.Control) { /* Make sure the network is still there */ - createControlNetwork(conn); + createControlNetwork(); intf.defBridgeNet(_linkLocalBridgeName, null, nic.getMac(), model); } else if (nic.getType() == TrafficType.Public) { if (nic.getBroadcastType() == BroadcastDomainType.Vlan @@ -2776,10 +2776,10 @@ public class LibvirtComputingResource extends ServerResourceBase implements return intf; } - private void createVif(Connect conn, LibvirtVMDef vm, NicTO nic) + private void createVif(LibvirtVMDef vm, NicTO nic) throws InternalErrorException, LibvirtException { vm.getDevices().addDevice( - createVif(conn, nic, getGuestNicModel(vm.getGuestOSType()))); + createVif(nic, getGuestNicModel(vm.getGuestOSType()))); } protected CheckSshAnswer execute(CheckSshCommand cmd) { @@ -4098,7 +4098,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements return new Pair(rx, tx); } - private void createControlNetwork(Connect conn) throws LibvirtException { + private void createControlNetwork() throws LibvirtException { _virtRouterResource.createControlNetwork(_linkLocalBridgeName); } diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtVMDef.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtVMDef.java index e1cfa548948..457d9ac6c64 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtVMDef.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtVMDef.java @@ -665,6 +665,13 @@ public class LibvirtVMDef { _model = model; } + public void defEthernet(String targetName, String macAddr, nicModel model) { + _netType = guestNetType.ETHERNET; + _networkName = targetName; + _macAddr = macAddr; + _model = model; + } + public void setHostNetType(hostNicType hostNetType) { _hostNetType = hostNetType; } diff --git a/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtVMDefTest.java b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtVMDefTest.java new file mode 100644 index 00000000000..b723875d4bb --- /dev/null +++ b/plugins/hypervisors/kvm/test/com/cloud/hypervisor/kvm/resource/LibvirtVMDefTest.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.cloud.hypervisor.kvm.resource; + +import junit.framework.TestCase; + +public class LibvirtVMDefTest extends TestCase { + + public void testInterfaceEtehrnet() { + LibvirtVMDef.InterfaceDef ifDef = new LibvirtVMDef.InterfaceDef(); + ifDef.defEthernet("targetDeviceName", "00:11:22:aa:bb:dd", LibvirtVMDef.InterfaceDef.nicModel.VIRTIO); + + String expected = "\n" + + "\n" + + "\n" + + "\n" + + "\n"; + + assertEquals(expected, ifDef.toString()); + } +}