Search in sources :

Example 1 with CollectionSetVisitorWrapper

use of org.opennms.netmgt.collection.support.CollectionSetVisitorWrapper in project opennms by OpenNMS.

the class CollectableService method wrapResourcesWithTimekeeper.

public static CollectionSetVisitor wrapResourcesWithTimekeeper(CollectionSetVisitor visitor, TimeKeeper timeKeeper) {
    // Wrap the given visitor and intercept the calls to visit the resources
    final CollectionSetVisitor wrappedVisitor = new CollectionSetVisitorWrapper(visitor) {

        private CollectionResource wrappedResource;

        private CollectionAttribute wrappedAttribute;

        private AttributeGroup wrappedGroup;

        @Override
        public void visitResource(CollectionResource resource) {
            // Wrap the given resource and return the custom timekeeper
            wrappedResource = new CollectionResourceWrapper(resource) {

                @Override
                public TimeKeeper getTimeKeeper() {
                    return timeKeeper;
                }
            };
            visitor.visitResource(wrappedResource);
        }

        @Override
        public void completeResource(CollectionResource resource) {
            visitor.completeResource(wrappedResource);
        }

        @Override
        public void visitAttribute(CollectionAttribute attribute) {
            // Wrap the given attribute and return the custom resource
            wrappedAttribute = new CollectionAttributeWrapper(attribute) {

                @Override
                public CollectionResource getResource() {
                    return wrappedResource;
                }
            };
            visitor.visitAttribute(wrappedAttribute);
        }

        @Override
        public void completeAttribute(CollectionAttribute attribute) {
            visitor.completeAttribute(wrappedAttribute);
        }

        @Override
        public void visitGroup(AttributeGroup group) {
            // Wrap the given attribute group and return the custom resource
            wrappedGroup = new AttributeGroupWrapper(group) {

                @Override
                public CollectionResource getResource() {
                    return wrappedResource;
                }
            };
            visitor.visitGroup(wrappedGroup);
        }

        @Override
        public void completeGroup(AttributeGroup group) {
            visitor.completeGroup(wrappedGroup);
        }
    };
    return wrappedVisitor;
}
Also used : CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) CollectionResource(org.opennms.netmgt.collection.api.CollectionResource) AttributeGroupWrapper(org.opennms.netmgt.collection.support.AttributeGroupWrapper) ConstantTimeKeeper(org.opennms.netmgt.collection.support.ConstantTimeKeeper) TimeKeeper(org.opennms.netmgt.collection.api.TimeKeeper) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) AttributeGroup(org.opennms.netmgt.collection.api.AttributeGroup) CollectionAttributeWrapper(org.opennms.netmgt.collection.support.CollectionAttributeWrapper) CollectionSetVisitorWrapper(org.opennms.netmgt.collection.support.CollectionSetVisitorWrapper) CollectionResourceWrapper(org.opennms.netmgt.collection.support.CollectionResourceWrapper)

Example 2 with CollectionSetVisitorWrapper

use of org.opennms.netmgt.collection.support.CollectionSetVisitorWrapper in project opennms by OpenNMS.

the class SnmpAttributeTest method testPersisting.

