use of org.opennms.rancid.RancidNode in project opennms by OpenNMS.
the class InventoryService method updateNodeOnRouterDb.
/**
* <p>updateNodeOnRouterDb</p>
*
* @param groupName a {@link java.lang.String} object.
* @param deviceName a {@link java.lang.String} object.
* @param deviceType a {@link java.lang.String} object.
* @param status a {@link java.lang.String} object.
* @param comment a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean updateNodeOnRouterDb(String groupName, String deviceName, String deviceType, String status, String comment) {
LOG.debug("InventoryService updateNodeOnRouterDb: {}->{}:{}:{}:{}", groupName, deviceName, deviceType, status, comment);
try {
RancidNode rn = RWSClientApi.getRWSRancidNodeTLO(m_cp, groupName, deviceName);
rn.setDeviceType(deviceType);
if (comment != null)
rn.setComment(comment);
if ("up".equalsIgnoreCase(status)) {
rn.setStateUp(true);
} else if ("down".equalsIgnoreCase(status)) {
rn.setStateUp(false);
}
RWSClientApi.updateRWSRancidNode(m_cp, rn);
} catch (Throwable e) {
LOG.debug("updateNodeOnRouterDb has given exception on node {}/{} {}", groupName, deviceName, e.getMessage());
return false;
}
return true;
}
use of org.opennms.rancid.RancidNode in project opennms by OpenNMS.
the class RancidProvisioningAdapter method buildRancidNodeMap.
private void buildRancidNodeMap() {
if (!isAdapterConfigured()) {
LOG.info("RANCID is not configured. Skipping node map generation.");
return;
}
List<OnmsNode> nodes = m_nodeDao.findAllProvisionedNodes();
m_onmsNodeRancidNodeMap = new ConcurrentHashMap<Integer, RancidNode>(nodes.size());
m_onmsNodeIpMap = new ConcurrentHashMap<Integer, String>(nodes.size());
for (OnmsNode onmsNode : nodes) {
String ipaddr = getSuitableIpForRancid(onmsNode);
if (ipaddr != null)
m_onmsNodeIpMap.putIfAbsent(onmsNode.getId(), ipaddr);
RancidNode rNode = getSuitableRancidNode(onmsNode);
if (rNode != null) {
m_onmsNodeRancidNodeMap.putIfAbsent(onmsNode.getId(), rNode);
}
}
}
use of org.opennms.rancid.RancidNode in project opennms by OpenNMS.
the class RancidProvisioningAdapter method doAdd.
/**
* <p>doAdd</p>
*
* @param nodeId a int.
* @param cp a {@link org.opennms.rancid.ConnectionProperties} object.
* @param retry a boolean.
* @throws org.opennms.netmgt.provision.ProvisioningAdapterException if any.
*/
public void doAdd(int nodeId, ConnectionProperties cp, boolean retry) throws ProvisioningAdapterException {
if (!isAdapterConfigured()) {
return;
}
LOG.debug("doAdd: adding nodeid: {}", nodeId);
final OnmsNode node = m_nodeDao.get(nodeId);
Assert.notNull(node, "doAdd: failed to return node for given nodeId:" + nodeId);
String ipaddress = m_template.execute(new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus arg0) {
return getSuitableIpForRancid(node);
}
});
RancidNode rNode = getSuitableRancidNode(node);
rNode.setStateUp(true);
try {
m_rwsConfig.getWriteLock().lock();
try {
if (m_onmsNodeRancidNodeMap.containsValue(rNode)) {
LOG.error("doAdd: Error Duplicate node: {}", node);
ProvisioningAdapterException e = new ProvisioningAdapterException("Duplicate node has been added: " + node);
sendAndThrow(nodeId, e);
return;
}
LOG.debug("doAdd: adding to router.db node: {}", node.getLabel());
RWSClientApi.createRWSRancidNode(cp, rNode);
m_onmsNodeIpMap.putIfAbsent(nodeId, ipaddress);
m_onmsNodeRancidNodeMap.put(Integer.valueOf(nodeId), rNode);
RWSClientApi.createOrUpdateRWSAuthNode(cp, rNode.getAuth());
} finally {
m_rwsConfig.getWriteLock().unlock();
}
} catch (ProvisioningAdapterException ae) {
sendAndThrow(nodeId, ae);
} catch (Throwable e) {
cp = getStandByRWSConnection();
if (retry && cp != null) {
LOG.info("doAdd: retry Add on standByConn: {}", cp.getUrl());
doAdd(nodeId, cp, false);
} else {
sendAndThrow(nodeId, e);
}
}
}
use of org.opennms.rancid.RancidNode in project opennms by OpenNMS.
the class RancidProvisioningAdapter method doUpdate.
/**
* <p>doUpdate</p>
*
* @param nodeId a int.
* @param cp a {@link org.opennms.rancid.ConnectionProperties} object.
* @param retry a boolean.
* @throws org.opennms.netmgt.provision.ProvisioningAdapterException if any.
*/
public void doUpdate(int nodeId, ConnectionProperties cp, boolean retry) throws ProvisioningAdapterException {
if (!isAdapterConfigured()) {
return;
}
LOG.debug("doUpdate: updating nodeid: {}", nodeId);
RancidNode rLocalNode = m_onmsNodeRancidNodeMap.get(Integer.valueOf(nodeId));
Assert.notNull(rLocalNode, "doUpdate: failed to get local node for given nodeId:" + nodeId);
LOG.debug("doUpdate: found local map Node: {}", rLocalNode);
final OnmsNode node = m_nodeDao.get(nodeId);
Assert.notNull(node, "doUpdate: failed to return node for given nodeId:" + nodeId);
String ipaddress = m_template.execute(new TransactionCallback<String>() {
@Override
public String doInTransaction(TransactionStatus arg0) {
return getSuitableIpForRancid(node);
}
});
m_onmsNodeIpMap.put(nodeId, ipaddress);
RancidNode rUpdatedNode = getSuitableRancidNode(node);
LOG.debug("doUpdate: found updated Node : {}", rUpdatedNode);
if (rLocalNode.getDeviceName() == null) {
LOG.warn("Current Rancid Node device name is null, aborting.");
return;
}
if (rUpdatedNode.getDeviceName() == null) {
LOG.warn("Updated Rancid Node device name is null, aborting.");
return;
}
if (rLocalNode.getDeviceName().equalsIgnoreCase(rUpdatedNode.getDeviceName())) {
try {
RancidNode rRemoteNode = RWSClientApi.getRWSRancidNodeTLO(cp, rLocalNode.getGroup(), rLocalNode.getDeviceName());
RancidNodeAuthentication rRemoteNodeAuth = RWSClientApi.getRWSAuthNode(cp, rLocalNode.getDeviceName());
LOG.debug("doUpdate: found Node in router.db : {}", rRemoteNode);
if (!rUpdatedNode.getDeviceType().equalsIgnoreCase(rRemoteNode.getDeviceType())) {
try {
// don't change the status of the node in update operation
rUpdatedNode.setStateUp(rRemoteNode.isStateUp());
LOG.debug("doUpdate: updating router.db");
RWSClientApi.updateRWSRancidNode(cp, rLocalNode);
} catch (Throwable e) {
LOG.error("doUpdate: failed to update node: {} Exception: {}", e.getMessage(), nodeId);
}
}
if (updateAuth(rUpdatedNode.getAuth(), rRemoteNodeAuth)) {
LOG.debug("doUpdate: updating authentication data");
try {
RWSClientApi.updateRWSAuthNode(cp, rUpdatedNode.getAuth());
} catch (Throwable e) {
LOG.error("doUpdate: Failed to update node authentication data: {} Exception: {}", e.getMessage(), nodeId);
}
}
rUpdatedNode.setStateUp(rLocalNode.isStateUp());
m_onmsNodeRancidNodeMap.put(nodeId, rUpdatedNode);
} catch (RancidApiException re) {
if (re.getRancidCode() == RancidApiException.RWS_RESOURCE_NOT_FOUND) {
LOG.warn("doUpdate: node not found in router.db: {}", rUpdatedNode);
try {
LOG.debug("doUpdate: adding Node to router.db for nodeid: {}", nodeId);
rUpdatedNode.setStateUp(true);
RWSClientApi.createRWSRancidNode(cp, rUpdatedNode);
RWSClientApi.createOrUpdateRWSAuthNode(cp, rUpdatedNode.getAuth());
m_onmsNodeRancidNodeMap.put(nodeId, rUpdatedNode);
} catch (RancidApiException e) {
LOG.error("doUpdate: Failed to create node: {} Exception: {}", e.getMessage(), nodeId);
sendAndThrow(nodeId, e);
}
} else {
cp = getStandByRWSConnection();
if (retry && cp != null) {
LOG.info("doUpdate: retry Update on standByConn: {}", cp.getUrl());
doUpdate(nodeId, cp, false);
} else {
sendAndThrow(nodeId, re);
}
}
}
} else {
LOG.debug("doUpdate: the device name is changed for Nodeid: {}", nodeId);
LOG.debug("doUpdate: calling doDelete for NodeId: {}", nodeId);
doDelete(nodeId, cp, retry);
try {
LOG.debug("doUpdate: adding Node to router.db for nodeid: {}", nodeId);
rUpdatedNode.setStateUp(true);
RWSClientApi.createRWSRancidNode(cp, rUpdatedNode);
RWSClientApi.createOrUpdateRWSAuthNode(cp, rUpdatedNode.getAuth());
m_onmsNodeRancidNodeMap.put(nodeId, rUpdatedNode);
m_onmsNodeIpMap.put(nodeId, ipaddress);
} catch (RancidApiException e) {
LOG.error("doUpdate: Failed to create node: {} Exception: {}", e.getMessage(), nodeId);
sendAndThrow(nodeId, e);
}
}
}
use of org.opennms.rancid.RancidNode in project opennms by OpenNMS.
the class ConfigurationReportCalculator method calculate.
/**
* <p>calculate</p>
*/
public void calculate() {
rlist = new RwsRancidlistreport();
rlist.setUser(user);
rlist.setReportRequestDate(StringUtils.toStringEfficiently(reportRequestDate));
// parse date
SimpleDateFormat format = new SimpleDateFormat("yyyy/M/d");
Date tmp_date = new Date();
try {
tmp_date = format.parse(theDate);
} catch (ParseException pe) {
tmp_date = Calendar.getInstance().getTime();
}
LOG.debug("calculate:report date[{}]", tmp_date.toString());
rlist.setReportDate(tmp_date.toString());
int totalGroups = 0;
int groupsMatching = 0;
int groupWithoutNodes = 0;
int groupsWithNodesWithoutconfigurationAtAll = 0;
int groupsWithNodesWithoutconfigurationAtReportDate = 0;
for (String groupName : getGroups()) {
LOG.debug("calculate:report group [{}]", groupName);
totalGroups++;
GroupXSet gs = new GroupXSet();
gs.setGroupXSetName(groupName);
int totalNodes = 0;
int nodeMatching = 0;
int nodesWithoutConfigurationAtAll = 0;
int nodesWithoutConfigurationAtReportDate = 0;
boolean groupHasDevices = false;
boolean groupHasNodesWithoutconfigurationAtAll = false;
boolean groupHasNodesWithoutconfigurationAtrequestDate = false;
for (String deviceName : getDeviceListOnGroup(groupName)) {
totalNodes++;
NodeSet ns = new NodeSet();
ns.setDevicename(deviceName);
ns.setGroupname(groupName);
LOG.debug("calculate:report device [{}]", deviceName);
RancidNode rancidNode = getFullNode(groupName, deviceName);
if (rancidNode == null) {
ns.setVersion("No Configurations found");
groupHasNodesWithoutconfigurationAtAll = true;
nodesWithoutConfigurationAtAll++;
gs.addNodeSet(ns);
continue;
}
InventoryNode invNode = new InventoryNode(rancidNode);
boolean found = false;
for (String versionMatch : getVersionListOnDevice(deviceName, groupName)) {
invNode = (InventoryNode) rancidNode.getNodeVersions().get(versionMatch);
LOG.debug("calculate:report parsing InventoryNode version[{}] date [{}]", invNode.getVersionId(), invNode.getCreationDate());
if (tmp_date.compareTo(invNode.getCreationDate()) > 0) {
found = true;
LOG.debug("calculate:report Date found is [{}] version is [{}]", invNode.getCreationDate(), versionMatch);
break;
}
}
// end for on version
if (found == false) {
// skip device
LOG.debug("calculate:report device has no inventory at this date[{}]", deviceName);
groupHasNodesWithoutconfigurationAtrequestDate = true;
nodesWithoutConfigurationAtReportDate++;
ns.setVersion("No configuration found at Report Date");
} else {
ns.setVersion(invNode.getVersionId());
ns.setConfigurationurl(invNode.getConfigurationUrl());
ns.setSwconfigurationurl(invNode.getSoftwareImageUrl());
ns.setStatus(rancidNode.getState());
ns.setCreationdate(invNode.getCreationDate().toString());
groupHasDevices = true;
nodeMatching++;
}
gs.addNodeSet(ns);
}
// end for on devices
gs.setTotalNodes(totalNodes);
gs.setNodesMatching(nodeMatching);
gs.setNodesWithoutconfigurationAtReportDate(nodesWithoutConfigurationAtReportDate);
gs.setNodesWithoutconfigurationAtAll(nodesWithoutConfigurationAtAll);
rlist.addGroupXSet(gs);
if (groupHasDevices)
groupsMatching++;
else
groupWithoutNodes++;
if (groupHasDevices && groupHasNodesWithoutconfigurationAtAll)
groupsWithNodesWithoutconfigurationAtAll++;
if (groupHasDevices && groupHasNodesWithoutconfigurationAtrequestDate)
groupsWithNodesWithoutconfigurationAtReportDate++;
}
// end for of groups
rlist.setTotalGroups(totalGroups);
rlist.setGroupWithoutNodes(groupWithoutNodes);
rlist.setGroupsMatching(groupsMatching);
rlist.setGroupsWithNodesWithoutconfigurationAtAll(groupsWithNodesWithoutconfigurationAtAll);
rlist.setGroupsWithNodesWithoutconfigurationAtReportDate(groupsWithNodesWithoutconfigurationAtReportDate);
}
Aggregations