コード例 #1
0
  /** {@inheritDoc} */
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String nodeIdString = request.getParameter("node");
    String labelType = request.getParameter("labeltype");
    String userLabel = request.getParameter("userlabel");

    if (nodeIdString == null) {
      throw new MissingParameterException("node", new String[] {"node", "labeltype", "userlabel"});
    }
    if (labelType == null) {
      throw new MissingParameterException(
          "labeltype", new String[] {"node", "labeltype", "userlabel"});
    }
    if (userLabel == null) {
      throw new MissingParameterException(
          "userlabel", new String[] {"node", "labeltype", "userlabel"});
    }

    try {
      final int nodeId = WebSecurityUtils.safeParseInt(nodeIdString);
      final OnmsNode node = NetworkElementFactory.getInstance(getServletContext()).getNode(nodeId);
      NodeLabelJDBCImpl oldLabel = new NodeLabelJDBCImpl(node.getLabel(), node.getLabelSource());
      NodeLabelJDBCImpl newLabel = null;

      if (labelType.equals("auto")) {
        newLabel = NodeLabelJDBCImpl.getInstance().computeLabel(nodeId);
      } else if (labelType.equals("user")) {
        newLabel = new NodeLabelJDBCImpl(userLabel, NodeLabelSource.USER);
      } else {
        throw new ServletException("Unexpected labeltype value: " + labelType);
      }

      final String newNodeLabel = newLabel.getLabel();
      boolean managedByProvisiond = node.getForeignSource() != null && node.getForeignId() != null;
      if (managedByProvisiond) {
        WebApplicationContext beanFactory =
            WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        final TransactionTemplate transactionTemplate =
            beanFactory.getBean(TransactionTemplate.class);
        final RequisitionAccessService requisitionService =
            beanFactory.getBean(RequisitionAccessService.class);
        transactionTemplate.execute(
            new TransactionCallback<RequisitionNode>() {
              @Override
              public RequisitionNode doInTransaction(TransactionStatus status) {
                MultivaluedMapImpl params = new MultivaluedMapImpl();
                params.putSingle("node-label", newNodeLabel);
                requisitionService.updateNode(node.getForeignSource(), node.getForeignId(), params);
                return requisitionService.getNode(node.getForeignSource(), node.getForeignId());
              }
            });
      }

      this.sendLabelChangeEvent(nodeId, oldLabel, newLabel);

      if (managedByProvisiond) {
        response.sendRedirect(
            Util.calculateUrlBase(
                request,
                "admin/nodelabelProvisioned.jsp?node="
                    + nodeIdString
                    + "&foreignSource="
                    + node.getForeignSource()));
      } else {
        NodeLabelJDBCImpl.getInstance().assignLabel(nodeId, newLabel);
        response.sendRedirect(
            Util.calculateUrlBase(request, "element/node.jsp?node=" + nodeIdString));
      }
    } catch (SQLException e) {
      throw new ServletException("Database exception", e);
    } catch (Throwable e) {
      throw new ServletException("Exception sending node label change event", e);
    }
  }
コード例 #2
0
ファイル: OnmsNode.java プロジェクト: eatlunchnow/opennms
  /**
   * mergeNodeAttributes
   *
   * @param scannedNode a {@link org.opennms.netmgt.model.OnmsNode} object.
   */
  public void mergeNodeAttributes(OnmsNode scannedNode, EventForwarder eventForwarder) {
    final String scannedLabel = scannedNode.getLabel();

    boolean send = false;

    if (m_oldLabel != null || m_oldLabelSource != null) {
      send = true;
    } else if (hasNewValue(scannedLabel, getLabel())) {
      m_oldLabel = getLabel();
      m_oldLabelSource = getLabelSource();
      send = true;
    }

    if (send) {
      LOG.debug("mergeNodeAttributes(): sending NODE_LABEL_CHANGED_EVENT_UEI");
      // Create a NODE_LABEL_CHANGED_EVENT_UEI event
      final EventBuilder bldr =
          new EventBuilder(
              EventConstants.NODE_LABEL_CHANGED_EVENT_UEI, "OnmsNode.mergeNodeAttributes");

      bldr.setNodeid(scannedNode.getId());
      bldr.setHost("host");

      if (m_oldLabel != null) {
        bldr.addParam(EventConstants.PARM_OLD_NODE_LABEL, m_oldLabel);
        if (m_oldLabelSource != null) {
          bldr.addParam(EventConstants.PARM_OLD_NODE_LABEL_SOURCE, m_oldLabelSource.toString());
        }
      }

      if (scannedLabel != null) {
        bldr.addParam(EventConstants.PARM_NEW_NODE_LABEL, scannedLabel);
        if (scannedNode.getLabelSource() != null) {
          bldr.addParam(
              EventConstants.PARM_NEW_NODE_LABEL_SOURCE, scannedNode.getLabelSource().toString());
        }
      }

      m_oldLabel = null;
      m_oldLabelSource = null;

      eventForwarder.sendNow(bldr.getEvent());

      // Update the node label value
      m_label = scannedLabel;
    } else {
      LOG.debug("mergeNodeAttributes(): skipping event.");
    }

    if (hasNewValue(scannedNode.getForeignSource(), getForeignSource())) {
      setForeignSource(scannedNode.getForeignSource());
    }

    if (hasNewValue(scannedNode.getForeignId(), getForeignId())) {
      setForeignId(scannedNode.getForeignId());
    }

    if (hasNewValue(scannedNode.getLabelSource(), getLabelSource())) {
      setLabelSource(scannedNode.getLabelSource());
    }

    if (hasNewValue(scannedNode.getNetBiosName(), getNetBiosDomain())) {
      setNetBiosName(scannedNode.getNetBiosDomain());
    }

    if (hasNewValue(scannedNode.getNetBiosDomain(), getNetBiosDomain())) {
      setNetBiosDomain(scannedNode.getNetBiosDomain());
    }

    if (hasNewValue(scannedNode.getOperatingSystem(), getOperatingSystem())) {
      setOperatingSystem(scannedNode.getOperatingSystem());
    }

    mergeAgentAttributes(scannedNode);

    mergeAdditionalCategories(scannedNode);
  }