/* * ============LICENSE_START======================================================= * Copyright (C) 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. * See the License for the specific language governing permissions and * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ package org.onap.cps.ncmp.rest.stub.controller; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; import javax.validation.Valid; import javax.validation.constraints.NotNull; import lombok.extern.slf4j.Slf4j; import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi; import org.onap.cps.ncmp.rest.model.CmHandleQueryRestParameters; import org.onap.cps.ncmp.rest.model.CmHandles; import org.onap.cps.ncmp.rest.model.Conditions; import org.onap.cps.ncmp.rest.model.RestModuleReference; import org.onap.cps.ncmp.rest.model.RestOutputCmHandle; import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.http.HttpStatus; 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-stub-base-path}") public class NetworkCmProxyStubController implements NetworkCmProxyApi { public static final String ASYNC_REQUEST_ID = "requestId"; @Value("${stub.path}") private String pathToResponseFiles; @Override public ResponseEntity createResourceDataRunningForCmHandle(@NotNull @Valid final String resourceIdentifier, final String cmHandleId, @Valid final Object body, final String contentType) { return new ResponseEntity<>(HttpStatus.CREATED); } @Override public ResponseEntity deleteResourceDataRunningForCmHandle(final String cmHandleId, @NotNull @Valid final String resourceIdentifier, final String contentType) { return new ResponseEntity<>(HttpStatus.NO_CONTENT); } @Override public ResponseEntity executeCmHandleSearch(@Valid final Conditions body) { final ObjectMapper mapper = new ObjectMapper(); CmHandles cmHandles = new CmHandles(); // read JSON file and map/convert to java POJO final ClassPathResource resource = new ClassPathResource(pathToResponseFiles + "cmHandlesSearch.json"); try (InputStream inputStream = resource.getInputStream()) { final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); cmHandles = mapper.readValue(string, CmHandles.class); } catch (final IOException exception) { log.error("Error reading the file.", exception); } return ResponseEntity.ok(cmHandles); } @Override public ResponseEntity getCmHandlePublicPropertiesByCmHandleId( final String cmHandleId) { return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } @Override public ResponseEntity> getModuleReferencesByCmHandle(final String cmHandleId) { return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } /** * Get resource data from operational datastore. * * @param cmHandleId cm handle identifier * @param resourceIdentifier resource identifier * @param optionsParamInQuery options query parameter * @param topicParamInQuery topic query parameter * @return {@code ResponseEntity} response from dmi plugin */ @Override public ResponseEntity getResourceDataOperationalForCmHandle(final String cmHandleId, final String resourceIdentifier, final String optionsParamInQuery, final String topicParamInQuery) { final ResponseEntity> asyncResponse = populateAsyncResponse(topicParamInQuery); final Map asyncResponseData = asyncResponse.getBody(); final ObjectMapper mapper = new ObjectMapper(); Object responseObject = null; // read JSON file and map/convert to java POJO final ClassPathResource resource = new ClassPathResource(pathToResponseFiles + "passthrough-operational-example.json"); try (InputStream inputStream = resource.getInputStream()) { final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); responseObject = mapper.readValue(string, Object.class); } catch (final IOException exception) { log.error("Error reading the file.", exception); } if (asyncResponseData == null) { return ResponseEntity.ok(responseObject); } return ResponseEntity.ok(asyncResponse); } @Override public ResponseEntity getResourceDataRunningForCmHandle(final String cmHandleId, final String resourceIdentifier, final String options, final String topic) { return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } @Override public ResponseEntity patchResourceDataRunningForCmHandle(final String resourceIdentifier, final String cmHandleId, final Object body, final String contentType) { return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } @Override public ResponseEntity> queryCmHandles(final CmHandleQueryRestParameters body) { return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } @Override public ResponseEntity retrieveCmHandleDetailsById(final String cmHandleId) { return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } @Override public ResponseEntity updateResourceDataRunningForCmHandle(@NotNull @Valid final String resourceIdentifier, final String cmHandleId, @Valid final Object body, final String contentType) { return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED); } private ResponseEntity> populateAsyncResponse(final String topicParamInQuery) { final Map responseData; if (topicParamInQuery != null) { responseData = getAsyncResponseData(); } else { responseData = null; } return ResponseEntity.ok().body(responseData); } private Map getAsyncResponseData() { final Map asyncResponseData = new HashMap<>(1); final String resourceDataRequestId = UUID.randomUUID().toString(); asyncResponseData.put(ASYNC_REQUEST_ID, resourceDataRequestId); return asyncResponseData; } }