RouterControlHelper { public String getRouterControlIp(final long routerId) { String routerControlIpAddress = null; final List<NicVO> nics = nicDao.listByVmId(routerId); for (final NicVO n : nics) { final NetworkVO nc = networkDao.findById(n.getNetworkId()); if (nc != null && nc.getTrafficType() == TrafficType.Control) { routerControlIpAddress = n.getIPv4Address(); break; } } if (routerControlIpAddress == null) { logger.warn("Unable to find router's control ip in its attached NICs!. routerId: " + routerId); final DomainRouterVO router = routerDao.findById(routerId); return router.getPrivateIpAddress(); } return routerControlIpAddress; }  String getRouterControlIp(final long routerId); String getRouterIpInNetwork(final long networkId, final long instanceId);  }
@Test public void testGetRouterControlIp() { List<NicVO> nics = new ArrayList<>(); NicVO nic1 = mock(NicVO.class); NicVO nic2 = mock(NicVO.class); NicVO nic3 = mock(NicVO.class); when(nic1.getNetworkId()).thenReturn(NW_ID_1); when(nic2.getNetworkId()).thenReturn(NW_ID_2); when(nic2.getIPv4Address()).thenReturn(IP4_ADDRES1); when(nic3.getNetworkId()).thenReturn(NW_ID_3); when(nic3.getIPv4Address()).thenReturn(IP4_ADDRES2); nics.add(nic1); nics.add(nic2); nics.add(nic3); when(this.nicDao.listByVmId(ROUTER_ID)).thenReturn(nics); NetworkVO nw1 = mock(NetworkVO.class); when(nw1.getTrafficType()).thenReturn(TrafficType.Public); NetworkVO nw2 = mock(NetworkVO.class); when(nw2.getTrafficType()).thenReturn(TrafficType.Control); NetworkVO nw3 = mock(NetworkVO.class); when(nw3.getTrafficType()).thenReturn(TrafficType.Control); when(this.nwDao.findById(NW_ID_1)).thenReturn(nw1); when(this.nwDao.findById(NW_ID_2)).thenReturn(nw2); when(this.nwDao.findById(NW_ID_3)).thenReturn(nw3); final String ip4address = this.routerControlHelper.getRouterControlIp(ROUTER_ID); assertEquals(DIDN_T_GET_THE_EXPECTED_IP4_ADDRESS, IP4_ADDRES1, ip4address); }
@Test public void testGetRouterControlIpWithRouterIp() { List<NicVO> nics = new ArrayList<>(); NicVO nic1 = mock(NicVO.class); when(nic1.getNetworkId()).thenReturn(NW_ID_1); when(nic1.getIPv4Address()).thenReturn(null); nics.add(nic1); when(this.nicDao.listByVmId(ROUTER_ID)).thenReturn(nics); NetworkVO nw1 = mock(NetworkVO.class); when(nw1.getTrafficType()).thenReturn(TrafficType.Public); when(this.nwDao.findById(NW_ID_1)).thenReturn(nw1); DomainRouterVO router = mock(DomainRouterVO.class); when(this.routerDao.findById(ROUTER_ID)).thenReturn(router); when(router.getPrivateIpAddress()).thenReturn(IP4_ADDRES1); final String ip4address = this.routerControlHelper.getRouterControlIp(ROUTER_ID); assertEquals(DIDN_T_GET_THE_EXPECTED_IP4_ADDRESS, IP4_ADDRES1, ip4address); }