public MeasureDescriptor getGenericMeasureDescriptor(
      String orgName,
      String vdcName,
      List<String> vappNames,
      String measureId,
      String hwItem,
      String networkName)
      throws MonitorException {

    String[] elementHref = getElementAndHref(orgName, vdcName, vappNames, hwItem, networkName);
    System.out.println(elementHref[0]);

    nodedirectory nd = db.get(nodedirectory.class, elementHref[0]);

    if ((measureId.contains("netInput_") || measureId.contains("netOutput_"))
        && nd.getTipo() != nodedirectory.TYPE_NIC) {
      return new MeasureDescriptor(
          measureId,
          nd.getFqnString() + "." + URICreation.FQN_SEPARATOR_MEASURE + "." + measureId,
          "",
          DataTypesUtils.STANDARD_BANDWIDTH_UNIT_DEFAULT,
          "0",
          "0",
          "");
    } else if (measureId.equals("diskUsage")) {
      return db.getDiskMeasureDescriptor(nd);

    } else {
      MeasureDescriptor result = db.getMeasureDescriptors(nd, measureId);

      if (result == null) throw new MonitorException("Measure " + measureId + " not available.");

      result.setHref(elementHref[1]);

      return result;
    }
  }
  public MeasuredValueList getMeasuredValueListRest(
      MeasureDescriptor md, MeasuredValueFilter filter) throws MonitorException {

    // Find the data to create the URL
    String url;

    nodedirectory nd =
        db.get(
            nodedirectory.class,
            md.getMeasurementTypeId().substring(0, md.getMeasurementTypeId().indexOf(".measure")));

    VEEReplica rep;

    if (nd == null
        || (nd.getTipo() != nodedirectory.TYPE_REPLICA && nd.getTipo() != nodedirectory.TYPE_NIC))
      return new MeasuredValueList(md);

    String path;

    if (nd.getTipo() == nodedirectory.TYPE_REPLICA) {
      path = URICreation.getURIVEEReplica(md.getMeasurementTypeId());
      rep = db.get(VEEReplica.class, nd.getInternalNodeId());
    } else if (nd.getTipo() == nodedirectory.TYPE_NIC) {
      path = URICreation.getURIHwItem(md.getMeasurementTypeId());
      path = translateNetInfrastructureId(path, nd.getFqnString());
      rep =
          db.get(
              VEEReplica.class,
              nd.getFqnString().substring(0, md.getMeasurementTypeId().indexOf(".network")));
    } else {
      return new MeasuredValueList(md);
    }

    if (rep.getDeployedOn() == null) return new MeasuredValueList(md);
    else {
      url =
          "http://"
              + rep.getDeployedOn().getHost()
              + ":"
              + rep.getDeployedOn().getPort()
              + path
              + URICreation.MONITOR
              + "/"
              + md.getName()
              + URICreation.VALUES;

      // Get the measures
      return restClient.getReplicaMeasure(url, filter, md);
    }
  }
  public MeasuredValueList getMeasuredValueListBD(MeasureDescriptor md, MeasuredValueFilter filter)
      throws MonitorException {
    String id = md.getMeasurementTypeId();
    String element = id.substring(0, id.indexOf(URICreation.FQN_SEPARATOR_MEASURE) - 1);

    nodedirectory nd = db.get(nodedirectory.class, element);
    List<MeasuredValue> valueList;

    if ((md.getName().contains("netInput_") || md.getName().contains("netOutput_"))
        && nd.getTipo() != nodedirectory.TYPE_NIC) {
      valueList =
          db.getNetworkValues(
              nd,
              filter.getFrom(),
              filter.getTo(),
              filter.getSamples(),
              filter.getInterval(),
              md.getName(),
              md.getValueType());
    } else {
      valueList =
          db.getMeasureValues(
              nd,
              filter.getFrom(),
              filter.getTo(),
              filter.getSamples(),
              filter.getInterval(),
              md.getName(),
              md.getValueType());
    }

    MeasuredValueList mvl = new MeasuredValueList(md);

    for (MeasuredValue mv : valueList) {
      mvl.add(mv);
    }

    return mvl;
  }
  public MeasuredValueList getMeasuredValueListJMS(MeasureDescriptor md, MeasuredValueFilter filter)
      throws MonitorException {

    try {
      SMIChannelEvent smi = new SMIChannelEvent(0, 0, SMIAction.GET_MEASURE_VALUE);

      Long seqNumber = ClothoUtils.getSecuenceNumber();
      smi.put(SMIChannelEvent.SEQUENCE_NUMBER, seqNumber.toString());

      String id = md.getMeasurementTypeId();

      smi.put(
          SMIChannelEvent.FQN_ID,
          id.substring(0, id.indexOf(URICreation.FQN_SEPARATOR_MEASURE) - 1));

      smi.put(SMIChannelEvent.MEASURE_NAME, md.getName());
      smi.put(SMIChannelEvent.MEASURE_TYPE_ID, md.getMeasurementTypeId());
      smi.put(
          SMIChannelEvent.MEASURE_FILTER_FROM,
          (filter.getFrom() == null)
              ? null
              : DataTypesUtils.date2String(filter.getFrom().getTime()));
      smi.put(
          SMIChannelEvent.MEASURE_FILTER_TO,
          (filter.getTo() == null) ? null : DataTypesUtils.date2String(filter.getTo().getTime()));
      smi.put(SMIChannelEvent.MEASURE_FILTER_NUMBER, String.valueOf(filter.getSamples()));
      smi.put(SMIChannelEvent.MEASURE_FILTER_INTERVAL, String.valueOf(filter.getInterval()));

      jmsConnectionData.smiChannelProducer.send(jmsConnectionData.session.createObjectMessage(smi));

      MessageConsumer messageConsumer =
          jmsConnectionData.session.createConsumer(
              jmsConnectionData.smiChannelReply,
              "element = '"
                  + md.getMeasurementTypeId()
                  + "' AND "
                  + SMIChannelEvent.SEQUENCE_NUMBER
                  + "='"
                  + seqNumber
                  + "'");
      ObjectMessage m = (ObjectMessage) messageConsumer.receive(ClothoUtils.EVENT_BUS_TIMEOUT);

      if (m == null) {
        log.error("Timeout waiting for a server response. Try later.");

        throw new MonitorException("Timeout waiting for a server response. Try later.");
      }

      smi = (SMIChannelEvent) m.getObject();
      Map<String, String> values =
          (HashMap<String, String>) smi.getSerializable(SMIChannelEvent.MEASURE_VALUES);
      String units = smi.get(SMIChannelEvent.MEASURE_UNITS);

      MeasuredValueList mvl = new MeasuredValueList();
      mvl.setHref(md.getHref() + URICreation.VALUES);

      for (String measureDate : values.keySet()) {
        MeasuredValue mv = new MeasuredValue();

        mv.setUnit(units);
        mv.setValue(values.get(measureDate));

        try {
          mv.setRegisterDate(DataTypesUtils.string2Date(measureDate));
        } catch (ParseException e) {
          log.error("SMDeploymentDriver: Wrong date format received: " + measureDate);
          continue;
        }

        mvl.add(mv);
      }

      return mvl;

    } catch (JMSException e) {
      log.error(
          "SMDeploymentDriver: Communication error accessing the SLM. Impossible to retrieve service data.",
          e);
      throw new MonitorException(
          "SMDeploymentDriver: Communication error accessing the SLM. Impossible to retrieve service data."
              + e.getMessage());
    }
  }
  public MeasureDescriptorList getGenericMeasureDescriptorList(
      String orgName, String vdcName, List<String> vappNames, String item, String networkName)
      throws MonitorException {

    String[] elementHref = getElementAndHref(orgName, vdcName, vappNames, item, networkName);

    MeasureDescriptorList result = new MeasureDescriptorList(elementHref[1], "");
    System.out.println(result.getHref());
    nodedirectory nd = db.get(nodedirectory.class, elementHref[0]);

    if (nd == null) return result;

    List<MeasureDescriptor> descriptorList = db.getMeasureDescriptorsList(nd);

    for (MeasureDescriptor md : descriptorList) {
      md.setHref(elementHref[1] + md.getName());
      result.add(md);
    }

    if (nd.getTipo() == nodedirectory.TYPE_VDC) {
      VDC vdc = db.get(VDC.class, nd.getFqnString());

      for (Zone z : vdc.getZones())
        for (Network network : z.getNetworks()) {
          result.add(
              new MeasureDescriptor(
                  "netInput_" + network.getName(),
                  nd.getFqnString()
                      + "."
                      + URICreation.FQN_SEPARATOR_MEASURE
                      + "."
                      + "netInput_"
                      + network.getName(),
                  "",
                  DataTypesUtils.STANDARD_BANDWIDTH_UNIT_DEFAULT,
                  "0",
                  "0",
                  ""));
          result.add(
              new MeasureDescriptor(
                  "netOutput_" + network.getName(),
                  nd.getFqnString()
                      + "."
                      + URICreation.FQN_SEPARATOR_MEASURE
                      + "."
                      + "netOutput_"
                      + network.getName(),
                  "",
                  DataTypesUtils.STANDARD_BANDWIDTH_UNIT_DEFAULT,
                  "0",
                  "0",
                  ""));
        }

    } else if (nd.getTipo() == nodedirectory.TYPE_SERVICE) {

      ServiceApplication sap = db.getServiceApplication(nd.getFqnString());

      for (Network network : sap.getNetworks()) {
        result.add(
            new MeasureDescriptor(
                "netInput_" + network.getName(),
                nd.getFqnString()
                    + "."
                    + URICreation.FQN_SEPARATOR_MEASURE
                    + "."
                    + "netInput_"
                    + network.getName(),
                "",
                DataTypesUtils.STANDARD_BANDWIDTH_UNIT_DEFAULT,
                "0",
                "0",
                ""));
        result.add(
            new MeasureDescriptor(
                "netOutput_" + network.getName(),
                nd.getFqnString()
                    + "."
                    + URICreation.FQN_SEPARATOR_MEASURE
                    + "."
                    + "netOutput_"
                    + network.getName(),
                "",
                DataTypesUtils.STANDARD_BANDWIDTH_UNIT_DEFAULT,
                "0",
                "0",
                ""));
      }
    }

    return result;
  }