use of org.opennms.netmgt.provision.persist.requisition.RequisitionInterface in project opennms by OpenNMS.
the class HeartbeatConsumer method provision.
private void provision(final OnmsMinion minion, final String prevLocation, final String nextLocation) {
// Return fast if automatic provisioning is disabled
if (!PROVISIONING) {
return;
}
// Return fast until the provisioner is running to pick up the events sent below
if (!this.eventSubscriptionService.hasEventListener(EventConstants.RELOAD_IMPORT_UEI)) {
return;
}
final String prevForeignSource = String.format(PROVISIONING_FOREIGN_SOURCE_PATTERN, prevLocation);
final String nextForeignSource = String.format(PROVISIONING_FOREIGN_SOURCE_PATTERN, nextLocation);
final Set<String> alteredForeignSources = Sets.newHashSet();
// Remove the node from the previous requisition, if location has changed
if (!Objects.equals(prevForeignSource, nextForeignSource)) {
final Requisition prevRequisition = this.deployedForeignSourceRepository.getRequisition(prevForeignSource);
if (prevRequisition != null && prevRequisition.getNode(minion.getId()) != null) {
prevRequisition.deleteNode(minion.getId());
prevRequisition.updateDateStamp();
deployedForeignSourceRepository.save(prevRequisition);
deployedForeignSourceRepository.flush();
alteredForeignSources.add(prevForeignSource);
}
}
Requisition nextRequisition = deployedForeignSourceRepository.getRequisition(nextForeignSource);
if (nextRequisition == null) {
nextRequisition = new Requisition(nextForeignSource);
nextRequisition.updateDateStamp();
// We have to save the requisition before we can alter the according foreign source definition
deployedForeignSourceRepository.save(nextRequisition);
// Remove all policies and detectors from the foreign source
final ForeignSource foreignSource = deployedForeignSourceRepository.getForeignSource(nextForeignSource);
foreignSource.setDetectors(Collections.emptyList());
foreignSource.setPolicies(Collections.emptyList());
deployedForeignSourceRepository.save(foreignSource);
alteredForeignSources.add(nextForeignSource);
}
RequisitionNode requisitionNode = nextRequisition.getNode(minion.getId());
if (requisitionNode == null) {
final RequisitionMonitoredService requisitionMonitoredService = new RequisitionMonitoredService();
requisitionMonitoredService.setServiceName("Minion-Heartbeat");
final RequisitionInterface requisitionInterface = new RequisitionInterface();
requisitionInterface.setIpAddr("127.0.0.1");
requisitionInterface.putMonitoredService(requisitionMonitoredService);
requisitionNode = new RequisitionNode();
requisitionNode.setNodeLabel(minion.getId());
requisitionNode.setForeignId(minion.getLabel() != null ? minion.getLabel() : minion.getId());
requisitionNode.setLocation(minion.getLocation());
requisitionNode.putInterface(requisitionInterface);
nextRequisition.putNode(requisitionNode);
nextRequisition.setDate(new Date());
deployedForeignSourceRepository.save(nextRequisition);
deployedForeignSourceRepository.flush();
alteredForeignSources.add(nextForeignSource);
}
for (final String alteredForeignSource : alteredForeignSources) {
final EventBuilder eventBuilder = new EventBuilder(EventConstants.RELOAD_IMPORT_UEI, "Web");
eventBuilder.addParam(EventConstants.PARM_URL, String.valueOf(deployedForeignSourceRepository.getRequisitionURL(alteredForeignSource)));
try {
eventProxy.send(eventBuilder.getEvent());
} catch (final EventProxyException e) {
throw new DataAccessResourceFailureException("Unable to send event to import group " + alteredForeignSource, e);
}
}
}
use of org.opennms.netmgt.provision.persist.requisition.RequisitionInterface in project opennms by OpenNMS.
the class FusedForeignSourceRepositoryTest method createNode.
protected RequisitionNode createNode(final String id) {
RequisitionNode node = new RequisitionNode();
node.setForeignId(id);
node.setNodeLabel("node " + id);
RequisitionInterface iface = new RequisitionInterface();
iface.setIpAddr("172.16.0." + id);
node.putInterface(iface);
return node;
}
use of org.opennms.netmgt.provision.persist.requisition.RequisitionInterface in project opennms by OpenNMS.
the class DnsRequisitionProvider method createRequisitionNode.
/**
* Creates an instance of the JaxB annotated RequisionNode class.
*
* @param rec
* @return a populated RequisitionNode based on defaults and data from the A
* record returned from a DNS zone transfer query.
*/
private RequisitionNode createRequisitionNode(DnsRequisitionRequest request, Record rec) {
String addr = null;
if ("A".equals(Type.string(rec.getType()))) {
final ARecord arec = (ARecord) rec;
addr = StringUtils.stripStart(arec.getAddress().toString(), "/");
} else if ("AAAA".equals(Type.string(rec.getType()))) {
final AAAARecord aaaarec = (AAAARecord) rec;
addr = aaaarec.rdataToString();
} else {
throw new IllegalArgumentException("Invalid record type " + Type.string(rec.getType()) + ". A or AAAA expected.");
}
final RequisitionNode n = new RequisitionNode();
final String host = rec.getName().toString();
final String nodeLabel = StringUtils.stripEnd(StringUtils.stripStart(host, "."), ".");
n.setBuilding(request.getForeignSource());
switch(request.getForeignIdHashSource()) {
case NODE_LABEL:
n.setForeignId(computeHashCode(nodeLabel));
LOG.debug("Generating foreignId from hash of nodelabel {}", nodeLabel);
break;
case IP_ADDRESS:
n.setForeignId(computeHashCode(addr));
LOG.debug("Generating foreignId from hash of ipAddress {}", addr);
break;
case NODE_LABEL_AND_IP_ADDRESS:
n.setForeignId(computeHashCode(nodeLabel + addr));
LOG.debug("Generating foreignId from hash of nodelabel+ipAddress {}{}", nodeLabel, addr);
break;
default:
n.setForeignId(computeHashCode(nodeLabel));
LOG.debug("Default case: Generating foreignId from hash of nodelabel {}", nodeLabel);
break;
}
n.setNodeLabel(nodeLabel);
final RequisitionInterface i = new RequisitionInterface();
i.setDescr("DNS-" + Type.string(rec.getType()));
i.setIpAddr(addr);
i.setSnmpPrimary(PrimaryType.PRIMARY);
i.setManaged(Boolean.TRUE);
i.setStatus(Integer.valueOf(1));
for (String service : request.getServices()) {
service = service.trim();
i.insertMonitoredService(new RequisitionMonitoredService(service));
LOG.debug("Adding provisioned service {}", service);
}
n.putInterface(i);
return n;
}
use of org.opennms.netmgt.provision.persist.requisition.RequisitionInterface in project opennms by OpenNMS.
the class VmwareImporter method getRequisition.
public Requisition getRequisition() {
logger.debug("Getting existing requisition (if any) for VMware management server {}", request.getHostname());
Requisition curReq = request.getExistingRequisition();
logger.debug("Building new requisition for VMware management server {}", request.getHostname());
Requisition newReq = buildVMwareRequisition();
logger.debug("Finished building new requisition for VMware management server {}", request.getHostname());
if (curReq == null) {
if (newReq == null) {
// FIXME Is this correct ? This is the old behavior
newReq = new Requisition(request.getForeignSource());
}
} else {
if (newReq == null) {
// If there is a requisition and the vCenter is not responding for some reason, it is better to use the old requisition,
// instead of returning an empty one, which can cause the lost of all the nodes from the DB.
newReq = curReq;
} else {
// The VMWare related assets and categories will be preserved.
for (RequisitionNode newNode : newReq.getNodes()) {
for (RequisitionNode curNode : curReq.getNodes()) {
if (newNode.getForeignId().equals(curNode.getForeignId())) {
// Add existing custom assets
for (RequisitionAsset asset : curNode.getAssets()) {
if (!asset.getName().startsWith("vmware")) {
newNode.putAsset(asset);
}
}
// Add existing custom categories
for (RequisitionCategory cat : curNode.getCategories()) {
if (!cat.getName().startsWith("VMWare")) {
newNode.putCategory(cat);
}
}
/*
* For each interface on the new requisition,
* - Retrieve the list of custom services from the corresponding interface on the existing requisition,
* matching the interface by the IP address
* - If the list of services is not empty, add them to the new interface
*/
for (RequisitionInterface intf : curNode.getInterfaces()) {
List<RequisitionMonitoredService> services = getManualyConfiguredServices(intf);
if (!services.isEmpty()) {
RequisitionInterface newIntf = getRequisitionInterface(newNode, intf.getIpAddr());
if (newIntf != null) {
newIntf.getMonitoredServices().addAll(services);
}
}
}
}
}
}
}
}
return newReq;
}
use of org.opennms.netmgt.provision.persist.requisition.RequisitionInterface in project opennms by OpenNMS.
the class SyslogOverlappingIpAddressIT method testAssociateSyslogsWithNodesWithOverlappingIpAddresses.
/**
* @see https://issues.opennms.org/browse/NMS-8798
*
* @throws Exception
*/
@Test
public void testAssociateSyslogsWithNodesWithOverlappingIpAddresses() throws Exception {
final Date startOfTest = new Date();
final String hostIpAddress = "1.2.3.4";
// Create requisition with two node in different locations but same IP
final RestClient client = new RestClient(testEnvironment.getServiceAddress(ContainerAlias.OPENNMS, 8980));
final Requisition requisition = new Requisition("overlapping");
final RequisitionNode node1 = new RequisitionNode();
node1.setNodeLabel("node_1");
node1.setLocation("MINION");
final RequisitionInterface interface1 = new RequisitionInterface();
interface1.setIpAddr(hostIpAddress);
interface1.setManaged(true);
interface1.setSnmpPrimary(PrimaryType.PRIMARY);
node1.setInterfaces(ImmutableList.of(interface1));
node1.setForeignId("node_1");
requisition.insertNode(node1);
final RequisitionNode node2 = new RequisitionNode();
node2.setNodeLabel("node_2");
node2.setLocation("BANANA");
final RequisitionInterface interface2 = new RequisitionInterface();
interface2.setIpAddr(hostIpAddress);
interface2.setManaged(true);
interface2.setSnmpPrimary(PrimaryType.PRIMARY);
node2.setInterfaces(ImmutableList.of(interface2));
node2.setForeignId("node_2");
requisition.insertNode(node2);
client.addOrReplaceRequisition(requisition);
client.importRequisition("overlapping");
// Wait for the nodes to be provisioned
final OnmsNode onmsNode1 = await().atMost(1, MINUTES).pollInterval(5, SECONDS).until(DaoUtils.findMatchingCallable(getDaoFactory().getDao(NodeDaoHibernate.class), new CriteriaBuilder(OnmsNode.class).eq("label", "node_1").toCriteria()), notNullValue());
final OnmsNode onmsNode2 = await().atMost(1, MINUTES).pollInterval(5, SECONDS).until(DaoUtils.findMatchingCallable(getDaoFactory().getDao(NodeDaoHibernate.class), new CriteriaBuilder(OnmsNode.class).eq("label", "node_2").toCriteria()), notNullValue());
// Sending syslog messages to each node and expect it to appear on the node
sendMessage(ContainerAlias.MINION, hostIpAddress, 1);
await().atMost(1, MINUTES).pollInterval(5, SECONDS).until(DaoUtils.countMatchingCallable(getDaoFactory().getDao(EventDaoHibernate.class), new CriteriaBuilder(OnmsEvent.class).eq("eventUei", "uei.opennms.org/vendor/cisco/syslog/SEC-6-IPACCESSLOGP/aclDeniedIPTraffic").ge("eventCreateTime", startOfTest).eq("node", onmsNode1).toCriteria()), is(1));
sendMessage(ContainerAlias.MINION_OTHER_LOCATION, hostIpAddress, 1);
await().atMost(1, MINUTES).pollInterval(5, SECONDS).until(DaoUtils.countMatchingCallable(getDaoFactory().getDao(EventDaoHibernate.class), new CriteriaBuilder(OnmsEvent.class).eq("eventUei", "uei.opennms.org/vendor/cisco/syslog/SEC-6-IPACCESSLOGP/aclDeniedIPTraffic").ge("eventCreateTime", startOfTest).eq("node", onmsNode2).toCriteria()), is(1));
}
Aggregations