private boolean isRuleMatchingFilter(final Notification notif, final String rule) {
   try {
     return FilterDaoFactory.getInstance().isRuleMatching(rule);
   } catch (FilterParseException e) {
     LOG.error("Invalid filter rule for notification {}: {}", notif.getName(), notif.getRule(), e);
     throw e;
   }
 }
  /** {@inheritDoc} */
  @Override
  public List<InetAddress> getIpList(final Package pkg) {
    try {
      getReadLock().lock();

      if (pkg == null) return null;

      final Filter filter = pkg.getFilter();
      if (filter == null) return null;

      final StringBuffer filterRules = new StringBuffer(filter.getContent());

      LOG.debug("getIpList: package is {}. filter rules are: {}", pkg.getName(), filterRules);
      FilterDaoFactory.getInstance().flushActiveIpAddressListCache();
      return FilterDaoFactory.getInstance().getActiveIPAddressList(filterRules.toString());
    } finally {
      getReadLock().unlock();
    }
  }
 /**
  * getIpList
  *
  * @param pkg a {@link org.opennms.netmgt.config.snmpinterfacepoller.Package} object.
  * @return a {@link java.util.List} object.
  */
 public List<InetAddress> getIpList(Package pkg) {
   StringBuffer filterRules = new StringBuffer(pkg.getFilter().getContent());
   if (m_verifyServer) {
     filterRules.append(" & (serverName == ");
     filterRules.append('\"');
     filterRules.append(m_localServer);
     filterRules.append('\"');
     filterRules.append(")");
   }
   if (log().isDebugEnabled())
     log()
         .debug(
             "createPackageIpMap: package is "
                 + pkg.getName()
                 + ". filer rules are  "
                 + filterRules.toString());
   List<InetAddress> ipList =
       FilterDaoFactory.getInstance().getActiveIPAddressList(filterRules.toString());
   return ipList;
 }
  /** {@inheritDoc} */
  public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    ThreadCategory.setPrefix(MapsConstants.LOG4J_CATEGORY);
    log = ThreadCategory.getInstance(this.getClass());
    String action = request.getParameter("action");
    String elems = request.getParameter("elems");
    log.debug("Adding Nodes action:" + action + ", elems=" + elems);

    BufferedWriter bw =
        new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
    try {
      Integer[] nodeids = null;

      boolean actionfound = false;

      if (action.equals(MapsConstants.ADDNODES_ACTION)) {
        log.debug("Adding nodes by id: " + elems);
        actionfound = true;
        String[] snodeids = elems.split(",");
        nodeids = new Integer[snodeids.length];
        for (int i = 0; i < snodeids.length; i++) {
          nodeids[i] = new Integer(snodeids[i]);
        }
      }

      if (action.equals(MapsConstants.ADDNODES_BY_CATEGORY_ACTION)) {
        log.debug("Adding nodes by category: " + elems);
        actionfound = true;
        String categoryName = elems;
        CategoryFactory.init();
        CatFactory cf = CategoryFactory.getInstance();
        cf.getReadLock().lock();
        try {
          final String rule = cf.getEffectiveRule(categoryName);
          final List<InetAddress> nodeIPs = FilterDaoFactory.getInstance().getIPAddressList(rule);
          LogUtils.debugf(this, "ips found: %s", nodeIPs.toString());
          nodeids = new Integer[nodeIPs.size()];
          for (int i = 0; i < nodeIPs.size(); i++) {
            final InetAddress nodeIp = nodeIPs.get(i);
            final List<Integer> ids =
                NetworkElementFactory.getInstance(getServletContext())
                    .getNodeIdsWithIpLike(InetAddressUtils.str(nodeIp));
            LogUtils.debugf(this, "Ids by ipaddress %s: %s", nodeIp, ids.toString());
            nodeids[i] = ids.get(0);
          }
        } finally {
          cf.getReadLock().unlock();
        }
      }

      if (action.equals(MapsConstants.ADDNODES_BY_LABEL_ACTION)) {
        log.debug("Adding nodes by label: " + elems);
        actionfound = true;
        List<OnmsNode> nodes = NetworkElementFactory.getInstance(getServletContext()).getAllNodes();
        nodeids = new Integer[nodes.size()];
        for (int i = 0; i < nodes.size(); i++) {
          nodeids[i] = nodes.get(i).getId();
        }
      }

      if (action.equals(MapsConstants.ADDRANGE_ACTION)) {
        log.debug("Adding nodes by range: " + elems);
        actionfound = true;
        nodeids =
            (Integer[])
                NetworkElementFactory.getInstance(getServletContext())
                    .getNodeIdsWithIpLike(elems)
                    .toArray(new Integer[0]);
      }

      if (action.equals(MapsConstants.ADDNODES_NEIG_ACTION)) {
        log.debug("Adding nodes neighbor of:" + elems);
        actionfound = true;
        nodeids =
            (Integer[])
                NetworkElementFactory.getInstance(getServletContext())
                    .getLinkedNodeIdOnNode(WebSecurityUtils.safeParseInt(elems))
                    .toArray(new Integer[0]);
      }

      if (action.equals(MapsConstants.ADDNODES_WITH_NEIG_ACTION)) {
        log.debug("Adding nodes with neighbor of:" + elems);
        actionfound = true;
        Set<Integer> linkednodeids =
            NetworkElementFactory.getInstance(getServletContext())
                .getLinkedNodeIdOnNode(WebSecurityUtils.safeParseInt(elems));
        linkednodeids.add(new Integer(elems));
        nodeids = linkednodeids.toArray(new Integer[linkednodeids.size()]);
      }

      VMap map = manager.openMap();
      if (log.isDebugEnabled()) log.debug("Got map from manager " + map);

      List<VElement> velems = new ArrayList<VElement>();
      // response for addElement
      if (actionfound) {
        log.debug("Before Checking map contains elems");

        for (int i = 0; i < nodeids.length; i++) {
          int elemId = nodeids[i].intValue();
          if (map.containsElement(elemId, MapsConstants.NODE_TYPE)) {
            log.debug(
                "Action: "
                    + action
                    + " . Map Contains Element: "
                    + elemId
                    + MapsConstants.NODE_TYPE);
            continue;
          }

          velems.add(manager.newElement(map.getId(), elemId, MapsConstants.NODE_TYPE));
        } // end for

        // get links and add elements to map
        map = manager.addElements(map, velems);
        log.debug("After getting/adding links");

        bw.write(ResponseAssembler.getAddElementResponse(null, velems, map.getLinks()));
      }
    } catch (Throwable e) {
      log.error("Error while adding nodes for action: " + action, e);
      bw.write(ResponseAssembler.getMapErrorResponse(action));
    } finally {
      bw.close();
    }

    return null;
  }