@Ignore
@SuppressWarnings("unchecked")
private void testPersisting(String matchValue, SnmpValue snmpValue) throws Exception {
    OnmsNode node = new OnmsNode();
    node.setId(3);
    OnmsIpInterface ipInterface = new OnmsIpInterface();
    ipInterface.setId(1);
    ipInterface.setNode(node);
    ipInterface.setIpAddress(InetAddressUtils.addr("192.168.1.1"));
    // It used to be 3, but I think it is more correct to use getStoreDir from DefaultCollectionAgentService on DefaultCollectionAgent (NMS-7516)
    expect(m_ipInterfaceDao.load(1)).andReturn(ipInterface).times(7);
    expect(m_rrdStrategy.getDefaultFileExtension()).andReturn(".myLittleEasyMockedStrategyAndMe").anyTimes();
    expect(m_rrdStrategy.createDefinition(isA(String.class), isA(String.class), isA(String.class), anyInt(), isAList(RrdDataSource.class), isAList(String.class))).andReturn(new Object());
    m_rrdStrategy.createFile(isA(Object.class));
    expect(m_rrdStrategy.openFile(isA(String.class))).andReturn(new Object());
    m_rrdStrategy.updateFile(isA(Object.class), isA(String.class), matches(".*:" + matchValue));
    m_rrdStrategy.closeFile(isA(Object.class));
    m_mocks.replayAll();
    SnmpCollectionAgent agent = DefaultCollectionAgent.create(ipInterface.getId(), m_ipInterfaceDao, new MockPlatformTransactionManager());
    OnmsSnmpCollection snmpCollection = new OnmsSnmpCollection(agent, new ServiceParameters(new HashMap<String, Object>()), new MockDataCollectionConfig(), m_locationAwareSnmpClient);
    NodeResourceType resourceType = new NodeResourceType(agent, snmpCollection);
    NodeInfo nodeInfo = resourceType.getNodeInfo();
    MibObject mibObject = new MibObject();
    mibObject.setOid(".1.3.6.1.4.1.12238.55.9997.4.1.2.9.116.101.109.112.95.117.108.107.111");
    mibObject.setInstance("1");
    mibObject.setAlias("temp_ulko");
    mibObject.setType("gauge");
    NumericAttributeType attributeType = new NumericAttributeType(resourceType, snmpCollection.getName(), mibObject, new AttributeGroupType("foo", AttributeGroupType.IF_TYPE_IGNORE));
    attributeType.storeResult(new SnmpCollectionSet(agent, snmpCollection, m_locationAwareSnmpClient), null, new SnmpResult(mibObject.getSnmpObjId(), new SnmpInstId(mibObject.getInstance()), snmpValue));
    RrdRepository repository = createRrdRepository();
    repository.setRraList(Collections.singletonList("RRA:AVERAGE:0.5:1:2016"));
    RrdPersisterFactory persisterFactory = new RrdPersisterFactory();
    persisterFactory.setRrdStrategy(m_rrdStrategy);
    persisterFactory.setResourceStorageDao(m_resourceStorageDao);
    CollectionSetVisitor persister = persisterFactory.createPersister(new ServiceParameters(Collections.emptyMap()), repository);
    final AtomicInteger count = new AtomicInteger(0);
    nodeInfo.visit(new CollectionSetVisitorWrapper(persister) {

        @Override
        public void visitAttribute(CollectionAttribute attribute) {
            super.visitAttribute(attribute);
            count.incrementAndGet();
        }
    });
    assertEquals(1, count.get());
}
Also used : HashMap(java.util.HashMap) CollectionSetVisitor(org.opennms.netmgt.collection.api.CollectionSetVisitor) MockDataCollectionConfig(org.opennms.netmgt.mock.MockDataCollectionConfig) SnmpInstId(org.opennms.netmgt.snmp.SnmpInstId) RrdPersisterFactory(org.opennms.netmgt.collection.persistence.rrd.RrdPersisterFactory) RrdDataSource(org.opennms.netmgt.rrd.RrdDataSource) MockPlatformTransactionManager(org.opennms.core.test.MockPlatformTransactionManager) CollectionSetVisitorWrapper(org.opennms.netmgt.collection.support.CollectionSetVisitorWrapper) SnmpResult(org.opennms.netmgt.snmp.SnmpResult) OnmsNode(org.opennms.netmgt.model.OnmsNode) RrdRepository(org.opennms.netmgt.rrd.RrdRepository) CollectionAttribute(org.opennms.netmgt.collection.api.CollectionAttribute) OnmsIpInterface(org.opennms.netmgt.model.OnmsIpInterface) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeGroupType(org.opennms.netmgt.collection.api.AttributeGroupType) MibObject(org.opennms.netmgt.config.datacollection.MibObject) ServiceParameters(org.opennms.netmgt.collection.api.ServiceParameters) MibObject(org.opennms.netmgt.config.datacollection.MibObject) Ignore(org.junit.Ignore)

Aggregations

CollectionAttribute (org.opennms.netmgt.collection.api.CollectionAttribute)2 CollectionSetVisitor (org.opennms.netmgt.collection.api.CollectionSetVisitor)2 CollectionSetVisitorWrapper (org.opennms.netmgt.collection.support.CollectionSetVisitorWrapper)2 HashMap (java.util.HashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Ignore (org.junit.Ignore)1 MockPlatformTransactionManager (org.opennms.core.test.MockPlatformTransactionManager)1 AttributeGroup (org.opennms.netmgt.collection.api.AttributeGroup)1 AttributeGroupType (org.opennms.netmgt.collection.api.AttributeGroupType)1 CollectionResource (org.opennms.netmgt.collection.api.CollectionResource)1 ServiceParameters (org.opennms.netmgt.collection.api.ServiceParameters)1 TimeKeeper (org.opennms.netmgt.collection.api.TimeKeeper)1 RrdPersisterFactory (org.opennms.netmgt.collection.persistence.rrd.RrdPersisterFactory)1 AttributeGroupWrapper (org.opennms.netmgt.collection.support.AttributeGroupWrapper)1 CollectionAttributeWrapper (org.opennms.netmgt.collection.support.CollectionAttributeWrapper)1 CollectionResourceWrapper (org.opennms.netmgt.collection.support.CollectionResourceWrapper)1 ConstantTimeKeeper (org.opennms.netmgt.collection.support.ConstantTimeKeeper)1 MibObject (org.opennms.netmgt.config.datacollection.MibObject)1 MockDataCollectionConfig (org.opennms.netmgt.mock.MockDataCollectionConfig)1 OnmsIpInterface (org.opennms.netmgt.model.OnmsIpInterface)1