Search in sources :

Example 16 with OnmsAttribute

use of org.opennms.netmgt.model.OnmsAttribute in project opennms by OpenNMS.

the class NewtsResourceStorageDaoTest method getAttributes.

@Test
public void getAttributes() {
    // Attributes are empty when the resource does not exist
    replay();
    assertEquals(0, m_nrs.getAttributes(ResourcePath.get("should", "not", "exist")).size());
    verify();
    // Metrics from all buckets should be present
    index(ResourcePath.get("a", "bucket1"), Sets.newHashSet("metric11", "metric12"));
    index(ResourcePath.get("a", "bucket2"), Sets.newHashSet("metric21", "metric22"));
    replay();
    Set<OnmsAttribute> attributes = m_nrs.getAttributes(ResourcePath.get("a"));
    assertEquals(4, attributes.size());
    // Verify the properties of a specific attribute
    RrdGraphAttribute metric11 = null;
    for (OnmsAttribute attribute : attributes) {
        if (attribute instanceof RrdGraphAttribute) {
            RrdGraphAttribute graphAttribute = (RrdGraphAttribute) attribute;
            if ("metric11".equals(graphAttribute.getName())) {
                metric11 = graphAttribute;
            }
        }
    }
    assertNotNull(metric11);
    verify();
}
Also used : OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Test(org.junit.Test)

Example 17 with OnmsAttribute

use of org.opennms.netmgt.model.OnmsAttribute in project opennms by OpenNMS.

the class GenericIndexResourceType method getResourceByPath.

public OnmsResource getResourceByPath(final ResourcePath path, final OnmsResource parent) {
    final Set<OnmsAttribute> set = new LazySet<OnmsAttribute>(new LazyResourceAttributeLoader(m_resourceStorageDao, path));
    final String index = path.getName();
    String label;
    if (m_resourceLabelExpression == null) {
        label = index;
    } else {
        SymbolTable symbolTable = new SymbolTable() {

            private int lastN;

            private boolean lastNSet = false;

            @Override
            public String getSymbolValue(String symbol) {
                if (symbol.equals("index")) {
                    return index;
                }
                Matcher subIndexMatcher = SUB_INDEX_PATTERN.matcher(symbol);
                if (subIndexMatcher.matches()) {
                    Matcher subIndexArgumentsMatcher = SUB_INDEX_ARGUMENTS_PATTERN.matcher(subIndexMatcher.group(1));
                    if (!subIndexArgumentsMatcher.matches()) {
                        // Invalid arguments
                        return null;
                    }
                    List<String> indexElements = tokenizeIndex(index);
                    int start;
                    int offset;
                    if ("n".equals(subIndexArgumentsMatcher.group(1)) && lastNSet) {
                        start = lastN;
                        lastNSet = false;
                    } else if ("n".equals(subIndexArgumentsMatcher.group(1))) {
                        // Invalid use of "n" when lastN is not set
                        return null;
                    } else {
                        offset = Integer.parseInt(subIndexArgumentsMatcher.group(1));
                        if (offset < 0) {
                            start = indexElements.size() + offset;
                        } else {
                            start = offset;
                        }
                    }
                    int end;
                    if ("n".equals(subIndexArgumentsMatcher.group(2))) {
                        end = start + Integer.parseInt(indexElements.get(start)) + 1;
                        start++;
                        lastN = end;
                        lastNSet = true;
                    } else {
                        if (subIndexArgumentsMatcher.group(2) == null) {
                            end = indexElements.size();
                        } else {
                            end = start + Integer.parseInt(subIndexArgumentsMatcher.group(2));
                        }
                    }
                    if (start < 0 || start >= indexElements.size()) {
                        // Bogus index start
                        return null;
                    }
                    if (end < 0 || end > indexElements.size()) {
                        // Bogus index end
                        return null;
                    }
                    StringBuffer indexSubString = new StringBuffer();
                    for (int i = start; i < end; i++) {
                        if (indexSubString.length() != 0) {
                            indexSubString.append(".");
                        }
                        indexSubString.append(indexElements.get(i));
                    }
                    return indexSubString.toString();
                }
                Matcher hexMatcher = HEX_PATTERN.matcher(symbol);
                if (hexMatcher.matches()) {
                    String subSymbol = getSymbolValue(hexMatcher.group(1));
                    List<String> indexElements = tokenizeIndex(subSymbol);
                    StringBuffer hexString = new StringBuffer();
                    for (String indexElement : indexElements) {
                        if (hexString.length() > 0) {
                            hexString.append(":");
                        }
                        try {
                            hexString.append(String.format("%02X", Integer.parseInt(indexElement)));
                        } catch (NumberFormatException e) {
                            return null;
                        }
                    }
                    return hexString.toString();
                }
                Matcher stringMatcher = STRING_PATTERN.matcher(symbol);
                if (stringMatcher.matches()) {
                    String subSymbol = getSymbolValue(stringMatcher.group(1));
                    List<String> indexElements = tokenizeIndex(subSymbol);
                    StringBuffer stringString = new StringBuffer();
                    for (String indexElement : indexElements) {
                        stringString.append(String.format("%c", Integer.parseInt(indexElement)));
                    }
                    return stringString.toString();
                }
                for (OnmsAttribute attr : set) {
                    if (symbol.equals(attr.getName())) {
                        if (StringPropertyAttribute.class.isAssignableFrom(attr.getClass())) {
                            StringPropertyAttribute stringAttr = (StringPropertyAttribute) attr;
                            return stringAttr.getValue();
                        }
                        if (ExternalValueAttribute.class.isAssignableFrom(attr.getClass())) {
                            ExternalValueAttribute extAttr = (ExternalValueAttribute) attr;
                            return extAttr.getValue();
                        }
                    }
                }
                return null;
            }

            private List<String> tokenizeIndex(final String index) {
                List<String> indexElements = new ArrayList<String>();
                StringTokenizer t = new StringTokenizer(index, ".");
                while (t.hasMoreTokens()) {
                    indexElements.add(t.nextToken());
                }
                return indexElements;
            }
        };
        label = PropertiesUtils.substitute(m_resourceLabelExpression, symbolTable);
    }
    final OnmsResource resource = new OnmsResource(index, label, this, set, path);
    resource.setParent(parent);
    return resource;
}
Also used : LazySet(org.opennms.core.utils.LazySet) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) SymbolTable(org.opennms.core.utils.PropertiesUtils.SymbolTable) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) StringPropertyAttribute(org.opennms.netmgt.model.StringPropertyAttribute) StringTokenizer(java.util.StringTokenizer) OnmsResource(org.opennms.netmgt.model.OnmsResource) ExternalValueAttribute(org.opennms.netmgt.model.ExternalValueAttribute)

