@Override
  public OMElement prepareResponseOMElement()
      throws InvalidInputException, RequestHandlerException, OperationFailedException, Exception {
    OMElement response = null;
    OMElement selectedNode = null;
    ServiceUtilities serviceUtil = new ServiceUtilities();
    String siteId = null;

    // siteId
    if ((selectedNode =
            serviceUtil.getNode(
                this.inputMsg,
                NETWORK_MONITORING_SERVICE_NS,
                nsPrefix,
                QueryEndpointsRequest.getSiteIDXPath(namespace)))
        != null) {
      siteId = selectedNode.getText().trim();
    } else {
      Object[] args = new Object[1];
      args[0] = RequestResponseConstants.RAW_SITEID;
      throw new InvalidInputException(
          DracErrorConstants.WS_MISSING_PARAMETER_IN_REQUEST_MESSAGE, args);
    }

    List<EndpointResourceUiType> endpoints = rh.getEndpointsForSiteId(token, siteId);

    if (nsPrefix.equals("xmlns")) {
      nsPrefix = "";
    }
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace ns = fac.createOMNamespace(NETWORK_MONITORING_SERVICE_NS, nsPrefix);
    response = fac.createOMElement(RequestResponseConstants.RAW_QUERY_ENDPOINTS_RESPONSE, ns);
    OMElement numOfElements =
        fac.createOMElement(RequestResponseConstants.RAW_NUM_OF_ELEMENTS_INCLUDED, ns, response);
    OMElement totalNumOfElements =
        fac.createOMElement(
            RequestResponseConstants.RAW_TOTAL_NUM_OF_MATCHING_ELEMENTS, ns, response);

    // Dead code
    // if (endpoints == null) {
    // numOfElements.setText("0");
    // totalNumOfElements.setText("0");
    // return response;
    // }
    if (endpoints.isEmpty()) {
      numOfElements.setText("0");
      totalNumOfElements.setText("0");
      return response;
    }
    boolean reducedList = true;
    totalNumOfElements.setText(new Integer(endpoints.size()).toString());
    if (endpoints.size() < CommonConstants.MAX_NUM_OF_ENDPOINTS_TO_BE_RETRIEVED) {
      numOfElements.setText(new Integer(endpoints.size()).toString());
      reducedList = false;
    } else {
      numOfElements.setText(
          new Integer(CommonConstants.MAX_NUM_OF_ENDPOINTS_TO_BE_RETRIEVED).toString());
    }
    int i = 0;
    for (EndpointResourceUiType endpoint : endpoints) {

      OMElement tna = fac.createOMElement(RequestResponseConstants.RAW_TNA, ns, response);
      tna.setText(endpoint.getTna());

      i++;
      if (reducedList && i == CommonConstants.MAX_NUM_OF_ENDPOINTS_TO_BE_RETRIEVED) {
        break;
      }
    }
    return response;
  }
