// nodeLabelChanged: EventConstants.NODE_LABEL_CHANGED_EVENT_UEI
  @Test
  public void testNodeLabelChanged() {
    MockNode element = m_network.getNode(1);
    String newLabel = "NEW LABEL";
    Event event = element.createNodeLabelChangedEvent(newLabel);
    m_pollerConfig.setNodeOutageProcessingEnabled(false);

    PollAnticipator poll = new PollAnticipator();
    element.addAnticipator(poll);

    poll.anticipateAllServices(element);

    startDaemons();

    // wait until after the first poll of the services
    poll.waitForAnticipated(1000L);

    assertEquals("Router", m_poller.getNetwork().getNode(1).getNodeLabel());

    // now delete the node and send a nodeDeleted event
    element.setLabel(newLabel);
    m_eventMgr.sendEventToListeners(event);

    assertEquals(newLabel, m_poller.getNetwork().getNode(1).getNodeLabel());
  }
  @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);
  }
 private void expectUpdateEvent() {
   m_eventManager
       .getEventAnticipator()
       .anticipateEvent(
           MockEventUtil.createEventBuilder("Test", EventConstants.SCHEDOUTAGES_CHANGED_EVENT_UEI)
               .getEvent());
 }
  // Test harness that tests any type of node, interface or element.
  private void testElementDeleted(MockElement element) {
    Event deleteEvent = element.createDeleteEvent();
    m_pollerConfig.setNodeOutageProcessingEnabled(false);

    PollAnticipator poll = new PollAnticipator();
    element.addAnticipator(poll);

    poll.anticipateAllServices(element);

    startDaemons();

    // wait til after the first poll of the services
    poll.waitForAnticipated(1000L);

    // now delete the node and send a nodeDeleted event
    m_network.removeElement(element);
    m_eventMgr.sendEventToListeners(deleteEvent);

    // reset the poll count and wait to see if any polls on the removed
    // element happened
    m_network.resetInvalidPollCount();

    // now ensure that no invalid polls have occurred
    sleep(3000);

    assertEquals(
        "Received a poll for an element that doesn't exist", 0, m_network.getInvalidPollCount());
  }
 @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
  public void testSuspendPollingResumeService() {

    MockService svc = m_network.getService(1, "192.168.1.2", "SMTP");

    startDaemons();

    sleep(2000);
    assertTrue(0 < svc.getPollCount());

    m_eventMgr.sendEventToListeners(MockEventUtil.createSuspendPollingServiceEvent("Test", svc));
    svc.resetPollCount();

    sleep(5000);
    assertEquals(0, svc.getPollCount());

    m_eventMgr.sendEventToListeners(MockEventUtil.createResumePollingServiceEvent("Test", svc));

    sleep(2000);
    assertTrue(0 < svc.getPollCount());
  }
  // 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);
  }
  public void testOutagesClosedOnDelete(MockElement element) {

    startDaemons();

    Event deleteEvent = element.createDeleteEvent();

    // bring down so we create an outage in the outages table
    anticipateDown(element);
    element.bringDown();
    verifyAnticipated(5000, false);

    m_outageAnticipator.anticipateOutageClosed(element, deleteEvent);

    // now delete the service
    m_eventMgr.sendEventToListeners(deleteEvent);
    m_network.removeElement(element);

    verifyAnticipated(5000);
  }
 private void verifyAnticipated(long millis, boolean checkUnanticipated) {
   // make sure the down events are received
   MockEventUtil.printEvents(
       "Events we're still waiting for: ", m_anticipator.waitForAnticipated(millis));
   assertTrue("Expected events not forthcoming", m_anticipator.waitForAnticipated(0).isEmpty());
   if (checkUnanticipated) {
     sleep(2000);
     MockEventUtil.printEvents("Unanticipated: ", m_anticipator.unanticipatedEvents());
     assertEquals("Received unexpected events", 0, m_anticipator.unanticipatedEvents().size());
   }
   sleep(1000);
   m_eventMgr.finishProcessingEvents();
   assertEquals(
       "Wrong number of outages opened",
       m_outageAnticipator.getExpectedOpens(),
       m_outageAnticipator.getActualOpens());
   assertEquals(
       "Wrong number of outages in outage table",
       m_outageAnticipator.getExpectedOutages(),
       m_outageAnticipator.getActualOutages());
   assertTrue(
       "Created outages don't match the expected outages", m_outageAnticipator.checkAnticipated());
 }
  // 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);

  }
 private void verifyEvents() {
   m_eventManager.getEventAnticipator().verifyAnticipated(1000, 0, 0, 0, 0);
 }