@Override public byte[] getLocalEngineID() { // lovingly stolen from SNMP4J final byte[] engineID = new byte[5]; engineID[0] = (byte) (0x80 | ((s_enterpriseId >> 24) & 0xFF)); engineID[1] = (byte) ((s_enterpriseId >> 16) & 0xFF); engineID[2] = (byte) ((s_enterpriseId >> 8) & 0xFF); engineID[3] = (byte) (s_enterpriseId & 0xFF); final byte[] ip = InetAddrUtils.getLocalHostAddress().getAddress(); if (ip.length == 4) { // IPv4 engineID[4] = 1; } else if (ip.length == 16) { // IPv6 engineID[4] = 2; } else { // Text engineID[4] = 4; } final byte[] bytes = new byte[engineID.length+ip.length]; System.arraycopy(engineID, 0, bytes, 0, engineID.length); System.arraycopy(ip, 0, bytes, engineID.length, ip.length); return bytes; }
@Test public void canCollectUsingOpenNMSWorkflow() throws CollectionInitializationException, CollectionException { // create the agent OnmsNode node = mock(OnmsNode.class); OnmsIpInterface iface = mock(OnmsIpInterface.class); when(iface.getNode()).thenReturn(node); when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress()); IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class); when(ifaceDao.load(1)).thenReturn(iface); PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class); final CollectionAgent agent = createAgent(1, ifaceDao, transMgr); // init() should execute without any exceptions final ServiceCollector opennmsCollector = getCollector(); initialize(opennmsCollector); // getEffectiveLocation() should execute without any exceptions // in this context there are no requirements on its return value final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID; opennmsCollector.getEffectiveLocation(targetLocation); // getRuntimeAttributes() should return a valid map final Map<String, Object> requiredParams = getRequiredParameters(); final Map<String, Object> runtimeAttrs = opennmsCollector.getRuntimeAttributes(agent, Collections.unmodifiableMap(requiredParams)); // collect() should return a valid collection set final Map<String, Object> allParms = new HashMap<>(); allParms.putAll(requiredParams); allParms.putAll(runtimeAttrs); final CollectionSet collectionSet = opennmsCollector.collect(agent, Collections.unmodifiableMap(allParms)); assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus()); // getRrdRepository() should return a valid repository assertNotNull(opennmsCollector.getRrdRepository(getCollectionName())); }
@Test public void canCollectUsingOpenNMSWorkflow() throws CollectionInitializationException, CollectionException { // create the agent OnmsNode node = mock(OnmsNode.class); OnmsIpInterface iface = mock(OnmsIpInterface.class); when(iface.getNode()).thenReturn(node); when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress()); IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class); when(ifaceDao.load(1)).thenReturn(iface); PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class); final CollectionAgent agent = createAgent(1, ifaceDao, transMgr); // init() should execute without any exceptions final ServiceCollector opennmsCollector = getCollector(); initialize(opennmsCollector); // getEffectiveLocation() should execute without any exceptions // in this context there are no requirements on its return value final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID; opennmsCollector.getEffectiveLocation(targetLocation); // getRuntimeAttributes() should return a valid map final Map<String, Object> requiredParams = getRequiredParameters(); final Map<String, Object> runtimeAttrs = opennmsCollector.getRuntimeAttributes(agent, Collections.unmodifiableMap(requiredParams)); // collect() should return a valid collection set final Map<String, Object> allParms = new HashMap<>(); allParms.putAll(requiredParams); allParms.putAll(runtimeAttrs); final CollectionSet collectionSet = opennmsCollector.collect(agent, Collections.unmodifiableMap(allParms)); assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus()); // getRrdRepository() should return a valid repository assertNotNull(opennmsCollector.getRrdRepository(getCollectionName())); }
OnmsIpInterface iface = mock(OnmsIpInterface.class); when(iface.getNode()).thenReturn(node); when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
OnmsIpInterface iface = mock(OnmsIpInterface.class); when(iface.getNode()).thenReturn(node); when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress());
private void createCollectableService() throws CollectionInitializationException, IOException { // Mock it all! OnmsIpInterface iface = mock(OnmsIpInterface.class, RETURNS_DEEP_STUBS); IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class); spec = mock(CollectionSpecification.class); scheduler = mock(Scheduler.class); SchedulingCompletedFlag schedulingCompletedFlag = mock(SchedulingCompletedFlag.class); PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class); RrdPersisterFactory persisterFactory = new RrdPersisterFactory(); persisterFactory.setRrdStrategy(rrdStrategy); ResourceStorageDao resourceStorageDao = mock(ResourceStorageDao.class); // Disable thresholding Map<String, Object> paramsMap = new HashMap<>(); paramsMap.put("thresholding-enabled", Boolean.FALSE.toString()); ServiceParameters params = new ServiceParameters(paramsMap); when(iface.getNode().getId()).thenReturn(1); when(spec.getServiceParameters()).thenReturn(params); when(spec.getRrdRepository(any())).thenReturn(createRrdRepository()); when(ifaceDao.load(any())).thenReturn(iface); when(iface.getIpAddress()).thenReturn(InetAddrUtils.getLocalHostAddress()); service = new CollectableService(iface, ifaceDao, spec, scheduler, schedulingCompletedFlag, transMgr, persisterFactory, resourceStorageDao); }
private static SnmpCollectionAgent createCollectionAgent() { SnmpCollectionAgent agent = EasyMock.createMock(SnmpCollectionAgent.class); EasyMock.expect(agent.getNodeId()).andReturn(1).anyTimes(); EasyMock.expect(agent.getStorageResourcePath()).andReturn(ResourcePath.get(String.valueOf(1))).anyTimes(); EasyMock.expect(agent.getHostAddress()).andReturn("127.0.0.1").anyTimes(); EasyMock.expect(agent.getSnmpInterfaceInfo((IfResourceType)EasyMock.anyObject())).andReturn(new HashSet<IfInfo>()).anyTimes(); EasyMock.expect(agent.getAttributeNames()).andReturn(Collections.emptySet()).anyTimes(); EasyMock.expect(agent.getAddress()).andReturn(InetAddrUtils.getLocalHostAddress()).anyTimes(); EasyMock.expect(agent.isStoreByForeignSource()).andReturn(false).anyTimes(); EasyMock.expect(agent.getNodeLabel()).andReturn("test").anyTimes(); EasyMock.expect(agent.getForeignSource()).andReturn(null).anyTimes(); EasyMock.expect(agent.getForeignId()).andReturn(null).anyTimes(); EasyMock.expect(agent.getLocationName()).andReturn(null).anyTimes(); EasyMock.expect(agent.getSysObjectId()).andReturn(null).anyTimes(); EasyMock.expect(agent.getSavedSysUpTime()).andReturn(0L).anyTimes(); EasyMock.replay(agent); return agent; }