use of org.opennms.rancid.InventoryElement2 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<InventoryElement2RP>();
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.InventoryElement2 in project opennms by OpenNMS.
the class NodeBaseInventory method expand.
/**
* <p>expand</p>
*
* @return a {@link java.lang.String} object.
*/
public String expand() {
Iterator<InventoryElement2> iter1 = ie.iterator();
String tot = "";
while (iter1.hasNext()) {
InventoryElement2 tmp = iter1.next();
tot = tot + "<" + tmp.expand() + ">\n";
}
return tot;
}
use of org.opennms.rancid.InventoryElement2 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