Package org.apache.stratos.cloud.controller.util

Examples of org.apache.stratos.cloud.controller.util.ServiceContext


            Map.Entry<String, Map<String, ServiceContext>> entry = (Map.Entry<String, Map<String, ServiceContext>>) it1.next();
           
            Map<String, ServiceContext> map = (Map<String, ServiceContext>) entry.getValue();
           
            for (Iterator<ServiceContext> it2 = map.values().iterator(); it2.hasNext();) {
                ServiceContext subjectedSerCtxt = (ServiceContext) it2.next();
               
                if (subjectedSerCtxt != null && subjectedSerCtxt.getCartridge() != null) {
                    List<IaasProvider> iaases = subjectedSerCtxt.getCartridge().getIaases();

                    for (IaasProvider iaas : iaases) {

                        ComputeService computeService = iaas.getComputeService();
                       
                        if(computeService == null){
                            continue;
                        }
                       
                        IaasContext ctxt = null;
                        if((ctxt = subjectedSerCtxt.getIaasContext(iaas.getType())) == null){
                          ctxt = subjectedSerCtxt.addIaasContext(iaas.getType());
                        }

                        // get list of node Ids
                        List<String> nodeIds = ctxt.getAllNodeIds();
View Full Code Here


        for (Iterator<?> it = newlyAddedNodes.entrySet().iterator(); it.hasNext();) {
            @SuppressWarnings("unchecked")
            Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
            String key = entry.getKey();
            String val = entry.getValue();
            ServiceContext ctxt = FasterLookUpDataHolder.getInstance().getServiceContext(key);
           
            log.debug("------ Node id: "+key+" --- node status: "+val+" -------- ctxt: "+ctxt);
           
            if (ctxt != null && key != null && val != null) {
                // bundle the data to be published
                bundleData(key, val, ctxt);
            }  
                   
        }
       
        // adding nodes with state changes
        Map<String, ValueDifference<String>> stateChangedNodes = diff.entriesDiffering();
       
        for (Iterator<?> it = stateChangedNodes.entrySet().iterator(); it.hasNext();) {
            @SuppressWarnings("unchecked")
            Map.Entry<String, ValueDifference<String>> entry = (Map.Entry<String, ValueDifference<String>>) it.next();
           
            String key = entry.getKey();
            String newState = entry.getValue().leftValue();
            ServiceContext ctxt = FasterLookUpDataHolder.getInstance().getServiceContext(key);
           
            log.debug("------- Node id: "+key+" --- node status: "+newState+" -------- ctxt: "+ctxt);
           
            if (ctxt != null && key != null && newState != null) {
                // bundle the data to be published
View Full Code Here

      log.warn("No service found in this configuration file : " + xmlSource.getPath());
      return serviceContextList;
    }

    for (Object obj : serviceNodes) {
      ServiceContext serviceCtxt = new ServiceContext();

      // set the definition file
      serviceCtxt.setFile(xmlSource);
     
      if (obj instanceof OMNode) {
        OMNode serviceNode = (OMNode) obj;

        if (serviceNode.getType() == OMNode.ELEMENT_NODE) {

          OMElement node = (OMElement) serviceNode;

          if (node.getAttribute(new QName(CloudControllerConstants.SERVICE_DOMAIN_ATTR)) == null) {
            String msg =
                         "Essential '" + CloudControllerConstants.SERVICE_DOMAIN_ATTR + "' " +
                                 "attribute of '" + CloudControllerConstants.SERVICE_ELEMENT +
                                 "' element cannot be found in " + xmlSource;

            handleException(msg);
          }

          // set domain name
          serviceCtxt.setDomainName(node.getAttribute(new QName(
                                                                CloudControllerConstants.SERVICE_DOMAIN_ATTR))
                                        .getAttributeValue());

          // set sub domain
          serviceCtxt.setSubDomainName(node.getAttribute(new QName(
                                                                   CloudControllerConstants.SERVICE_SUB_DOMAIN_ATTR))
                                           .getAttributeValue());

          // set tenant range
          serviceCtxt.setTenantRange(node.getAttribute(new QName(
                                                                 CloudControllerConstants.SERVICE_TENANT_RANGE_ATTR))
                                         .getAttributeValue());

          OMNode cartridgeNode =
                                 getFirstMatchingNode(xpath +
                                                              CloudControllerConstants.CARTRIDGE_ELEMENT_XPATH,
                                                      node);

          if (cartridgeNode != null && cartridgeNode.getType() == OMNode.ELEMENT_NODE) {

            OMElement cartridgeElt = (OMElement) cartridgeNode;

            String type =
                cartridgeElt.getAttribute(new QName(CloudControllerConstants.TYPE_ATTR))
                              .getAttributeValue();

            if ("".equals(type)) {
              String msg =
                           "Essential '" + CloudControllerConstants.TYPE_ATTR + "' " +
                                   " attribute of '" +
                                   CloudControllerConstants.CARTRIDGE_ELEMENT + "' of '" +
                                   CloudControllerConstants.SERVICE_ELEMENT +
                                   "' element cannot be found in " + xmlSource;

              handleException(msg);
            }
           
            // set Cartridge type
            serviceCtxt.setCartridgeType(type);

          }
          if (serviceCtxt.getCartridgeType() == null) {
            String msg =
                         "Essential '" + CloudControllerConstants.CARTRIDGE_ELEMENT +
                                 "' element" + " has not specified in " + xmlSource;
            handleException(msg);
          }

          // load payload
          loadPayload(getMatchingNodes(xpath + CloudControllerConstants.PAYLOAD_ELEMENT_XPATH,
                                       node), serviceCtxt);

          // load host name
          loadHostName(getMatchingNodes(xpath + CloudControllerConstants.HOST_ELEMENT_XPATH,
                                        node), serviceCtxt);

          // load properties
          loadProperties(node, serviceCtxt.getProperties());

        }
      }

      FasterLookUpDataHolder.getInstance().addServiceContext(serviceCtxt);
View Full Code Here

    log.info("Starting new instance of domain : " + domainName
        + " and sub domain : " + subDomainName);

    // get the subjected ServiceContext
    ServiceContext serviceCtxt = dataHolder
        .getServiceContext(domainName, subDomainName);

    if (serviceCtxt == null) {
      String msg = "Not a registered service: domain - " + domainName
          + ", sub domain - " + subDomainName;
      log.fatal(msg);
      throw new CloudControllerException(msg);
    }

    // load Cartridge
    serviceCtxt.setCartridge(loadCartridge(serviceCtxt.getCartridgeType(),
        serviceCtxt.getPayload(), dataHolder
            .getCartridges()));

    if (serviceCtxt.getCartridge() == null) {
      String msg = "There's no registered Cartridge found. Domain - "
          + domainName + ", sub domain - " + subDomainName;
      log.fatal(msg);
      throw new CloudControllerException(msg);
    }

    if (serviceCtxt.getCartridge().getIaases().isEmpty()) {
      String msg = "There's no registered IaaSes found for Cartridge type: "
          + serviceCtxt.getCartridge().getType();
      log.fatal(msg);
      throw new CloudControllerException(msg);
    }

    // sort the IaasProviders according to scale up order
    Collections.sort(serviceCtxt.getCartridge().getIaases(),
        IaasProviderComparator.ascending(IaasProviderComparator
            .getComparator(IaasProviderComparator.SCALE_UP_SORT)));

    for (IaasProvider iaas : serviceCtxt.getCartridge().getIaases()) {

      IaasContext ctxt = null;
      if ((ctxt = serviceCtxt.getIaasContext(iaas.getType())) == null) {
        ctxt = serviceCtxt.addIaasContext(iaas.getType());
      }

      if (iaas.getMaxInstanceLimit() > dataHolder.getActiveInstanceCount(iaas.getType())) {
        try {

          iaas.getIaas().setDynamicPayload(iaas);

          // get the ComputeService
          computeService = iaas.getComputeService();

          // corresponding Template
          template = iaas.getTemplate();

          if (template == null) {
            String msg = "Failed to start an instance in "
                + iaas.getType()
                + ". Reason : Template is null. You have not specify a matching service "
                + "element in the configuration file of Autoscaler.\n Hence, will try to "
                + "start in another IaaS if available.";
            log.error(msg);
            continue;
          }

          // set instance name as the host name
          // template.getOptions().userMetadata("Name",
          // serviceCtxt.getHostName());
          // template.getOptions().as(TemplateOptions.class).userMetadata("Name",
          // serviceCtxt.getHostName());

          // generate the group id from domain name and sub domain
          // name.
          // Should have lower-case ASCII letters, numbers, or dashes.
          // Should have a length between 3-15
          String str = domainName.concat("-" + subDomainName)
              .substring(0, 10);
          String group = str.replaceAll("[^a-z0-9-]", "");

          NodeMetadata node;

          // create and start a node
          Set<? extends NodeMetadata> nodes = computeService
              .createNodesInGroup(group, 1, template);

          node = nodes.iterator().next();

          String autoAssignIpProp = iaas
              .getProperty(CloudControllerConstants.AUTO_ASSIGN_IP_PROPERTY);

          // acquire the lock
          lock.lock();

          try {
            // reset ip
            ip = "";
            // default behavior is autoIpAssign=false
            if (autoAssignIpProp == null
                || (autoAssignIpProp != null && autoAssignIpProp
                    .equals("false"))) {
              // allocate an IP address - manual IP assigning mode
              ip = iaas.getIaas().associateAddress(iaas, node);
            }

            if (ip.isEmpty()
                && node.getPublicAddresses() != null
                && node.getPublicAddresses().iterator()
                    .hasNext()) {
              ip = node.getPublicAddresses().iterator().next();
            }

            // if not public IP is assigned, we're using private IP
            if (ip.isEmpty()
                && node.getPrivateAddresses() != null
                && node.getPrivateAddresses().iterator()
                    .hasNext()) {
              ip = node.getPrivateAddresses().iterator().next();
            }

            if (node.getId() == null) {
              String msg = "Node id of the starting instance is null.\n"
                  + node.toString();
              log.fatal(msg);
              throw new CloudControllerException(msg);
            }

            // add node ID
            ctxt.addNodeId(node.getId());
            ctxt.addNodeToPublicIp(node.getId(), ip);

            // to faster look up
            dataHolder.addNodeId(
                node.getId(), serviceCtxt);

            serviceCtxt.getCartridge().setLastlyUsedIaas(iaas);

            // add this ip to the topology
            appendToPublicIpProperty(ip, serviceCtxt);

            dataHolder.updateActiveInstanceCount(iaas.getType(), 1);
View Full Code Here

    subDomainName = checkSubDomain(subDomainName);

    log.info("Starting to terminate an instance of domain : " + domainName
        + " and sub domain : " + subDomainName);

    ServiceContext serviceCtxt = dataHolder
        .getServiceContext(domainName, subDomainName);

    if (serviceCtxt == null) {
      String msg = "Not a registered service: domain - " + domainName
          + ", sub domain - " + subDomainName;
      log.fatal(msg);
      throw new CloudControllerException(msg);
    }

    // load Cartridge, if null
    if (serviceCtxt.getCartridge() == null) {
      serviceCtxt.setCartridge(loadCartridge(
          serviceCtxt.getCartridgeType(), serviceCtxt.getPayload(),
          dataHolder.getCartridges()));
    }

    // if still, Cartridge is null
    if (serviceCtxt.getCartridge() == null) {
      String msg = "There's no registered Cartridge found. Domain - "
          + domainName + ", sub domain - " + subDomainName;
      log.fatal(msg);
      throw new CloudControllerException(msg);
    }

    // sort the IaasProviders according to scale down order
    Collections
        .sort(serviceCtxt.getCartridge().getIaases(),
            IaasProviderComparator.ascending(IaasProviderComparator
                .getComparator(IaasProviderComparator.SCALE_DOWN_SORT)));

    // traverse in scale down order
    for (IaasProvider iaas : serviceCtxt.getCartridge().getIaases()) {

      String msg = "Failed to terminate an instance in "
          + iaas.getType()
          + ". Hence, will try to terminate an instance in another IaaS if possible.";

      String nodeId = null;

      IaasContext ctxt = serviceCtxt.getIaasContext(iaas.getType());

      // terminate the last instance first
      for (String id : Lists.reverse(ctxt.getNodeIds())) {
        if (id != null) {
          nodeId = id;
View Full Code Here

    subDomainName = checkSubDomain(subDomainName);

    log.info("Starting to terminate the last instance spawned, of domain : "
        + domainName + " and sub domain : " + subDomainName);

    ServiceContext serviceCtxt = dataHolder
        .getServiceContext(domainName, subDomainName);

    if (serviceCtxt == null) {
      String msg = "Not a registered service: domain - " + domainName
          + ", sub domain - " + subDomainName;
      log.fatal(msg);
      throw new CloudControllerException(msg);
    }

    // load Cartridge, if null
    if (serviceCtxt.getCartridge() == null) {
      serviceCtxt.setCartridge(loadCartridge(
          serviceCtxt.getCartridgeType(), serviceCtxt.getPayload(),
          dataHolder.getCartridges()));
    }

    if (serviceCtxt.getCartridge() == null) {
      String msg = "There's no registered Cartridge found. Domain - "
          + domainName + ", sub domain - " + subDomainName;
      log.fatal(msg);
      throw new CloudControllerException(msg);
    }

    IaasProvider iaas = serviceCtxt.getCartridge().getLastlyUsedIaas();
    // this is required since, we need to find the correct reference.
    // caz if the lastly used iaas retrieved from registry, it is not a
    // reference.
    iaas = serviceCtxt.getCartridge().getIaasProvider(iaas.getType());

    if (iaas != null) {

      String nodeId = null;
      IaasContext ctxt = serviceCtxt.getIaasContext(iaas.getType());

      int i = 0;
      for (i = ctxt.getNodeIds().size() - 1; i >= 0; i--) {
        String id = ctxt.getNodeIds().get(i);
        if (id != null) {
View Full Code Here

    subDomainName = checkSubDomain(subDomainName);

    log.info("Starting to terminate all instances of domain : "
        + domainName + " and sub domain : " + subDomainName);

    ServiceContext serviceCtxt = dataHolder
        .getServiceContext(domainName, subDomainName);

    if (serviceCtxt == null) {
      String msg = "Not a registered service: domain - " + domainName
          + ", sub domain - " + subDomainName;
      log.fatal(msg);
      throw new CloudControllerException(msg);
    }

    // load Cartridge, if null
    if (serviceCtxt.getCartridge() == null) {
      serviceCtxt.setCartridge(loadCartridge(
          serviceCtxt.getCartridgeType(), serviceCtxt.getPayload(),
          dataHolder.getCartridges()));
    }

    if (serviceCtxt.getCartridge() == null) {
      String msg = "There's no registered Cartridge found. Domain - "
          + domainName + ", sub domain - " + subDomainName;
      log.fatal(msg);
      throw new CloudControllerException(msg);
    }

    // sort the IaasProviders according to scale down order
    Collections
        .sort(serviceCtxt.getCartridge().getIaases(),
            IaasProviderComparator.ascending(IaasProviderComparator
                .getComparator(IaasProviderComparator.SCALE_DOWN_SORT)));

    // traverse in scale down order
    for (IaasProvider iaas : serviceCtxt.getCartridge().getIaases()) {

      IaasContext ctxt = serviceCtxt.getIaasContext(iaas.getType());

      if (ctxt == null) {
        log.error("Iaas Context for " + iaas.getType()
            + " not found. Cannot terminate instances");
        continue;
View Full Code Here

    subDomainName = checkSubDomain(subDomainName);

    int pendingInstanceCount = 0;

    ServiceContext subjectedSerCtxt = dataHolder
        .getServiceContext(domainName, subDomainName);

    if (subjectedSerCtxt != null
        && subjectedSerCtxt.getCartridgeType() != null) {

      // load cartridge
      subjectedSerCtxt.setCartridge(loadCartridge(subjectedSerCtxt
          .getCartridgeType(), subjectedSerCtxt.getPayload(),
          dataHolder.getCartridges()));

      if (subjectedSerCtxt.getCartridge() == null) {
        return pendingInstanceCount;
      }

      List<IaasProvider> iaases = subjectedSerCtxt.getCartridge()
          .getIaases();

      for (IaasProvider iaas : iaases) {

        ComputeService computeService = iaas.getComputeService();

        IaasContext ctxt = null;
        if ((ctxt = subjectedSerCtxt.getIaasContext(iaas.getType())) == null) {
          ctxt = subjectedSerCtxt.addIaasContext(iaas.getType());
        }

        // get list of node Ids which are belong to this domain- sub
        // domain
        List<String> nodeIds = ctxt.getNodeIds();
View Full Code Here

      String tenantRange, String cartridgeType, String hostName,
      Properties properties, byte[] payload)
      throws UnregisteredCartridgeException {

    // create a ServiceContext dynamically
    ServiceContext newServiceCtxt = new ServiceContext();
    newServiceCtxt.setDomainName(domain);
    newServiceCtxt.setSubDomainName(subDomain);
    newServiceCtxt.setTenantRange(tenantRange);
    newServiceCtxt.setHostName(hostName);

    if (properties != null && properties.getProperties() != null) {
      // add properties
      for (Property property : properties.getProperties()) {
        if (property != null && property.getName() != null) {
          newServiceCtxt.setProperty(property.getName(),
              property.getValue());
        }
      }
    }

    newServiceCtxt.setCartridgeType(cartridgeType);

    for (Cartridge cartridge : dataHolder
        .getCartridges()) {
      if (cartridge.getType().equals(cartridgeType)) {
        newServiceCtxt.setCartridge(cartridge);
        break;
      }
    }

    if (newServiceCtxt.getCartridge() == null) {
      String msg = "Registration failed - Unregistered Cartridge type: "
          + cartridgeType;
      log.error(msg);
      throw new UnregisteredCartridgeException(msg);
    }

    if (payload != null && payload.length != 0) {

      // write payload file
      try {
        String uniqueName = domain + "-" + subDomain + ".txt";
        FileUtils.forceMkdir(new File(
            CloudControllerConstants.PAYLOAD_DIR));
        File payloadFile = new File(
            CloudControllerConstants.PAYLOAD_DIR + uniqueName);
        FileUtils.writeByteArrayToFile(payloadFile, payload);
        newServiceCtxt.setPayloadFile(payloadFile.getPath());

      } catch (IOException e) {
        String msg = "Failed while persisting the payload of domain : "
            + domain + ", sub domain : " + subDomain;
        log.error(msg, e);
        throw new CloudControllerException(msg, e);
      }

    } else {
      log.debug("Payload is null or empty for :\n "
          + newServiceCtxt.toNode().toString());
    }

    // persist
    try {
      String uniqueName = domain + "-" + subDomain + "-"
          + UUID.randomUUID() + ".xml";
      FileUtils.writeStringToFile(new File(
          CloudControllerConstants.SERVICES_DIR + uniqueName),
          newServiceCtxt.toXml());
    } catch (IOException e) {
      String msg = "Failed while persisting the service configuration - domain : "
          + domain
          + ", sub domain : "
          + subDomain
          + ", tenant range: "
          + tenantRange
          + ", cartridge type: "
          + cartridgeType;
      log.error(msg, e);
      throw new CloudControllerException(msg, e);
    }

    log.info("Service successfully registered! Domain - " + domain
        + ", Sub domain - " + newServiceCtxt.getSubDomainName()
        + ", Cartridge type - " + cartridgeType);

    return true;
  }
View Full Code Here

      throws UnregisteredServiceException {

    subDomain = checkSubDomain(subDomain);

    // find the service context
    ServiceContext subjectedSerCtxt = dataHolder
        .getServiceContext(domain, subDomain);

    if (subjectedSerCtxt == null) {
      throw new UnregisteredServiceException(
          "No registered service found for domain: " + domain
              + " - sub domain: " + subDomain);
    }

    // get the service definition file.
    File serviceDefFile = subjectedSerCtxt.getFile();

    // delete that file, so that it gets automatically undeployed.
    return serviceDefFile.delete();
  }
View Full Code Here

TOP

Related Classes of org.apache.stratos.cloud.controller.util.ServiceContext

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.