/*******************************************************************************
 *
 *  Copyright FUJITSU LIMITED 2016
 *
 *  Creation Date: 2016-05-24
 *
 *******************************************************************************/

package org.oscm.app.vmware.business;

import org.oscm.app.v1_0.exceptions.APPlatformException;

import com.vmware.vim25.GuestInfo;
import com.vmware.vim25.GuestNicInfo;

/**
 * Custom generation of access information output.
 */
public class VMwareAccessInfo {

    private static final String PATTERN_IP = "${IP}";
    private static final String PATTERN_HOST = "${HOST}";
    private static final String PATTERN_CPU = "${CPU}";
    private static final String PATTERN_MEM = "${MEM}";
    private static final String PATTERN_DISKS = "${DISK}";
    private static final String PATTERN_RESPUSER = "${RESPUSER}";

    private VMPropertyHandler paramHandler;

    public VMwareAccessInfo(VMPropertyHandler paramHandler) {
        this.paramHandler = paramHandler;
    }

    /**
     * Returns the generated access info for the given VM.
     */
    public String generateAccessInfo(GuestInfo guestInfo)
            throws APPlatformException {

        String myHOST = guestInfo.getHostName();
        String hostName;
        if (myHOST != null) {
            hostName = guestInfo.getHostName().split("\\.", 2)[0];
        } else {
            hostName = "Unkown hostname (probably missing vmware tools).\nInstance name "
                    + paramHandler.getInstanceName() + ".";
            myHOST = "Unkown(InstanceName " + paramHandler.getInstanceName()
                    + ")";
        }

        String accessInfoPattern = paramHandler.getAccessInfo();
        if (accessInfoPatternUndefined(accessInfoPattern)) {
            return hostName;
        }

        String accessInfo = accessInfoPattern.replace(PATTERN_IP,
                getIpAddress(guestInfo));
        accessInfo = accessInfo.replace(PATTERN_HOST, myHOST);
        accessInfo = accessInfo.replace(PATTERN_CPU,
                Integer.toString(paramHandler.getConfigCPUs()));
        accessInfo = accessInfo.replace(PATTERN_MEM,
                paramHandler.formatMBasGB(paramHandler.getConfigMemoryMB()));
        accessInfo = accessInfo.replace(PATTERN_DISKS,
                paramHandler.getDataDisksAsString());
        accessInfo = accessInfo.replace(PATTERN_RESPUSER, getResponsibleUser());
        accessInfo = accessInfo.replace("<br>", "<br>\r\n");
        return accessInfo;
    }

    private boolean accessInfoPatternUndefined(String accessInfoPattern) {
        return accessInfoPattern == null
                || accessInfoPattern.trim().length() == 0;
    }

    private String getIpAddress(GuestInfo guestInfo) {
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= paramHandler.getNumberOfNetworkAdapter(); i++) {
            GuestNicInfo info = getNicInfo(guestInfo,
                    paramHandler.getNetworkAdapter(i));
            if (info != null) {
                sb.append(paramHandler.getNetworkAdapter(i) + ": ");
                sb.append(info.getIpAddress());
                if (i < paramHandler.getNumberOfNetworkAdapter()) {
                    sb.append(", ");
                }
            }
        }
        return sb.toString();
    }

    GuestNicInfo getNicInfo(GuestInfo guestInfo, String adapter) {
        for (GuestNicInfo info : guestInfo.getNet()) {
            if (info != null && adapter.equals(info.getNetwork())) {
                return info;
            }
        }
        return null;
    }

    private String getResponsibleUser() {
        String respuser = paramHandler
                .getResponsibleUserAsString(paramHandler.getLocale());
        if (respuser == null) {
            respuser = "";
        }
        return respuser;
    }

}

@@ -2915,16 +2915,20 @@
 estInfo,
+ i);
 %0A       
@@ -2928,32 +2928,64 @@
             
-        
+if (info != null) %7B%0A                if (
 paramHandler
@@ -3005,18 +3005,24 @@
 apter(i)
-);
+ != null
 %0A       
@@ -3026,36 +3026,90 @@
         
-if (info != null) %7B%0A
+            && !paramHandler.getNetworkAdapter(i).trim().isEmpty()) %7B%0A    
         
@@ -3165,24 +3165,42 @@
 i) + %22: %22);%0A
+                %7D%0A
             
@@ -3472,22 +3472,13 @@
 fo, 
-String adapter
+int i
 ) %7B%0A
@@ -3564,27 +3564,178 @@
 null
- && a
+) %7B%0A                if (paramHandler.isAdapterConfiguredByDhcp(i)) %7B%0A                    return info;%0A                %7D else if (paramHandler.getNetworkA
 dapter
+(i)
 .equals(
 info
@@ -3730,16 +3730,41 @@
 .equals(
+%0A                        
 info.get
@@ -3785,32 +3785,36 @@
                 
+    
 return info;%0A   
@@ -3802,32 +3802,36 @@
    return info;%0A
+    
             %7D%0A  
@@ -3820,32 +3820,46 @@
               %7D%0A
+            %7D%0A
         %7D%0A      
@@ -3960,33 +3960,16 @@
 mHandler
-%0A                
 .getResp
@@ -3996,24 +3996,41 @@
 paramHandler
+%0A                
 .getLocale()

Check for DHCP network settings for IP address - Suzana