Search in sources :

Example 1 with StringPropertyAttribute

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

the class ResourceAttributeFilteringResourceVisitorTest method testVisitWithStringPropertyMatch.

public void testVisitWithStringPropertyMatch() throws Exception {
    ResourceAttributeFilteringResourceVisitor filteringVisitor = new ResourceAttributeFilteringResourceVisitor();
    filteringVisitor.setDelegatedVisitor(m_delegatedVisitor);
    filteringVisitor.setResourceAttributeKey("ifSpeed");
    filteringVisitor.setResourceAttributeValueMatch("1000000000");
    filteringVisitor.afterPropertiesSet();
    Set<OnmsAttribute> attributes = new HashSet<OnmsAttribute>(1);
    attributes.add(new StringPropertyAttribute("ifSpeed", "1000000000"));
    MockResourceType resourceType = new MockResourceType();
    resourceType.setName("interfaceSnmp");
    OnmsResource resource = new OnmsResource("1", "Node One", resourceType, attributes, ResourcePath.get("foo"));
    // Expect
    m_delegatedVisitor.visit(resource);
    m_mocks.replayAll();
    filteringVisitor.visit(resource);
    m_mocks.verifyAll();
}
Also used : StringPropertyAttribute(org.opennms.netmgt.model.StringPropertyAttribute) OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) HashSet(java.util.HashSet) MockResourceType(org.opennms.netmgt.mock.MockResourceType)

Example 2 with StringPropertyAttribute

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

the class RrdStatisticAttributeVisitorTest method testVisitWithNonRrdAttribute.

public void testVisitWithNonRrdAttribute() throws Exception {
    RrdStatisticAttributeVisitor attributeVisitor = new RrdStatisticAttributeVisitor();
    attributeVisitor.setFetchStrategy(m_fetchStrategy);
    attributeVisitor.setConsolidationFunction("AVERAGE");
    attributeVisitor.setStartTime(m_startTime);
    attributeVisitor.setEndTime(m_endTime);
    attributeVisitor.setStatisticVisitor(m_statisticVisitor);
    attributeVisitor.afterPropertiesSet();
    MockResourceType resourceType = new MockResourceType();
    resourceType.setName("something other than interfaceSnmp");
    OnmsAttribute attribute = new StringPropertyAttribute("ifInOctets", "one billion octets!");
    attribute.setResource(new OnmsResource("1", "Node One", resourceType, Collections.singleton(attribute), ResourcePath.get("foo")));
    m_mocks.replayAll();
    attributeVisitor.visit(attribute);
    m_mocks.verifyAll();
}
Also used : StringPropertyAttribute(org.opennms.netmgt.model.StringPropertyAttribute) OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) MockResourceType(org.opennms.netmgt.mock.MockResourceType)

Example 3 with StringPropertyAttribute

use of org.opennms.netmgt.model.StringPropertyAttribute 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 4 with StringPropertyAttribute

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

the class NewtsResourceStorageDao method getAttributes.

@Override
public Set<OnmsAttribute> getAttributes(ResourcePath path) {
    Set<OnmsAttribute> attributes = Sets.newHashSet();
    // Fetch the resource-level attributes in parallel
    Future<Map<String, String>> stringAttributes = ForkJoinPool.commonPool().submit(getResourceAttributesCallable(path));
    // Gather the list of metrics available under the resource path
    SearchResults results = searchFor(path, 0, true);
    for (Result result : results) {
        final String resourceId = result.getResource().getId();
        final ResourcePath resultPath = toResourcePath(resourceId);
        if (!path.equals(resultPath)) {
            // This shouldn't happen
            LOG.warn("Encountered non-child resource {} when searching for {} with depth {}. Ignoring resource.", result.getResource(), path, 0);
            continue;
        }
        for (String metric : result.getMetrics()) {
            // Use the metric name as the dsName
            // Store the resource id in the rrdFile field
            attributes.add(new RrdGraphAttribute(metric, "", resourceId));
        }
    }
    // Add the resource level attributes to the result set
    try {
        stringAttributes.get().entrySet().stream().map(e -> new StringPropertyAttribute(e.getKey(), e.getValue())).forEach(attr -> attributes.add(attr));
    } catch (InterruptedException | ExecutionException e) {
        throw Throwables.propagate(e);
    }
    return attributes;
}
Also used : NewtsUtils.toResourcePath(org.opennms.netmgt.newts.support.NewtsUtils.toResourcePath) IntStream(java.util.stream.IntStream) Context(org.opennms.newts.api.Context) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Callable(java.util.concurrent.Callable) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) CassandraIndexer(org.opennms.newts.cassandra.search.CassandraIndexer) StringPropertyAttribute(org.opennms.netmgt.model.StringPropertyAttribute) NewtsUtils(org.opennms.netmgt.newts.support.NewtsUtils) NewtsUtils.toResourceId(org.opennms.netmgt.newts.support.NewtsUtils.toResourceId) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) Optional(com.google.common.base.Optional) Map(java.util.Map) Sample(org.opennms.newts.api.Sample) NewtsWriter(org.opennms.netmgt.newts.NewtsWriter) Path(java.nio.file.Path) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Query(org.opennms.newts.api.search.Query) CassandraSampleRepository(org.opennms.newts.persistence.cassandra.CassandraSampleRepository) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Resource(org.opennms.newts.api.Resource) Set(java.util.Set) Throwables(com.google.common.base.Throwables) CassandraSearcher(org.opennms.newts.cassandra.search.CassandraSearcher) ResourceStorageDao(org.opennms.netmgt.dao.api.ResourceStorageDao) Result(org.opennms.newts.api.search.SearchResults.Result) SearchableResourceMetadataCache(org.opennms.netmgt.newts.support.SearchableResourceMetadataCache) Sets(com.google.common.collect.Sets) ExecutionException(java.util.concurrent.ExecutionException) NewtsUtils.findResourcesWithMetricsAtDepth(org.opennms.netmgt.newts.support.NewtsUtils.findResourcesWithMetricsAtDepth) List(java.util.List) SearchResults(org.opennms.newts.api.search.SearchResults) ForkJoinPool(java.util.concurrent.ForkJoinPool) ResourcePath(org.opennms.netmgt.model.ResourcePath) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) SearchResults(org.opennms.newts.api.search.SearchResults) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Result(org.opennms.newts.api.search.SearchResults.Result) StringPropertyAttribute(org.opennms.netmgt.model.StringPropertyAttribute) NewtsUtils.toResourcePath(org.opennms.netmgt.newts.support.NewtsUtils.toResourcePath) ResourcePath(org.opennms.netmgt.model.ResourcePath) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

OnmsAttribute (org.opennms.netmgt.model.OnmsAttribute)4 StringPropertyAttribute (org.opennms.netmgt.model.StringPropertyAttribute)4 OnmsResource (org.opennms.netmgt.model.OnmsResource)3 MockResourceType (org.opennms.netmgt.mock.MockResourceType)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (com.google.common.base.Optional)1 Preconditions (com.google.common.base.Preconditions)1 Throwables (com.google.common.base.Throwables)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 StringTokenizer (java.util.StringTokenizer)1 Callable (java.util.concurrent.Callable)1 ExecutionException (java.util.concurrent.ExecutionException)1