@Test
  public void testNodeGainedServiceWhileNodeDownAndServiceUp() {

    startDaemons();

    MockNode node = m_network.getNode(4);
    MockService svc = m_network.getService(4, "192.168.1.6", "SNMP");

    anticipateDown(node);

    node.bringDown();

    verifyAnticipated(5000);

    resetAnticipated();

    anticipateUp(node);
    anticipateDown(svc, true);

    MockService newSvc = m_network.addService(4, "192.168.1.6", "SMTP");

    m_db.writeService(newSvc);

    Event e = MockEventUtil.createNodeGainedServiceEvent("Test", newSvc);
    m_eventMgr.sendEventToListeners(e);

    sleep(5000);
    System.err.println(m_db.getOutages());

    verifyAnticipated(8000);
  }
  @Before
  public void setUp() throws Exception {
    MockLogAppender.setupLogging();
    MockDatabase db = new MockDatabase();
    DataSourceFactory.setInstance(db);

    RrdUtils.setStrategy(m_strategy);

    m_provisioner = new OpenNMSProvisioner();

    m_eventManager = new MockEventIpcManager();
    m_provisioner.setEventManager(m_eventManager);

    m_capsdConfig = new TestCapsdConfigManager(CAPSD_CONFIG);
    CapsdConfigFactory.setInstance(m_capsdConfig);

    m_pollerConfig = new TestPollerConfigManager(POLLER_CONFIG, "localhost", false);
    PollerConfigFactory.setInstance(m_pollerConfig);

    m_provisioner.setCapsdConfig(m_capsdConfig);
    m_provisioner.setPollerConfig(m_pollerConfig);

    InputStream configStream =
        ConfigurationTestUtils.getInputStreamForConfigFile("opennms-server.xml");
    OpennmsServerConfigFactory onmsSvrConfig = new OpennmsServerConfigFactory(configStream);
    configStream.close();
    OpennmsServerConfigFactory.setInstance(onmsSvrConfig);

    configStream = ConfigurationTestUtils.getInputStreamForConfigFile("database-schema.xml");
    DatabaseSchemaConfigFactory.setInstance(new DatabaseSchemaConfigFactory(configStream));
    configStream.close();

    configStream =
        ConfigurationTestUtils.getInputStreamForResource(
            this, "/org/opennms/netmgt/capsd/collectd-configuration.xml");
    CollectdConfigFactory.setInstance(
        new CollectdConfigFactory(
            configStream, onmsSvrConfig.getServerName(), onmsSvrConfig.verifyServer()));
    configStream.close();

    JdbcTemplate jdbcTemplate = new JdbcTemplate(db);

    m_syncer = new JdbcCapsdDbSyncer();
    m_syncer.setJdbcTemplate(jdbcTemplate);
    m_syncer.setOpennmsServerConfig(OpennmsServerConfigFactory.getInstance());
    m_syncer.setCapsdConfig(m_capsdConfig);
    m_syncer.setPollerConfig(m_pollerConfig);
    m_syncer.setCollectdConfig(CollectdConfigFactory.getInstance());
    m_syncer.setNextSvcIdSql(db.getNextServiceIdStatement());
    m_syncer.afterPropertiesSet();

    m_syncer.syncServices();
    m_provisioner.setCapsdDbSyncer(m_syncer);
  }
  @Test
  public void testNodeGainedDynamicService() throws Exception {
    m_pollerConfig.setNodeOutageProcessingEnabled(true);

    startDaemons();

    TestCapsdConfigManager capsdConfig = new TestCapsdConfigManager(CAPSD_CONFIG);

    InputStream configStream =
        ConfigurationTestUtils.getInputStreamForConfigFile("opennms-server.xml");
    OpennmsServerConfigFactory onmsSvrConfig = new OpennmsServerConfigFactory(configStream);
    configStream.close();

    configStream = ConfigurationTestUtils.getInputStreamForConfigFile("database-schema.xml");
    DatabaseSchemaConfigFactory.setInstance(new DatabaseSchemaConfigFactory(configStream));
    configStream.close();

    configStream =
        ConfigurationTestUtils.getInputStreamForResource(
            this, "/org/opennms/netmgt/capsd/collectd-configuration.xml");
    CollectdConfigFactory collectdConfig =
        new CollectdConfigFactory(
            configStream, onmsSvrConfig.getServerName(), onmsSvrConfig.verifyServer());
    configStream.close();

    JdbcTemplate jdbcTemplate = new JdbcTemplate(m_db);

    JdbcCapsdDbSyncer syncer = new JdbcCapsdDbSyncer();
    syncer.setJdbcTemplate(jdbcTemplate);
    syncer.setOpennmsServerConfig(onmsSvrConfig);
    syncer.setCapsdConfig(capsdConfig);
    syncer.setPollerConfig(m_pollerConfig);
    syncer.setCollectdConfig(collectdConfig);
    syncer.setNextSvcIdSql(m_db.getNextServiceIdStatement());
    syncer.afterPropertiesSet();

    OpenNMSProvisioner provisioner = new OpenNMSProvisioner();
    provisioner.setPollerConfig(m_pollerConfig);
    provisioner.setCapsdConfig(capsdConfig);
    provisioner.setCapsdDbSyncer(syncer);

    provisioner.setEventManager(m_eventMgr);
    provisioner.addServiceDNS("MyDNS", 3, 100, 1000, 500, 3000, 53, "www.opennms.org");

    assertNotNull("The service id for MyDNS is null", m_db.getServiceID("MyDNS"));
    MockUtil.println("The service id for MyDNS is: " + m_db.getServiceID("MyDNS").toString());

    m_anticipator.reset();

    testSendNodeGainedService("MyDNS", "HTTP");
  }
  private void testSendNodeGainedServices(
      int nodeid, String nodeLabel, String ipAddr, String... svcNames) {
    assertNotNull(svcNames);
    assertTrue(svcNames.length > 0);

    MockNode node = m_network.addNode(nodeid, nodeLabel);
    m_db.writeNode(node);
    MockInterface iface = m_network.addInterface(nodeid, ipAddr);
    m_db.writeInterface(iface);

    List<MockService> services = new ArrayList<MockService>();
    for (String svcName : svcNames) {
      MockService svc = m_network.addService(nodeid, ipAddr, svcName);
      m_db.writeService(svc);
      m_pollerConfig.addService(svc);
      services.add(svc);
    }

    MockVisitor gainSvcSender =
        new MockVisitorAdapter() {
          public void visitService(MockService svc) {
            Event event = MockEventUtil.createNodeGainedServiceEvent("Test", svc);
            m_eventMgr.sendEventToListeners(event);
          }
        };
    node.visit(gainSvcSender);

    MockService svc1 = services.get(0);

    PollAnticipator anticipator = new PollAnticipator();
    svc1.addAnticipator(anticipator);

    anticipator.anticipateAllServices(svc1);

    StringBuffer didNotOccur = new StringBuffer();
    for (MockService service : anticipator.waitForAnticipated(10000)) {
      didNotOccur.append(service.toString());
    }
    StringBuffer unanticipatedStuff = new StringBuffer();
    for (MockService service : anticipator.unanticipatedPolls()) {
      unanticipatedStuff.append(service.toString());
    }

    assertEquals(unanticipatedStuff.toString(), "", didNotOccur.toString());

    anticipateDown(svc1);

    svc1.bringDown();

    verifyAnticipated(10000);
  }
 @After
 public void tearDown() throws Exception {
   m_eventMgr.finishProcessingEvents();
   stopDaemons();
   sleep(200);
   if (m_assertLevel != null) {
     MockLogAppender.assertNotGreaterOrEqual(m_assertLevel);
   }
   m_db.drop();
   MockUtil.println("------------ End Test  --------------------------");
 }
  // test open outages for unmanaged services
  @Test
  public void testReparentCausesStatusChange() {

    m_pollerConfig.setNodeOutageProcessingEnabled(true);

    MockNode node1 = m_network.getNode(1);
    MockNode node2 = m_network.getNode(2);

    MockInterface dotOne = m_network.getInterface(1, "192.168.1.1");
    MockInterface dotTwo = m_network.getInterface(1, "192.168.1.2");
    MockInterface dotThree = m_network.getInterface(2, "192.168.1.3");

    //
    // Plan to bring down both nodes except the reparented interface
    // the node owning the interface should be up while the other is down
    // after reparenting we should got the old owner go down while the other
    // comes up.
    //
    anticipateDown(node2);
    anticipateDown(dotOne);

    // bring down both nodes but bring iface back up
    node1.bringDown();
    node2.bringDown();
    dotTwo.bringUp();

    Event reparentEvent = MockEventUtil.createReparentEvent("Test", "192.168.1.2", 1, 2);

    startDaemons();

    verifyAnticipated(2000);

    m_db.reparentInterface(dotTwo.getIpAddr(), dotTwo.getNodeId(), node2.getNodeId());
    dotTwo.moveTo(node2);

    resetAnticipated();
    anticipateDown(node1, true);
    anticipateUp(node2, true);
    anticipateDown(dotThree, true);

    m_eventMgr.sendEventToListeners(reparentEvent);

    verifyAnticipated(20000);
  }
  // test open outages for unmanaged services
  @Test
  public void testUnmangedWithOpenOutageAtStartup() {
    // before we start we need to initialize the database

    // create an outage for the service
    MockService svc = m_network.getService(1, "192.168.1.1", "SMTP");
    MockInterface iface = m_network.getInterface(1, "192.168.1.2");

    Event svcLostEvent = MockEventUtil.createNodeLostServiceEvent("Test", svc);
    m_db.writeEvent(svcLostEvent);
    createOutages(svc, svcLostEvent);

    Event ifaceDownEvent = MockEventUtil.createInterfaceDownEvent("Test", iface);
    m_db.writeEvent(ifaceDownEvent);
    createOutages(iface, ifaceDownEvent);

    // mark the service as unmanaged
    m_db.setServiceStatus(svc, 'U');
    m_db.setInterfaceStatus(iface, 'U');

    // assert that we have an open outage
    assertEquals(1, m_db.countOpenOutagesForService(svc));
    assertEquals(1, m_db.countOutagesForService(svc));

    assertEquals(iface.getServices().size(), m_db.countOutagesForInterface(iface));
    assertEquals(iface.getServices().size(), m_db.countOpenOutagesForInterface(iface));

    startDaemons();

    // assert that we have no open outages
    assertEquals(0, m_db.countOpenOutagesForService(svc));
    assertEquals(1, m_db.countOutagesForService(svc));

    assertEquals(0, m_db.countOpenOutagesForInterface(iface));
    assertEquals(iface.getServices().size(), m_db.countOutagesForInterface(iface));
  }
  // interfaceReparented: EventConstants.INTERFACE_REPARENTED_EVENT_UEI
  @Test
  public void testInterfaceReparented() throws Exception {
    m_assertLevel = null;

    m_pollerConfig.setNodeOutageProcessingEnabled(true);

    MockNode node1 = m_network.getNode(1);
    MockNode node2 = m_network.getNode(2);

    assertNotNull("Node 1 should have 192.168.1.1", node1.getInterface("192.168.1.1"));
    assertNotNull("Node 1 should have 192.168.1.2", node1.getInterface("192.168.1.2"));

    assertNull("Node 2 should not yet have 192.168.1.2", node2.getInterface("192.168.1.2"));
    assertNotNull("Node 2 should have 192.168.1.3", node2.getInterface("192.168.1.3"));

    MockInterface dotTwo = m_network.getInterface(1, "192.168.1.2");
    MockInterface dotThree = m_network.getInterface(2, "192.168.1.3");

    Event reparentEvent = MockEventUtil.createReparentEvent("Test", "192.168.1.2", 1, 2);

    // we are going to reparent to node 2 so when we bring down its only
    // current interface we expect an interface down not the whole node.
    anticipateDown(dotThree);

    startDaemons();

    final int waitTime = 2000;
    final int verifyTime = 2000;

    sleep(waitTime);

    // move the reparented interface and send a reparented event
    dotTwo.moveTo(node2);
    m_db.reparentInterface(dotTwo.getIpAddr(), node1.getNodeId(), node2.getNodeId());

    // send the reparent event to the daemons
    m_eventMgr.sendEventToListeners(reparentEvent);

    sleep(waitTime);

    // now bring down the other interface on the new node
    // System.err.println("Bring Down:"+node2Iface);
    dotThree.bringDown();

    verifyAnticipated(verifyTime);

    resetAnticipated();
    anticipateDown(node2);

    // System.err.println("Bring Down:"+reparentedIface);
    dotTwo.bringDown();

    sleep(waitTime);

    verifyAnticipated(verifyTime);

    node1 = m_network.getNode(1);
    node2 = m_network.getNode(2);

    assertNotNull("Node 1 should still have 192.168.1.1", node1.getInterface("192.168.1.1"));
    assertNull("Node 1 should no longer have 192.168.1.2", node1.getInterface("192.168.1.2"));

    assertNotNull("Node 2 should now have 192.168.1.2", node2.getInterface("192.168.1.2"));
    assertNotNull("Node 2 should still have 192.168.1.3", node2.getInterface("192.168.1.3"));
  }
  @Before
  public void setUp() throws Exception {
    m_assertLevel = Level.WARN;

    // System.setProperty("mock.logLevel", "DEBUG");
    // System.setProperty("mock.debug", "true");
    MockUtil.println("------------ Begin Test  --------------------------");
    MockLogAppender.setupLogging();

    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.addService("SNMP");
    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.addService("SMTP");
    m_network.addService("SNMP");
    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");
    m_network.addNode(4, "DownNode");
    m_network.addInterface("192.168.1.6");
    m_network.addService("SNMP");
    //		m_network.addInterface("fe80:0000:0000:0000:0231:f982:0123:4567");
    //		m_network.addService("SNMP");

    m_db = new MockDatabase();
    m_db.populate(m_network);
    DataSourceFactory.setInstance(m_db);

    //		DemandPollDao demandPollDao = new DemandPollDaoHibernate(m_db);
    //		demandPollDao.setAllocateIdStmt(m_db
    //				.getNextSequenceValStatement("demandPollNxtId"));
    //		m_demandPollDao = demandPollDao;

    m_pollerConfig = new MockPollerConfig(m_network);
    m_pollerConfig.setNextOutageIdSql(m_db.getNextOutageIdStatement());
    m_pollerConfig.setNodeOutageProcessingEnabled(true);
    m_pollerConfig.setCriticalService("ICMP");
    m_pollerConfig.addPackage("TestPackage");
    m_pollerConfig.addDowntime(1000L, 0L, -1L, false);
    m_pollerConfig.setDefaultPollInterval(1000L);
    m_pollerConfig.populatePackage(m_network);
    m_pollerConfig.addPackage("TestPkg2");
    m_pollerConfig.addDowntime(1000L, 0L, -1L, false);
    m_pollerConfig.setDefaultPollInterval(2000L);
    m_pollerConfig.addService(m_network.getService(2, "192.168.1.3", "HTTP"));

    m_anticipator = new EventAnticipator();
    m_outageAnticipator = new OutageAnticipator(m_db);

    m_eventMgr = new MockEventIpcManager();
    m_eventMgr.setEventWriter(m_db);
    m_eventMgr.setEventAnticipator(m_anticipator);
    m_eventMgr.addEventListener(m_outageAnticipator);
    m_eventMgr.setSynchronous(false);

    QueryManager queryManager = new DefaultQueryManager();
    queryManager.setDataSource(m_db);

    DefaultPollContext pollContext = new DefaultPollContext();
    pollContext.setEventManager(m_eventMgr);
    pollContext.setLocalHostName("localhost");
    pollContext.setName("Test.DefaultPollContext");
    pollContext.setPollerConfig(m_pollerConfig);
    pollContext.setQueryManager(queryManager);

    PollableNetwork network = new PollableNetwork(pollContext);

    m_poller = new Poller();
    m_poller.setDataSource(m_db);
    m_poller.setEventManager(m_eventMgr);
    m_poller.setNetwork(network);
    m_poller.setQueryManager(queryManager);
    m_poller.setPollerConfig(m_pollerConfig);
    m_poller.setPollOutagesConfig(m_pollerConfig);

    MockOutageConfig config = new MockOutageConfig();
    config.setGetNextOutageID(m_db.getNextOutageIdStatement());

    RrdUtils.setStrategy(new NullRrdStrategy());

    // m_outageMgr = new OutageManager();
    // m_outageMgr.setEventMgr(m_eventMgr);
    // m_outageMgr.setOutageMgrConfig(config);
    // m_outageMgr.setDbConnectionFactory(m_db);

  }