/* ****************************************************************************
*
* postSubscribeContext - 
*/
std::string postSubscribeContext(ConnectionInfo* ciP, int components, std::vector<std::string>& compV, ParseData* parseDataP)
{
  SubscribeContextResponse  scr;
  std::string               answer;

  // FIXME P6: by the moment, we are assuming that notification will be sent in the same format than the one
  // used to do the subscription, so we are passing ciP->inFomat. This is just an heuristic, the client could want
  // for example to use XML in the subscription message but wants notifications in JSON. We need a more
  // flexible approach, to be implemented
  ciP->httpStatusCode = mongoSubscribeContext(&parseDataP->scr.res, &scr, ciP->tenant, ciP->uriParam);
  answer = scr.render(SubscribeContext, ciP->outFormat, "");

  return answer;
}
/* ****************************************************************************
*
* SubscribeContextRequest::check - 
*/
std::string SubscribeContextRequest::check(RequestType requestType, Format format, std::string indent, std::string predetectedError, int counter)
{
  SubscribeContextResponse response;
  std::string              res;

  /* First, check optional fields only in the case they are present */
  /* Second, check the other (mandatory) fields */

  if (((res = entityIdVector.check(SubscribeContext, format, indent, predetectedError, counter))        != "OK") ||
      ((res = attributeList.check(SubscribeContext, format, indent, predetectedError, counter))         != "OK") ||      
      ((res = duration.check(SubscribeContext, format, indent, predetectedError, counter))              != "OK") ||
      ((res = restriction.check(SubscribeContext, format, indent, predetectedError, restrictions))      != "OK") ||
      ((res = notifyConditionVector.check(SubscribeContext, format, indent, predetectedError, counter)) != "OK") ||
      ((res = throttling.check(SubscribeContext, format, indent, predetectedError, counter))            != "OK"))
  {
    response.subscribeError.errorCode.fill(SccBadRequest, std::string("invalid payload: ") + res);
    return response.render(SubscribeContext, format, indent);
  }

  return "OK";
}
/* ****************************************************************************
*
* postSubscribeContext - 
*
* POST /v1/subscribeContext
* POST /ngsi10/subscribeContext
*
* Payload In:  SubscribeContextRequest
* Payload Out: SubscribeContextResponse
*
* URI parameters
*   - notifyFormat=XXX    (used by mongoBackend)
*/
std::string postSubscribeContext
(
  ConnectionInfo*            ciP,
  int                        components,
  std::vector<std::string>&  compV,
  ParseData*                 parseDataP
)
{
  SubscribeContextResponse  scr;
  std::string               answer;

  //
  // FIXME P0: Only *one* service path is allowed for subscriptions.
  //           Personally (kz) I kind of like that. If you want additional service-paths, just add another subscription!
  //           However, we need to at least state that HERE is where we limit the number of service paths to *one*.
  //
  if (ciP->servicePathV.size() > 1)
  {
    char  noOfV[STRING_SIZE_FOR_INT];
    snprintf(noOfV, sizeof(noOfV), "%lu", ciP->servicePathV.size());
    std::string details = std::string("max *one* service-path allowed for subscriptions (") + noOfV + " given";

    alarmMgr.badInput(clientIp, details);

    scr.subscribeError.errorCode.fill(SccBadRequest, "max one service-path allowed for subscriptions");

    TIMED_RENDER(answer = scr.render(SubscribeContext, ""));
    return answer;
  }

  TIMED_MONGO(ciP->httpStatusCode = mongoSubscribeContext(&parseDataP->scr.res, &scr, ciP->tenant, ciP->uriParam, ciP->httpHeaders.xauthToken, ciP->servicePathV));
  TIMED_RENDER(answer = scr.render(SubscribeContext, ""));

  parseDataP->scr.res.release();

  return answer;
}