AlignmentTimer { public void runAlignment() { m_txService.executeWithTx(new TXTemplate<Void>() { @Override public Void execute() { List<Device> devices = m_deviceDao.findAllDevices(); for (Device device : devices) { try { m_pmaRegistry.executeWithPmaSession(device.getDeviceName(), session -> { if (device.isNeverAligned()) { LOGGER.debug(String.format("Device %s is " + NEVER_ALIGNED, device)); AlignmentOption alignmentOption = device.getAlignmentOption(); if (AlignmentOption.PUSH.equals(alignmentOption)) { if (deviceHasConfigurations(device)) { session.forceAlign(); } } else if (AlignmentOption.PULL.equals(alignmentOption)) { invokeUploadConfig(device, session); LOGGER.debug("Upload config done"); m_deviceDao.updateDeviceAlignmentState(device.getDeviceName(), ALIGNED); } } else if (!device.isAligned()) { session.align(); } return null; }); } catch (ExecutionException e) { LOGGER.error(String.format("Could not align device %s", device), e); } } return null; } }); } AlignmentTimer(PmaRegistry pmaRegistry, AdapterManager adaperMgr,
                          DeviceDao deviceDao, TxService txService); TxService getTxService(); void setTxService(TxService txService); DeviceDao getDeviceDao(); void setDeviceDao(DeviceDao deviceDao); void init(); void destroy(); void runAlignment();  }
@Test public void testNewDevicesAreAligned() throws ExecutionException { m_timer.runAlignment(); verify(m_pmaSession, times(2)).align(); verify(m_pmaSession, never()).forceAlign(); }
@Test public void testNewDevicesWithConfigurationsAreAligned() throws ExecutionException, NetconfMessageBuilderException { Map<NetConfResponse, List<Notification>> map = new HashMap<>(); NetConfResponse netConfResponse = DocumentToPojoTransformer.getNetconfResponse(DocumentUtils.stringToDocument(SAMPLE_NON_EMPTY_NC_RES)); map.put(netConfResponse, Collections.emptyList()); when(m_pmaSession.executeNC(anyString())).thenReturn(map); m_timer.runAlignment(); verify(m_pmaSession, times(2)).align(); }
@Test public void testUploadConfig() throws ExecutionException, InterruptedException, NetconfMessageBuilderException { m_newDevice.getDeviceManagement().setPushPmaConfigurationToDevice("false"); Future<NetConfResponse> responseFuture = mock(Future.class); NetConfResponse netConfResponse = null; try { netConfResponse = DocumentToPojoTransformer.getNetconfResponse(DocumentUtils.stringToDocument(SAMPLE_NON_EMPTY_NC_RES)); } catch (NetconfMessageBuilderException e) { throw new RuntimeException(e); } when(responseFuture.get()).thenReturn(netConfResponse); when(m_devInterface.getConfig(eq(m_newDevice), anyObject())).thenReturn(responseFuture); Map<NetConfResponse, List<Notification>> map = new HashMap<>(); NetConfResponse netConfRespons = DocumentToPojoTransformer.getNetconfResponse(DocumentUtils.stringToDocument(OK_RESPONSE)); map.put(netConfRespons, Collections.emptyList()); when(m_pmaSession.executeNC(EDIT_CONFIG_REQ)).thenReturn(map); m_timer.runAlignment(); assertEquals("true", m_newDevice.getDeviceManagement().getPushPmaConfigurationToDevice()); verify(m_deviceDao).updateDeviceAlignmentState("new-device", ALIGNED); }
@Test public void testUploadConfigError() throws ExecutionException, InterruptedException, NetconfMessageBuilderException { m_newDevice.getDeviceManagement().setPushPmaConfigurationToDevice("false"); Future<NetConfResponse> responseFuture = mock(Future.class); NetConfResponse netConfResponse = null; try { netConfResponse = DocumentToPojoTransformer.getNetconfResponse(DocumentUtils.stringToDocument(SAMPLE_NON_EMPTY_NC_RES)); } catch (NetconfMessageBuilderException e) { throw new RuntimeException(e); } when(responseFuture.get()).thenReturn(netConfResponse); when(m_devInterface.getConfig(eq(m_newDevice), anyObject())).thenReturn(responseFuture); Map<NetConfResponse, List<Notification>> map = new HashMap<>(); NetConfResponse netConfRespons = DocumentToPojoTransformer.getNetconfResponse(DocumentUtils.stringToDocument(ERROR_RESPONSE)); map.put(netConfRespons, Collections.emptyList()); when(m_pmaSession.executeNC(EDIT_CONFIG_REQ)).thenReturn(map); m_timer.runAlignment(); assertEquals("false", m_newDevice.getDeviceManagement().getPushPmaConfigurationToDevice()); verify(m_deviceDao, never()).updateDeviceAlignmentState(anyString(), anyString()); assertTrue(m_newDevice.isNeverAligned()); }
@Test public void testUploadConfigEmptyResponseFromDevice() throws ExecutionException, InterruptedException { m_newDevice.getDeviceManagement().setPushPmaConfigurationToDevice("false"); Future<NetConfResponse> responseFuture = mock(Future.class); NetConfResponse netConfResponse = null; try { netConfResponse = DocumentToPojoTransformer.getNetconfResponse(DocumentUtils.stringToDocument(SAMPLE_EMPTY_NC_RES)); } catch (NetconfMessageBuilderException e) { throw new RuntimeException(e); } when(responseFuture.get()).thenReturn(netConfResponse); when(m_devInterface.getConfig(eq(m_newDevice), anyObject())).thenReturn(responseFuture); m_timer.runAlignment(); assertEquals("true", m_newDevice.getDeviceManagement().getPushPmaConfigurationToDevice()); verify(m_deviceDao).updateDeviceAlignmentState("new-device", ALIGNED); }