Esempio n. 1
0
  private <N> void addMetricAndAvailInstances(
      ID resourceId,
      ResourceType<L> type,
      L parentLocation,
      N baseNode,
      Resource.Builder<L> builder,
      Session<L> session) {

    for (MetricType<L> metricType : type.getMetricTypes()) {
      AttributeLocation<L> location = metricType.getAttributeLocation();
      try {
        final AttributeLocation<L> instanceLocation =
            session.getLocationResolver().absolutize(parentLocation, location);
        if (session.getDriver().attributeExists(instanceLocation)) {
          ID id =
              InventoryIdUtil.generateMetricInstanceId(session.getFeedId(), resourceId, metricType);
          Name name = metricType.getName();

          MeasurementInstance<L, MetricType<L>> metricInstance =
              new MeasurementInstance<>(id, name, instanceLocation, metricType);
          builder.metric(metricInstance);
        }
      } catch (ProtocolException e) {
        log.warnFailedToLocate(
            e,
            metricType.getClass().getName(),
            String.valueOf(location),
            String.valueOf(parentLocation));
      }
    }

    for (AvailType<L> availType : type.getAvailTypes()) {
      AttributeLocation<L> location = availType.getAttributeLocation();
      try {
        final AttributeLocation<L> instanceLocation =
            session.getLocationResolver().absolutize(parentLocation, location);
        if (session.getDriver().attributeExists(instanceLocation)) {
          ID id =
              InventoryIdUtil.generateAvailInstanceId(session.getFeedId(), resourceId, availType);
          Name name = availType.getName();

          MeasurementInstance<L, AvailType<L>> availInstance =
              new MeasurementInstance<>(id, name, instanceLocation, availType);
          builder.avail(availInstance);
        }
      } catch (ProtocolException e) {
        log.warnFailedToLocate(
            e,
            availType.getClass().getName(),
            String.valueOf(location),
            String.valueOf(parentLocation));
      }
    }
  }
Esempio n. 2
0
  /**
   * Discovers children of the given type underneath the given parent.
   *
   * @param parent look under this resource to find its children (if null, this looks for root
   *     resources)
   * @param childType only find children of this type
   * @param session session used to query the managed endpoint
   * @param resourceConsumer if not null, will be a listener that gets notified when resources are
   *     discovered
   */
  public <N> void discoverChildren(
      Resource<L> parent,
      ResourceType<L> childType,
      Session<L> session,
      Consumer<Resource<L>> resourceConsumer) {

    try {

      L parentLocation = parent != null ? parent.getLocation() : null;
      log.debugf("Discovering children of [%s] of type [%s]", parent, childType);
      final L childQuery =
          session.getLocationResolver().absolutize(parentLocation, childType.getLocation());
      Map<L, N> nativeResources = session.getDriver().fetchNodes(childQuery);

      for (Map.Entry<L, N> entry : nativeResources.entrySet()) {
        L location = entry.getKey(); // this is the unique DMR address for this resource
        String resourceName =
            session
                .getLocationResolver()
                .applyTemplate(
                    childType.getResourceNameTemplate(), location, session.getEndpoint().getName());
        ID id = InventoryIdUtil.generateResourceId(session.getEndpoint(), location.toString());
        Builder<L> builder =
            Resource.<L>builder()
                .id(id)
                .name(new Name(resourceName))
                .location(location)
                .type(childType);

        if (parent != null) {
          builder.parent(parent);
        }

        // get the configuration of the resource
        discoverResourceConfiguration(id, childType, location, entry.getValue(), builder, session);

        // populate the metrics/avails based on the resource's type
        addMetricAndAvailInstances(id, childType, location, entry.getValue(), builder, session);

        Resource<L> resource = builder.build();
        log.debugf("Discovered resource [%s]", resource);
        if (resourceConsumer != null) {
          resourceConsumer.accept(resource);
        }

        // recursively discover children of child types
        Set<ResourceType<L>> childTypes = session.getResourceTypeManager().getChildren(childType);
        for (ResourceType<L> nextLevelChildType : childTypes) {
          discoverChildren(resource, nextLevelChildType, session, resourceConsumer);
        }
      }
    } catch (Exception e) {
      log.errorFailedToDiscoverResources(e, session.getEndpoint());
      resourceConsumer.report(e);
    }
  }