Exemplo n.º 1
0
  /**
   * Method for creating a component connection.
   *
   * @param body RequestObject. Body should be ComponentConnection.
   * @return res ResponseObject. Body should be ComponentConnection.
   */
  private Response postConnections(final ComponentConnection body) {

    // forced to auto-number
    String connId = getUniqueID();
    body.setProperty(ObjectProperty.PropertyNames.OBJECT_ID, connId);

    ComponentConnection curr = null;
    String logicId = body.getProperty(ComponentConnectionLogicAndNetwork.LOGIC_ID);
    String networkId = body.getProperty(ComponentConnectionLogicAndNetwork.NETWORK_ID);

    if (!mapCompAndCompMgr.containsKey(logicId) || !mapCompAndCompMgr.containsKey(networkId)) {
      log.warn(
          "Failed to create Connection " + "Logic:{} Network:{} Cause:Not Exsists Component",
          logicId,
          networkId);
      return new Response(Response.BAD_REQUEST, "Not Exsists Component");
    }

    if (body.getObjectType().equals(ComponentConnectionLogicAndNetwork.TYPE)) {
      curr =
          new ComponentConnectionLogicAndNetwork(
              connId, body.getConnectionType(), body.getObjectState(), logicId, networkId);
    } else {
      log.warn(
          "Failed to create Connection " + "Logic:{} Network:{} Cause:Unexpected ConnectionType:{}",
          body.getObjectType());
      return new Response(Response.BAD_REQUEST, "Unexpected ConnectionType");
    }

    curr.setConnectionState(ComponentConnection.State.INITIALIZING);
    connectionTable.put(connId, curr);

    componentConnectionChanged(ComponentConnectionChanged.Action.add.name(), null, curr);

    log.info(
        "Created Component Connection ID:{} Logic:{} and Network:{}", connId, logicId, networkId);
    return new Response(Response.OK, curr);
  }
Exemplo n.º 2
0
  /**
   * Method for update a component connection.
   *
   * @param connId ComponentID to be updated.
   * @param body RequestObject. Body should be ComponentConnection.
   * @return res ResponseObject. Body should be ComponentConnection.
   */
  private Response putConnections(final String connId, final ComponentConnection body) {

    ComponentConnection curr = null;
    String logicId = body.getProperty(ComponentConnectionLogicAndNetwork.LOGIC_ID);
    String networkId = body.getProperty(ComponentConnectionLogicAndNetwork.NETWORK_ID);
    if (body.getObjectType().equals(ComponentConnectionLogicAndNetwork.TYPE)) {
      curr =
          new ComponentConnectionLogicAndNetwork(
              connId, body.getConnectionType(), body.getObjectState(), logicId, networkId);
    } else {
      log.warn(
          "Failed to update Connection " + "Logic:{} Network:{} : Unexpected ConnectionType:{}",
          body.getObjectType());
      return new Response(Response.BAD_REQUEST, "Unexpected ConnectionType.");
    }

    ComponentConnection prev = connectionTable.get(connId);
    String prevConnState = null;
    if (prev != null) {
      prevConnState = prev.getProperty(ComponentConnection.CONNECTION_STATE);
    }
    String connState = curr.getProperty(ComponentConnection.CONNECTION_STATE);

    // create new connection
    if (!connectionTable.containsKey(connId)) {
      if (curr == null || connState.equals(ComponentConnection.State.INITIALIZING)) {
        curr.setConnectionState(ComponentConnection.State.INITIALIZING);
        connectionTable.put(connId, curr);

        componentConnectionChanged(ComponentConnectionChanged.Action.add.name(), null, curr);
        log.info(
            "Created Component Connection ID:{} Logic:{} and Network:{}",
            connId,
            logicId,
            networkId);
        return new Response(Response.CREATED, curr);
      } else {
        return new Response(Response.BAD_REQUEST, "Unexpected ComponentConnection.State.");
      }
    }

    // check ConponentConnection.
    if (prev != null
        && prev.equals(curr)
        && prevConnState != null
        && prevConnState.equals(connState)) {
      log.info(
          "No need Update Component Connection ID:{} Logic:{} and Network:{}",
          connId,
          logicId,
          networkId);
      return new Response(Response.OK, curr);
    }

    // update connectionTable
    connectionTable.put(connId, curr);
    log.info(
        "Update Component Connection ID:{} Logic:{} and Network:{} state:{}",
        connId,
        logicId,
        networkId,
        connState);

    // new connection
    if (connState == null) {
      connState = ComponentConnection.State.INITIALIZING;
      curr.setConnectionState(connState);
    }
    // status:"finalizing"
    if (connState.equals(ComponentConnection.State.FINALIZING)) {
      return new Response(Response.OK, curr);
    }
    // status:"finalizing" --> status:"noe"
    if (prevConnState != null
        && prevConnState.equals(ComponentConnection.State.FINALIZING)
        && connState.equals(ComponentConnection.State.NONE)) {
      connectionTable.remove(connId);
      return new Response(Response.OK, curr);
    }

    componentConnectionChanged(ComponentConnectionChanged.Action.update.name(), prev, curr);

    log.info(
        "Updated Component Connection ID:{} Logic:{} and Network:{}", connId, logicId, networkId);
    return new Response(Response.OK, curr);
  }