View Javadoc

1   /*
2    * JBoss, Home of Professional Open Source.
3    * Copyright 2013, Red Hat Middleware LLC, and individual contributors
4    * as indicated by the @author tags. See the copyright.txt file in the
5    * distribution for a full listing of individual contributors.
6    *
7    * This is free software; you can redistribute it and/or modify it
8    * under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */
22  
23  package org.redhat.vmtruckloader.util;
24  
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import com.vmware.vim25.VirtualDevice;
33  import com.vmware.vim25.VirtualEthernetCard;
34  import com.vmware.vim25.VirtualMachineConfigInfo;
35  import com.vmware.vim25.mo.VirtualMachine;
36  
37  /**
38   * 
39   * @author Romain Pelisse - romain@redhat.com
40   *
41   */
42  public class VirtualMachineUtils {
43  
44      private static final Logger LOGGER = LoggerFactory.getLogger(VirtualMachineUtils.class);
45      
46  	public static String getMacAddress(VirtualMachine vm) {
47  		if ( vm == null )
48  			throw new IllegalArgumentException("Provided instance of " + VirtualMachine.class.getName() + " was 'null'.");
49  		
50          final List<String> macAddresses = getMacAddresses(vm);
51          if (macAddresses.isEmpty() )
52              throw new IllegalStateException("Machine " + vm.getName() + " has no @MAC address !");
53          return macAddresses.get(0);
54      }
55  
56  	public static List<String> getMacAddresses(VirtualMachine vm) {
57  
58          final VirtualMachineConfigInfo vmConfigInfo = vm.getConfig();
59          if ( vmConfigInfo == null ) throw new IllegalStateException("No ConfigInfo for VM:" + vm.getName());
60          if ( vmConfigInfo.getHardware() == null ) throw new IllegalStateException("No Hardware for VM:" + vm.getName());
61          if ( vmConfigInfo.getHardware().getDevice() == null ) throw new IllegalStateException("No Device for VM:" + vm.getName());
62  
63          final VirtualDevice[] vdev= vmConfigInfo.getHardware().getDevice();
64          if ( vdev.length == 0 )
65              return Collections.emptyList();
66          else {
67              final List<String> macAddresses = new ArrayList<String>(vdev.length);
68              for(int idDevice=0; idDevice < vdev.length; idDevice++) {
69                  if((vdev[idDevice] instanceof VirtualEthernetCard)) {
70                      final String macAddress = ((VirtualEthernetCard) vdev[idDevice]).macAddress;
71                      macAddresses.add(macAddress);
72                      if ( LOGGER.isDebugEnabled() )
73                          LOGGER.debug("@MAC:" + macAddress);
74                  }
75              }
76              return macAddresses;
77          }
78      }
79  }