Example #2
0
  public static Schedule create(LoginToken token, Locale locale, CreateScheduleForm form)
      throws Exception {

    RequestHandler rh = RequestHandler.INSTANCE;

    String userID = token.getUser();
    String srcChannel = "-1";
    String destChannel = "-1";

    /* Instantiating ScheduleType Object */
    // ScheduleType sch = new ScheduleType();

    /* Retrieving values from CreateScheduleForm Java bean */
    String srctna = form.getSrcTna();
    String desttna = form.getDestTna();
    String rate = form.getRate();
    String startDate = form.getStartdate();
    log.debug("Start date: " + startDate);
    String endDate = form.getEnddate();
    log.debug("End date: " + endDate);
    String startTime = form.getStartTime();
    log.debug("Start time: " + startTime);
    String endTime = form.getEndTime();
    log.debug("End time: " + endTime);
    String schName = form.getSchName();
    String email = form.getEmail();
    boolean recurrence = form.isRecurrence();
    boolean startNow = form.isStartNow();
    boolean endNever = form.isEndNever();
    srcChannel = form.getSourceChannel();
    destChannel = form.getDestChannel();
    String billingGroup = form.getBillingGroup();
    String recEndByDate = form.getRecEndDate();
    log.debug("Rec End by date: " + recEndByDate);
    String srcGroup = form.getSrcGroup();
    String destGroup = form.getDestGroup();
    String srcResGroup = form.getSrcResGroup();
    String destResGroup = form.getDestResGroup();
    long duration = form.getDuration() * 60 * 1000;
    String protectionType = form.getProtectionType();
    String srlg = form.getSrlg().trim();
    String srsg = form.getSrsg().trim();
    String algorithm = form.getAlgorithm();
    String metric = form.getRoutingMetric();
    String activationType = form.getScheduleType();
    int metricValue = form.getMetricValue();
    String srcVlan = form.getSrcVlan();
    String dstVlan = form.getDstVlan();
    boolean vcatRoutingOption = form.isVcatRoutingOption();

    int rateVal = 0;
    TimeZone tz = DracHelper.getTimeZone(token);

    try {
      rateVal = Integer.parseInt(rate);
    } catch (NumberFormatException nfe) {
      log.error("ScheduleHelper--Rate value is not a number: " + rate, nfe);
    }

    log.debug("ScheduleHelper--Retrieved the values from Create form");

    // remove
    Schedule.ACTIVATION_TYPE type = null;
    try {
      type = Schedule.ACTIVATION_TYPE.valueOf(activationType);
    } catch (Exception e) {
      log.error("Error: ", e);
      type = ACTIVATION_TYPE.RESERVATION_AUTOMATIC;
    }

    PathType path = new PathType();
    try {
      path.setProtectionType(PROTECTION_TYPE.valueOf(protectionType));
    } catch (IllegalArgumentException e) {
      log.error("No such protection type: " + protectionType + ". Setting to unprotected!!", e);
    }

    EndPointType sourceEndpoint = rh.findEndpointByTna(token, srctna);
    if (sourceEndpoint != null) {
      path.setSourceEndPoint(sourceEndpoint);
      path.setSource(sourceEndpoint.getNode());
    } else {
      log.warn("Source endpoint " + srctna + " not found, using string values");
      sourceEndpoint = new EndPointType();
      sourceEndpoint.setName(srctna);
      path.setSourceEndPoint(sourceEndpoint);
    }
    int channel = -1;
    if (!"".equals(srcChannel)) {
      try {
        channel = Integer.parseInt(srcChannel);
      } catch (NumberFormatException nfe) {
        log.warn("source channel not a number", nfe);
      }
    }

    path.getSourceEndPoint().setChannelNumber(channel);
    path.setSrcVlanId(srcVlan);

    EndPointType targetEndpoint = rh.findEndpointByTna(token, desttna);
    if (targetEndpoint != null) {
      path.setTargetEndPoint(targetEndpoint);
      path.setTarget(targetEndpoint.getNode());
    } else {
      log.warn("Target endpoint " + desttna + " not found, using string values");
      targetEndpoint = new EndPointType();
      targetEndpoint.setName(desttna);
      path.setTargetEndPoint(targetEndpoint);
    }

    channel = -1;
    if (!"".equals(destChannel)) {
      try {
        channel = Integer.parseInt(destChannel);
      } catch (NumberFormatException nfe) {
        log.warn("destChannel channel not a number", nfe);
      }
    }
    path.getTargetEndPoint().setChannelNumber(channel);
    path.setDstVlanId(dstVlan);

    path.setRate(rateVal);
    path.setSrlg(srlg);

    if (CreateScheduleForm.CSPF_ALG.equals(algorithm)) {
      if (CreateScheduleForm.COST_METRIC.equals(metric)) {
        path.setCost(metricValue);
      } else if (CreateScheduleForm.HOP_METRIC.equals(metric)) {
        path.setHop(metricValue);
      } else if (CreateScheduleForm.METRIC2_METRIC.equals(metric)) {
        path.setMetric(metricValue);
      }
    }
    path.setSharedRiskServiceGroup(srsg);

    path.setVcatRoutingOption(vcatRoutingOption);

    UserType userType =
        new UserType(
            userID,
            new UserGroupName(billingGroup),
            srcGroup,
            destGroup,
            srcResGroup,
            destResGroup,
            email);

    /* Assigning values to Schedule Object */

    long scheduleStartTime = 0;
    if (!startNow) {
      if (startTime != null && startDate != null) {
        scheduleStartTime = DracHelper.parseWebDateToMillis(locale, tz, startDate, startTime);
      }
    } else {
      scheduleStartTime = new Date().getTime();
    }

    Long scheduleEndTime = null;
    if (!endNever) {
      if (endTime != null && endDate != null) {
        scheduleEndTime =
            Long.valueOf(DracHelper.parseWebDateToMillis(locale, tz, endDate, endTime));
      }
    } else {
      Calendar cal = Calendar.getInstance();
      cal.set(2999, 11, 31, 0, 0);
      scheduleEndTime = Long.valueOf(cal.getTimeInMillis());
      // somehow the end never button trashes the duration value by setting it to 0 in most cases.
      duration = scheduleEndTime.longValue() - scheduleStartTime;
    }

    // sanity check
    if (duration != scheduleEndTime.longValue() - scheduleStartTime) {
      log.warn("Duration of service does not match start/end time");
    }

    RecurrenceType rec;
    if (recurrence) {
      // the end time of the schedule is really the end time of the last service
      long startOfLastService =
          DracHelper.parseWebDateToMillis(locale, tz, recEndByDate, startTime);

      long finalEndTime = startOfLastService + duration;

      scheduleEndTime = Long.valueOf(finalEndTime);

      RecurrenceFreq freq = RecurrenceFreq.parseString(form.getFrequency());
      if (RecurrenceFreq.FREQ_WEEKLY.equals(freq)) {
        List<Integer> weekdayList = new ArrayList<Integer>();
        if (form.isWeeklySun()) {
          weekdayList.add(Integer.valueOf(Calendar.SUNDAY));
        }
        if (form.isWeeklyMon()) {
          weekdayList.add(Integer.valueOf(Calendar.MONDAY));
        }
        if (form.isWeeklyTue()) {
          weekdayList.add(Integer.valueOf(Calendar.TUESDAY));
        }
        if (form.isWeeklyWed()) {
          weekdayList.add(Integer.valueOf(Calendar.WEDNESDAY));
        }
        if (form.isWeeklyThu()) {
          weekdayList.add(Integer.valueOf(Calendar.THURSDAY));
        }
        if (form.isWeeklyFri()) {
          weekdayList.add(Integer.valueOf(Calendar.FRIDAY));
        }
        if (form.isWeeklySat()) {
          weekdayList.add(Integer.valueOf(Calendar.SATURDAY));
        }
        int[] weekdays = new int[weekdayList.size()];
        for (int i = 0; i < weekdays.length; i++) {
          weekdays[i] = weekdayList.get(i).intValue();
        }
        rec = new RecurrenceType(freq, 0, 0, weekdays);

      } else if (RecurrenceFreq.FREQ_MONTHLY.equals(freq)) {
        String monthlyDay = form.getMonthlyDay();
        int monthDay = 0;
        try {
          monthDay = Integer.parseInt(monthlyDay);
        } catch (NumberFormatException nfe) {
          log.error("ScheduleHelper--MonthlyDay value is not a number: " + monthlyDay, nfe);
        }
        rec = new RecurrenceType(freq, 0, monthDay, null);
      } else if (RecurrenceFreq.FREQ_YEARLY.equals(freq)) {
        String yearlyDay = form.getYearlyDay();
        String yearlyMonth = form.getYearlyMonth();
        int yearDay = 0;
        int yearMonth = 0;
        try {
          yearDay = Integer.parseInt(yearlyDay);
        } catch (NumberFormatException nfe) {
          log.error("ScheduleHelper--yearDay value is not a number: " + yearlyDay, nfe);
        }
        try {
          yearMonth = Integer.parseInt(yearlyMonth);
        } catch (NumberFormatException nfe) {
          log.error("ScheduleHelper--yearMonth value is not a number: " + yearlyMonth, nfe);
        }

        rec = new RecurrenceType(freq, yearDay, yearMonth, null);
      } else if (RecurrenceFreq.FREQ_DAILY.equals(freq)) {

        rec = new RecurrenceType(RecurrenceFreq.FREQ_DAILY, 0, 0, null);
      } else {
        // What kind of recurrence is this??
        log.error("ScheduleHelper--Recurrence Type is not valid: " + freq);
        throw new Exception("ScheduleHelper--Recurrence Type is not valid: " + freq);
      }
    } else {
      rec = new RecurrenceType(RecurrenceFreq.FREQ_ONCE, 0, 0, null);
    }

    /* Calling CreateSchedule Function */
    String schID = null;
    log.debug("Calling CreateSchedule");

    Schedule sch =
        new Schedule(
            type,
            "unknown",
            schName,
            SCHEDULE.EXECUTION_PENDING,
            scheduleStartTime,
            scheduleEndTime,
            duration,
            userType,
            path,
            recurrence,
            rec,
            null);

    ScheduleHelper.debugSchedule(sch);

    log.debug("Creating schedule " + sch.toDebugString());
    schID = rh.createScheduleAsync(token, sch);
    log.debug("Generated Schedule Id:" + schID);

    /*
     * @TODO: This is wrong, we are setting the ID as the task number, not the
     * actual schedule id!
     */
    sch.setId(schID);

    return sch;
  }