Package com.abiquo.am.model

Examples of com.abiquo.am.model.TemplateDto


    // TODO String userID, String category
        throws AMException
    {
        envelope = checkEnvelopeIsValid(envelope);

        TemplateDto diskInfo = null;

        Map<String, VirtualDiskDescType> diskIdToDiskFormat =
            new HashMap<String, VirtualDiskDescType>();
        Map<String, FileType> fileIdToFileType = new HashMap<String, FileType>();
        Map<String, List<String>> diskIdToVSs = new HashMap<String, List<String>>();
        Map<String, TemplateDto> requiredByVSs = new HashMap<String, TemplateDto>();
        DiskSectionType diskSectionType;

        try
        {
            ContentType contentType = OVFEnvelopeUtils.getTopLevelVirtualSystemContent(envelope);

            ProductSectionType product =
                OVFEnvelopeUtils.getSection(contentType, ProductSectionType.class);
            OperatingSystemSectionType ossection = null;
            try
            {
                ossection =
                    OVFEnvelopeUtils.getSection(contentType, OperatingSystemSectionType.class);
            }
            catch (Exception e) // no such section
            {

            }

            String description = null;
            if (product.getInfo() != null && product.getInfo().getValue() != null)
            {
                description = product.getInfo().getValue();
            }

            String categoryName = null;
            if (product.getOtherAttributes().containsKey(new QName("CategoryName")))
            {
                categoryName = product.getOtherAttributes().get(new QName("CategoryName"));
            }

            String iconPath = getIconPath(product, fileIdToFileType, ovfId);

            diskSectionType = OVFEnvelopeUtils.getSection(envelope, DiskSectionType.class);

            for (FileType fileType : envelope.getReferences().getFile())
            {
                fileIdToFileType.put(fileType.getId(), fileType);
            }
            // Create a hash
            for (VirtualDiskDescType virtualDiskDescType : diskSectionType.getDisk())
            {
                diskIdToDiskFormat.put(virtualDiskDescType.getDiskId(), virtualDiskDescType);
            }

            if (contentType instanceof VirtualSystemType)
            {

                VirtualSystemType vs = (VirtualSystemType) contentType;
                TemplateDto req = getDiskInfo(vs, diskIdToDiskFormat, diskIdToVSs);

                requiredByVSs.put(vs.getId(), req);
            }
            else if (contentType instanceof VirtualSystemCollectionType)
            {
                List<VirtualSystemType> virtualSystems =
                    OVFEnvelopeUtils.getVirtualSystems((VirtualSystemCollectionType) contentType);

                for (VirtualSystemType virtualSystemType : virtualSystems)
                {
                    TemplateDto req =
                        getDiskInfo(virtualSystemType, diskIdToDiskFormat, diskIdToVSs);

                    requiredByVSs.put(virtualSystemType.getId(), req);
                }
            }

            for (VirtualDiskDescType diskDescType : diskIdToDiskFormat.values())
            {

                String diskId = diskDescType.getDiskId();
                String fileId = diskDescType.getFileRef();

                if (!fileIdToFileType.containsKey(fileId))
                {
                    String msg = "File Id [" + fileId + "] not found on the ReferencesSection";
                    throw new IdNotFoundException(msg);
                }

                FileType file = fileIdToFileType.get(fileId);
                final String filePath = file.getHref();
                final Long fileSize = file.getSize().longValue();

                String format = diskDescType.getFormat(); // XXX using the same format on the OVF
                // disk

                if (!diskIdToVSs.containsKey(diskId))
                {
                    throw new IdNotFoundException("Any virtualSystem is using diskId[" + diskId
                        + "]"); // XXX warning ??
                }

                if (diskIdToVSs.size() != 1)
                {
                    throw new AMException(AMError.TEMPLATE_INVALID, String.format(
                        "There are more than one virtual system on the OVF Envelope [%s]", ovfId));
                }

                for (String vssName : diskIdToVSs.get(diskId))
                {
                    diskInfo = new TemplateDto();
                    diskInfo.setName(vssName);
                    diskInfo.setUrl(ovfId);
                    diskInfo.setLoginUser(getProductPropertyValue(product, "user"));
                    diskInfo.setLoginPassword(getProductPropertyValue(product, "password"));
                    diskInfo.setOsType(getOSType(ossection));
                    diskInfo.setOsVersion(getOSVersion(ossection));

                    DiskFormat diskFormat = DiskFormat.fromValue(format);
                    DiskFormatType ovfDiskFormat = DiskFormatType.valueOf(diskFormat.name());

                    diskInfo.setDiskFileFormat(ovfDiskFormat);
                    diskInfo.setDiskFileSize(fileSize);

                    // Note that getHRef() will now return the relative path
                    // of the file at the downloaded repository space

                    if (filePath.startsWith("http:"))
                    {
                        diskInfo.setDiskFilePath(FilenameUtils.getName(filePath));
                    }
                    else
                    {
                        diskInfo.setDiskFilePath(filePath);
                    }

                    diskInfo.setIconPath(iconPath);
                    diskInfo.setDescription(description);
                    diskInfo.setCategoryName(categoryName);
                    // XXX diskInfo.setSO(value);

                    if (!requiredByVSs.containsKey(vssName))
                    {
                        throw new IdNotFoundException("VirtualSystem id not found [" + vssName
                            + "]");
                    }

                    TemplateDto requirement = requiredByVSs.get(vssName);

                    // XXX disk format ::: diskInfo.setImageType(requirement.getImageType());

                    diskInfo.setCpu(requirement.getCpu());
                    diskInfo.setRam(requirement.getRam());
                    diskInfo.setHd(requirement.getHd());

                    diskInfo.setRamSizeUnit(requirement.getRamSizeUnit());
                    diskInfo.setHdSizeUnit(requirement.getHdSizeUnit());

                    diskInfo.setEthernetDriverType(requirement.getEthernetDriverType());
                    diskInfo.setDiskControllerType(requirement.getDiskControllerType());

                    // TODO diskInfo.setEnterpriseId(enterpriseId);
                    // diskInfo.setUserId(userId); TODO user ID
                    // diskInfo.getCategories().add(category); TODO category
View Full Code Here


    private static TemplateDto getDiskInfo(final VirtualSystemType vsystem,
        final Map<String, VirtualDiskDescType> diskDescByName,
        final Map<String, List<String>> diskIdToVSs) throws IdAlreadyExistsException,
        RequiredAttributeException, SectionNotPresentException
    {
        TemplateDto dReq = new TemplateDto();
        VirtualHardwareSectionType hardwareSectionType;

        try
        {
            hardwareSectionType =
                OVFEnvelopeUtils.getSection(vsystem, VirtualHardwareSectionType.class);
        }
        catch (InvalidSectionException e)
        {
            throw new SectionNotPresentException("VirtualHardware on a virtualSystem", e);
        }

        dReq.setCpu(-1);
        dReq.setHd(Long.valueOf(-1));
        dReq.setRam(Long.valueOf(-1));

        // XXX now we are using ONLY the Disk format

        // XXX String vsType = hardwareSectionType.getSystem().getVirtualSystemType().getValue();
        // XXX dReq.setImageType(vsType);
        // XXX log.debug("Using ''virtualSystemType'' [{}]", vsType);

        for (RASDType rasdType : hardwareSectionType.getItem())
        {
            ResourceType resourceType = rasdType.getResourceType();
            int resTnumeric = Integer.parseInt(resourceType.getValue());

            // TODO use CIMResourceTypeEnum from value and then a SWITCH

            // Get the information on the ram
            if (CIMResourceTypeEnum.Processor.getNumericResourceType() == resTnumeric)
            {
                String cpuVal = rasdType.getVirtualQuantity().getValue().toString();

                dReq.setCpu(Integer.parseInt(cpuVal));

                // TODO if(rasdType.getAllocationUnits()!= null)
            }
            else if (CIMResourceTypeEnum.Memory.getNumericResourceType() == resTnumeric)
            {
                BigInteger ramVal = rasdType.getVirtualQuantity().getValue();

                dReq.setRam(ramVal.longValue());

                if (rasdType.getAllocationUnits() != null
                    & rasdType.getAllocationUnits().getValue() != null)
                {
                    final String allocationUnits = rasdType.getAllocationUnits().getValue();

                    final MemorySizeUnit ramSizeUnit = getMemoryUnitsFromOVF(allocationUnits);

                    dReq.setRamSizeUnit(ramSizeUnit);
                }
            }
            else if (CIMResourceTypeEnum.Disk_Drive.getNumericResourceType() == resTnumeric)
            {
                // HD requirements are extracted from the associated Disk on ''hostResource''
                String diskId = getVirtualSystemDiskId(rasdType.getHostResource());

                if (!diskDescByName.containsKey(diskId))
                {
                    String msg = "DiskId [" + diskId + "] not found on disk section";
                    throw new IdAlreadyExistsException(msg);
                }

                if (!diskIdToVSs.containsKey(diskId))
                {
                    List<String> vss = new LinkedList<String>();
                    vss.add(vsystem.getId()); // XXX

                    diskIdToVSs.put(diskId, vss);
                }
                else
                {
                    diskIdToVSs.get(diskId).add(vsystem.getId());
                }

                VirtualDiskDescType diskDescType = diskDescByName.get(diskId);

                String capacity = diskDescType.getCapacity();

                dReq.setHd(Long.parseLong(capacity));

                final String allocationUnits = diskDescType.getCapacityAllocationUnits();
                final MemorySizeUnit hdSizeUnit = getMemoryUnitsFromOVF(allocationUnits);

                dReq.setHdSizeUnit(hdSizeUnit);
                // dReq.setImageSize(diskDescType.get);
            }
            else if (CIMResourceTypeEnum.Ethernet_Adapter.getNumericResourceType() == resTnumeric)
            {
                String ethDriver = null;
                EthernetDriverType ethDriverType = null;
                try
                {
                    ethDriver = rasdType.getResourceSubType().getValue();
                    ethDriverType = EthernetDriverType.valueOf(ethDriver);
                }
                catch (Exception e)
                {
                    LOG.error("Invalid ethernet adapter type {}", ethDriver != null ? ethDriver
                        : "-ResourceSubType- not found");
                }

                if (dReq.getEthernetDriverType() != null && ethDriverType != null)
                {
                    LOG.warn("Overwrite ethernet adapter type form {} to {}", dReq
                        .getEthernetDriverType().name(), ethDriverType.name());
                }
                else if (ethDriverType != null)
                {
                    dReq.setEthernetDriverType(ethDriverType);
                }
            }
            else if (CIMResourceTypeEnum.IDE_Controller.getNumericResourceType() == resTnumeric)
            {
                if (dReq.getDiskControllerType() != null)
                {
                    LOG.warn("Overwrite disk controller type form {} to {}", dReq
                        .getDiskControllerType().name(), DiskControllerType.IDE.name());
                }

                dReq.setDiskControllerType(DiskControllerType.IDE);
            }
            else if (CIMResourceTypeEnum.Parallel_SCSI_HBA.getNumericResourceType() == resTnumeric)
            {
                if (dReq.getDiskControllerType() != null)
                {
                    LOG.warn("Overwrite disk controller type form {} to {}", dReq
                        .getDiskControllerType().name(), DiskControllerType.SCSI.name());
                }

                dReq.setDiskControllerType(DiskControllerType.SCSI);
            }
        }// rasd

        // if the Ethernet/Disk controller is not specified let the API properties be used as default

        if (dReq.getCpu() == -1)
        {
            throw new RequiredAttributeException("Not CPU RASD element found on the envelope");
        }
        if (dReq.getRam() == -1)
        {
            throw new RequiredAttributeException("Not RAM RASD element found on the envelope");
        }
        if (dReq.getHd() == -1)
        {
            throw new RequiredAttributeException("Not HD RASD element found on the envelope");
        }

        return dReq;
View Full Code Here

    public static TemplateDto createTemplateDto(final String ovfId, EnvelopeType envelope)
        throws AMException
    {
        envelope = checkEnvelopeIsValid(envelope);

        TemplateDto diskInfo = null;

        Map<String, VirtualDiskDescType> diskIdToDiskFormat =
            new HashMap<String, VirtualDiskDescType>();
        Map<String, FileType> fileIdToFileType = new HashMap<String, FileType>();
        Map<String, List<String>> diskIdToVSs = new HashMap<String, List<String>>();
        Map<String, TemplateDto> requiredByVSs = new HashMap<String, TemplateDto>();
        DiskSectionType diskSectionType;

        try
        {
            ContentType contentType = OVFEnvelopeUtils.getTopLevelVirtualSystemContent(envelope);

            ProductSectionType product =
                OVFEnvelopeUtils.getSection(contentType, ProductSectionType.class);
            OperatingSystemSectionType ossection = null;
            try
            {
                ossection =
                    OVFEnvelopeUtils.getSection(contentType, OperatingSystemSectionType.class);
            }
            catch (Exception e) // no such section
            {
            }

            String description = null;
            if (product.getInfo() != null && product.getInfo().getValue() != null)
            {
                description = product.getInfo().getValue();
            }

            String categoryName = null;
            if (product.getOtherAttributes().containsKey(new QName("CategoryName")))
            {
                categoryName = product.getOtherAttributes().get(new QName("CategoryName"));
            }

            String iconPath = getIconPath(product, fileIdToFileType, ovfId);

            diskSectionType = OVFEnvelopeUtils.getSection(envelope, DiskSectionType.class);

            for (FileType fileType : envelope.getReferences().getFile())
            {
                fileIdToFileType.put(fileType.getId(), fileType);
            }
            // Create a hash
            for (VirtualDiskDescType virtualDiskDescType : diskSectionType.getDisk())
            {
                diskIdToDiskFormat.put(virtualDiskDescType.getDiskId(), virtualDiskDescType);
            }

            if (contentType instanceof VirtualSystemType)
            {

                VirtualSystemType vs = (VirtualSystemType) contentType;
                TemplateDto req = getDiskInfo(vs, diskIdToDiskFormat, diskIdToVSs);

                requiredByVSs.put(vs.getId(), req);
            }
            else if (contentType instanceof VirtualSystemCollectionType)
            {
                List<VirtualSystemType> virtualSystems =
                    OVFEnvelopeUtils.getVirtualSystems((VirtualSystemCollectionType) contentType);

                for (VirtualSystemType virtualSystemType : virtualSystems)
                {
                    TemplateDto req =
                        getDiskInfo(virtualSystemType, diskIdToDiskFormat, diskIdToVSs);

                    requiredByVSs.put(virtualSystemType.getId(), req);
                }
            }

            for (VirtualDiskDescType diskDescType : diskIdToDiskFormat.values())
            {

                String diskId = diskDescType.getDiskId();
                String fileId = diskDescType.getFileRef();

                if (!fileIdToFileType.containsKey(fileId))
                {
                    String msg = "File Id [" + fileId + "] not found on the ReferencesSection";
                    throw new IdNotFoundException(msg);
                }

                FileType file = fileIdToFileType.get(fileId);
                final String filePath = file.getHref();
                final Long fileSize = file.getSize().longValue();

                String format = diskDescType.getFormat(); // XXX using the same format on the OVF
                // disk

                if (!diskIdToVSs.containsKey(diskId))
                {
                    throw new IdNotFoundException("Any virtualSystem is using diskId[" + diskId
                        + "]"); // XXX warning ??
                }

                if (diskIdToVSs.size() != 1)
                {
                    throw new AMException(AMError.TEMPLATE_INVALID, String.format(
                        "There are more than one virtual system on the OVF Envelope [%s]", ovfId));
                }

                for (String vssName : diskIdToVSs.get(diskId))
                {
                    diskInfo = new TemplateDto();
                    diskInfo.setName(vssName);
                    diskInfo.setUrl(ovfId);
                    diskInfo.setLoginUser(getProductPropertyValue(product, "user"));
                    diskInfo.setLoginPassword(getProductPropertyValue(product, "password"));
                    diskInfo.setOsType(getOSType(ossection));
                    diskInfo.setOsVersion(getOSVersion(ossection));

                    DiskFormatOVF diskFormat = DiskFormatOVF.fromValue(format);

                    diskInfo.setDiskFileFormat(diskFormat.name());
                    diskInfo.setDiskFileSize(fileSize);

                    // Note that getHRef() will now return the relative path
                    // of the file at the downloaded repository space

                    if (filePath.startsWith("http:"))
                    {
                        diskInfo.setDiskFilePath(FilenameUtils.getName(filePath));
                    }
                    else
                    {
                        diskInfo.setDiskFilePath(filePath);
                    }

                    diskInfo.setIconUrl(iconPath);
                    diskInfo.setDescription(description);
                    diskInfo.setCategoryName(categoryName);
                    // XXX diskInfo.setSO(value);

                    if (!requiredByVSs.containsKey(vssName))
                    {
                        throw new IdNotFoundException("VirtualSystem id not found [" + vssName
                            + "]");
                    }

                    TemplateDto requirement = requiredByVSs.get(vssName);

                    // XXX disk format ::: diskInfo.setImageType(requirement.getImageType());

                    diskInfo.setRequiredCpu(requirement.getRequiredCpu());
                    diskInfo.setRequiredRamInMB(requirement.getRequiredRamInMB());
                    diskInfo.setRequiredHDInMB(requirement.getRequiredHDInMB());

                    diskInfo.setEthernetDriverType(requirement.getEthernetDriverType());
                    diskInfo.setDiskControllerType(requirement.getDiskControllerType());

                    // TODO diskInfo.setEnterpriseId(enterpriseId);
                    // diskInfo.setUserId(userId); TODO user ID
                    // diskInfo.getCategories().add(category); TODO category
View Full Code Here

    private static TemplateDto getDiskInfo(final VirtualSystemType vsystem,
        final Map<String, VirtualDiskDescType> diskDescByName,
        final Map<String, List<String>> diskIdToVSs) throws IdAlreadyExistsException,
        RequiredAttributeException, SectionNotPresentException
    {
        TemplateDto dReq = new TemplateDto();
        VirtualHardwareSectionType hardwareSectionType;

        try
        {
            hardwareSectionType =
                OVFEnvelopeUtils.getSection(vsystem, VirtualHardwareSectionType.class);
        }
        catch (InvalidSectionException e)
        {
            throw new SectionNotPresentException("VirtualHardware on a virtualSystem", e);
        }

        dReq.setRequiredCpu(-1);
        dReq.setRequiredRamInMB(-1l);
        dReq.setRequiredHDInMB(-1l);

        // XXX now we are using ONLY the Disk format

        // XXX String vsType = hardwareSectionType.getSystem().getVirtualSystemType().getValue();
        // XXX dReq.setImageType(vsType);
        // XXX log.debug("Using ''virtualSystemType'' [{}]", vsType);

        for (RASDType rasdType : hardwareSectionType.getItem())
        {
            ResourceType resourceType = rasdType.getResourceType();
            int resTnumeric = Integer.parseInt(resourceType.getValue());

            // TODO use CIMResourceTypeEnum from value and then a SWITCH

            // Get the information on the ram
            if (CIMResourceTypeEnum.Processor.getNumericResourceType() == resTnumeric)
            {
                String cpuVal = rasdType.getVirtualQuantity().getValue().toString();

                dReq.setRequiredCpu(Integer.parseInt(cpuVal));

                // TODO if(rasdType.getAllocationUnits()!= null)
            }
            else if (CIMResourceTypeEnum.Memory.getNumericResourceType() == resTnumeric)
            {
                Long ramVal = rasdType.getVirtualQuantity().getValue().longValue();
                String allocationUnits = allocationUnits(rasdType);

                dReq.setRequiredRamInMB(getMegaByes(ramVal, allocationUnits));
            }
            else if (CIMResourceTypeEnum.Disk_Drive.getNumericResourceType() == resTnumeric)
            {
                // HD requirements are extracted from the associated Disk on ''hostResource''
                String diskId = getVirtualSystemDiskId(rasdType.getHostResource());

                if (!diskDescByName.containsKey(diskId))
                {
                    String msg = "DiskId [" + diskId + "] not found on disk section";
                    throw new IdAlreadyExistsException(msg);
                }

                if (!diskIdToVSs.containsKey(diskId))
                {
                    List<String> vss = new LinkedList<String>();
                    vss.add(vsystem.getId()); // XXX

                    diskIdToVSs.put(diskId, vss);
                }
                else
                {
                    diskIdToVSs.get(diskId).add(vsystem.getId());
                }

                VirtualDiskDescType diskDescType = diskDescByName.get(diskId);
                final String capacity = diskDescType.getCapacity();
                final String allocationUnits = diskDescType.getCapacityAllocationUnits();

                dReq.setRequiredHDInMB(getMegaByes(Long.parseLong(capacity), allocationUnits));
            }
            else if (CIMResourceTypeEnum.Ethernet_Adapter.getNumericResourceType() == resTnumeric)
            {
                String ethDriver = null;
                try
                {
                    ethDriver = rasdType.getResourceSubType().getValue();
                }
                catch (Exception e)
                {
                    LOG.error("Invalid ethernet adapter type {}", ethDriver != null ? ethDriver
                        : "-ResourceSubType- not found");
                }

                if (ethDriver != null)
                {
                    dReq.setEthernetDriverType(ethDriver);
                }
            }
            else if (CIMResourceTypeEnum.IDE_Controller.getNumericResourceType() == resTnumeric)
            {
                dReq.setDiskControllerType("IDE");
            }
            else if (CIMResourceTypeEnum.Parallel_SCSI_HBA.getNumericResourceType() == resTnumeric)
            {
                dReq.setDiskControllerType("SCSI");
            }
        }// rasd

        // if the Ethernet/Disk controller is not specified let the API properties be used as
        // default

        if (dReq.getRequiredCpu() == -1)
        {
            throw new RequiredAttributeException("Not CPU RASD element found on the envelope");
        }
        if (dReq.getRequiredRamInMB() == -1)
        {
            throw new RequiredAttributeException("Not RAM RASD element found on the envelope");
        }
        if (dReq.getRequiredHDInMB() == -1)
        {
            throw new RequiredAttributeException("Not HD RASD element found on the envelope");
        }

        return dReq;
View Full Code Here

    public static TemplateDto createTemplateDto(final String ovfId, EnvelopeType envelope)
        throws AMException
    {
        envelope = checkEnvelopeIsValid(envelope);

        TemplateDto diskInfo = null;

        Map<String, VirtualDiskDescType> diskIdToDiskFormat =
            new HashMap<String, VirtualDiskDescType>();
        Map<String, FileType> fileIdToFileType = new HashMap<String, FileType>();
        Map<String, List<String>> diskIdToVSs = new HashMap<String, List<String>>();
        Map<String, TemplateDto> requiredByVSs = new HashMap<String, TemplateDto>();
        DiskSectionType diskSectionType;

        try
        {
            ContentType contentType = OVFEnvelopeUtils.getTopLevelVirtualSystemContent(envelope);

            ProductSectionType product =
                OVFEnvelopeUtils.getSection(contentType, ProductSectionType.class);
            OperatingSystemSectionType ossection = null;
            try
            {
                ossection =
                    OVFEnvelopeUtils.getSection(contentType, OperatingSystemSectionType.class);
            }
            catch (Exception e) // no such section
            {
            }

            String description = null;
            if (product.getInfo() != null && product.getInfo().getValue() != null)
            {
                description = product.getInfo().getValue();
            }

            String categoryName = null;
            if (product.getOtherAttributes().containsKey(new QName("CategoryName")))
            {
                categoryName = product.getOtherAttributes().get(new QName("CategoryName"));
            }

            String iconPath = getIconPath(product, fileIdToFileType, ovfId);

            diskSectionType = OVFEnvelopeUtils.getSection(envelope, DiskSectionType.class);

            for (FileType fileType : envelope.getReferences().getFile())
            {
                fileIdToFileType.put(fileType.getId(), fileType);
            }
            // Create a hash
            for (VirtualDiskDescType virtualDiskDescType : diskSectionType.getDisk())
            {
                diskIdToDiskFormat.put(virtualDiskDescType.getDiskId(), virtualDiskDescType);
            }

            if (contentType instanceof VirtualSystemType)
            {

                VirtualSystemType vs = (VirtualSystemType) contentType;
                TemplateDto req = getDiskInfo(vs, diskIdToDiskFormat, diskIdToVSs);

                requiredByVSs.put(vs.getId(), req);
            }
            else if (contentType instanceof VirtualSystemCollectionType)
            {
                List<VirtualSystemType> virtualSystems =
                    OVFEnvelopeUtils.getVirtualSystems((VirtualSystemCollectionType) contentType);

                for (VirtualSystemType virtualSystemType : virtualSystems)
                {
                    TemplateDto req =
                        getDiskInfo(virtualSystemType, diskIdToDiskFormat, diskIdToVSs);

                    requiredByVSs.put(virtualSystemType.getId(), req);
                }
            }

            for (VirtualDiskDescType diskDescType : diskIdToDiskFormat.values())
            {

                String diskId = diskDescType.getDiskId();
                String fileId = diskDescType.getFileRef();

                if (!fileIdToFileType.containsKey(fileId))
                {
                    String msg = "File Id [" + fileId + "] not found on the ReferencesSection";
                    throw new IdNotFoundException(msg);
                }

                FileType file = fileIdToFileType.get(fileId);
                final String filePath = file.getHref();
                final Long fileSize = file.getSize().longValue();

                String format = diskDescType.getFormat(); // XXX using the same format on the OVF
                // disk

                if (!diskIdToVSs.containsKey(diskId))
                {
                    throw new IdNotFoundException("Any virtualSystem is using diskId[" + diskId
                        + "]"); // XXX warning ??
                }

                if (diskIdToVSs.size() != 1)
                {
                    throw new AMException(AMError.TEMPLATE_INVALID, String.format(
                        "There are more than one virtual system on the OVF Envelope [%s]", ovfId));
                }

                for (String vssName : diskIdToVSs.get(diskId))
                {
                    diskInfo = new TemplateDto();
                    diskInfo.setName(vssName);
                    diskInfo.setUrl(ovfId);
                    diskInfo.setLoginUser(getProductPropertyValue(product, "user"));
                    diskInfo.setLoginPassword(getProductPropertyValue(product, "password"));
                    diskInfo.setOsType(getOSType(ossection));
                    diskInfo.setOsVersion(getOSVersion(ossection));

                    DiskFormatOVF diskFormat = DiskFormatOVF.fromValue(format);

                    diskInfo.setDiskFileFormat(diskFormat.name());
                    diskInfo.setDiskFileSize(fileSize);

                    // Note that getHRef() will now return the relative path
                    // of the file at the downloaded repository space

                    if (filePath.startsWith("http:"))
                    {
                        diskInfo.setDiskFilePath(FilenameUtils.getName(filePath));
                    }
                    else
                    {
                        diskInfo.setDiskFilePath(filePath);
                    }

                    diskInfo.setIconUrl(iconPath);
                    diskInfo.setDescription(description);
                    diskInfo.setCategoryName(categoryName);
                    // XXX diskInfo.setSO(value);

                    if (!requiredByVSs.containsKey(vssName))
                    {
                        throw new IdNotFoundException("VirtualSystem id not found [" + vssName
                            + "]");
                    }

                    TemplateDto requirement = requiredByVSs.get(vssName);

                    // XXX disk format ::: diskInfo.setImageType(requirement.getImageType());

                    diskInfo.setRequiredCpu(requirement.getRequiredCpu());
                    diskInfo.setRequiredRamInMB(requirement.getRequiredRamInMB());
                    diskInfo.setRequiredHDInMB(requirement.getRequiredHDInMB());

                    diskInfo.setEthernetDriverType(requirement.getEthernetDriverType());
                    diskInfo.setDiskControllerType(requirement.getDiskControllerType());
                    diskInfo.setDiskController(requirement.getDiskController());

                    // TODO diskInfo.setEnterpriseId(enterpriseId);
                    // diskInfo.setUserId(userId); TODO user ID
                    // diskInfo.getCategories().add(category); TODO category
View Full Code Here

    private static TemplateDto getDiskInfo(final VirtualSystemType vsystem,
        final Map<String, VirtualDiskDescType> diskDescByName,
        final Map<String, List<String>> diskIdToVSs) throws IdAlreadyExistsException,
        RequiredAttributeException, SectionNotPresentException
    {
        TemplateDto dReq = new TemplateDto();
        VirtualHardwareSectionType hardwareSectionType;

        try
        {
            hardwareSectionType =
                OVFEnvelopeUtils.getSection(vsystem, VirtualHardwareSectionType.class);
        }
        catch (InvalidSectionException e)
        {
            throw new SectionNotPresentException("VirtualHardware on a virtualSystem", e);
        }

        dReq.setRequiredCpu(-1);
        dReq.setRequiredRamInMB(-1l);
        dReq.setRequiredHDInMB(-1l);

        // XXX now we are using ONLY the Disk format

        // XXX String vsType = hardwareSectionType.getSystem().getVirtualSystemType().getValue();
        // XXX dReq.setImageType(vsType);
        // XXX log.debug("Using ''virtualSystemType'' [{}]", vsType);

        for (RASDType rasdType : hardwareSectionType.getItem())
        {
            ResourceType resourceType = rasdType.getResourceType();
            int resTnumeric = Integer.parseInt(resourceType.getValue());

            // TODO use CIMResourceTypeEnum from value and then a SWITCH

            // Get the information on the ram
            if (CIMResourceTypeEnum.Processor.getNumericResourceType() == resTnumeric)
            {
                String cpuVal = rasdType.getVirtualQuantity().getValue().toString();

                dReq.setRequiredCpu(Integer.parseInt(cpuVal));

                // TODO if(rasdType.getAllocationUnits()!= null)
            }
            else if (CIMResourceTypeEnum.Memory.getNumericResourceType() == resTnumeric)
            {
                Long ramVal = rasdType.getVirtualQuantity().getValue().longValue();
                String allocationUnits = allocationUnits(rasdType);

                dReq.setRequiredRamInMB(getMegaByes(ramVal, allocationUnits));
            }
            else if (CIMResourceTypeEnum.Disk_Drive.getNumericResourceType() == resTnumeric)
            {
                // HD requirements are extracted from the associated Disk on ''hostResource''
                String diskId = getVirtualSystemDiskId(rasdType.getHostResource());

                if (!diskDescByName.containsKey(diskId))
                {
                    String msg = "DiskId [" + diskId + "] not found on disk section";
                    throw new IdAlreadyExistsException(msg);
                }

                if (!diskIdToVSs.containsKey(diskId))
                {
                    List<String> vss = new LinkedList<String>();
                    vss.add(vsystem.getId()); // XXX

                    diskIdToVSs.put(diskId, vss);
                }
                else
                {
                    diskIdToVSs.get(diskId).add(vsystem.getId());
                }

                VirtualDiskDescType diskDescType = diskDescByName.get(diskId);
                final String capacity = diskDescType.getCapacity();
                final String allocationUnits = diskDescType.getCapacityAllocationUnits();

                dReq.setRequiredHDInMB(getMegaByes(Long.parseLong(capacity), allocationUnits));
            }
            else if (CIMResourceTypeEnum.Ethernet_Adapter.getNumericResourceType() == resTnumeric)
            {
                String ethDriver = null;
                try
                {
                    ethDriver = rasdType.getResourceSubType().getValue();
                }
                catch (Exception e)
                {
                    LOG.error("Invalid ethernet adapter type {}", ethDriver != null ? ethDriver
                        : "-ResourceSubType- not found");
                }

                if (ethDriver != null)
                {
                    dReq.setEthernetDriverType(ethDriver);
                }
            }
            else if (CIMResourceTypeEnum.IDE_Controller.getNumericResourceType() == resTnumeric)
            {
                dReq.setDiskControllerType("IDE");
            }
            else if (CIMResourceTypeEnum.Parallel_SCSI_HBA.getNumericResourceType() == resTnumeric)
            {
                dReq.setDiskControllerType("SCSI");
                dReq.setDiskController(rasdType.getResourceSubType() != null ? rasdType
                    .getResourceSubType().getValue() : null);
            }
        }// rasd

        // if the Ethernet/Disk controller is not specified let the API properties be used as
        // default

        if (dReq.getRequiredCpu() == -1)
        {
            throw new RequiredAttributeException("Not CPU RASD element found on the envelope");
        }
        if (dReq.getRequiredRamInMB() == -1)
        {
            throw new RequiredAttributeException("Not RAM RASD element found on the envelope");
        }
        if (dReq.getRequiredHDInMB() == -1)
        {
            throw new RequiredAttributeException("Not HD RASD element found on the envelope");
        }

        return dReq;
View Full Code Here

    public static TemplateDto createTemplateDto(final String ovfId, EnvelopeType envelope)
        throws AMException
    {
        envelope = checkEnvelopeIsValid(envelope);

        TemplateDto diskInfo = null;

        Map<String, VirtualDiskDescType> diskIdToDiskFormat =
            new HashMap<String, VirtualDiskDescType>();
        Map<String, FileType> fileIdToFileType = new HashMap<String, FileType>();
        Map<String, List<String>> diskIdToVSs = new HashMap<String, List<String>>();
        Map<String, TemplateDto> requiredByVSs = new HashMap<String, TemplateDto>();
        DiskSectionType diskSectionType;

        try
        {
            ContentType contentType = OVFEnvelopeUtils.getTopLevelVirtualSystemContent(envelope);

            ProductSectionType product =
                OVFEnvelopeUtils.getSection(contentType, ProductSectionType.class);
            OperatingSystemSectionType ossection = null;
            try
            {
                ossection =
                    OVFEnvelopeUtils.getSection(contentType, OperatingSystemSectionType.class);
            }
            catch (Exception e) // no such section
            {
            }

            String description = null;
            if (product.getInfo() != null && product.getInfo().getValue() != null)
            {
                description = product.getInfo().getValue();
            }

            String categoryName = null;
            if (product.getOtherAttributes().containsKey(new QName("CategoryName")))
            {
                categoryName = product.getOtherAttributes().get(new QName("CategoryName"));
            }

            String iconPath = getIconPath(product, fileIdToFileType, ovfId);

            diskSectionType = OVFEnvelopeUtils.getSection(envelope, DiskSectionType.class);

            for (FileType fileType : envelope.getReferences().getFile())
            {
                fileIdToFileType.put(fileType.getId(), fileType);
            }
            // Create a hash
            for (VirtualDiskDescType virtualDiskDescType : diskSectionType.getDisk())
            {
                diskIdToDiskFormat.put(virtualDiskDescType.getDiskId(), virtualDiskDescType);
            }

            if (contentType instanceof VirtualSystemType)
            {

                VirtualSystemType vs = (VirtualSystemType) contentType;
                TemplateDto req = getDiskInfo(vs, diskIdToDiskFormat, diskIdToVSs);

                requiredByVSs.put(vs.getId(), req);
            }
            else if (contentType instanceof VirtualSystemCollectionType)
            {
                List<VirtualSystemType> virtualSystems =
                    OVFEnvelopeUtils.getVirtualSystems((VirtualSystemCollectionType) contentType);

                for (VirtualSystemType virtualSystemType : virtualSystems)
                {
                    TemplateDto req =
                        getDiskInfo(virtualSystemType, diskIdToDiskFormat, diskIdToVSs);

                    requiredByVSs.put(virtualSystemType.getId(), req);
                }
            }

            for (VirtualDiskDescType diskDescType : diskIdToDiskFormat.values())
            {

                String diskId = diskDescType.getDiskId();
                String fileId = diskDescType.getFileRef();

                if (!fileIdToFileType.containsKey(fileId))
                {
                    String msg = "File Id [" + fileId + "] not found on the ReferencesSection";
                    throw new IdNotFoundException(msg);
                }

                FileType file = fileIdToFileType.get(fileId);
                final String filePath = file.getHref();
                final Long fileSize = file.getSize().longValue();

                String format = diskDescType.getFormat(); // XXX using the same format on the OVF
                // disk

                if (!diskIdToVSs.containsKey(diskId))
                {
                    throw new IdNotFoundException("Any virtualSystem is using diskId[" + diskId
                        + "]"); // XXX warning ??
                }

                if (diskIdToVSs.size() != 1)
                {
                    throw new AMException(AMError.TEMPLATE_INVALID, String.format(
                        "There are more than one virtual system on the OVF Envelope [%s]", ovfId));
                }

                for (String vssName : diskIdToVSs.get(diskId))
                {
                    diskInfo = new TemplateDto();
                    diskInfo.setName(vssName);
                    diskInfo.setUrl(ovfId);
                    diskInfo.setLoginUser(getProductPropertyValue(product, "user"));
                    diskInfo.setLoginPassword(getProductPropertyValue(product, "password"));
                    diskInfo.setOsType(getOSType(ossection));
                    diskInfo.setOsVersion(getOSVersion(ossection));

                    DiskFormatOVF diskFormat = DiskFormatOVF.fromValue(format);

                    diskInfo.setDiskFileFormat(diskFormat.name());
                    diskInfo.setDiskFileSize(fileSize);

                    // Note that getHRef() will now return the relative path
                    // of the file at the downloaded repository space

                    if (filePath.startsWith("http:"))
                    {
                        diskInfo.setDiskFilePath(FilenameUtils.getName(filePath));
                    }
                    else
                    {
                        diskInfo.setDiskFilePath(filePath);
                    }

                    diskInfo.setIconUrl(iconPath);
                    diskInfo.setDescription(description);
                    diskInfo.setCategoryName(categoryName);
                    // XXX diskInfo.setSO(value);

                    if (!requiredByVSs.containsKey(vssName))
                    {
                        throw new IdNotFoundException("VirtualSystem id not found [" + vssName
                            + "]");
                    }

                    TemplateDto requirement = requiredByVSs.get(vssName);

                    // XXX disk format ::: diskInfo.setImageType(requirement.getImageType());

                    diskInfo.setRequiredCpu(requirement.getRequiredCpu());
                    diskInfo.setRequiredRamInMB(requirement.getRequiredRamInMB());
                    diskInfo.setRequiredHDInMB(requirement.getRequiredHDInMB());

                    diskInfo.setEthernetDriverType(requirement.getEthernetDriverType());
                    diskInfo.setDiskControllerType(requirement.getDiskControllerType());

                    // TODO diskInfo.setEnterpriseId(enterpriseId);
                    // diskInfo.setUserId(userId); TODO user ID
                    // diskInfo.getCategories().add(category); TODO category
View Full Code Here

    private static TemplateDto getDiskInfo(final VirtualSystemType vsystem,
        final Map<String, VirtualDiskDescType> diskDescByName,
        final Map<String, List<String>> diskIdToVSs) throws IdAlreadyExistsException,
        RequiredAttributeException, SectionNotPresentException
    {
        TemplateDto dReq = new TemplateDto();
        VirtualHardwareSectionType hardwareSectionType;

        try
        {
            hardwareSectionType =
                OVFEnvelopeUtils.getSection(vsystem, VirtualHardwareSectionType.class);
        }
        catch (InvalidSectionException e)
        {
            throw new SectionNotPresentException("VirtualHardware on a virtualSystem", e);
        }

        dReq.setRequiredCpu(-1);
        dReq.setRequiredRamInMB(-1l);
        dReq.setRequiredHDInMB(-1l);

        // XXX now we are using ONLY the Disk format

        // XXX String vsType = hardwareSectionType.getSystem().getVirtualSystemType().getValue();
        // XXX dReq.setImageType(vsType);
        // XXX log.debug("Using ''virtualSystemType'' [{}]", vsType);

        for (RASDType rasdType : hardwareSectionType.getItem())
        {
            ResourceType resourceType = rasdType.getResourceType();
            int resTnumeric = Integer.parseInt(resourceType.getValue());

            // TODO use CIMResourceTypeEnum from value and then a SWITCH

            // Get the information on the ram
            if (CIMResourceTypeEnum.Processor.getNumericResourceType() == resTnumeric)
            {
                String cpuVal = rasdType.getVirtualQuantity().getValue().toString();

                dReq.setRequiredCpu(Integer.parseInt(cpuVal));

                // TODO if(rasdType.getAllocationUnits()!= null)
            }
            else if (CIMResourceTypeEnum.Memory.getNumericResourceType() == resTnumeric)
            {
                Long ramVal = rasdType.getVirtualQuantity().getValue().longValue();
                String allocationUnits = allocationUnits(rasdType);

                dReq.setRequiredRamInMB(getMegaByes(ramVal, allocationUnits));
            }
            else if (CIMResourceTypeEnum.Disk_Drive.getNumericResourceType() == resTnumeric)
            {
                // HD requirements are extracted from the associated Disk on ''hostResource''
                String diskId = getVirtualSystemDiskId(rasdType.getHostResource());

                if (!diskDescByName.containsKey(diskId))
                {
                    String msg = "DiskId [" + diskId + "] not found on disk section";
                    throw new IdAlreadyExistsException(msg);
                }

                if (!diskIdToVSs.containsKey(diskId))
                {
                    List<String> vss = new LinkedList<String>();
                    vss.add(vsystem.getId()); // XXX

                    diskIdToVSs.put(diskId, vss);
                }
                else
                {
                    diskIdToVSs.get(diskId).add(vsystem.getId());
                }

                VirtualDiskDescType diskDescType = diskDescByName.get(diskId);
                final String capacity = diskDescType.getCapacity();
                final String allocationUnits = diskDescType.getCapacityAllocationUnits();

                dReq.setRequiredHDInMB(getMegaByes(Long.parseLong(capacity), allocationUnits));
            }
            else if (CIMResourceTypeEnum.Ethernet_Adapter.getNumericResourceType() == resTnumeric)
            {
                String ethDriver = null;
                try
                {
                    ethDriver = rasdType.getResourceSubType().getValue();
                }
                catch (Exception e)
                {
                    LOG.error("Invalid ethernet adapter type {}", ethDriver != null ? ethDriver
                        : "-ResourceSubType- not found");
                }

                if (ethDriver != null)
                {
                    dReq.setEthernetDriverType(ethDriver);
                }
            }
            else if (CIMResourceTypeEnum.IDE_Controller.getNumericResourceType() == resTnumeric)
            {
                dReq.setDiskControllerType("IDE");
            }
            else if (CIMResourceTypeEnum.Parallel_SCSI_HBA.getNumericResourceType() == resTnumeric)
            {
                dReq.setDiskControllerType("SCSI");
            }
        }// rasd

        // if the Ethernet/Disk controller is not specified let the API properties be used as
        // default

        if (dReq.getRequiredCpu() == -1)
        {
            throw new RequiredAttributeException("Not CPU RASD element found on the envelope");
        }
        if (dReq.getRequiredRamInMB() == -1)
        {
            throw new RequiredAttributeException("Not RAM RASD element found on the envelope");
        }
        if (dReq.getRequiredHDInMB() == -1)
        {
            throw new RequiredAttributeException("Not HD RASD element found on the envelope");
        }

        return dReq;
View Full Code Here

TOP

Related Classes of com.abiquo.am.model.TemplateDto

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.