Example 18 with OnmsAttribute

use of org.opennms.netmgt.model.OnmsAttribute in project opennms by OpenNMS.

the class InterfaceSnmpResourceType method getResourceByParentPathAndInterface.

private OnmsResource getResourceByParentPathAndInterface(ResourcePath parent, String intf) {
    final ResourcePath path = ResourcePath.get(parent, intf);
    final LazyResourceAttributeLoader loader = new LazyResourceAttributeLoader(m_resourceStorageDao, path);
    final Set<OnmsAttribute> set = new LazySet<OnmsAttribute>(loader);
    return new OnmsResource(intf, intf, this, set, path);
}
Also used : LazySet(org.opennms.core.utils.LazySet) ResourcePath(org.opennms.netmgt.model.ResourcePath) OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute)

Example 19 with OnmsAttribute

use of org.opennms.netmgt.model.OnmsAttribute in project opennms by OpenNMS.

the class PropertiesGraphDao method getPrefabGraphsForResource.

/** {@inheritDoc} */
@Override
public PrefabGraph[] getPrefabGraphsForResource(final OnmsResource resource) {
    if (resource == null) {
        LOG.warn("returning empty graph list for resource because it is null");
        return new PrefabGraph[0];
    }
    Set<OnmsAttribute> attributes = new LinkedHashSet<>(resource.getAttributes());
    // Check if there are no attributes
    if (attributes.size() == 0) {
        LOG.debug("returning empty graph list for resource {} because its attribute list is empty", resource);
        return new PrefabGraph[0];
    }
    Set<String> availableRrdAttributes = new LinkedHashSet<>(resource.getRrdGraphAttributes().keySet());
    Set<String> availableStringAttributes = new LinkedHashSet<>(resource.getStringPropertyAttributes().keySet());
    Set<String> availableExternalAttributes = new LinkedHashSet<>(resource.getExternalValueAttributes().keySet());
    // Check if there are no RRD attributes
    if (availableRrdAttributes.size() == 0) {
        LOG.debug("returning empty graph list for resource {} because it has no RRD attributes", resource);
        return new PrefabGraph[0];
    }
    String resourceType = resource.getResourceType().getName();
    Map<String, PrefabGraph> returnList = new LinkedHashMap<String, PrefabGraph>();
    for (PrefabGraph query : getAllPrefabGraphs()) {
        if (resourceType != null && !query.hasMatchingType(resourceType)) {
            LOG.debug("skipping {} because its types \"{}\" does not match resourceType \"{}\"", query.getName(), StringUtils.arrayToDelimitedString(query.getTypes(), ", "), resourceType);
            continue;
        }
        if (!verifyAttributesExist(query, "RRD", Arrays.asList(query.getColumns()), availableRrdAttributes)) {
            continue;
        }
        if (!verifyAttributesExist(query, "string property", Arrays.asList(query.getPropertiesValues()), availableStringAttributes)) {
            continue;
        }
        if (!verifyAttributesExist(query, "external value", Arrays.asList(query.getExternalValues()), availableExternalAttributes)) {
            continue;
        }
        LOG.debug("adding {} to query list", query.getName());
        returnList.put(query.getName(), query);
    }
    if (LOG.isDebugEnabled()) {
        ArrayList<String> nameList = new ArrayList<String>(returnList.size());
        for (PrefabGraph graph : returnList.values()) {
            nameList.add(graph.getName());
        }
        LOG.debug("found {} prefabricated graphs for resource {}: {}", nameList.size(), resource, StringUtils.collectionToDelimitedString(nameList, ", "));
    }
    final Set<String> suppressReports = new HashSet<String>();
    for (final Entry<String, PrefabGraph> entry : returnList.entrySet()) {
        suppressReports.addAll(Arrays.asList(entry.getValue().getSuppress()));
    }
    suppressReports.retainAll(returnList.keySet());
    if (suppressReports.size() > 0) {
        LOG.debug("suppressing {} prefabricated graphs for resource {}: {}", suppressReports.size(), resource, StringUtils.collectionToDelimitedString(suppressReports, ", "));
    }
    for (final String suppressReport : suppressReports) {
        returnList.remove(suppressReport);
    }
    return returnList.values().toArray(new PrefabGraph[returnList.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PrefabGraph(org.opennms.netmgt.model.PrefabGraph) ArrayList(java.util.ArrayList) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 20 with OnmsAttribute

use of org.opennms.netmgt.model.OnmsAttribute in project opennms by OpenNMS.

the class DistributedStatusResourceType method createResource.

private OnmsResource createResource(String definitionName, String locationMonitorId, String ipAddress) {
    String monitor = definitionName + "-" + locationMonitorId;
    String label = ipAddress + " from " + monitor;
    final ResourcePath path = getRelativeInterfacePath(locationMonitorId, ipAddress);
    final LazyResourceAttributeLoader loader = new LazyResourceAttributeLoader(m_resourceStorageDao, path);
    final Set<OnmsAttribute> set = new LazySet<OnmsAttribute>(loader);
    return new OnmsResource(getResourceName(locationMonitorId, ipAddress), label, this, set, path);
}
Also used : LazySet(org.opennms.core.utils.LazySet) ResourcePath(org.opennms.netmgt.model.ResourcePath) OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute)

Aggregations

OnmsAttribute (org.opennms.netmgt.model.OnmsAttribute)45 OnmsResource (org.opennms.netmgt.model.OnmsResource)40 MockResourceType (org.opennms.netmgt.mock.MockResourceType)30 RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)21 HashSet (java.util.HashSet)13 ResourcePath (org.opennms.netmgt.model.ResourcePath)13 Test (org.junit.Test)7 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)7 Source (org.opennms.netmgt.measurements.model.Source)7 ExternalValueAttribute (org.opennms.netmgt.model.ExternalValueAttribute)7 LazySet (org.opennms.core.utils.LazySet)6 HashMap (java.util.HashMap)5 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)5 AttributeStatistic (org.opennms.netmgt.model.AttributeStatistic)4 ResourceId (org.opennms.netmgt.model.ResourceId)4 OnmsNode (org.opennms.netmgt.model.OnmsNode)3 OnmsResourceType (org.opennms.netmgt.model.OnmsResourceType)3 StringPropertyAttribute (org.opennms.netmgt.model.StringPropertyAttribute)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2