2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications Copyright (C) 2021-2023 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.OperationEnum.CREATE;
27 import static org.onap.cps.ncmp.api.impl.operations.OperationEnum.DELETE;
28 import static org.onap.cps.ncmp.api.impl.operations.OperationEnum.PATCH;
29 import static org.onap.cps.ncmp.api.impl.operations.OperationEnum.UPDATE;
31 import java.util.Collection;
32 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.NcmpDatastoreRequestHandler;
43 import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreResourceRequestHandlerFactory;
44 import org.onap.cps.ncmp.rest.exceptions.InvalidDatastoreException;
45 import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper;
46 import org.onap.cps.ncmp.rest.model.CmHandlePublicProperties;
47 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
48 import org.onap.cps.ncmp.rest.model.RestModuleDefinition;
49 import org.onap.cps.ncmp.rest.model.RestModuleReference;
50 import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
51 import org.onap.cps.ncmp.rest.model.RestOutputCmHandleCompositeState;
52 import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
53 import org.onap.cps.ncmp.rest.util.DeprecationHelper;
54 import org.onap.cps.utils.JsonObjectMapper;
55 import org.springframework.http.HttpStatus;
56 import org.springframework.http.ResponseEntity;
57 import org.springframework.web.bind.annotation.RequestMapping;
58 import org.springframework.web.bind.annotation.RestController;
62 @RequestMapping("${rest.api.ncmp-base-path}")
63 @RequiredArgsConstructor
64 public class NetworkCmProxyController implements NetworkCmProxyApi {
66 private static final String NO_BODY = null;
67 private final NetworkCmProxyDataService networkCmProxyDataService;
68 private final JsonObjectMapper jsonObjectMapper;
69 private final DeprecationHelper deprecationHelper;
70 private final NcmpRestInputMapper ncmpRestInputMapper;
71 private final CmHandleStateMapper cmHandleStateMapper;
72 private final NcmpDatastoreResourceRequestHandlerFactory ncmpDatastoreResourceRequestHandlerFactory;
75 * Get resource data from datastore.
77 * @param datastoreName name of the datastore
78 * @param cmHandle cm handle identifier
79 * @param resourceIdentifier resource identifier
80 * @param optionsParamInQuery options query parameter
81 * @param topicParamInQuery topic query parameter
82 * @param includeDescendantsAsObject whether include descendants
83 * @return {@code ResponseEntity} response from dmi plugin
87 public ResponseEntity<Object> getResourceDataForCmHandle(final String datastoreName,
88 final String cmHandle,
89 final String resourceIdentifier,
90 final String optionsParamInQuery,
91 final String topicParamInQuery,
92 final Boolean includeDescendantsAsObject) {
94 final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler =
95 ncmpDatastoreResourceRequestHandlerFactory.getNcmpResourceRequestHandler(
96 DatastoreType.fromDatastoreName(datastoreName));
98 final boolean includeDescendants = toPrimitiveFlag(includeDescendantsAsObject);
100 return ncmpDatastoreRequestHandler.executeRequest(cmHandle, resourceIdentifier,
101 optionsParamInQuery, topicParamInQuery, includeDescendants);
105 public ResponseEntity<Object> getResourceDataForCmHandleBatch(final String resourceIdentifier,
106 final String topicParamInQuery,
107 final String datastoreName,
108 final Object requestBody,
109 final String optionsParamInQuery,
110 final Boolean includeDescendantsAsObject) {
112 final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler =
113 ncmpDatastoreResourceRequestHandlerFactory.getNcmpResourceRequestHandler(
114 DatastoreType.fromDatastoreName(datastoreName));
116 final List<String> cmHandleIds = jsonObjectMapper.convertJsonString(jsonObjectMapper.asJsonString(requestBody),
119 final boolean includeDescendants = toPrimitiveFlag(includeDescendantsAsObject);
121 return ncmpDatastoreRequestHandler.executeRequest(cmHandleIds, resourceIdentifier,
122 optionsParamInQuery, topicParamInQuery, includeDescendants);
126 * Query resource data from datastore.
128 * @param datastoreName name of the datastore
129 * @param cmHandle cm handle identifier
130 * @param cpsPath CPS Path
131 * @param optionsParamInQuery options query parameter
132 * @param topicParamInQuery topic query parameter
133 * @param includeDescendantsAsObject whether include descendants
134 * @return {@code ResponseEntity} response from dmi plugin
138 public ResponseEntity<Object> queryResourceDataForCmHandle(final String datastoreName,
139 final String cmHandle,
140 final String cpsPath,
141 final String optionsParamInQuery,
142 final String topicParamInQuery,
143 final Boolean includeDescendantsAsObject) {
144 validateDataStore(DatastoreType.OPERATIONAL, datastoreName);
145 final NcmpDatastoreRequestHandler ncmpCachedResourceRequestHandler =
146 ncmpDatastoreResourceRequestHandlerFactory.getNcmpResourceRequestHandler(
147 DatastoreType.fromDatastoreName(datastoreName));
149 final boolean includeDescendants = toPrimitiveFlag(includeDescendantsAsObject);
151 return ncmpCachedResourceRequestHandler.executeRequest(cmHandle, cpsPath, includeDescendants);
155 * Patch resource data from passthrough-running.
157 * @param resourceIdentifier resource identifier
158 * @param datastoreName name of the datastore
159 * @param cmHandle cm handle identifier
160 * @param requestBody the request body
161 * @param contentType content type of body
162 * @return {@code ResponseEntity} response from dmi plugin
166 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
167 final String datastoreName,
168 final String cmHandle,
169 final Object requestBody,
170 final String contentType) {
172 validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
174 final Object responseObject = networkCmProxyDataService
175 .writeResourceDataPassThroughRunningForCmHandle(
176 cmHandle, resourceIdentifier, PATCH,
177 jsonObjectMapper.asJsonString(requestBody), contentType);
178 return ResponseEntity.ok(responseObject);
182 * Create resource data in datastore pass-through running for given cm-handle.
184 * @param resourceIdentifier resource identifier
185 * @param datastoreName name of the datastore
186 * @param cmHandle cm handle identifier
187 * @param requestBody the request body
188 * @param contentType content type of body
189 * @return {@code ResponseEntity} response from dmi plugin
192 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
193 final String datastoreName,
194 final String cmHandle,
195 final Object requestBody,
196 final String contentType) {
198 validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
200 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
201 resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType);
202 return new ResponseEntity<>(HttpStatus.CREATED);
206 * Update resource data in datastore pass-through running for given cm-handle.
208 * @param resourceIdentifier resource identifier
209 * @param datastoreName name of the datastore
210 * @param cmHandle cm handle identifier
211 * @param requestBody the request body
212 * @param contentType content type of the body
213 * @return response entity
217 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
218 final String datastoreName,
219 final String cmHandle,
220 final Object requestBody,
221 final String contentType) {
222 validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
224 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
225 resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType);
226 return new ResponseEntity<>(HttpStatus.OK);
230 * Delete resource data in datastore pass-through running for a given cm-handle.
232 * @param datastoreName name of the datastore
233 * @param cmHandle cm handle identifier
234 * @param resourceIdentifier resource identifier
235 * @param contentType content type of the body
236 * @return response entity no content if request is successful
239 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
240 final String cmHandle,
241 final String resourceIdentifier,
242 final String contentType) {
244 validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
246 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
247 resourceIdentifier, DELETE, NO_BODY, contentType);
248 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
252 * Query and return cm handles that match the given query parameters.
254 * @param cmHandleQueryParameters the cm handle query parameters
255 * @return collection of cm handles
258 @SuppressWarnings("deprecation") // mapOldConditionProperties method will be removed in Release 12
259 public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
260 final CmHandleQueryParameters cmHandleQueryParameters) {
261 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
262 deprecationHelper.mapOldConditionProperties(cmHandleQueryParameters);
263 final Collection<NcmpServiceCmHandle> cmHandles = networkCmProxyDataService
264 .executeCmHandleSearch(cmHandleQueryApiParameters);
265 final List<RestOutputCmHandle> outputCmHandles =
266 cmHandles.stream().map(this::toRestOutputCmHandle).collect(Collectors.toList());
267 return ResponseEntity.ok(outputCmHandles);
271 * Query and return cm handle ids that match the given query parameters.
273 * @param cmHandleQueryParameters the cm handle query parameters
274 * @return collection of cm handle ids
277 public ResponseEntity<List<String>> searchCmHandleIds(
278 final CmHandleQueryParameters cmHandleQueryParameters) {
279 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
280 jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class);
281 final Collection<String> cmHandleIds
282 = networkCmProxyDataService.executeCmHandleIdSearch(cmHandleQueryApiParameters);
283 return ResponseEntity.ok(List.copyOf(cmHandleIds));
287 * Search for Cm Handle and Properties by Name.
289 * @param cmHandleId cm-handle identifier
290 * @return cm handle and its properties
293 public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
294 final NcmpServiceCmHandle ncmpServiceCmHandle = networkCmProxyDataService.getNcmpServiceCmHandle(cmHandleId);
295 final RestOutputCmHandle restOutputCmHandle = toRestOutputCmHandle(ncmpServiceCmHandle);
296 return ResponseEntity.ok(restOutputCmHandle);
300 * Get Cm Handle Properties by Cm Handle Id.
302 * @param cmHandleId cm-handle identifier
303 * @return cm handle properties
306 public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
307 final String cmHandleId) {
308 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
309 cmHandlePublicProperties.add(networkCmProxyDataService.getCmHandlePublicProperties(cmHandleId));
310 final RestOutputCmHandlePublicProperties restOutputCmHandlePublicProperties =
311 new RestOutputCmHandlePublicProperties();
312 restOutputCmHandlePublicProperties.setPublicCmHandleProperties(cmHandlePublicProperties);
313 return ResponseEntity.ok(restOutputCmHandlePublicProperties);
317 * Get Cm Handle State by Cm Handle Id.
319 * @param cmHandleId cm-handle identifier
320 * @return cm handle state
323 public ResponseEntity<RestOutputCmHandleCompositeState> getCmHandleStateByCmHandleId(
324 final String cmHandleId) {
325 final CompositeState cmHandleState = networkCmProxyDataService.getCmHandleCompositeState(cmHandleId);
326 final RestOutputCmHandleCompositeState restOutputCmHandleCompositeState =
327 new RestOutputCmHandleCompositeState();
328 restOutputCmHandleCompositeState.setState(
329 cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(cmHandleState));
330 return ResponseEntity.ok(restOutputCmHandleCompositeState);
334 * Return module definitions for a cm handle.
336 * @param cmHandleId cm-handle identifier
337 * @return list of module definitions (module name, revision, yang resource content)
340 public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
341 final List<RestModuleDefinition> restModuleDefinitions =
342 networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId).stream()
343 .map(ncmpRestInputMapper::toRestModuleDefinition)
344 .collect(Collectors.toList());
345 return new ResponseEntity<>(restModuleDefinitions, HttpStatus.OK);
349 * Return module references for a cm handle.
351 * @param cmHandle the cm handle
352 * @return module references for cm handle. Namespace will be always blank because restConf does not include this.
354 public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
355 final List<RestModuleReference> restModuleReferences =
356 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
357 .map(ncmpRestInputMapper::toRestModuleReference)
358 .collect(Collectors.toList());
359 return new ResponseEntity<>(restModuleReferences, HttpStatus.OK);
363 * Set the data sync enabled flag, along with the data sync state for the specified cm handle.
365 * @param cmHandleId cm handle id
366 * @param dataSyncEnabledFlag data sync enabled flag
367 * @return response entity ok if request is successful
370 public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
371 final Boolean dataSyncEnabledFlag) {
372 networkCmProxyDataService.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag);
373 return new ResponseEntity<>(HttpStatus.OK);
377 private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) {
378 final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle();
379 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
380 restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId());
381 cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties());
382 restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties);
383 restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(
384 ncmpServiceCmHandle.getCompositeState()));
385 return restOutputCmHandle;
388 private void validateDataStore(final DatastoreType acceptableDataStoreType, final String requestedDatastoreName) {
389 final DatastoreType datastoreType = DatastoreType.fromDatastoreName(requestedDatastoreName);
391 if (acceptableDataStoreType != datastoreType) {
392 throw new InvalidDatastoreException(requestedDatastoreName + " is not supported");
396 private static boolean toPrimitiveFlag(final Boolean includeDescendantsAsObject) {
397 if (includeDescendantsAsObject == null) {
400 return includeDescendantsAsObject.booleanValue();