Search in sources :

Example 1 with ExternalValueAttribute

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

the class PropertiesGraphDaoIT method testGetPrefabGraphsForResource.

@Test
public void testGetPrefabGraphsForResource() {
    MockResourceType resourceType = new MockResourceType();
    resourceType.setName("interface");
    HashSet<OnmsAttribute> attributes = new HashSet<OnmsAttribute>(0);
    attributes.add(new RrdGraphAttribute("ifInOctets", "", ""));
    attributes.add(new RrdGraphAttribute("ifOutOctets", "", ""));
    attributes.add(new ExternalValueAttribute("ifSpeed", ""));
    OnmsResource resource = new OnmsResource("node", "1", resourceType, attributes, ResourcePath.get("foo"));
    PrefabGraph[] graphs = m_dao.getPrefabGraphsForResource(resource);
    assertEquals("prefab graph array size", 1, graphs.length);
    assertEquals("prefab graph[0] name", "mib2.bits", graphs[0].getName());
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) PrefabGraph(org.opennms.netmgt.model.PrefabGraph) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) ExternalValueAttribute(org.opennms.netmgt.model.ExternalValueAttribute) MockResourceType(org.opennms.netmgt.mock.MockResourceType) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with ExternalValueAttribute

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

the class ResourceAttributeFilteringResourceVisitorTest method testVisitWithExternalValueMatch.

public void testVisitWithExternalValueMatch() 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 ExternalValueAttribute("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 : OnmsResource(org.opennms.netmgt.model.OnmsResource) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) ExternalValueAttribute(org.opennms.netmgt.model.ExternalValueAttribute) HashSet(java.util.HashSet) MockResourceType(org.opennms.netmgt.mock.MockResourceType)

Example 3 with ExternalValueAttribute

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

the class ReportDefinitionTest method testFilteredResourceAttributeFilteringWithMatch.

public void testFilteredResourceAttributeFilteringWithMatch() throws Exception {
    OnmsAttribute rrdAttribute = new RrdGraphAttribute("IfInOctets", "something", "something else");
    ExternalValueAttribute externalValueAttribute = new ExternalValueAttribute("ifSpeed", "100000000");
    Set<OnmsAttribute> attributes = new HashSet<OnmsAttribute>();
    attributes.add(rrdAttribute);
    attributes.add(externalValueAttribute);
    final OnmsNode node = new OnmsNode();
    node.setId(1);
    node.setLabel("Node One");
    EasyMock.expect(m_nodeDao.load(1)).andReturn(node);
    MockResourceType resourceType = new MockResourceType();
    resourceType.setName("interfaceSnmp");
    OnmsResource resource = new OnmsResource(node.getId().toString(), node.getLabel(), resourceType, attributes, ResourcePath.get("foo"));
    ReportDefinition def = createReportDefinition();
    def.getReport().getPackage().setFilter("");
    def.setResourceAttributeKey(externalValueAttribute.getName());
    def.setResourceAttributeValueMatch(externalValueAttribute.getValue());
    ReportInstance report = def.createReport(m_nodeDao, m_resourceDao, m_fetchStrategy, m_filterDao);
    SortedMap<Integer, String> sortedNodeMap = new TreeMap<Integer, String>();
    sortedNodeMap.put(node.getId(), node.getLabel());
    EasyMock.expect(m_filterDao.getNodeMap("")).andReturn(sortedNodeMap);
    EasyMock.expect(m_resourceDao.getResourceForNode(node)).andReturn(resource);
    Source source = new Source();
    source.setLabel("result");
    source.setResourceId(resource.getId().toString());
    source.setAttribute(rrdAttribute.getName());
    source.setAggregation("AVERAGE");
    FetchResults results = new FetchResults(new long[] { report.getStartTime() }, Collections.singletonMap("result", new double[] { 100.0 }), report.getEndTime() - report.getStartTime(), Collections.emptyMap());
    EasyMock.expect(m_fetchStrategy.fetch(report.getStartTime(), report.getEndTime(), 1, 0, null, null, Collections.singletonList(source), false)).andReturn(results);
    m_mocks.replayAll();
    report.walk();
    assertEquals("results size", 1, report.getResults().size());
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) TreeMap(java.util.TreeMap) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) Source(org.opennms.netmgt.measurements.model.Source) OnmsResource(org.opennms.netmgt.model.OnmsResource) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) ExternalValueAttribute(org.opennms.netmgt.model.ExternalValueAttribute) HashSet(java.util.HashSet) MockResourceType(org.opennms.netmgt.mock.MockResourceType)

