Esempio n. 1
0
  /**
   * Method for registering component manager.
   *
   * @param compMgrId ComponentManager ID.
   * @param body RequestObject. Body should be ComponentManager's objectProperty.
   * @return res ResponseObject. Body should be register objectProperty.
   */
  private Response putComponentManagers(final String compMngId, final ObjectProperty body) {
    // Register the componentManager ID into the HashSet.
    if (!componentMgrsSet.add(compMngId)) {
      // Registered already
      log.warn("ComponentManager is already registerd, ID:{}", compMngId);
      return new Response(Response.CONFLICT, "ComponentManager is already registerd");
    }
    body.setProperty(ObjectProperty.PropertyNames.OBJECT_ID, compMngId);

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

    String componentTypes = body.getProperty(ObjectProperty.PropertyNames.COMPONENT_TYPES);
    if (componentTypes != null) {
      List<String> typeList = Arrays.asList(componentTypes.split(","));
      log.debug("use component_types {} about {} in request body.", typeList, body.getObjectId());

      componentManagerChanged(ComponentManagerChanged.Action.add.name(), null, body);

      updateCreatableComponentType(body.getObjectId(), typeList);
      componentMgrsSet.add(body.getObjectId());
      compMgrsObjProps.put(body.getObjectId(), body);
    } else {
      log.warn("ComponentTypes is not set request body, Comopnent ID:{}", body.getObjectId());
      return new Response(Response.BAD_REQUEST, null);
    }

    log.info("Registerd ComponentManager Object ID:{}", body.getObjectId());
    return new Response(Response.OK, body);
  }
Esempio n. 2
0
 private void updateComponentManager(final String id, final ObjectProperty body) {
   compMgrsObjProps.put(id, body);
   if (componentMgrsSet.add(id)) {
     log.warn("ComponentManager is not registerd, ID:{}", id);
     return;
   }
   String componentTypes = body.getProperty(ComponentManager2.ATTR_COMPTYPE);
   if (componentTypes == null) {
     log.warn("ComponentTypes is not set request body, Comopnent ID:{}", id);
     return;
   }
   List<String> typeList = Arrays.asList(componentTypes.split(","));
   log.debug("use component_types {} about {} in request body.", typeList, id);
   updateCreatableComponentType(id, typeList);
 }
Esempio n. 3
0
 /**
  * Method for getting the ObjectProperty.
  *
  * @param id Object ID to be acquired.
  * @return res ResponseObject. Body should be ObjectProperty.
  * @throws Exception if an error occurs.
  */
 private Response getObjectById(final String id) throws Exception {
   if (componentStateList.containsKey(id) || componentMgrsSet.contains(id)) {
     ObjectProperty op = getPropertyFromOtherComponent(id);
     if (op != null) {
       return new Response(Response.OK, op);
     } else {
       return new Response(Response.NOT_FOUND, op);
     }
   } else if (eventManagerProperty.getObjectId().equals(id)) {
     return new Response(Response.OK, eventManagerProperty);
   } else if (getObjectId().equals(id)) {
     return new Response(Response.OK, objectProperty);
   }
   return new Response(Response.NOT_FOUND, null);
 }
Esempio n. 4
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);
  }