Example #5
0
  @Override
  protected void setUp() throws Exception {
    EventIpcManager m_eventIpcManager;
    NodeDao m_nodeDao;

    MockLogAppender.setupLogging();

    Resource threshdResource = new ClassPathResource("/etc/thresholds.xml");
    File homeDir = threshdResource.getFile().getParentFile().getParentFile();
    System.setProperty("opennms.home", homeDir.getAbsolutePath());

    // Test setup
    m_eventIpcManager = m_easyMockUtils.createMock(EventIpcManager.class);
    EventIpcManagerFactory.setIpcManager(m_eventIpcManager);
    m_nodeDao = m_easyMockUtils.createMock(NodeDao.class);
    m_ipIfDao = m_easyMockUtils.createMock(IpInterfaceDao.class);
    m_scheduler = new MockScheduler();

    m_eventIpcManager.addEventListener(isA(EventListener.class));
    expectLastCall().anyTimes();
    m_eventIpcManager.addEventListener(isA(EventListener.class), isACollection(String.class));
    expectLastCall().anyTimes();
    m_eventIpcManager.addEventListener(isA(EventListener.class), isA(String.class));
    expectLastCall().anyTimes();
    m_eventIpcManager.removeEventListener(isA(EventListener.class));
    expectLastCall().anyTimes();

    //        MockNetwork m_network = new MockNetwork();
    //        m_network.setCriticalService("ICMP");
    //        m_network.addNode(1, "Router");
    //        m_network.addInterface("192.168.1.1");
    //        m_network.addService("ICMP");
    //        m_network.addService("SMTP");
    //        m_network.addInterface("192.168.1.2");
    //        m_network.addService("ICMP");
    //        m_network.addService("SMTP");
    //        m_network.addNode(2, "Server");
    //        m_network.addInterface("192.168.1.3");
    //        m_network.addService("ICMP");
    //        m_network.addService("HTTP");
    //        m_network.addNode(3, "Firewall");
    //        m_network.addInterface("192.168.1.4");
    //        m_network.addService("SMTP");
    //        m_network.addService("HTTP");
    //        m_network.addInterface("192.168.1.5");
    //        m_network.addService("SMTP");
    //        m_network.addService("HTTP");
    //
    //        MockDatabase m_db = new MockDatabase();
    //        m_db.populate(m_network);
    //
    //        DataSourceFactory.setInstance(m_db);

    // Mock the FilterDao without using EasyMockUtils so that it can be verified separately
    m_filterDao = EasyMock.createMock(FilterDao.class);
    List<InetAddress> allIps = new ArrayList<InetAddress>();
    allIps.add(addr("192.168.1.1"));
    allIps.add(addr("192.168.1.2"));
    allIps.add(addr("192.168.1.3"));
    allIps.add(addr("192.168.1.4"));
    allIps.add(addr("192.168.1.5"));
    expect(m_filterDao.getActiveIPAddressList("IPADDR IPLIKE *.*.*.*"))
        .andReturn(allIps)
        .anyTimes();
    expect(m_filterDao.getActiveIPAddressList("IPADDR IPLIKE 1.1.1.1"))
        .andReturn(new ArrayList<InetAddress>(0))
        .anyTimes();
    EasyMock.replay(m_filterDao);
    FilterDaoFactory.setInstance(m_filterDao);

    // This call will also ensure that the poll-outages.xml file can parse IPv4
    // and IPv6 addresses.
    Resource resource = new ClassPathResource("etc/poll-outages.xml");
    PollOutagesConfigFactory factory = new PollOutagesConfigFactory(resource);
    factory.afterPropertiesSet();
    PollOutagesConfigFactory.setInstance(factory);

    final MockTransactionTemplate transTemplate = new MockTransactionTemplate();
    transTemplate.afterPropertiesSet();

    m_collectd = new Collectd();
    m_collectd.setEventIpcManager(m_eventIpcManager);
    // m_collectd.setCollectdConfigFactory(m_collectdConfigFactory);
    m_collectd.setNodeDao(m_nodeDao);
    m_collectd.setIpInterfaceDao(m_ipIfDao);
    m_collectd.setFilterDao(m_filterDao);
    m_collectd.setScheduler(m_scheduler);
    m_collectd.setTransactionTemplate(transTemplate);
    // m_collectd.afterPropertiesSet();

    ThresholdingConfigFactory.setInstance(
        new ThresholdingConfigFactory(
            ConfigurationTestUtils.getInputStreamForConfigFile("thresholds.xml")));
  }