2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications Copyright (C) 2021-2022 Nordix Foundation
5 * Modifications Copyright (C) 2021 highstreet technologies GmbH
6 * Modifications Copyright (C) 2021-2022 Bell Canada
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.cps.ncmp.rest.controller;
26 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE;
27 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.DELETE;
28 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH;
29 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE;
31 import java.util.List;
33 import java.util.stream.Collectors;
34 import lombok.RequiredArgsConstructor;
35 import lombok.extern.slf4j.Slf4j;
36 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
37 import org.onap.cps.ncmp.api.inventory.CompositeState;
38 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
39 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
40 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
41 import org.onap.cps.ncmp.rest.controller.handlers.DatastoreType;
42 import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreResourceRequestHandler;
43 import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreResourceRequestHandlerFactory;
44 import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper;
45 import org.onap.cps.ncmp.rest.model.CmHandlePublicProperties;
46 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
47 import org.onap.cps.ncmp.rest.model.RestModuleDefinition;
48 import org.onap.cps.ncmp.rest.model.RestModuleReference;
49 import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
50 import org.onap.cps.ncmp.rest.model.RestOutputCmHandleCompositeState;
51 import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
52 import org.onap.cps.ncmp.rest.util.DeprecationHelper;
53 import org.onap.cps.utils.JsonObjectMapper;
54 import org.springframework.http.HttpStatus;
55 import org.springframework.http.ResponseEntity;
56 import org.springframework.web.bind.annotation.RequestMapping;
57 import org.springframework.web.bind.annotation.RestController;
61 @RequestMapping("${rest.api.ncmp-base-path}")
62 @RequiredArgsConstructor
63 public class NetworkCmProxyController implements NetworkCmProxyApi {
65 private static final String NO_BODY = null;
66 private final NetworkCmProxyDataService networkCmProxyDataService;
67 private final JsonObjectMapper jsonObjectMapper;
68 private final DeprecationHelper deprecationHelper;
69 private final NcmpRestInputMapper ncmpRestInputMapper;
70 private final CmHandleStateMapper cmHandleStateMapper;
71 private final NcmpDatastoreResourceRequestHandlerFactory ncmpDatastoreResourceRequestHandlerFactory;
74 * Get resource data from datastore.
76 * @param datastoreName name of the datastore
77 * @param cmHandle cm handle identifier
78 * @param resourceIdentifier resource identifier
79 * @param optionsParamInQuery options query parameter
80 * @param topicParamInQuery topic query parameter
81 * @param includeDescendants whether include descendants
82 * @return {@code ResponseEntity} response from dmi plugin
86 public ResponseEntity<Object> getResourceDataForCmHandle(final String datastoreName,
87 final String cmHandle,
88 final String resourceIdentifier,
89 final String optionsParamInQuery,
90 final String topicParamInQuery,
91 final Boolean includeDescendants) {
93 final NcmpDatastoreResourceRequestHandler ncmpDatastoreResourceRequestHandler =
94 ncmpDatastoreResourceRequestHandlerFactory.getNcmpDatastoreResourceRequestHandler(
95 DatastoreType.fromDatastoreName(datastoreName));
97 return ncmpDatastoreResourceRequestHandler.getResourceData(cmHandle, resourceIdentifier,
98 optionsParamInQuery, topicParamInQuery, includeDescendants);
102 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
103 final String cmHandle,
104 final Object requestBody,
105 final String contentType) {
106 final Object responseObject = networkCmProxyDataService
107 .writeResourceDataPassThroughRunningForCmHandle(
108 cmHandle, resourceIdentifier, PATCH,
109 jsonObjectMapper.asJsonString(requestBody), contentType);
110 return ResponseEntity.ok(responseObject);
114 * Create resource data in datastore pass-through running for given cm-handle.
116 * @param resourceIdentifier resource identifier
117 * @param cmHandle cm handle identifier
118 * @param requestBody the request body
119 * @param contentType content type of body
120 * @return {@code ResponseEntity} response from dmi plugin
123 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
124 final String cmHandle,
125 final Object requestBody,
126 final String contentType) {
127 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
128 resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType);
129 return new ResponseEntity<>(HttpStatus.CREATED);
133 * Update resource data in datastore pass-through running for given cm-handle.
135 * @param resourceIdentifier resource identifier
136 * @param cmHandle cm handle identifier
137 * @param requestBody the request body
138 * @param contentType content type of the body
139 * @return response entity
142 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
143 final String cmHandle,
144 final Object requestBody,
145 final String contentType) {
146 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
147 resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType);
148 return new ResponseEntity<>(HttpStatus.OK);
153 * Delete resource data in datastore pass-through running for a given cm-handle.
155 * @param resourceIdentifier resource identifier
156 * @param cmHandle cm handle identifier
157 * @param contentType content type of the body
158 * @return response entity no content if request is successful
161 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String cmHandle,
162 final String resourceIdentifier,
163 final String contentType) {
164 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
165 resourceIdentifier, DELETE, NO_BODY, contentType);
166 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
170 * Query and return cm handles that match the given query parameters.
172 * @param cmHandleQueryParameters the cm handle query parameters
173 * @return collection of cm handles
176 @SuppressWarnings("deprecation") // mapOldConditionProperties method will be removed in Release 12
177 public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
178 final CmHandleQueryParameters cmHandleQueryParameters) {
179 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
180 deprecationHelper.mapOldConditionProperties(cmHandleQueryParameters);
181 final Set<NcmpServiceCmHandle> cmHandles = networkCmProxyDataService
182 .executeCmHandleSearch(cmHandleQueryApiParameters);
183 final List<RestOutputCmHandle> outputCmHandles =
184 cmHandles.stream().map(this::toRestOutputCmHandle).collect(Collectors.toList());
185 return ResponseEntity.ok(outputCmHandles);
189 * Query and return cm handle ids that match the given query parameters.
191 * @param cmHandleQueryParameters the cm handle query parameters
192 * @return collection of cm handle ids
195 public ResponseEntity<List<String>> searchCmHandleIds(
196 final CmHandleQueryParameters cmHandleQueryParameters) {
197 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
198 jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class);
199 final Set<String> cmHandleIds = networkCmProxyDataService.executeCmHandleIdSearch(cmHandleQueryApiParameters);
200 return ResponseEntity.ok(List.copyOf(cmHandleIds));
204 * Search for Cm Handle and Properties by Name.
206 * @param cmHandleId cm-handle identifier
207 * @return cm handle and its properties
210 public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
211 final NcmpServiceCmHandle ncmpServiceCmHandle = networkCmProxyDataService.getNcmpServiceCmHandle(cmHandleId);
212 final RestOutputCmHandle restOutputCmHandle = toRestOutputCmHandle(ncmpServiceCmHandle);
213 return ResponseEntity.ok(restOutputCmHandle);
217 * Get Cm Handle Properties by Cm Handle Id.
219 * @param cmHandleId cm-handle identifier
220 * @return cm handle properties
223 public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
224 final String cmHandleId) {
225 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
226 cmHandlePublicProperties.add(networkCmProxyDataService.getCmHandlePublicProperties(cmHandleId));
227 final RestOutputCmHandlePublicProperties restOutputCmHandlePublicProperties =
228 new RestOutputCmHandlePublicProperties();
229 restOutputCmHandlePublicProperties.setPublicCmHandleProperties(cmHandlePublicProperties);
230 return ResponseEntity.ok(restOutputCmHandlePublicProperties);
234 * Get Cm Handle State by Cm Handle Id.
236 * @param cmHandleId cm-handle identifier
237 * @return cm handle state
240 public ResponseEntity<RestOutputCmHandleCompositeState> getCmHandleStateByCmHandleId(
241 final String cmHandleId) {
242 final CompositeState cmHandleState = networkCmProxyDataService.getCmHandleCompositeState(cmHandleId);
243 final RestOutputCmHandleCompositeState restOutputCmHandleCompositeState =
244 new RestOutputCmHandleCompositeState();
245 restOutputCmHandleCompositeState.setState(
246 cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(cmHandleState));
247 return ResponseEntity.ok(restOutputCmHandleCompositeState);
251 * Return module definitions for a cm handle.
253 * @param cmHandleId cm-handle identifier
254 * @return list of module definitions (module name, revision, yang resource content)
257 public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
258 final List<RestModuleDefinition> restModuleDefinitions =
259 networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId).stream()
260 .map(ncmpRestInputMapper::toRestModuleDefinition)
261 .collect(Collectors.toList());
262 return new ResponseEntity<>(restModuleDefinitions, HttpStatus.OK);
266 * Return module references for a cm handle.
268 * @param cmHandle the cm handle
269 * @return module references for cm handle. Namespace will be always blank because restConf does not include this.
271 public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
272 final List<RestModuleReference> restModuleReferences =
273 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
274 .map(ncmpRestInputMapper::toRestModuleReference)
275 .collect(Collectors.toList());
276 return new ResponseEntity<>(restModuleReferences, HttpStatus.OK);
280 * Set the data sync enabled flag, along with the data sync state for the specified cm handle.
282 * @param cmHandleId cm handle id
283 * @param dataSyncEnabledFlag data sync enabled flag
284 * @return response entity ok if request is successful
287 public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
288 final Boolean dataSyncEnabledFlag) {
289 networkCmProxyDataService.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag);
290 return new ResponseEntity<>(HttpStatus.OK);
293 private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) {
294 final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle();
295 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
296 restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId());
297 cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties());
298 restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties);
299 restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(
300 ncmpServiceCmHandle.getCompositeState()));
301 return restOutputCmHandle;