/**
   * sets up a gmpls connection through the gmpls web service.
   *
   * @param gmplsConnection connection to be established
   * @return true if connection could be established
   * @throws InvalidRequestFaultException request was not valid
   * @throws SoapFault should not happen
   */
  public static int setUpConnection(final GmplsConnection gmplsConnection) throws SoapFault {
    AJaxbSerializer jserGmpls =
        org.opennaas.extensions.gmpls.serviceinterface.databinding.utils.JaxbSerializer
            .getInstance();
    try {
      gmplsConnection.setStatus(StatusType.SETUP_IN_PROGRESS);
      DbManager.updateStatus(gmplsConnection, "Trying to setup the Connection");
    } catch (UnexpectedFaultException e1) {
      e1.printStackTrace();
    }
    final CreatePathResponse response =
        (CreatePathResponse)
            jserGmpls.elementToObject(
                ContextListener.getGmplsWS()
                    .createPath(jserGmpls.objectToElement(gmplsConnection.getCreatePathRequest())));
    CreatePathResponseType resp = response.getCreatePathResponse();
    gmplsConnection.setPathId(resp.getPathIdentifier().getPathIdentifier());
    gmplsConnection.setStatus(StatusType.ACTIVE);

    DbManager.updatePathId(gmplsConnection);
    DbManager.updateStatus(gmplsConnection, "PathId is " + gmplsConnection.getPathId());
    Notifications.subscribe(gmplsConnection.getPathId());
    return gmplsConnection.getPathId();
  }
  /**
   * Creates a new reservation in the thin Nrps.
   *
   * @param createReservationRequest request
   * @return response for request as CreateReservationResponseType
   * @throws UnexpectedFaultException if reservation is not of type fixed
   * @throws InvalidReservationIDFaultException
   */
  public CreateReservationResponseType createReservation(
      final CreateReservationType createReservationRequest)
      throws UnexpectedFaultException, InvalidReservationIDFaultException {

    CreateReservationResponseType response = new CreateReservationResponseType();

    long jobId = System.currentTimeMillis();
    if (createReservationRequest.isSetJobID()
        && createReservationRequest.getJobID().longValue() > 0) {
      jobId = createReservationRequest.getJobID().longValue();
    }

    long reservationId =
        DbManager.insertReservation(jobId, createReservationRequest.getNotificationConsumerURL());

    boolean success = true;
    boolean partSuccess = false;

    List<GmplsConnection> connections = new ArrayList<GmplsConnection>();

    response.setJobID(Long.valueOf(jobId));
    response.setReservationID(WebserviceUtils.convertReservationID(reservationId));

    for (ServiceConstraintType sct : createReservationRequest.getService()) {
      if (sct.getTypeOfReservation()
          .equals(
              org.opennaas.extensions.idb.serviceinterface.databinding.jaxb.ReservationType
                  .FIXED)) {

        Calendar calStart =
            Helpers.xmlCalendarToCalendar(sct.getFixedReservationConstraints().getStartTime());
        Timestamp startTime = new Timestamp(calStart.getTime().getTime());

        calStart.setTimeZone(SimpleTimeZone.getDefault());
        calStart.add(Calendar.SECOND, sct.getFixedReservationConstraints().getDuration());
        Timestamp endTime = new Timestamp(calStart.getTimeInMillis());
        for (ConnectionConstraintType cct : sct.getConnections()) {
          GmplsConnection con = new GmplsConnection();
          con.setJobId(jobId);
          con.setReservationId(reservationId);
          con.setServiceId(sct.getServiceID());
          con.setConnectionId(cct.getConnectionID());
          con.setSrcTNA(cct.getSource().getEndpointId());
          con.setDestTNA(cct.getTarget().get(0).getEndpointId());
          con.setStartTime(startTime);
          con.setEndTime(endTime);
          con.setBandwidth(cct.getMinBW());
          con.setAutoActivation(sct.isAutomaticActivation());

          try {
            partSuccess = DbManager.insertConnection(con);
          } catch (SourcePortUnavailableException e) {
            partSuccess = false;
            logger.error(e.getMessage(), e);
          } catch (DestinationPortUnavailableException e) {
            partSuccess = false;
            logger.error(e.getMessage(), e);
          } catch (SourceAndDestinationPortUnavailableException e) {
            partSuccess = false;
            logger.error(e.getMessage(), e);
          } catch (PathNotFoundException e) {
            partSuccess = false;
            logger.error(e.getMessage(), e);
          }

          if (partSuccess) {
            connections.add(con);
          }

          success &= partSuccess;
        }
      } else {
        DbManager.deleteWholeReservation(reservationId);
        throw new UnexpectedFaultException("Only FIXED ReservationType supported");
      }
    }

    if (!success) {
      logger.debug("No success");
      DbManager.deleteWholeReservation(reservationId);
      throw new UnexpectedFaultException("No Path found.");
    }
    for (GmplsConnection gmplsConnection : connections) {
      try {
        if (gmplsConnection.isAutoActivation()) {

          if (gmplsConnection
                  .getStartTime()
                  .before(Helpers.xmlCalendarToDate(Helpers.generateXMLCalendar(0, 0)))
              && gmplsConnection
                  .getEndTime()
                  .after(Helpers.xmlCalendarToDate(Helpers.generateXMLCalendar()))) {
            if (0 <= setUpConnection(gmplsConnection)) {
              JobManager.getInstance().schedulePathTermination(gmplsConnection);

            } else {
              gmplsConnection.setStatus(StatusType.UNKNOWN);
              DbManager.updateStatus(
                  gmplsConnection, "Path could not be set up! PathId returned is 0");
              throw new UnexpectedFaultException("Path could not be set up");
            }
          } else {
            JobManager.getInstance().schedulePathSetUp(gmplsConnection);
            JobManager.getInstance().schedulePathTermination(gmplsConnection);
            gmplsConnection.setStatus(StatusType.PENDING);
            DbManager.updateStatus(gmplsConnection, "Connection has been scheduled");
          }
        }
      } catch (SchedulerException e) {
        gmplsConnection.setStatus(StatusType.UNKNOWN);
        DbManager.updateStatus(gmplsConnection, e.getMessage());
        throw new UnexpectedFaultException(e);
      } catch (InvalidRequestFaultException e) {
        gmplsConnection.setStatus(StatusType.UNKNOWN);
        DbManager.updateStatus(gmplsConnection, e.getMessage());
        throw new UnexpectedFaultException(e);
      } catch (SoapFault e) {
        gmplsConnection.setStatus(StatusType.UNKNOWN);
        DbManager.updateStatus(gmplsConnection, e.getMessage());
        throw new UnexpectedFaultException(e);
      }
    }
    Notifications.addTopic(response.getReservationID());
    return response;
  }