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=19b91933306fc892fef1076f57538f94e615b5d7;hb=ea01d09861289ff162dff318713d1c24eba89259;hp=419f6e926866e1cce4fd3ef0ffa9c535457eeb24;hpb=05e7b8a207b1c641468e77d754fe1a5ae9e1a51e;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 419f6e926..19b919333 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 @@ -1,15 +1,16 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2021 Pantheon.tech - * Modifications (C) 2021-2022 Nordix Foundation + * Modifications Copyright (C) 2021-2022 Nordix Foundation * Modification Copyright (C) 2021 highstreet technologies GmbH - * Modifications (C) 2021 Bell Canada + * Modifications (C) 2021-2022 Bell Canada * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -30,23 +31,30 @@ import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.UUID; import java.util.stream.Collectors; import javax.validation.Valid; import javax.validation.constraints.NotNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.modelmapper.ModelMapper; import org.onap.cps.ncmp.api.NetworkCmProxyDataService; +import org.onap.cps.ncmp.api.impl.exception.InvalidTopicException; +import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi; import org.onap.cps.ncmp.rest.model.CmHandleProperties; import org.onap.cps.ncmp.rest.model.CmHandleProperty; +import org.onap.cps.ncmp.rest.model.CmHandlePublicProperties; 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.ncmp.rest.model.RestModuleReference; +import org.onap.cps.ncmp.rest.model.RestOutputCmHandle; +import org.onap.cps.utils.CpsValidator; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -60,30 +68,41 @@ import org.springframework.web.bind.annotation.RestController; public class NetworkCmProxyController implements NetworkCmProxyApi { private static final String NO_BODY = null; + private static final String NO_REQUEST_ID = null; + private static final String NO_TOPIC = null; + public static final String ASYNC_REQUEST_ID = "requestId"; - private final ModelMapper modelMapper; private final NetworkCmProxyDataService networkCmProxyDataService; private final JsonObjectMapper jsonObjectMapper; + private final NcmpRestInputMapper ncmpRestInputMapper; /** * 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 + * @param topicParamInQuery topic 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 @Valid String optionsParamInQuery, + final @Valid String topicParamInQuery) { + final ResponseEntity> asyncResponse = populateAsyncResponse(topicParamInQuery); + final Map asyncResponseData = asyncResponse.getBody(); + final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle, resourceIdentifier, - acceptParamInHeader, - optionsParamInQuery); - return ResponseEntity.ok(responseObject); + optionsParamInQuery, + asyncResponseData == null ? NO_TOPIC : topicParamInQuery, + asyncResponseData == null ? NO_REQUEST_ID : asyncResponseData.get(ASYNC_REQUEST_ID).toString()); + + if (asyncResponseData == null) { + return ResponseEntity.ok(responseObject); + } + return ResponseEntity.ok(asyncResponse); } /** @@ -91,20 +110,28 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { * * @param cmHandle cm handle identifier * @param resourceIdentifier resource identifier - * @param acceptParamInHeader accept header parameter * @param optionsParamInQuery options query parameter + * @param topicParamInQuery topic 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 @Valid String optionsParamInQuery, + final @Valid String topicParamInQuery) { + final ResponseEntity> asyncResponse = populateAsyncResponse(topicParamInQuery); + final Map asyncResponseData = asyncResponse.getBody(); + final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle, resourceIdentifier, - acceptParamInHeader, - optionsParamInQuery); - return ResponseEntity.ok(responseObject); + optionsParamInQuery, + asyncResponseData == null ? NO_TOPIC : topicParamInQuery, + asyncResponseData == null ? NO_REQUEST_ID : asyncResponseData.get(ASYNC_REQUEST_ID).toString()); + + if (asyncResponseData == null) { + return ResponseEntity.ok(responseObject); + } + return ResponseEntity.ok(asyncResponse); } @Override @@ -185,18 +212,30 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { return ResponseEntity.ok(cmHandles); } + /** + * Search for Cm Handle and Properties by Name. + * @param cmHandleId cm-handle identifier + * @return cm handle and its properties + */ + @Override + public ResponseEntity retrieveCmHandleDetailsById(final String cmHandleId) { + final NcmpServiceCmHandle ncmpServiceCmHandle = networkCmProxyDataService.getNcmpServiceCmHandle(cmHandleId); + final RestOutputCmHandle restOutputCmHandle = toRestOutputCmHandle(ncmpServiceCmHandle); + return ResponseEntity.ok(restOutputCmHandle); + } + /** * Return module references for a cm handle. * * @param cmHandle the cm handle - * @return module references for cm handle + * @return module references for cm handle. Namespace will be always blank because restConf does not include this. */ - public ResponseEntity> getModuleReferencesByCmHandle(final String cmHandle) { - final List moduleReferences = + public ResponseEntity> getModuleReferencesByCmHandle(final String cmHandle) { + final List restModuleReferences = networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream() - .map(moduleReference -> modelMapper.map(moduleReference, ModuleReference.class)) + .map(ncmpRestInputMapper::toRestModuleReference) .collect(Collectors.toList()); - return new ResponseEntity<>(moduleReferences, HttpStatus.OK); + return new ResponseEntity<>(restModuleReferences, HttpStatus.OK); } private Collection processConditions(final List conditionProperties) { @@ -233,4 +272,42 @@ public class NetworkCmProxyController implements NetworkCmProxyApi { } return cmHandleProperties; } + + private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) { + final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle(); + final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties(); + restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleID()); + cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties()); + restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties); + return restOutputCmHandle; + } + + private ResponseEntity> populateAsyncResponse(final String topicParamInQuery) { + final boolean processAsynchronously = hasTopicParameter(topicParamInQuery); + final Map responseData; + if (processAsynchronously) { + responseData = getAsyncResponseData(); + } else { + responseData = null; + } + return ResponseEntity.ok().body(responseData); + } + + private static boolean hasTopicParameter(final String topicName) { + if (topicName == null) { + return false; + } + if (CpsValidator.validateTopicName(topicName)) { + return true; + } + throw new InvalidTopicException("Topic name " + topicName + " is invalid", "invalid topic"); + } + + private Map getAsyncResponseData() { + final Map asyncResponseData = new HashMap<>(1); + final String resourceDataRequestId = UUID.randomUUID().toString(); + asyncResponseData.put(ASYNC_REQUEST_ID, resourceDataRequestId); + return asyncResponseData; + } + }