use of org.opennms.rancid.RancidNode in project opennms by OpenNMS.
the class InventoryReportCalculator method calculate.
/**
* <p>calculate</p>
*/
public void calculate() {
rnbi = new RwsNbinventoryreport();
rnbi.setUser(user);
rnbi.setReportRequestDate(StringUtils.toStringEfficiently(reportRequestDate));
boolean withKey = false;
if (theField.compareTo("") != 0) {
withKey = true;
rnbi.setTheField(theField);
}
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());
rnbi.setReportDate(tmp_date.toString());
int totalGroups = 0;
int groupsMatching = 0;
int groupWithoutNodes = 0;
int groupsWithNodesWithoutinventoryAtAll = 0;
int groupsWithNodesWithoutinventoryAtReportDate = 0;
for (String groupName : getGroups()) {
LOG.debug("calculate:report group [{}]", groupName);
totalGroups++;
GroupSet gs = new GroupSet();
gs.setGroupSetName(groupName);
int totalNodes = 0;
int nodeMatching = 0;
int nodesWithoutinventoryAtAll = 0;
int nodesWithoutinventoryAtReportDate = 0;
boolean groupHasDevices = false;
boolean groupHasNodesWithoutinventoryAtAll = false;
boolean groupHasNodesWithoutinventoryAtrequestDate = false;
for (String deviceName : getDeviceListOnGroup(groupName)) {
totalNodes++;
LOG.debug("calculate:report device [{}]", deviceName);
RancidNode rancidNode = getFullNode(groupName, deviceName);
if (rancidNode == null) {
groupHasNodesWithoutinventoryAtAll = true;
nodesWithoutinventoryAtAll++;
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) {
LOG.debug("calculate: device has no configuration at this date[{}]", deviceName);
groupHasNodesWithoutinventoryAtrequestDate = true;
nodesWithoutinventoryAtReportDate++;
continue;
}
// we have groupname devicename and version
NodeBaseInventory nodeBaseInv = getNodeBaseInventory(deviceName, groupName, invNode.getVersionId());
Nbisinglenode nbisn = new Nbisinglenode();
boolean includeNbisn = false;
nbisn.setConfigurationurl(nodeBaseInv.getConfigurationurl());
nbisn.setCreationdate(nodeBaseInv.getCreationdate());
nbisn.setDevicename(nodeBaseInv.getDevicename());
nbisn.setGroupname(nodeBaseInv.getGroupname());
nbisn.setStatus(nodeBaseInv.getStatus());
nbisn.setSwconfigurationurl(nodeBaseInv.getSwconfigurationurl());
nbisn.setVersion(nodeBaseInv.getVersion());
List<InventoryElement2RP> ie2rpList = new ArrayList<>();
for (InventoryElement2 ie2 : nodeBaseInv.getIe()) {
InventoryElement2RP ie2rp = new InventoryElement2RP();
boolean addInventoryElement = false;
for (Tuple tuple : ie2.getTupleList()) {
if (withKey) {
if (Pattern.matches(theField, tuple.getDescription()) || Pattern.matches(theField, tuple.getName())) {
includeNbisn = true;
addInventoryElement = true;
}
} else {
includeNbisn = true;
addInventoryElement = true;
}
if (tuple.getName().equalsIgnoreCase("name")) {
ie2rp.setName(tuple.getDescription());
} else {
TupleRP trp = new TupleRP();
trp.setName(tuple.getName());
trp.setDescription(tuple.getDescription());
ie2rp.addTupleRP(trp);
}
}
for (InventoryMemory im : ie2.getMemoryList()) {
if (withKey) {
if (Pattern.matches(theField, "Memory") || Pattern.matches(theField, im.getType())) {
includeNbisn = true;
addInventoryElement = true;
}
} else {
includeNbisn = true;
addInventoryElement = true;
}
InventoryMemoryRP imrp = new InventoryMemoryRP();
imrp.setType(im.getType());
imrp.setSize(im.getSize());
ie2rp.addInventoryMemoryRP(imrp);
}
for (InventorySoftware is : ie2.getSoftwareList()) {
if (withKey) {
if (Pattern.matches(theField, "Software") || Pattern.matches(theField, is.getType()) || Pattern.matches(theField, is.getVersion())) {
includeNbisn = true;
addInventoryElement = true;
}
} else {
includeNbisn = true;
addInventoryElement = true;
}
InventorySoftwareRP isrp = new InventorySoftwareRP();
isrp.setType(is.getType());
isrp.setVersion(is.getVersion());
ie2rp.addInventorySoftwareRP(isrp);
}
if (addInventoryElement)
ie2rpList.add(ie2rp);
}
nbisn.setInventoryElement2RP(ie2rpList);
if (includeNbisn) {
nodeMatching++;
groupHasDevices = true;
gs.addNbisinglenode(nbisn);
}
}
gs.setTotalNodes(totalNodes);
gs.setNodesMatching(nodeMatching);
gs.setNodesWithoutinventoryAtReportDate(nodesWithoutinventoryAtReportDate);
gs.setNodesWithoutinventoryAtAll(nodesWithoutinventoryAtAll);
rnbi.addGroupSet(gs);
if (groupHasDevices)
groupsMatching++;
else
groupWithoutNodes++;
if (groupHasDevices && groupHasNodesWithoutinventoryAtAll)
groupsWithNodesWithoutinventoryAtAll++;
if (groupHasDevices && groupHasNodesWithoutinventoryAtrequestDate)
groupsWithNodesWithoutinventoryAtReportDate++;
}
// end for groups
rnbi.setTotalGroups(totalGroups);
rnbi.setGroupsMatching(groupsMatching);
rnbi.setGroupsWithNodesWithoutinventoryAtAll(groupsWithNodesWithoutinventoryAtAll);
rnbi.setGroupsWithNodesWithoutinventoryAtReportDate(groupsWithNodesWithoutinventoryAtReportDate);
rnbi.setGroupWithoutNodes(groupWithoutNodes);
}
use of org.opennms.rancid.RancidNode in project opennms by OpenNMS.
the class InventoryService method getRancidNodeList.
/**
* <p>getRancidNodeList</p>
*
* @param nodeid a int.
* @param group a {@link java.lang.String} object.
* @return a java$util$Map object.
*/
public Map<String, Object> getRancidNodeList(int nodeid, String group) {
LOG.debug("getRancidlist start: nodeid: {} group: {}", nodeid, group);
Map<String, Object> nodeModel = getRancidNodeBase(nodeid);
String rancidName = (String) nodeModel.get("id");
List<InventoryWrapper> ranlist = new ArrayList<>();
RancidNode rn;
try {
rn = RWSClientApi.getRWSRancidNodeInventory(m_cp, group, rancidName);
nodeModel.put("devicename", rn.getDeviceName());
} catch (RancidApiException e) {
if (e.getRancidCode() == 2) {
LOG.debug("No Inventory found in CVS repository for nodeid:{} nodeLabel: {}", nodeid, rancidName);
} else {
nodeModel.put("RWSStatus", e.getLocalizedMessage());
LOG.error(e.getLocalizedMessage());
}
return nodeModel;
}
RWSResourceList versionList;
try {
versionList = RWSClientApi.getRWSResourceConfigList(m_cp, group, rancidName);
} catch (RancidApiException e) {
nodeModel.put("RWSStatus", e.getLocalizedMessage());
LOG.error(e.getLocalizedMessage());
return nodeModel;
}
List<String> versionListStr = versionList.getResource();
Iterator<String> iter1 = versionListStr.iterator();
String vs;
while (iter1.hasNext()) {
vs = iter1.next();
InventoryNode in = (InventoryNode) rn.getNodeVersions().get(vs);
InventoryWrapper inwr = new InventoryWrapper(in.getVersionId(), in.getCreationDate(), group, in.getConfigurationUrl());
ranlist.add(inwr);
}
nodeModel.put("grouptable", ranlist);
return nodeModel;
}
use of org.opennms.rancid.RancidNode in project opennms by OpenNMS.
the class InventoryService method createNodeOnRouterDb.
/**
* <p>createNodeOnRouterDb</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 createNodeOnRouterDb(String groupName, String deviceName, String deviceType, String status, String comment) {
LOG.debug("InventoryService createNodeOnRouterDb: {}->{}:{}:{}:{}", groupName, deviceName, deviceType, status, comment);
try {
RancidNode rn = new RancidNode(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.createRWSRancidNode(m_cp, rn);
} catch (Throwable e) {
LOG.debug("createNodeOnRouterDb 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 InventoryService method switchStatus.
/**
* <p>switchStatus</p>
*
* @param groupName a {@link java.lang.String} object.
* @param deviceName a {@link java.lang.String} object.
* @return a boolean.
*/
public boolean switchStatus(String groupName, String deviceName) {
LOG.debug("InventoryService switchStatus {}/{}", groupName, deviceName);
try {
RancidNode rn = RWSClientApi.getRWSRancidNodeTLO(m_cp, groupName, deviceName);
if (rn.isStateUp()) {
LOG.debug("InventoryService switchStatus :down");
rn.setStateUp(false);
} else {
LOG.debug("InventoryService switchStatus :up");
rn.setStateUp(true);
}
RWSClientApi.updateRWSRancidNode(m_cp, rn);
} catch (Throwable e) {
LOG.debug("switchStatus 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 InventoryService method getInventory.
/**
* <p>getInventory</p>
*
* @param nodeid a int.
* @param group a {@link java.lang.String} object.
* @param version a {@link java.lang.String} object.
* @return a java$util$Map object.
*/
public Map<String, Object> getInventory(int nodeid, String group, String version) {
LOG.debug("getInventoryNode start: nodeid: {} group: {} version: {}", nodeid, group, version);
Map<String, Object> nodeModel = getRancidNodeBase(nodeid);
String rancidName = (String) nodeModel.get("id");
try {
RancidNode rn = RWSClientApi.getRWSRancidNodeInventory(m_cp, group, rancidName);
InventoryNode in = (InventoryNode) rn.getNodeVersions().get(version);
nodeModel.put("devicename", rancidName);
nodeModel.put("groupname", group);
nodeModel.put("version", version);
nodeModel.put("status", in.getParent().getState());
nodeModel.put("creationdate", in.getCreationDate());
nodeModel.put("swconfigurationurl", in.getSoftwareImageUrl());
nodeModel.put("configurationurl", in.getConfigurationUrl());
LOG.debug("getInventoryNode date: {}", in.getCreationDate());
List<InventoryElement2> ie = RWSClientApi.getRWSRancidNodeInventoryElement2(m_cp, rn, version);
Iterator<InventoryElement2> iter1 = ie.iterator();
while (iter1.hasNext()) {
InventoryElement2 ietmp = iter1.next();
LOG.debug("Adding inventory: {}", ietmp.expand());
}
nodeModel.put("inventory", ie);
} catch (RancidApiException e) {
if (e.getRancidCode() == 2) {
LOG.debug("No Inventory found in CVS repository for nodeid:{} nodeLabel: {}", nodeid, rancidName);
} else {
nodeModel.put("RWSStatus", e.getLocalizedMessage());
LOG.error(e.getLocalizedMessage());
}
}
return nodeModel;
}
Aggregations