private static Iaas loadIaas(IaasProvider iaasProvider) throws InvalidIaasProviderException {
    try {

      if (iaasProvider.getClassName() == null) {
        String msg =
            "You have not specified a class which represents the iaas of type: ["
                + iaasProvider.getType()
                + "].";
        log.error(msg);
        throw new InvalidIaasProviderException(msg);
      }

      Constructor<?> c =
          Class.forName(iaasProvider.getClassName()).getConstructor(IaasProvider.class);
      Iaas iaas = (Iaas) c.newInstance(iaasProvider);
      return iaas;
    } catch (Exception e) {
      String msg =
          "Class ["
              + iaasProvider.getClassName()
              + "] which represents the iaas of type: ["
              + iaasProvider.getType()
              + "] has failed to instantiate.";
      log.error(msg, e);
      throw new InvalidIaasProviderException(msg, e);
    }
  }
  public static Iaas setDefaultIaas(IaasProvider iaasProvider) throws InvalidIaasProviderException {

    Iaas iaas = loadIaas(iaasProvider);

    try {
      ComputeServiceBuilderUtil.buildDefaultComputeService(iaasProvider);
      iaasProvider.setIaas(iaas);
      return iaas;
    } catch (Exception e) {
      String msg =
          "Unable to build the jclouds object for iaas " + "of type: " + iaasProvider.getType();
      log.error(msg, e);
      throw new InvalidIaasProviderException(msg, e);
    }
  }
 public static Iaas getIaas(IaasProvider iaasProvider) throws InvalidIaasProviderException {
   if (iaasProvider.getImage() != null) {
     return setIaas(iaasProvider);
   } else {
     return setDefaultIaas(iaasProvider);
   }
 }
  @SuppressWarnings("unchecked")
  public static Cartridge toCartridge(CartridgeConfig config) {
    if (config == null) {
      return null;
    }
    Cartridge cartridge = new Cartridge();

    // populate cartridge
    cartridge.setType(config.getType());
    cartridge.setDisplayName(config.getDisplayName());
    cartridge.setDescription(config.getDescription());
    cartridge.setHostName(config.getHostName());
    String[] deploymentDirs = config.getDeploymentDirs();
    if ((deploymentDirs != null) && (deploymentDirs.length > 0)) {
      cartridge.setDeploymentDirs(Arrays.asList(deploymentDirs));
    }
    cartridge.setProvider(config.getProvider());
    cartridge.setVersion(config.getVersion());
    cartridge.setBaseDir(config.getBaseDir());

    if (config.getPortMappings() != null) {
      cartridge.setPortMappings(Arrays.asList(config.getPortMappings()));
    }

    if (config.getPersistence() != null) {
      cartridge.setPersistence(config.getPersistence());
    }
    cartridge.setMultiTenant(config.isMultiTenant());
    cartridge.setDefaultAutoscalingPolicy(config.getDefaultAutoscalingPolicy());
    cartridge.setDefaultDeploymentPolicy(config.getDefaultDeploymentPolicy());
    cartridge.setServiceGroup(config.getServiceGroup());
    org.apache.stratos.cloud.controller.pojo.Properties props = config.getProperties();
    if (props != null) {
      for (Property prop : props.getProperties()) {
        cartridge.addProperty(prop.getName(), prop.getValue());
      }
    }

    // populate LB config
    cartridge.setLbConfig(config.getLbConfig());

    List<IaasProvider> iaases = FasterLookUpDataHolder.getInstance().getIaasProviders();

    // populate IaaSes
    IaasConfig[] iaasConfigs = config.getIaasConfigs();
    if (iaasConfigs != null) {
      for (IaasConfig iaasConfig : iaasConfigs) {
        if (iaasConfig != null) {
          IaasProvider iaasProvider = null;
          if (iaases != null) {
            // check whether this is a reference to a predefined IaaS.
            for (IaasProvider iaas : iaases) {
              if (iaas.getType().equals(iaasConfig.getType())) {
                iaasProvider = new IaasProvider(iaas);
                break;
              }
            }
          }

          if (iaasProvider == null) {
            iaasProvider = new IaasProvider();
            iaasProvider.setType(iaasConfig.getType());
          }

          String className = iaasConfig.getClassName();
          if (className != null) {
            iaasProvider.setClassName(className);
          }

          String name = iaasConfig.getName();
          if (name != null) {
            iaasProvider.setName(name);
          }

          String identity = iaasConfig.getIdentity();
          if (identity != null) {
            iaasProvider.setIdentity(identity);
          }

          String credential = iaasConfig.getCredential();
          if (credential != null) {
            iaasProvider.setCredential(credential);
          }

          String provider = iaasConfig.getProvider();
          if (provider != null) {
            iaasProvider.setProvider(provider);
          }
          String imageId = iaasConfig.getImageId();
          if (imageId != null) {
            iaasProvider.setImage(imageId);
          }

          byte[] payload = iaasConfig.getPayload();
          if (payload != null) {
            iaasProvider.setPayload(payload);
          }

          org.apache.stratos.cloud.controller.pojo.Properties props1 = iaasConfig.getProperties();
          if (props1 != null) {
            for (Property prop : props1.getProperties()) {
              iaasProvider.addProperty(prop.getName(), prop.getValue());
            }
          }

          NetworkInterfaces networkInterfaces = iaasConfig.getNetworkInterfaces();
          if (networkInterfaces != null && networkInterfaces.getNetworkInterfaces() != null) {
            iaasProvider.setNetworkInterfaces(networkInterfaces.getNetworkInterfaces());
          }

          cartridge.addIaasProvider(iaasProvider);
        }
      }
    }

    return cartridge;
  }