Search in sources :

Example 1 with CustomProperties

use of com.vmware.photon.controller.model.adapters.vsphere.CustomProperties in project photon-model by vmware.

the class OvfParser method parse.

/**
 * Produces several descriptions based on the different hardware configuration defined in the
 * OVF descriptor.
 *
 * @param doc
 *            OVF descriptor to parse
 * @param template
 *            use as a basis of the ComputeDescription.
 * @return
 */
public List<ComputeDescription> parse(Document doc, ComputeDescription template) {
    CustomProperties cust = CustomProperties.of(template);
    NodeList props = nodes(doc, "/ovf:Envelope/ovf:VirtualSystem/ovf:ProductSection/ovf:Property");
    for (Element prop : iterableElements(props)) {
        String userConfigurable = attr("ovf:userConfigurable", prop);
        if (!"true".equals(userConfigurable)) {
            continue;
        }
        String key = attr("ovf:key", prop);
        Element section = (Element) prop.getParentNode();
        String instanceId = attr("ovf:instance", section);
        String classId = attr("ovf:class", section);
        String description = text(prop, "ovf:Description/text()");
        cust.put(property(classId, key, instanceId), description);
    }
    if (template.name == null) {
        String productName = text(doc, "/ovf:Envelope/ovf:VirtualSystem/ovf:ProductSection/ovf:Product/text()");
        String productVersion = text(doc, "/ovf:Envelope/ovf:VirtualSystem/ovf:ProductSection/ovf:Version/text()");
        template.name = productName + " " + productVersion;
    }
    NodeList hwItems = nodes(doc, "/ovf:Envelope/ovf:VirtualSystem/ovf:VirtualHardwareSection/ovf:Item");
    Map<String, ComputeDescription> hwByConfigName = new HashMap<>();
    for (Element item : iterableElements(hwItems)) {
        String configName = attr("ovf:configuration", item);
        ComputeDescription desc = hwByConfigName.get(configName);
        if (desc == null) {
            desc = Utils.clone(template);
            desc.documentSelfLink = UUID.randomUUID().toString();
            desc.id = "ovf-imported-" + desc.documentSelfLink;
            desc.customProperties.put(PROP_OVF_CONFIGURATION, configName);
            hwByConfigName.put(configName, desc);
        }
        String resourceType = text(item, "rasd:ResourceType/text()");
        if (RESOURCE_TYPE_CPU.equals(resourceType)) {
            long qty = Long.parseLong(text(item, "rasd:VirtualQuantity/text()"));
            desc.cpuCount = qty;
        }
        if (RESOURCE_TYPE_MEMORY.equals(resourceType)) {
            double qty = Double.parseDouble(text(item, "rasd:VirtualQuantity/text()"));
            long mult = memAllocationUnit2Multiplier(text(item, "rasd:AllocationUnits/text()"));
            desc.totalMemoryBytes = (long) (qty * mult);
        }
    }
    for (Iterator<ComputeDescription> it = hwByConfigName.values().iterator(); it.hasNext(); ) {
        ComputeDescription desc = it.next();
        if (desc.cpuCount <= 0) {
            it.remove();
        }
    }
    return new ArrayList<>(hwByConfigName.values());
}
Also used : ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) CustomProperties(com.vmware.photon.controller.model.adapters.vsphere.CustomProperties)

Example 2 with CustomProperties

use of com.vmware.photon.controller.model.adapters.vsphere.CustomProperties in project photon-model by vmware.

the class NetworkDeviceBackingFactory method getDistributedPortBackingInfo.

/**
 * Backing info for distributed virtual switch port or portgroup
 */