Example 4 with ExternalValueAttribute

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

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

the class ReportDefinitionTest method testUnfilteredResourceAttributeFilteringWithMatch.

public void testUnfilteredResourceAttributeFilteringWithMatch() throws Exception {
    OnmsAttribute rrdAttribute = new RrdGraphAttribute("IfInOctets", "something", "something else");
    ExternalValueAttribute externalValueAttribute = new ExternalValueAttribute("ifSpeed", "100000000");
    Set<OnmsAttribute> attributes = new HashSet<OnmsAttribute>();
    attributes.add(rrdAttribute);
    attributes.add(externalValueAttribute);
    MockResourceType resourceType = new MockResourceType();
    resourceType.setName("interfaceSnmp");
    OnmsResource resource = new OnmsResource("1", "Node One", resourceType, attributes, ResourcePath.get("foo"));
    EasyMock.expect(m_resourceDao.findTopLevelResources()).andReturn(Collections.singletonList(resource));
    ReportDefinition def = createReportDefinition();
    def.setResourceAttributeKey(externalValueAttribute.getName());
    def.setResourceAttributeValueMatch(externalValueAttribute.getValue());
    ReportInstance report = def.createReport(m_nodeDao, m_resourceDao, m_fetchStrategy, m_filterDao);
    rrdAttribute.setResource(new OnmsResource("1", "Node One", resourceType, Collections.singleton(rrdAttribute), ResourcePath.get("foo")));
    Source source = new Source();
    source.setLabel("result");
    source.setResourceId(rrdAttribute.getResource().getId().toString());
    source.setAttribute(rrdAttribute.getName());
    source.setAggregation("AVERAGE");
    FetchResults results = new FetchResults(new long[] { report.getStartTime() }, Collections.singletonMap("result", new double[] { 100.0 }), report.getEndTime() - report.getStartTime(), Collections.emptyMap());
    EasyMock.expect(m_fetchStrategy.fetch(report.getStartTime(), report.getEndTime(), 1, 0, null, null, Collections.singletonList(source), false)).andReturn(results);
    m_mocks.replayAll();
    report.walk();
    m_mocks.verifyAll();
    assertEquals("results size", 1, report.getResults().size());
    m_mocks.replayAll();
}
Also used : OnmsResource(org.opennms.netmgt.model.OnmsResource) FetchResults(org.opennms.netmgt.measurements.api.FetchResults) OnmsAttribute(org.opennms.netmgt.model.OnmsAttribute) RrdGraphAttribute(org.opennms.netmgt.model.RrdGraphAttribute) ExternalValueAttribute(org.opennms.netmgt.model.ExternalValueAttribute) Source(org.opennms.netmgt.measurements.model.Source) HashSet(java.util.HashSet) MockResourceType(org.opennms.netmgt.mock.MockResourceType)

Aggregations

ExternalValueAttribute (org.opennms.netmgt.model.ExternalValueAttribute)7 OnmsAttribute (org.opennms.netmgt.model.OnmsAttribute)7 OnmsResource (org.opennms.netmgt.model.OnmsResource)7 HashSet (java.util.HashSet)6 MockResourceType (org.opennms.netmgt.mock.MockResourceType)6 RrdGraphAttribute (org.opennms.netmgt.model.RrdGraphAttribute)5 Test (org.junit.Test)3 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)3 FetchResults (org.opennms.netmgt.measurements.api.FetchResults)2 Source (org.opennms.netmgt.measurements.model.Source)2 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 TreeMap (java.util.TreeMap)1 Matcher (java.util.regex.Matcher)1 LazySet (org.opennms.core.utils.LazySet)1 SymbolTable (org.opennms.core.utils.PropertiesUtils.SymbolTable)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 StringPropertyAttribute (org.opennms.netmgt.model.StringPropertyAttribute)1