diff options
Diffstat (limited to 'cps-ncmp-service/src')
3 files changed, 136 insertions, 79 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java index 08370ab89..291b0bf33 100755 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java @@ -50,6 +50,7 @@ import org.onap.cps.ncmp.api.models.CmHandle; import org.onap.cps.ncmp.api.models.DmiPluginRegistration; import org.onap.cps.ncmp.api.models.GenericRequestBody; import org.onap.cps.ncmp.api.models.PersistenceCmHandle; +import org.onap.cps.ncmp.api.models.PersistenceCmHandle.AdditionalProperty; import org.onap.cps.ncmp.api.models.PersistenceCmHandlesList; import org.onap.cps.ncmp.api.models.YangResource; import org.onap.cps.spi.FetchDescendantsOption; @@ -58,6 +59,7 @@ import org.onap.cps.spi.exceptions.DataValidationException; import org.onap.cps.spi.model.DataNode; import org.onap.cps.spi.model.ModuleReference; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -171,8 +173,8 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final String fieldsQueryParam, final Integer depthQueryParam) { - final var cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle); - final var dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get(NCMP_DMI_SERVICE_NAME)); + final DataNode cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle); + final String dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get(NCMP_DMI_SERVICE_NAME)); final String dmiRequestBody = getGenericRequestBody(cmHandleDataNode); final ResponseEntity<Object> response = dmiOperations.getResourceDataOperationalFromDmi(dmiServiceName, cmHandle, @@ -190,8 +192,8 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final String acceptParam, final String fields, final Integer depth) { - final var cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle); - final var dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get(NCMP_DMI_SERVICE_NAME)); + final DataNode cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle); + final String dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get(NCMP_DMI_SERVICE_NAME)); final String dmiRequestBody = getGenericRequestBody(cmHandleDataNode); final ResponseEntity<Object> response = dmiOperations.getResourceDataPassThroughRunningFromDmi(dmiServiceName, cmHandle, @@ -208,17 +210,17 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService final @NotNull String resourceIdentifier, final @NotNull String requestBody, final String contentType) { - final var cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle); - final var dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get(NCMP_DMI_SERVICE_NAME)); - final Collection<DataNode> cmHandlePropertiesList = cmHandleDataNode.getChildDataNodes(); - final Map<String, String> cmHandlePropertiesMap = getCmHandlePropertiesAsMap(cmHandlePropertiesList); - final var dmiRequestBodyObject = GenericRequestBody.builder() + final DataNode cmHandleDataNode = fetchDataNodeFromDmiRegistryForCmHandle(cmHandle); + final String dmiServiceName = String.valueOf(cmHandleDataNode.getLeaves().get(NCMP_DMI_SERVICE_NAME)); + final Collection<DataNode> cmHandlePropertiesAsDataNodes = cmHandleDataNode.getChildDataNodes(); + final Map<String, String> cmHandlePropertiesAsMap = getCmHandlePropertiesAsMap(cmHandlePropertiesAsDataNodes); + final GenericRequestBody dmiRequestBodyObject = GenericRequestBody.builder() .operation(GenericRequestBody.OperationEnum.CREATE) .dataType(contentType) .data(requestBody) - .cmHandleProperties(cmHandlePropertiesMap) + .cmHandleProperties(cmHandlePropertiesAsMap) .build(); - final var dmiRequestBody = prepareOperationBody(dmiRequestBodyObject); + final String dmiRequestBody = prepareOperationBody(dmiRequestBodyObject); final ResponseEntity<String> responseEntity = dmiOperations .createResourceDataPassThroughRunningFromDmi(dmiServiceName, cmHandle, @@ -250,16 +252,30 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } } - private static Map<String, String> getCmHandlePropertiesAsMap(final Collection<DataNode> cmHandlePropertiesList) { - if (cmHandlePropertiesList == null || cmHandlePropertiesList.isEmpty()) { + private static Map<String, String> getCmHandlePropertiesAsMap( + final Collection<DataNode> cmHandlePropertiesAsDataNode) { + if (cmHandlePropertiesAsDataNode.isEmpty()) { return Collections.emptyMap(); } - final Map<String, String> cmHandlePropertiesMap = new LinkedHashMap<>(); - for (final var node: cmHandlePropertiesList) { - cmHandlePropertiesMap.put(String.valueOf(node.getLeaves().get("name")), - String.valueOf(node.getLeaves().get("value"))); + final Map<String, String> cmHandlePropertiesAsMap = new LinkedHashMap<>(); + for (final DataNode dataNode: cmHandlePropertiesAsDataNode) { + cmHandlePropertiesAsMap.put(String.valueOf(dataNode.getLeaves().get("name")), + String.valueOf(dataNode.getLeaves().get("value"))); } - return cmHandlePropertiesMap; + return cmHandlePropertiesAsMap; + } + + private static Map<String, String> getCmHandlePropertiesAsMap( + final List<AdditionalProperty> cmHandlePropertiesAsList) { + if (cmHandlePropertiesAsList == null || cmHandlePropertiesAsList.isEmpty()) { + return Collections.emptyMap(); + } + final Map<String, String> cmHandlePropertiesAsMap = new LinkedHashMap<>(); + for (final AdditionalProperty additionalProperty: cmHandlePropertiesAsList) { + cmHandlePropertiesAsMap.put(additionalProperty.getName(), + additionalProperty.getValue()); + } + return cmHandlePropertiesAsMap; } private static Object handleResponse(final @NotNull ResponseEntity<Object> responseEntity) { @@ -281,11 +297,11 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } private String getGenericRequestBody(final DataNode cmHandleDataNode) { - final Collection<DataNode> cmHandlePropertiesList = cmHandleDataNode.getChildDataNodes(); - final Map<String, String> cmHandlePropertiesMap = getCmHandlePropertiesAsMap(cmHandlePropertiesList); - final var requestBodyObject = GenericRequestBody.builder() + final Collection<DataNode> cmHandlePropertiesAsDataNodes = cmHandleDataNode.getChildDataNodes(); + final Map<String, String> cmHandlePropertiesAsMap = getCmHandlePropertiesAsMap(cmHandlePropertiesAsDataNodes); + final GenericRequestBody requestBodyObject = GenericRequestBody.builder() .operation(GenericRequestBody.OperationEnum.READ) - .cmHandleProperties(cmHandlePropertiesMap) + .cmHandleProperties(cmHandlePropertiesAsMap) .build(); return prepareOperationBody(requestBodyObject); } @@ -337,6 +353,11 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } } + protected void createAnchorAndSyncModel(final PersistenceCmHandle persistenceCmHandle) { + createAnchor(persistenceCmHandle); + fetchAndSyncModules(persistenceCmHandle); + } + private static PersistenceCmHandle toPersistenceCmHandle(final String dmiPluginService, final CmHandle cmHandle) { final PersistenceCmHandle persistenceCmHandle = new PersistenceCmHandle(); @@ -361,47 +382,68 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService } } - protected void createAnchorAndSyncModel(final PersistenceCmHandle cmHandle) { - final var modulesForCmHandle = - dmiOperations.getResourceFromDmi(cmHandle.getDmiServiceName(), cmHandle.getId(), "modules"); - final List<ModuleReference> moduleReferencesFromDmiForCmHandle = - getModuleReferences(modulesForCmHandle); - final var knownModuleReferencesInCps = - cpsModuleService.getYangResourceModuleReferences(NF_PROXY_DATASPACE_NAME); - final List<ModuleReference> existingModuleReferences = new ArrayList<>(); + private void fetchAndSyncModules(final PersistenceCmHandle persistenceCmHandle) { + final Map<String, String> cmHandlePropertiesAsMap = getCmHandlePropertiesAsMap( + persistenceCmHandle.getAdditionalProperties()); + final List<ModuleReference> moduleReferencesFromCmHandle = + fetchModuleReferencesFromDmi(persistenceCmHandle, cmHandlePropertiesAsMap); + final List<ModuleReference> existingModuleReferences = new ArrayList<>(); final List<ModuleReference> unknownModuleReferences = new ArrayList<>(); - for (final ModuleReference moduleReferenceFromDmiForCmHandle : moduleReferencesFromDmiForCmHandle) { + prepareModuleSubsets(moduleReferencesFromCmHandle, existingModuleReferences, unknownModuleReferences); + + final Map<String, String> newYangResourcesModuleNameToContentMap = + getNewYangResourcesFromDmi(persistenceCmHandle, unknownModuleReferences, cmHandlePropertiesAsMap); + + cpsModuleService.createSchemaSetFromModules(NF_PROXY_DATASPACE_NAME, persistenceCmHandle.getId(), + newYangResourcesModuleNameToContentMap, existingModuleReferences); + } + + private void prepareModuleSubsets(final List<ModuleReference> moduleReferencesFromCmHandle, + final List<ModuleReference> existingModuleReferences, + final List<ModuleReference> unknownModuleReferences) { + + final Collection<ModuleReference> knownModuleReferencesInCps = + cpsModuleService.getYangResourceModuleReferences(NF_PROXY_DATASPACE_NAME); + + for (final ModuleReference moduleReferenceFromDmiForCmHandle : moduleReferencesFromCmHandle) { if (knownModuleReferencesInCps.contains(moduleReferenceFromDmiForCmHandle)) { existingModuleReferences.add(moduleReferenceFromDmiForCmHandle); } else { unknownModuleReferences.add(moduleReferenceFromDmiForCmHandle); } } - - final JsonObject requestBodyAsJson = getRequestBodyAsJson(unknownModuleReferences); - - final Map<String, String> newYangResourcesModuleNameToContentMap = - getNewYangResources(cmHandle, requestBodyAsJson.toString()); - - cpsModuleService.createSchemaSetFromModules(NF_PROXY_DATASPACE_NAME, cmHandle.getId(), - newYangResourcesModuleNameToContentMap, existingModuleReferences); - - cpsAdminService.createAnchor(NF_PROXY_DATASPACE_NAME, cmHandle.getId(), cmHandle.getId()); } - private static JsonObject getRequestBodyAsJson(final List<ModuleReference> unknownModuleReferences) { + private List<ModuleReference> fetchModuleReferencesFromDmi(final PersistenceCmHandle persistenceCmHandle, + final Map<String, String> cmHandlePropertiesAsMap) { + final GenericRequestBody requestBodyObject = GenericRequestBody.builder() + .operation(GenericRequestBody.OperationEnum.READ) + .cmHandleProperties(cmHandlePropertiesAsMap) + .build(); + final String jsonBody = prepareOperationBody(requestBodyObject); + final ResponseEntity<String> dmiFetchModulesResponseEntity = + dmiOperations.getResourceFromDmiWithJsonData(persistenceCmHandle.getDmiServiceName(), + jsonBody, persistenceCmHandle.getId(), "modules"); + return toModuleReferences(dmiFetchModulesResponseEntity); + } - final JsonObject requestBodyAsJson = new JsonObject(); - requestBodyAsJson.addProperty("operation", "read"); + private void createAnchor(final PersistenceCmHandle persistenceCmHandle) { + cpsAdminService.createAnchor(NF_PROXY_DATASPACE_NAME, persistenceCmHandle.getId(), persistenceCmHandle.getId()); + } + private String getRequestBodyToFetchYangResourceFromDmi(final List<ModuleReference> unknownModuleReferences, + final Map<String, String> cmHandlePropertiesAsMap) { final JsonArray moduleReferencesAsJson = getModuleReferencesAsJson(unknownModuleReferences); - final JsonObject data = new JsonObject(); data.add("modules", moduleReferencesAsJson); - requestBodyAsJson.add("data", data); - - return requestBodyAsJson; + final GenericRequestBody dmiRequestBodyObject = GenericRequestBody.builder() + .operation(GenericRequestBody.OperationEnum.READ) + .dataType(MediaType.APPLICATION_JSON_VALUE) + .data(data.toString()) + .cmHandleProperties(cmHandlePropertiesAsMap) + .build(); + return prepareOperationBody(dmiRequestBodyObject); } private static JsonArray getModuleReferencesAsJson(final List<ModuleReference> unknownModuleReferences) { @@ -416,9 +458,17 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService return moduleReferences; } - private Map<String, String> getNewYangResources(final PersistenceCmHandle cmHandle, final String jsonData) { - final var moduleResourcesAsJsonString = dmiOperations.getResourceFromDmiWithJsonData( - cmHandle.getDmiServiceName(), jsonData, cmHandle.getId(), "moduleResources"); + private Map<String, String> getNewYangResourcesFromDmi(final PersistenceCmHandle persistenceCmHandle, + final List<ModuleReference> unknownModuleReferences, + final Map<String, String> cmHandlePropertiesAsMap) { + final String jsonData = getRequestBodyToFetchYangResourceFromDmi( + unknownModuleReferences, cmHandlePropertiesAsMap); + + final ResponseEntity<String> moduleResourcesAsJsonString = dmiOperations.getResourceFromDmiWithJsonData( + persistenceCmHandle.getDmiServiceName(), + jsonData, + persistenceCmHandle.getId(), + "moduleResources"); final JsonArray moduleResources = new Gson().fromJson(moduleResourcesAsJsonString.getBody(), JsonArray.class); @@ -444,20 +494,21 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService return yangResource; } - private static List<ModuleReference> getModuleReferences(final ResponseEntity<String> response) { - final List<ModuleReference> modulesFromDmiForCmHandle = new ArrayList<>(); - final JsonObject convertedObject = new Gson().fromJson(response.getBody(), JsonObject.class); - final JsonArray moduleReferencesAsJson = convertedObject.getAsJsonArray("schemas"); + private static List<ModuleReference> toModuleReferences( + final ResponseEntity<String> dmiFetchModulesResponseEntity) { + final List<ModuleReference> moduleReferences = new ArrayList<>(); + final JsonObject bodyAsJsonObject = new Gson().fromJson(dmiFetchModulesResponseEntity.getBody(), + JsonObject.class); + final JsonArray moduleReferencesAsJson = bodyAsJsonObject.getAsJsonArray("schemas"); for (final JsonElement moduleReferenceAsJson : moduleReferencesAsJson) { - final ModuleReference moduleReference = - toModuleReference((JsonObject) moduleReferenceAsJson); - modulesFromDmiForCmHandle.add(moduleReference); + final ModuleReference moduleReference = toModuleReference((JsonObject) moduleReferenceAsJson); + moduleReferences.add(moduleReference); } - return modulesFromDmiForCmHandle; + return moduleReferences; } private static ModuleReference toModuleReference(final JsonObject moduleReferenceAsJson) { - final var moduleReference = new ModuleReference(); + final ModuleReference moduleReference = new ModuleReference(); moduleReference.setModuleName(moduleReferenceAsJson.get("moduleName").getAsString()); moduleReference.setRevision(moduleReferenceAsJson.get(REVISION).getAsString()); return moduleReference; diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/PersistenceCmHandle.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/PersistenceCmHandle.java index a3f47040b..88d97d25f 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/PersistenceCmHandle.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/PersistenceCmHandle.java @@ -58,7 +58,8 @@ public class PersistenceCmHandle { } @AllArgsConstructor - private static class AdditionalProperty { + @Getter + public static class AdditionalProperty { @JsonProperty() private final String name; diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy index 55dd26bda..2b376e97b 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy @@ -66,7 +66,6 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { def cmHandle = 'some handle' def noTimestamp = null def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']" - def cmHandleForModelSync = new PersistenceCmHandle(id:'some cm handle', dmiServiceName: 'some service name') def expectedDataspaceName = 'NFP-Operational' @@ -145,7 +144,7 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { def 'Register or re-register a DMI Plugin with #scenario cm handles.'() { given: 'a registration ' - NetworkCmProxyDataServiceImpl objectUnderTest = getObjectUnderTestWithModelSyncDisabled() + def objectUnderTest = getObjectUnderTestWithModelSyncDisabled() def dmiPluginRegistration = new DmiPluginRegistration() dmiPluginRegistration.dmiPlugin = 'my-server' persistenceCmHandle.cmHandleID = '123' @@ -385,8 +384,8 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { >> { new ResponseEntity<>(HttpStatus.OK) } where: scenario | includeCmHandleProperties || expectedJsonForCmhandleProperties - 'with' | true || '{"testName":"testValue"}' - 'without' | false || '{}' + 'with' | true || '{"testName":"testValue"}' + 'without' | false || '{}' } def 'Write resource data for pass-through running from dmi using POST "not found" response (from DMI).'() { @@ -410,14 +409,27 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { def 'Sync model for a (new) cm handle with #scenario'() { given: 'DMI Plug-in returns a list of module references' - getModulesForCmHandle() def knownModule1 = new ModuleReference('module1', '1') def knownOtherModule = new ModuleReference('some other module', 'some revision') + and: 'persistence cm handle is given' + def cmHandleForModelSync = new PersistenceCmHandle(id:'some cm handle', dmiServiceName: 'some service name') + and: 'additional properties are set as required' + if (additionalProperties!=null) { + cmHandleForModelSync.setAdditionalProperties(additionalProperties) + } + and: 'dmi operations returns some module references' + def jsonData = TestUtils.getResourceFileContent('cmHandleModules.json') + def expectedJsonBody = '{"operation":"read","cmHandleProperties":' + expectedJsonForAdditionalProperties + '}' + mockDmiProperties.getAuthUsername() >> 'someUser' + mockDmiProperties.getAuthPassword() >> 'somePassword' + def moduleReferencesFromCmHandleAsJson = new ResponseEntity<String>(jsonData, HttpStatus.OK) + mockDmiOperations.getResourceFromDmiWithJsonData('some service name', expectedJsonBody, 'some cm handle', 'modules') >> moduleReferencesFromCmHandleAsJson and: 'CPS-Core returns list of known modules' mockCpsModuleService.getYangResourceModuleReferences(_) >> [knownModule1, knownOtherModule] and: 'DMI-Plugin returns resource(s) for "new" module(s)' def moduleResources = new ResponseEntity<String>(sdncReponseBody, HttpStatus.OK) - mockDmiOperations.getResourceFromDmiWithJsonData(_, _, _, 'moduleResources') >> moduleResources + def jsonDataToFetchYangResource = '{"operation":"read","dataType":"application/json","data":"{\\"modules\\":[{\\"name\\":\\"module2\\",\\"revision\\":\\"1\\"}]}","cmHandleProperties":' + expectedJsonForAdditionalProperties + '}' + mockDmiOperations.getResourceFromDmiWithJsonData('some service name', jsonDataToFetchYangResource, 'some cm handle', 'moduleResources') >> moduleResources when: 'module Sync is triggered' objectUnderTest.createAnchorAndSyncModel(cmHandleForModelSync) then: 'the CPS module service is called once with the correct parameters' @@ -425,12 +437,13 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { and: 'admin service create anchor method has been called with correct parameters' 1 * mockCpsAdminService.createAnchor(expectedDataspaceName, cmHandleForModelSync.getId(), cmHandleForModelSync.getId()) where: 'the following responses are received from SDNC' - scenario | sdncReponseBody || expectedYangResourceToContentMap - 'one unknown module' | '[{"moduleName" : "someModule", "revision" : "1","yangSource": "[some yang source]"}]' || [someModule: 'some yang source'] - 'no unknown module' | '[]' || [:] + scenario | additionalProperties | sdncReponseBody || expectedYangResourceToContentMap | expectedJsonForAdditionalProperties + 'one unknown module' | ['name1':'value1'] | '[{"moduleName" : "someModule", "revision" : "1","yangSource": "[some yang source]"}]' || [someModule: 'some yang source'] | '{"name1":"value1"}' + 'no add. properties' | [:] | '[{"moduleName" : "someModule", "revision" : "1","yangSource": "[some yang source]"}]' || [someModule: 'some yang source'] | '{}' + 'additional properties is null' | null | '[{"moduleName" : "someModule", "revision" : "1","yangSource": "[some yang source]"}]' || [someModule: 'some yang source'] | '{}' + 'no unknown module' | [:] | '[]' || [:] | '{}' } - def 'Getting Yang Resources.'() { when: 'yang resources is called' objectUnderTest.getYangResourcesModuleReferences('some cm handle') @@ -438,14 +451,6 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { 1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some cm handle') } - def getModulesForCmHandle() { - def jsonData = TestUtils.getResourceFileContent('cmHandleModules.json') - mockDmiProperties.getAuthUsername() >> 'someUser' - mockDmiProperties.getAuthPassword() >> 'somePassword' - def moduleReferencesFromCmHandleAsJson = new ResponseEntity<String>(jsonData, HttpStatus.OK) - mockDmiOperations.getResourceFromDmi(_, cmHandleForModelSync.getId(), 'modules') >> moduleReferencesFromCmHandleAsJson - } - def getObjectUnderTestWithModelSyncDisabled() { def objectUnderTest = Spy(new NetworkCmProxyDataServiceImpl(mockDmiOperations, mockCpsModuleService, mockCpsDataService, mockCpsQueryService, mockCpsAdminService, spyObjectMapper)) |