Example #1
0
  /**
   * Method for getting the list of componentType.
   *
   * @return res ResponseObject. {@literal Body should be HashMap<ComponentType,
   *     list[ComponentManager.property]>}.
   */
  private Response getComponentTypes() {
    ComponentTypesHash componentTypesHash = new ComponentTypesHash();

    for (String compMgrId : componentMgrsSet) {
      Response resp = getComponentTypes(compMgrId);
      if (resp.isError("GET")) {
        log.warn("invalid GET:" + resp.statusCode);
        return resp;
      }
      try {
        ComponentTypesHash types = resp.getBody(ComponentTypesHash.class);
        for (String type : types.keySet()) {
          if (componentTypesHash.containsKey(type)) {
            ComponentType compType = componentTypesHash.get(type);
            compType.addCmId(compMgrId);
          } else {
            ComponentType compType = types.get(type).clone();
            compType.addCmId(compMgrId);
            componentTypesHash.put(type, compType);
          }
        }
      } catch (ParseBodyException e) {
        return new Response(Response.INTERNAL_SERVER_ERROR, "Failed GET component_types.");
      }
    }
    return new Response(Response.OK, componentTypesHash);
  }
Example #2
0
 /**
  * Method for getting the CompoentManager's componentTypes.
  *
  * @return res ResponseObject. {@literal Body should be HashMap<String, ComponentType>}.
  */
 private Response getComponentTypes(String compmgrId) {
   try {
     Response resp = request(compmgrId, Method.GET, "component_types", null);
     if (resp.isError("GET")) {
       log.warn("invalid GET:" + resp.statusCode);
       return resp;
     }
     ComponentTypesHash types = resp.getBody(ComponentTypesHash.class);
     return new Response(Response.OK, types);
   } catch (Exception ex) {
     log.error("Recieved Message Exception.", ex);
     return new Response(Response.INTERNAL_SERVER_ERROR, "Failed GET component_types.");
   }
 }
Example #3
0
  /**
   * Method for creating a component.
   *
   * @param compId ComponentID to be deleted.
   * @param body RequestObject. Body should be component's objectProperty.
   * @return res ResponseObject. Body should be component's objectProperty.
   */
  private Response putComponent(final String compId, final ObjectProperty body) {
    String createdType = body.getObjectType();

    if (!allComponentTypes.containsKey(createdType)) {
      log.warn("Not Creatable Component Type:{}", createdType);
      return new Response(Response.BAD_REQUEST, "Not Creatable Component Type");
    }
    if (allComponentTypes.get(createdType).isEmpty()) {
      log.warn("Not Creatable Component Type:{}", createdType);
      return new Response(Response.BAD_REQUEST, "Not Creatable Component Type");
    }

    String compMgrId = null;
    if (body.getProperty(ObjectProperty.PropertyNames.CM_ID) != null) {
      String reqCmId = body.getProperty(ObjectProperty.PropertyNames.CM_ID);
      for (String cmId : allComponentTypes.get(createdType)) {
        if (reqCmId.equals(cmId)) {
          compMgrId = reqCmId;
          break;
        }
      }
    } else {
      ArrayList<String> compMgrIds = new ArrayList<String>(allComponentTypes.get(createdType));
      compMgrId = compMgrIds.get(0);
    }

    if (compMgrId == null) {
      log.warn("Not Creatable Component Type:{}", createdType);
      return new Response(Response.BAD_REQUEST, null);
    }

    ObjectProperty createdObjProp = null;

    if (compId == null) {
      return new Response(Response.INTERNAL_SERVER_ERROR, "Component Id is null");
    } else if (componentStateList.containsKey(compId)) {
      return new Response(Response.CONFLICT, "ComponentId is already registerd");
    }
    body.setProperty(ObjectProperty.PropertyNames.OBJECT_ID, compId);

    try {
      this.eventSubscription.addFilter(compId, ObjectPropertyChanged.TYPE);
      try {
        this.applyEventSubscription();
      } catch (Exception e) {
        log.error("Recieved Message Exception.", e);
      }

      String path = String.format("components/%s", body.getObjectId());
      Response resp = request(compMgrId, Method.PUT, path, body);
      if (!resp.statusCode.equals(Response.CREATED)) {
        log.warn("Failed to create Component Type:{} StatusCode:{}", createdType, resp.statusCode);
        return resp;
      }
      createdObjProp = resp.getBody(ObjectProperty.class);
      if (createdObjProp != null) {
        componentStateList.put(compId, ObjectProperty.State.RUNNING);
        mapCompAndCompMgr.put(compId, compMgrId);
        this.componetsObjectProperties.put(compId, createdObjProp);

        log.info("Created Component Type:{} ID:{}", createdType, compId);
        // wait components's subscription
        Thread.sleep(WAIT_SUBSCRIPTION_TIME);
        return resp;
      }
    } catch (Exception e) {
      log.error("Exception to create Component Type:{} ID:{}", createdType, compId, e);
      return new Response(Response.INTERNAL_SERVER_ERROR, null);
    }

    log.error("Unknwon Failed to create Component Type:{} ID:{}", createdType, compId);
    return new Response(Response.INTERNAL_SERVER_ERROR, null);
  }