X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-ncmp-rest%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fcps%2Fncmp%2Frest%2Fcontroller%2FNetworkCmProxyController.java;h=e3c457eaf48647b97936390c9c9aad55520a221b;hb=18a79ebafa6c2b8737eb0702e1c17acbf57159ba;hp=e9e022345f7abc4a83506e9d01ef287f6e6c8138;hpb=4f4178c7af4ca5571a0813a5c79f35b11c825d35;p=cps.git diff --git a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java index e9e022345..e3c457eaf 100755 --- a/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java +++ b/cps-ncmp-rest/src/main/java/org/onap/cps/ncmp/rest/controller/NetworkCmProxyController.java @@ -3,6 +3,7 @@ * Copyright (C) 2021 Pantheon.tech * Modifications (C) 2021 Nordix Foundation * Modification Copyright (C) 2021 highstreet technologies GmbH + * Modifications (C) 2021 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,16 +22,32 @@ package org.onap.cps.ncmp.rest.controller; +import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE; +import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.DELETE; +import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH; +import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import lombok.extern.slf4j.Slf4j; +import org.modelmapper.ModelMapper; import org.onap.cps.ncmp.api.NetworkCmProxyDataService; -import org.onap.cps.ncmp.api.models.DmiPluginRegistration; import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi; -import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration; +import org.onap.cps.ncmp.rest.model.CmHandleProperties; +import org.onap.cps.ncmp.rest.model.CmHandleProperty; +import org.onap.cps.ncmp.rest.model.CmHandles; +import org.onap.cps.ncmp.rest.model.ConditionProperties; +import org.onap.cps.ncmp.rest.model.Conditions; +import org.onap.cps.ncmp.rest.model.ModuleNameAsJsonObject; +import org.onap.cps.ncmp.rest.model.ModuleNamesAsJsonArray; +import org.onap.cps.ncmp.rest.model.ModuleReference; import org.onap.cps.spi.FetchDescendantsOption; import org.onap.cps.spi.model.DataNode; import org.onap.cps.utils.DataMapUtils; @@ -39,36 +56,35 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +@Slf4j @RestController @RequestMapping("${rest.api.ncmp-base-path}") public class NetworkCmProxyController implements NetworkCmProxyApi { private static final Gson GSON = new GsonBuilder().create(); + private static final String NO_BODY = null; + private final ModelMapper modelMapper = new ModelMapper(); private final NetworkCmProxyDataService networkCmProxyDataService; - private final ObjectMapper objectMapper; - /** * Constructor Injection for Dependencies. * @param networkCmProxyDataService Data Service Interface - * @param objectMapper Object Mapper */ - public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService, - final ObjectMapper objectMapper) { + public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService) { this.networkCmProxyDataService = networkCmProxyDataService; - this.objectMapper = objectMapper; } /** * Create Node. * @deprecated This Method is no longer used as part of NCMP. */ + // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642 @Override @Deprecated(forRemoval = false) - public ResponseEntity createNode(final String jsonData, final String cmHandle, - final String parentNodeXpath) { - networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, jsonData); + public ResponseEntity createNode(final String cmHandle, @Valid final Object jsonData, + @Valid final String parentNodeXpath) { + networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, GSON.toJson(jsonData)); return new ResponseEntity<>(HttpStatus.CREATED); } @@ -76,11 +92,12 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { * Add List-node Child Element. * @deprecated This Method is no longer used as part of NCMP. */ + // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642 @Override @Deprecated(forRemoval = false) - public ResponseEntity addListNodeElements(final String jsonData, final String parentNodeXpath, - final String cmHandle) { - networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData); + public ResponseEntity addListNodeElements(@NotNull @Valid final String parentNodeXpath, + final String cmHandle, @Valid final Object jsonData) { + networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, GSON.toJson(jsonData)); return new ResponseEntity<>(HttpStatus.CREATED); } @@ -88,6 +105,7 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { * Get Node By CM Handle and X-Path. * @deprecated This Method is no longer used as part of NCMP. */ + // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642 @Override @Deprecated(forRemoval = false) public ResponseEntity getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath, @@ -98,23 +116,11 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK); } - /** - * Update DMI Plugin Registration (used for first registration also). - * @param restDmiPluginRegistration the registration data - */ - @Override - public ResponseEntity updateDmiPluginRegistration( - final @Valid RestDmiPluginRegistration restDmiPluginRegistration) { - final DmiPluginRegistration dmiPluginRegistration = - convertRestObjectToJavaApiObject(restDmiPluginRegistration); - networkCmProxyDataService.updateDmiPluginRegistration(dmiPluginRegistration); - return new ResponseEntity<>(HttpStatus.CREATED); - } - /** * Query Data Nodes. * @deprecated This Method is no longer used as part of NCMP. */ + // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642 @Override @Deprecated(forRemoval = false) public ResponseEntity queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath, @@ -130,11 +136,12 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { * Replace Node With Descendants. * @deprecated This Method is no longer used as part of NCMP. */ + // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642 @Override @Deprecated(forRemoval = false) - public ResponseEntity replaceNode(@Valid final String jsonData, final String cmHandle, + public ResponseEntity replaceNode(final String cmHandle, @Valid final Object jsonData, @Valid final String parentNodeXpath) { - networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, jsonData); + networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, GSON.toJson(jsonData)); return new ResponseEntity<>(HttpStatus.OK); } @@ -142,18 +149,181 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { * Update Node Leaves. * @deprecated This Method is no longer used as part of NCMP. */ + // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642 @Override @Deprecated(forRemoval = false) - public ResponseEntity updateNodeLeaves(@Valid final String jsonData, final String cmHandle, + public ResponseEntity updateNodeLeaves(final String cmHandle, @Valid final Object jsonData, @Valid final String parentNodeXpath) { - networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, jsonData); + networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, GSON.toJson(jsonData)); return new ResponseEntity<>(HttpStatus.OK); } + /** + * Get resource data from operational datastore. + * + * @param cmHandle cm handle identifier + * @param resourceIdentifier resource identifier + * @param acceptParamInHeader accept header parameter + * @param optionsParamInQuery options query parameter + * @return {@code ResponseEntity} response from dmi plugin + */ + @Override + public ResponseEntity getResourceDataOperationalForCmHandle(final String cmHandle, + final @NotNull @Valid String resourceIdentifier, + final String acceptParamInHeader, + final @Valid String optionsParamInQuery) { + final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle, + resourceIdentifier, + acceptParamInHeader, + optionsParamInQuery); + return ResponseEntity.ok(responseObject); + } - private DmiPluginRegistration convertRestObjectToJavaApiObject( - final RestDmiPluginRegistration restDmiPluginRegistration) { - return objectMapper.convertValue(restDmiPluginRegistration, DmiPluginRegistration.class); + /** + * Get resource data from pass-through running datastore. + * + * @param cmHandle cm handle identifier + * @param resourceIdentifier resource identifier + * @param acceptParamInHeader accept header parameter + * @param optionsParamInQuery options query parameter + * @return {@code ResponseEntity} response from dmi plugin + */ + @Override + public ResponseEntity getResourceDataRunningForCmHandle(final String cmHandle, + final @NotNull @Valid String resourceIdentifier, + final String acceptParamInHeader, + final @Valid String optionsParamInQuery) { + final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle, + resourceIdentifier, + acceptParamInHeader, + optionsParamInQuery); + return ResponseEntity.ok(responseObject); } + @Override + public ResponseEntity patchResourceDataRunningForCmHandle(final String resourceIdentifier, + final String cmHandle, + final Object requestBody, final String contentType) { + networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, + resourceIdentifier, PATCH, GSON.toJson(requestBody), contentType); + return new ResponseEntity<>(HttpStatus.OK); + } + + /** + * Create resource data in datastore pass-through running for given cm-handle. + * + * @param resourceIdentifier resource identifier + * @param cmHandle cm handle identifier + * @param requestBody the request body + * @param contentType content type of body + * @return {@code ResponseEntity} response from dmi plugin + */ + @Override + public ResponseEntity createResourceDataRunningForCmHandle(final String resourceIdentifier, + final String cmHandle, final Object requestBody, final String contentType) { + networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, + resourceIdentifier, CREATE, GSON.toJson(requestBody), contentType); + return new ResponseEntity<>(HttpStatus.CREATED); + } + + /** + * Update resource data in datastore pass-through running for given cm-handle. + * + * @param resourceIdentifier resource identifier + * @param cmHandle cm handle identifier + * @param requestBody the request body + * @param contentType content type of the body + * @return response entity + */ + @Override + public ResponseEntity updateResourceDataRunningForCmHandle(final String resourceIdentifier, + final String cmHandle, + final Object requestBody, + final String contentType) { + networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, + resourceIdentifier, UPDATE, GSON.toJson(requestBody), contentType); + return new ResponseEntity<>(HttpStatus.OK); + } + + + /** + * Delete resource data in datastore pass-through running for a given cm-handle. + * + * @param resourceIdentifier resource identifier + * @param cmHandle cm handle identifier + * @param contentType content type of the body + * @return response entity no content if request is successful + */ + @Override + public ResponseEntity deleteResourceDataRunningForCmHandle(final String cmHandle, + final String resourceIdentifier, + final String contentType) { + networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle, + resourceIdentifier, DELETE, NO_BODY, contentType); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + /** + * Execute cm handle search. + * + * @param conditions the conditions + * @return cm handles returned from search. + */ + @Override + public ResponseEntity executeCmHandleSearch(final Conditions conditions) { + final List conditionProperties = + conditions.getConditions().stream().collect(Collectors.toList()); + final CmHandles cmHandles = new CmHandles(); + cmHandles.setCmHandles(toCmHandleProperties(processConditions(conditionProperties))); + return ResponseEntity.ok(cmHandles); + } + + /** + * Return module references for a cm handle. + * + * @param cmHandle the cm handle + * @return module references for cm handle + */ + public ResponseEntity> getModuleReferencesByCmHandle(final String cmHandle) { + final List moduleReferences = + networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream() + .map(moduleReference -> modelMapper.map(moduleReference, ModuleReference.class)) + .collect(Collectors.toList()); + return new ResponseEntity<>(moduleReferences, HttpStatus.OK); + } + + private Collection processConditions(final List conditionProperties) { + for (final ConditionProperties conditionProperty : conditionProperties) { + if (conditionProperty.getName().equals("hasAllModules")) { + return executeCmHandleSearchesForModuleNames(conditionProperty); + } else { + log.warn("Unrecognized condition name {}.", conditionProperty.getName()); + } + } + log.warn("No valid conditions found {}.", conditionProperties); + return Collections.emptyList(); + } + + private Collection executeCmHandleSearchesForModuleNames(final ConditionProperties conditionProperties) { + return networkCmProxyDataService + .executeCmHandleHasAllModulesSearch(getModuleNames(conditionProperties.getConditionParameters())); + } + + private Collection getModuleNames(final ModuleNamesAsJsonArray moduleNamesAsJsonArray) { + final Collection moduleNames = new ArrayList<>(moduleNamesAsJsonArray.size()); + for (final ModuleNameAsJsonObject moduleNameAsJsonObject : moduleNamesAsJsonArray) { + moduleNames.add(moduleNameAsJsonObject.getModuleName()); + } + return moduleNames; + } + + private CmHandleProperties toCmHandleProperties(final Collection cmHandleIdentifiers) { + final CmHandleProperties cmHandleProperties = new CmHandleProperties(); + for (final String cmHandleIdentifier : cmHandleIdentifiers) { + final CmHandleProperty cmHandleProperty = new CmHandleProperty(); + cmHandleProperty.setCmHandleId(cmHandleIdentifier); + cmHandleProperties.add(cmHandleProperty); + } + return cmHandleProperties; + } }