use of org.opennms.netmgt.dao.api.IpInterfaceDao in project opennms by OpenNMS.
the class CollectorComplianceTest method canCollectUsingMinionWorkflow.
@Test
public void canCollectUsingMinionWorkflow() throws CollectionInitializationException, CollectionException {
Assume.assumeTrue(runsOnMinion);
// create the agent
OnmsNode node = mock(OnmsNode.class);
when(node.getId()).thenReturn(1);
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 = DefaultCollectionAgent.create(1, ifaceDao, transMgr);
// init() should execute without any exceptions
final ServiceCollector opennmsCollector = getCollector();
initialize(opennmsCollector);
// getEffectiveLocation() should return the original location
final String targetLocation = "!" + MonitoringLocationDao.DEFAULT_MONITORING_LOCATION_ID;
assertEquals("Location cannot be altered.", targetLocation, 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));
// marshalParameters() should marshal all parameters to strings
final Map<String, Object> allParms = new HashMap<>();
allParms.putAll(requiredParams);
allParms.putAll(runtimeAttrs);
final Map<String, String> marshaledParms = opennmsCollector.marshalParameters(Collections.unmodifiableMap(allParms));
beforeMinion();
// create a separate instance of the collector
final ServiceCollector minionCollector = getNewCollector();
// unmarshalParameters() should unmarshal all parameters from strings
final Map<String, Object> unmarshaledParms = minionCollector.unmarshalParameters(Collections.unmodifiableMap(marshaledParms));
// collect() should return a valid collection set
final CollectionAgentDTO agentDTO = new CollectionAgentDTO(agent);
final CollectionSet collectionSet = minionCollector.collect(agentDTO, Collections.unmodifiableMap(unmarshaledParms));
assertEquals(CollectionStatus.SUCCEEDED, collectionSet.getStatus());
afterMinion();
// the collection set should be marshalable
JaxbUtils.marshal(collectionSet);
// getRrdRepository() should return a valid repository
assertNotNull(opennmsCollector.getRrdRepository(getCollectionName()));
}
use of org.opennms.netmgt.dao.api.IpInterfaceDao in project opennms by OpenNMS.
the class CollectorComplianceTest method canCollectUsingOpenNMSWorkflow.
@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()));
}
use of org.opennms.netmgt.dao.api.IpInterfaceDao in project opennms by OpenNMS.
the class DefaultCollectionAgentTest method verifyThatTheIpAndNodeIdAreCached.
/**
* NMS-5105: When processing serviceDeleted and interfaceDeleted events
* in Collectd we need to match both the Node ID and IP Address of
* the service that is being collected with the information from the event.
*
* Since the entities have been deleted, we not longer be able to reach
* in the database to fetch the required details. Instead, they
* should be loaded when the agent is created, and cached for the lifetime
* of the object.
*/
@Test
public void verifyThatTheIpAndNodeIdAreCached() {
OnmsNode node = new OnmsNode();
node.setId(11);
OnmsIpInterface iface = new OnmsIpInterface();
iface.setId(42);
iface.setNode(node);
iface.setIpAddress(InetAddressUtils.ONE_TWENTY_SEVEN);
IpInterfaceDao ifaceDao = EasyMock.createMock(IpInterfaceDao.class);
EasyMock.expect(ifaceDao.load(iface.getId())).andReturn(iface).times(5);
EasyMock.replay(ifaceDao);
PlatformTransactionManager transMgr = new MockPlatformTransactionManager();
CollectionAgent agent = DefaultCollectionAgent.create(iface.getId(), ifaceDao, transMgr);
EasyMock.verify(ifaceDao);
assertEquals(iface.getIpAddress(), agent.getAddress());
assertEquals(node.getId().intValue(), agent.getNodeId());
}
use of org.opennms.netmgt.dao.api.IpInterfaceDao in project opennms by OpenNMS.
the class DefaultCollectionAgentTest method canGetLocationAwareAgentConfig.
/**
* Verifies that the SNMP agent configuration is retrieved using
* the location name that is associated with the interface/node.
*/
@Test
public void canGetLocationAwareAgentConfig() {
// Mock the peer factory
SnmpPeerFactory snmpPeerFactory = mock(SnmpPeerFactory.class);
SnmpPeerFactory.setInstance(snmpPeerFactory);
// Mock the other arguments required to create a DefaultCollectionAgent
Integer ifaceId = 1;
IpInterfaceDao ifaceDao = mock(IpInterfaceDao.class);
PlatformTransactionManager transMgr = mock(PlatformTransactionManager.class);
OnmsIpInterface ipIface = mock(OnmsIpInterface.class, RETURNS_DEEP_STUBS);
when(ifaceDao.load(ifaceId)).thenReturn(ipIface);
when(ipIface.getNode().getLocation().getLocationName()).thenReturn("Ocracoke");
// Retrieve the agent configuration
SnmpCollectionAgent agent = DefaultSnmpCollectionAgent.create(ifaceId, ifaceDao, transMgr);
agent.getAgentConfig();
// Verify
verify(snmpPeerFactory, times(1)).getAgentConfig(any(), eq("Ocracoke"));
}
use of org.opennms.netmgt.dao.api.IpInterfaceDao in project opennms by OpenNMS.
the class PersistRegexSelectorStrategyTest method setUp.
@Before
public void setUp() throws Exception {
ipInterfaceDao = EasyMock.createMock(IpInterfaceDao.class);
String localhost = InetAddress.getLocalHost().getHostAddress();
NetworkBuilder builder = new NetworkBuilder();
builder.addNode("myNode");
builder.addInterface(localhost).setIsManaged("M").setIsSnmpPrimary("P");
OnmsNode node = builder.getCurrentNode();
node.setId(1);
OnmsIpInterface ipInterface = node.getIpInterfaces().iterator().next();
EasyMock.expect(ipInterfaceDao.load(1)).andReturn(ipInterface).anyTimes();
EasyMock.replay(ipInterfaceDao);
Package pkg = new Package();
pkg.setName("junitTestPackage");
Filter filter = new Filter();
filter.setContent("IPADDR != '0.0.0.0'");
pkg.setFilter(filter);
Service service = new Service();
service.setName("SNMP");
pkg.addService(service);
Map<String, Object> map = new TreeMap<String, Object>();
List<org.opennms.netmgt.config.collectd.Parameter> params = pkg.getService("SNMP").getParameters();
for (org.opennms.netmgt.config.collectd.Parameter p : params) {
map.put(p.getKey(), p.getValue());
}
map.put("collection", "default");
serviceParams = new ServiceParameters(map);
LocationAwareSnmpClient locationAwareSnmpClient = new LocationAwareSnmpClientRpcImpl(new MockRpcClientFactory());
PlatformTransactionManager ptm = new MockPlatformTransactionManager();
SnmpCollectionAgent agent = DefaultSnmpCollectionAgent.create(1, ipInterfaceDao, ptm);
OnmsSnmpCollection snmpCollection = new OnmsSnmpCollection(agent, serviceParams, new MockDataCollectionConfigDao(), locationAwareSnmpClient);
org.opennms.netmgt.config.datacollection.ResourceType rt = new org.opennms.netmgt.config.datacollection.ResourceType();
rt.setName("myResourceType");
StorageStrategy storageStrategy = new StorageStrategy();
storageStrategy.setClazz("org.opennms.netmgt.collection.support.IndexStorageStrategy");
rt.setStorageStrategy(storageStrategy);
PersistenceSelectorStrategy persistenceSelectorStrategy = new PersistenceSelectorStrategy();
persistenceSelectorStrategy.setClazz("org.opennms.netmgt.collectd.PersistRegexSelectorStrategy");
Parameter param = new Parameter();
param.setKey(PersistRegexSelectorStrategy.MATCH_EXPRESSION);
param.setValue("#name matches '^agalue.*$'");
persistenceSelectorStrategy.addParameter(param);
rt.setPersistenceSelectorStrategy(persistenceSelectorStrategy);
GenericIndexResourceType resourceType = new GenericIndexResourceType(agent, snmpCollection, rt);
resourceA = new GenericIndexResource(resourceType, rt.getName(), new SnmpInstId("1.2.3.4.5.6.7.8.9.1.1"));
AttributeGroupType groupType = new AttributeGroupType("mib2-interfaces", AttributeGroupType.IF_TYPE_ALL);
MibObject mibObject = new MibObject();
mibObject.setOid(".1.2.3.4.5.6.7.8.9.2.1");
mibObject.setInstance("1");
mibObject.setAlias("name");
mibObject.setType("string");
StringAttributeType attributeType = new StringAttributeType(resourceType, snmpCollection.getName(), mibObject, groupType);
SnmpValue snmpValue = new Snmp4JValueFactory().getOctetString("agalue rules!".getBytes());
resourceA.setAttributeValue(attributeType, snmpValue);
resourceB = new GenericIndexResource(resourceType, rt.getName(), new SnmpInstId("1.2.3.4.5.6.7.8.9.1.2"));
}
Aggregations