Search in sources :

Example 6 with RancidNodeAuthentication

use of org.opennms.rancid.RancidNodeAuthentication 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));
    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)

Aggregations

RancidNodeAuthentication (org.opennms.rancid.RancidNodeAuthentication)6 RancidApiException (org.opennms.rancid.RancidApiException)3 RancidNode (org.opennms.rancid.RancidNode)3 RWSResourceList (org.opennms.rancid.RWSResourceList)2 OnmsAssetRecord (org.opennms.netmgt.model.OnmsAssetRecord)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1