DeviceManagementSubsystem extends AbstractSubSystem { @Override @Transactional(value = Transactional.TxType.REQUIRED, rollbackOn = {RuntimeException.class, Exception.class}) protected Map<ModelNodeId, List<Element>> retrieveStateAttributes(Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes) throws GetAttributeException { Map<ModelNodeId, List<Element>> stateInfo = new HashMap<>(); try { Document document = DocumentUtils.createDocument(); for (Map.Entry<ModelNodeId, Pair<List<QName>, List<FilterNode>>> entry : mapAttributes.entrySet()) { List<Element> stateElements = new ArrayList<>(); ModelNodeId nodeId = entry.getKey(); List<FilterNode> filters = entry.getValue().getSecond(); Element deviceStateElement = null; Element newDevicesElement = null; Element deviceAdapterElement = null; if (nodeId.matchesTemplate(DEVICE_ID_TEMPLATE)) { Device device = getDevice(nodeId.getRdnValue("name")); if (device != null) { deviceStateElement = buildDeviceStateElement(document, device); } } else if (nodeId.matchesTemplate(NETWORK_MANAGER_ID_TEMPLATE)) { List<NewDeviceInfo> newDeviceInfos = m_connectionManager.getNewDevices(); List<DeviceAdapter> adapters = new ArrayList<>(m_adapterManager.getAllDeviceAdapters()); if (!newDeviceInfos.isEmpty()) { newDevicesElement = buildNewDeviceStateElement(document, newDeviceInfos); } deviceAdapterElement = buildAdaptersStateElement(document, adapters); } for (FilterNode filter : filters) { if (DEVICE_STATE.equals(filter.getNodeName()) && NS.equals(filter.getNamespace())) { Element filteredDeviceStateElement = document.createElementNS(NS, DEVICE_STATE); m_subtreeFilterUtil.doFilter(document, filter, m_schemaRegistry.getDataSchemaNode(MANAGED_DEVICES_SP), deviceStateElement, filteredDeviceStateElement); deviceStateElement = filteredDeviceStateElement; if (deviceStateElement != null) { stateElements.add(deviceStateElement); } } else if (NEW_DEVICES.equals(filter.getNodeName()) && NS.equals(filter.getNamespace())) { Element filteredNewDevicesElement = document.createElementNS(NS, NEW_DEVICES); m_subtreeFilterUtil.doFilter(document, filter, m_schemaRegistry.getDataSchemaNode(NEW_DEVICES_SP), newDevicesElement, filteredNewDevicesElement); newDevicesElement = filteredNewDevicesElement; if (newDevicesElement != null) { stateElements.add(newDevicesElement); } } else if (DEVICE_ADAPTERS.equals(filter.getNodeName()) && NS.equals(filter.getNamespace())) { Element filteredDeviceAdaptersElement = document.createElementNS(NS, DEVICE_ADAPTERS); m_subtreeFilterUtil.doFilter(document, filter, m_schemaRegistry.getDataSchemaNode(DEVICE_ADAPTERS_SP), deviceAdapterElement, filteredDeviceAdaptersElement); deviceAdapterElement = filteredDeviceAdaptersElement; if (deviceAdapterElement != null) { stateElements.add(deviceAdapterElement); } } stateInfo.put(nodeId, stateElements); } } } catch (Exception e) { LOGGER.error("Error in retrieveStateAttributes: ", e); throw e; } return stateInfo; } DeviceManagementSubsystem(SchemaRegistry schemaRegistry, AdapterManager adapterManager); DeviceManager getDeviceManager(); void setDeviceManager(DeviceManager deviceManager); NetconfConnectionManager getConnectionManager(); void setConnectionManager(NetconfConnectionManager connectionManager); @Override void notifyChanged(List<ChangeNotification> changeNotificationList);  }
@Test public void retrieveStateAttributesOfDeviceA_DeviceB_Newdevices_DeviceAdapters() throws Exception { Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document deviceStateFilter = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-device-state-request.xml")); mapAttributes.put(DEVICE_A_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(deviceStateFilter)))); mapAttributes.put(DEVICE_B_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(deviceStateFilter)))); Document filterNewDevicesWithDuid = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-new-devices-with-duid-request.xml")); Document filterDeviceAdapterWithType = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-device-adapter-with-type-request.xml")); mapAttributes.put(NETWORK_MANAGER_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(filterNewDevicesWithDuid), getFilterNode(filterDeviceAdapterWithType)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(3, stateInfo.size()); List<Element> deviceAState = stateInfo.get(DEVICE_A_ID_TEMPLATE); assertEquals(1, deviceAState.size()); List<Element> deviceBState = stateInfo.get(DEVICE_B_ID_TEMPLATE); assertEquals(1, deviceBState.size()); List<Element> newDeviceAndAdapter = stateInfo.get(NETWORK_MANAGER_ID_TEMPLATE); assertEquals(2, newDeviceAndAdapter.size()); Document deviceAStatedocument = getDeviceStateDocument(DEVICE_A); Document deviceBStatedocument = getDeviceStateDocument(DEVICE_B); TestUtil.assertXMLEquals(deviceAStatedocument.getDocumentElement(), deviceAState.get(0)); TestUtil.assertXMLEquals(deviceBStatedocument.getDocumentElement(), deviceBState.get(0)); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-new-devices-with-duid-response.xml")).getDocumentElement(), newDeviceAndAdapter.get(0)); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/device-adapters-response.xml")).getDocumentElement(), newDeviceAndAdapter.get(1)); }
@Test public void retrieveStateAttributesOfDeviceA_DeviceB() throws Exception { Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document deviceStateFilter = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-connection-state-request.xml")); mapAttributes.put(DEVICE_A_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(deviceStateFilter)))); mapAttributes.put(DEVICE_B_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(deviceStateFilter)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(2, stateInfo.size()); List<Element> deviceAState = stateInfo.get(DEVICE_A_ID_TEMPLATE); assertEquals(1, deviceAState.size()); List<Element> deviceBState = stateInfo.get(DEVICE_B_ID_TEMPLATE); assertEquals(1, deviceBState.size()); Document deviceAStatedocument = getDeviceStateDocumentWithConnectionStateFilter(DEVICE_A); Document deviceBStatedocument = getDeviceStateDocumentWithConnectionStateFilter(DEVICE_B); TestUtil.assertXMLEquals(deviceAStatedocument.getDocumentElement(), deviceAState.get(0)); TestUtil.assertXMLEquals(deviceBStatedocument.getDocumentElement(), deviceBState.get(0)); }
@Test public void retrieveStateAttributesOfDeviceA() throws Exception { Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document deviceStateFilter = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-connection-state-request.xml")); mapAttributes.put(DEVICE_A_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(deviceStateFilter)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(1, stateInfo.size()); List<Element> deviceAState = stateInfo.get(DEVICE_A_ID_TEMPLATE); assertEquals(1, deviceAState.size()); Document deviceAStatedocument = getDeviceStateDocumentWithConnectionStateFilter(DEVICE_A); TestUtil.assertXMLEquals(deviceAStatedocument.getDocumentElement(), deviceAState.get(0)); }
@Test public void retrieveStateAttributesOfDeviceB() throws Exception { Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document deviceStateFilter = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-connection-state-request.xml")); mapAttributes.put(DEVICE_B_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(deviceStateFilter)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(1, stateInfo.size()); List<Element> deviceBState = stateInfo.get(DEVICE_B_ID_TEMPLATE); assertEquals(1, deviceBState.size()); Document deviceBStatedocument = getDeviceStateDocumentWithConnectionStateFilter(DEVICE_B); TestUtil.assertXMLEquals(deviceBStatedocument.getDocumentElement(), deviceBState.get(0)); }
@Test public void retrieveStateAttributesNewDevicesAndDeviceAdapters() throws Exception { Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document filterNewDevicesWithDuid = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-new-devices-with-duid-request.xml")); Document filterDeviceAdapterWithType = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-device-adapter-with-type-request.xml")); mapAttributes.put(NETWORK_MANAGER_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(filterNewDevicesWithDuid), getFilterNode(filterDeviceAdapterWithType)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(1, stateInfo.size()); List<Element> newDeviceAndAdapter = stateInfo.get(NETWORK_MANAGER_ID_TEMPLATE); assertEquals(2, newDeviceAndAdapter.size()); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-new-devices-with-duid-response.xml")).getDocumentElement(), newDeviceAndAdapter.get(0)); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/device-adapters-response.xml")).getDocumentElement(), newDeviceAndAdapter.get(1)); }
@Test public void retrieveStateAttributesNewDevice() throws Exception { Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document filterNewDevicesWithDuid = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-new-devices-with-duid-request.xml")); mapAttributes.put(NETWORK_MANAGER_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(filterNewDevicesWithDuid)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(1, stateInfo.size()); List<Element> newDevice = stateInfo.get(NETWORK_MANAGER_ID_TEMPLATE); assertEquals(1, newDevice.size()); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-new-devices-with-duid-response.xml")).getDocumentElement(), newDevice.get(0)); }
@Test public void retrieveStateAttributesDeviceAdapters() throws Exception { Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document filterDeviceAdapterWithType = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-device-adapter-with-type-request.xml")); mapAttributes.put(NETWORK_MANAGER_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(filterDeviceAdapterWithType)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(1, stateInfo.size()); List<Element> deviceAdapter = stateInfo.get(NETWORK_MANAGER_ID_TEMPLATE); assertEquals(1, deviceAdapter.size()); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/device-adapters-response.xml")).getDocumentElement(), deviceAdapter.get(0)); }
@Test public void retrieveStateAttributesDeviceAdaptersWithInUseFilter() throws Exception { prepareDevicesForAdapterInUseTest(); Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document filterDeviceAdapterWithType = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-device-adapter-with-inuse.xml")); mapAttributes.put(NETWORK_MANAGER_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(filterDeviceAdapterWithType)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(1, stateInfo.size()); List<Element> deviceAdapter = stateInfo.get(NETWORK_MANAGER_ID_TEMPLATE); assertEquals(1, deviceAdapter.size()); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/device-adapters-inuse-response.xml")).getDocumentElement(), deviceAdapter.get(0)); }
@Test public void retrieveStateAttributesDeviceAdaptersWithoutFilter() throws Exception { prepareDevicesForAdapterInUseTest(); Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document filterDeviceAdapterWithType = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-device-adapter.xml")); mapAttributes.put(NETWORK_MANAGER_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(filterDeviceAdapterWithType)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(1, stateInfo.size()); List<Element> deviceAdapter = stateInfo.get(NETWORK_MANAGER_ID_TEMPLATE); assertEquals(1, deviceAdapter.size()); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-device-adapter-response.xml")).getDocumentElement(), deviceAdapter.get(0)); }
@Test public void retrieveStateAttributesAllDeviceAdapters() throws Exception { Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document filterDeviceAdapterWithType = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/get-device-adapters.xml")); mapAttributes.put(NETWORK_MANAGER_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(filterDeviceAdapterWithType)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); List<Element> deviceAdapter = stateInfo.get(NETWORK_MANAGER_ID_TEMPLATE); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/get-device-adapters-response.xml")).getDocumentElement(), deviceAdapter.get(0)); }
@Test public void retrieveStateAttributesOfDeviceA_DeviceB_WhenAdapterNotdeployed() throws Exception { when(m_adapterManager.getAdapterContext(any())).thenReturn(null); Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document deviceStateFilter = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-device-state-request.xml")); mapAttributes.put(DEVICE_A_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(deviceStateFilter)))); mapAttributes.put(DEVICE_B_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(deviceStateFilter)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(2, stateInfo.size()); List<Element> deviceAState = stateInfo.get(DEVICE_A_ID_TEMPLATE); assertEquals(1, deviceAState.size()); List<Element> deviceBState = stateInfo.get(DEVICE_B_ID_TEMPLATE); assertEquals(1, deviceBState.size()); String expectedDeviceState = "<device-state xmlns=\"urn:bbf:yang:obbaa:network-manager\"/>"; assertEquals(expectedDeviceState, DocumentUtils.documentToPrettyString(deviceAState.get(0)).trim()); assertEquals(expectedDeviceState, DocumentUtils.documentToPrettyString(deviceBState.get(0)).trim()); }
@Test public void retrieveStateAttributesDeviceAdaptersWithInUseFilterFactoryGarmentTagRetrievalDisabled() throws Exception { System.setProperty("ENABLE_FACTORY_GARMENT_TAG_RETRIEVAL", "false"); prepareDevicesForAdapterInUseTest(); Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document filterDeviceAdapterWithType = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-device-adapter-with-inuse.xml")); mapAttributes.put(NETWORK_MANAGER_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(filterDeviceAdapterWithType)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(1, stateInfo.size()); List<Element> deviceAdapter = stateInfo.get(NETWORK_MANAGER_ID_TEMPLATE); assertEquals(1, deviceAdapter.size()); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/device-adapters-inuse-response-garment-tag-disabled.xml")).getDocumentElement(), deviceAdapter.get(0)); }
@Test public void retrieveStateAttributesDeviceAdaptersWithoutFilterFactoryGarmentTagRetrievalDisabled() throws Exception { System.setProperty("ENABLE_FACTORY_GARMENT_TAG_RETRIEVAL", "false"); prepareDevicesForAdapterInUseTest(); Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document filterDeviceAdapterWithType = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-device-adapter.xml")); mapAttributes.put(NETWORK_MANAGER_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(filterDeviceAdapterWithType)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); assertEquals(1, stateInfo.size()); List<Element> deviceAdapter = stateInfo.get(NETWORK_MANAGER_ID_TEMPLATE); assertEquals(1, deviceAdapter.size()); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/filter-device-adapter-response-garment-tag-disabled.xml")).getDocumentElement(), deviceAdapter.get(0)); }
@Test public void retrieveStateAttributesAllDeviceAdaptersFactoryGarmentTagRetrievalDisabled() throws Exception { System.setProperty("ENABLE_FACTORY_GARMENT_TAG_RETRIEVAL", "false"); Map<ModelNodeId, Pair<List<QName>, List<FilterNode>>> mapAttributes = new HashMap<>(); Document filterDeviceAdapterWithType = DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/get-device-adapters.xml")); mapAttributes.put(NETWORK_MANAGER_ID_TEMPLATE, new Pair<>(Collections.emptyList(), Arrays.asList(getFilterNode(filterDeviceAdapterWithType)))); Map<ModelNodeId, List<Element>> stateInfo = m_deviceManagementSubsystem.retrieveStateAttributes(mapAttributes); List<Element> deviceAdapter = stateInfo.get(NETWORK_MANAGER_ID_TEMPLATE); TestUtil.assertXMLEquals(DocumentUtils.loadXmlDocument(DeviceManagementSubsystemTest.class.getResourceAsStream("/get-device-adapters-response-garment-tag-disabled.xml")).getDocumentElement(), deviceAdapter.get(0)); }