private static VirtualEthernetCardDistributedVirtualPortBackingInfo getDistributedPortBackingInfo(CustomProperties props, QueryConfigTargetRequest queryConfigTargetRequest) {
    DistributedVirtualSwitchPortConnection port = new DistributedVirtualSwitchPortConnection();
    String portGroupKey = props.getString(DvsProperties.PORT_GROUP_KEY);
    String dvsUuid = props.getString(DvsProperties.DVS_UUID);
    if (StringUtil.isNullOrEmpty(dvsUuid)) {
        // NSX-V sets the value to a list of dvPortGroupsKeys as the logical switch is
        // created in a transport zone which could be associated with multiple clusters.
        // Hence dvPortGroup is created per cluster. The configTarget will filter based on
        // the cluster where machine is being provisioned.
        Type listType = new TypeToken<ArrayList<String>>() {
        }.getType();
        final List<String> portGroupIds = Utils.fromJson(portGroupKey, listType);
        // NSX-V doesn't have UUID information in its API response
        DistributedVirtualPortgroupInfo info = null;
        try {
            ConfigTarget configTarget = queryConfigTargetRequest.getConfigTarget();
            info = configTarget.getDistributedVirtualPortgroup().stream().filter(d -> {
                return portGroupIds.contains(d.getPortgroupKey());
            }).findFirst().orElse(null);
        } catch (Exception e) {
            logger.error("getDistributedPortBackingInfo::Failed to get dvportgroup info.", e);
        }
        if (info == null) {
            throw new IllegalArgumentException("getDistributedPortBackingInfo::The port group " + "information is not found for key: " + portGroupKey);
        }
        portGroupKey = info.getPortgroupKey();
        dvsUuid = info.getSwitchUuid();
    }
    port.setPortgroupKey(portGroupKey);
    port.setSwitchUuid(dvsUuid);
    VirtualEthernetCardDistributedVirtualPortBackingInfo backing = new VirtualEthernetCardDistributedVirtualPortBackingInfo();
    backing.setPort(port);
    return backing;
}
Also used : ResourceState(com.vmware.photon.controller.model.resources.ResourceState) TypeToken(com.google.gson.reflect.TypeToken) Logger(org.slf4j.Logger) DistributedVirtualPortgroupInfo(com.vmware.vim25.DistributedVirtualPortgroupInfo) VirtualEthernetCardOpaqueNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo) LoggerFactory(org.slf4j.LoggerFactory) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) StringUtil(io.netty.util.internal.StringUtil) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) ArrayList(java.util.ArrayList) List(java.util.List) Utils(com.vmware.xenon.common.Utils) CustomProperties(com.vmware.photon.controller.model.adapters.vsphere.CustomProperties) Type(java.lang.reflect.Type) ConfigTarget(com.vmware.vim25.ConfigTarget) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) QueryConfigTargetRequest(com.vmware.photon.controller.model.adapters.vsphere.QueryConfigTargetRequest) DistributedVirtualSwitchPortConnection(com.vmware.vim25.DistributedVirtualSwitchPortConnection) Type(java.lang.reflect.Type) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) ArrayList(java.util.ArrayList) ConfigTarget(com.vmware.vim25.ConfigTarget) DistributedVirtualSwitchPortConnection(com.vmware.vim25.DistributedVirtualSwitchPortConnection) DistributedVirtualPortgroupInfo(com.vmware.vim25.DistributedVirtualPortgroupInfo)

Aggregations

CustomProperties (com.vmware.photon.controller.model.adapters.vsphere.CustomProperties)2 ArrayList (java.util.ArrayList)2 TypeToken (com.google.gson.reflect.TypeToken)1 QueryConfigTargetRequest (com.vmware.photon.controller.model.adapters.vsphere.QueryConfigTargetRequest)1 VimNames (com.vmware.photon.controller.model.adapters.vsphere.util.VimNames)1 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)1 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)1 ResourceState (com.vmware.photon.controller.model.resources.ResourceState)1 ConfigTarget (com.vmware.vim25.ConfigTarget)1 DistributedVirtualPortgroupInfo (com.vmware.vim25.DistributedVirtualPortgroupInfo)1 DistributedVirtualSwitchPortConnection (com.vmware.vim25.DistributedVirtualSwitchPortConnection)1 VirtualDeviceBackingInfo (com.vmware.vim25.VirtualDeviceBackingInfo)1 VirtualEthernetCardDistributedVirtualPortBackingInfo (com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo)1 VirtualEthernetCardNetworkBackingInfo (com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo)1 VirtualEthernetCardOpaqueNetworkBackingInfo (com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo)1 Utils (com.vmware.xenon.common.Utils)1 StringUtil (io.netty.util.internal.StringUtil)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 List (java.util.List)1