Search in sources :

Example 6 with RancidApiException

use of org.opennms.rancid.RancidApiException in project opennms by OpenNMS.

the class InventoryService method checkRancidNode.

/**
 * <p>checkRancidNode</p>
 *
 * @param deviceName a {@link java.lang.String} object.
 * @return a boolean.
 */
public boolean checkRancidNode(String deviceName) {
    LOG.debug("checkRancidNode start {}", deviceName);
    // Group list
    try {
        RWSResourceList groups = RWSClientApi.getRWSResourceGroupsList(m_cp);
        List<String> grouplist = groups.getResource();
        Iterator<String> iter1 = grouplist.iterator();
        if (iter1.hasNext()) {
            String groupname = iter1.next();
            LOG.debug("checkRancidNode {} group {}", deviceName, groupname);
            try {
                RancidNode rn = RWSClientApi.getRWSRancidNodeTLO(m_cp, groupname, deviceName);
                if (rn != null) {
                    return true;
                } else {
                    return false;
                }
            } catch (RancidApiException e) {
                LOG.debug("No inventory information associated to {}", deviceName);
                return false;
            }
        }
    } catch (Throwable e) {
        return false;
    }
    return true;
}
Also used : RancidNode(org.opennms.rancid.RancidNode) RWSResourceList(org.opennms.rancid.RWSResourceList) RancidApiException(org.opennms.rancid.RancidApiException)

Example 7 with RancidApiException

use of org.opennms.rancid.RancidApiException 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);
        }
    }
}
Also used : RancidNode(org.opennms.rancid.RancidNode) RancidNodeAuthentication(org.opennms.rancid.RancidNodeAuthentication) OnmsNode(org.opennms.netmgt.model.OnmsNode) TransactionStatus(org.springframework.transaction.TransactionStatus) RancidApiException(org.opennms.rancid.RancidApiException)

Example 8 with RancidApiException

use of org.opennms.rancid.RancidApiException 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;
}
Also used : RancidNode(org.opennms.rancid.RancidNode) ArrayList(java.util.ArrayList) RancidApiException(org.opennms.rancid.RancidApiException) RWSResourceList(org.opennms.rancid.RWSResourceList) InventoryNode(org.opennms.rancid.InventoryNode)

Example 9 with RancidApiException

use of org.opennms.rancid.RancidApiException in project opennms by OpenNMS.

the class InventoryService method getBuckets.

/**
 * <p>getBuckets</p>
 *
 * @param nodeid a int.
 * @return a java$util$Map object.
 */
public Map<String, Object> getBuckets(int nodeid) {
    LOG.debug("getBuckets start: nodeid: {}", nodeid);
    Map<String, Object> nodeModel = getRancidNodeBase(nodeid);
    String rancidName = (String) nodeModel.get("id");
    List<BucketItem> bucketlist = new ArrayList<>();
    try {
        RWSBucket bucket = RWSClientApi.getBucket(m_cp, rancidName);
        nodeModel.put("bucketexist", true);
        bucketlist.addAll(bucket.getBucketItem());
    } catch (RancidApiException e) {
        if (e.getRancidCode() == 2) {
            nodeModel.put("bucketexist", false);
            LOG.debug("No entry in storage for nodeid:{} nodeLabel: {}", nodeid, rancidName);
        } else {
            nodeModel.put("RWSStatus", e.getLocalizedMessage());
            LOG.error(e.getLocalizedMessage());
        }
    }
    nodeModel.put("bucketlistsize", bucketlist.size());
    nodeModel.put("bucketitems", bucketlist);
    return nodeModel;
}
Also used : ArrayList(java.util.ArrayList) BucketItem(org.opennms.rancid.RWSBucket.BucketItem) RancidApiException(org.opennms.rancid.RancidApiException) RWSBucket(org.opennms.rancid.RWSBucket)

Example 10 with RancidApiException

use of org.opennms.rancid.RancidApiException 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;
}
Also used : RancidNode(org.opennms.rancid.RancidNode) RancidApiException(org.opennms.rancid.RancidApiException) InventoryElement2(org.opennms.rancid.InventoryElement2) InventoryNode(org.opennms.rancid.InventoryNode)

Aggregations

RancidApiException (org.opennms.rancid.RancidApiException)10 RancidNode (org.opennms.rancid.RancidNode)9 RWSResourceList (org.opennms.rancid.RWSResourceList)6 InventoryNode (org.opennms.rancid.InventoryNode)5 ArrayList (java.util.ArrayList)4 RancidNodeAuthentication (org.opennms.rancid.RancidNodeAuthentication)3 RWSBucket (org.opennms.rancid.RWSBucket)2 BucketItem (org.opennms.rancid.RWSBucket.BucketItem)2 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 InventoryElement2 (org.opennms.rancid.InventoryElement2)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1