2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications Copyright (C) 2021-2024 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.DatastoreType.OPERATIONAL;
27 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING;
28 import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE;
29 import static org.onap.cps.ncmp.api.impl.operations.OperationType.DELETE;
30 import static org.onap.cps.ncmp.api.impl.operations.OperationType.PATCH;
31 import static org.onap.cps.ncmp.api.impl.operations.OperationType.UPDATE;
33 import io.micrometer.core.annotation.Timed;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.List;
38 import java.util.stream.Collectors;
39 import lombok.RequiredArgsConstructor;
40 import lombok.extern.slf4j.Slf4j;
41 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
42 import org.onap.cps.ncmp.api.impl.exception.InvalidDatastoreException;
43 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
44 import org.onap.cps.ncmp.api.impl.operations.DatastoreType;
45 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
46 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
47 import org.onap.cps.ncmp.api.models.CmResourceAddress;
48 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
49 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
50 import org.onap.cps.ncmp.rest.controller.handlers.NcmpCachedResourceRequestHandler;
51 import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreRequestHandler;
52 import org.onap.cps.ncmp.rest.controller.handlers.NcmpPassthroughResourceRequestHandler;
53 import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper;
54 import org.onap.cps.ncmp.rest.mapper.DataOperationRequestMapper;
55 import org.onap.cps.ncmp.rest.model.CmHandlePublicProperties;
56 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
57 import org.onap.cps.ncmp.rest.model.DataOperationRequest;
58 import org.onap.cps.ncmp.rest.model.RestModuleDefinition;
59 import org.onap.cps.ncmp.rest.model.RestModuleReference;
60 import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
61 import org.onap.cps.ncmp.rest.model.RestOutputCmHandleCompositeState;
62 import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
63 import org.onap.cps.ncmp.rest.util.DeprecationHelper;
64 import org.onap.cps.spi.model.ModuleDefinition;
65 import org.onap.cps.utils.JsonObjectMapper;
66 import org.springframework.http.HttpStatus;
67 import org.springframework.http.ResponseEntity;
68 import org.springframework.util.StringUtils;
69 import org.springframework.web.bind.annotation.RequestMapping;
70 import org.springframework.web.bind.annotation.RestController;
74 @RequestMapping("${rest.api.ncmp-base-path}")
75 @RequiredArgsConstructor
76 public class NetworkCmProxyController implements NetworkCmProxyApi {
78 private static final String NO_BODY = null;
79 private final NetworkCmProxyDataService networkCmProxyDataService;
80 private final JsonObjectMapper jsonObjectMapper;
81 private final DeprecationHelper deprecationHelper;
82 private final NcmpRestInputMapper ncmpRestInputMapper;
83 private final CmHandleStateMapper cmHandleStateMapper;
84 private final NcmpCachedResourceRequestHandler ncmpCachedResourceRequestHandler;
85 private final NcmpPassthroughResourceRequestHandler ncmpPassthroughResourceRequestHandler;
86 private final DataOperationRequestMapper dataOperationRequestMapper;
87 private final Map<String, TrustLevel> trustLevelPerCmHandle;
90 * Get resource data from datastore.
92 * @param datastoreName name of the datastore
93 * @param cmHandle cm handle identifier
94 * @param resourceIdentifier resource identifier
95 * @param optionsParamInQuery options query parameter
96 * @param topicParamInQuery topic query parameter
97 * @param includeDescendants whether to include descendants or not
98 * @param authorization contents of Authorization header, or null if not present
99 * @return {@code ResponseEntity} response from dmi plugin
102 @Timed(value = "cps.ncmp.controller.get", description = "Time taken to get resource data from datastore")
103 public ResponseEntity<Object> getResourceDataForCmHandle(final String datastoreName,
104 final String cmHandle,
105 final String resourceIdentifier,
106 final String optionsParamInQuery,
107 final String topicParamInQuery,
108 final Boolean includeDescendants,
109 final String authorization) {
110 final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler = getNcmpDatastoreRequestHandler(datastoreName);
111 final CmResourceAddress cmResourceAddress = new CmResourceAddress(datastoreName, cmHandle, resourceIdentifier);
112 return ncmpDatastoreRequestHandler.executeRequest(cmResourceAddress, optionsParamInQuery, topicParamInQuery,
113 includeDescendants, authorization);
117 public ResponseEntity<Object> executeDataOperationForCmHandles(final String topicParamInQuery,
118 final DataOperationRequest dataOperationRequest,
119 final String authorization) {
120 return ncmpPassthroughResourceRequestHandler.executeRequest(topicParamInQuery,
121 dataOperationRequestMapper.toDataOperationRequest(dataOperationRequest), authorization);
125 * Query resource data from datastore.
127 * @param datastoreName name of the datastore
128 * @param cmHandle cm handle identifier
129 * @param cpsPath CPS Path
130 * @param optionsParamInQuery options query parameter
131 * @param topicParamInQuery topic query parameter
132 * @param includeDescendants whether to include descendants or not
133 * @return {@code ResponseEntity} response from dmi plugin
137 public ResponseEntity<Object> queryResourceDataForCmHandle(final String datastoreName,
138 final String cmHandle,
139 final String cpsPath,
140 final String optionsParamInQuery,
141 final String topicParamInQuery,
142 final Boolean includeDescendants) {
143 validateDataStore(OPERATIONAL, datastoreName);
144 return ncmpCachedResourceRequestHandler.executeRequest(cmHandle, cpsPath, includeDescendants);
148 * Patch resource data from passthrough-running.
150 * @param datastoreName name of the datastore
151 * @param cmHandle cm handle identifier
152 * @param resourceIdentifier resource identifier
153 * @param requestBody the request body
154 * @param contentType content type of body
155 * @param authorization contents of Authorization header, or null if not present
156 * @return {@code ResponseEntity} response from dmi plugin
160 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String datastoreName,
161 final String cmHandle,
162 final String resourceIdentifier,
163 final Object requestBody,
164 final String contentType,
165 final String authorization) {
167 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
169 final Object responseObject = networkCmProxyDataService
170 .writeResourceDataPassThroughRunningForCmHandle(
171 cmHandle, resourceIdentifier, PATCH,
172 jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
173 return ResponseEntity.ok(responseObject);
177 * Create resource data in datastore pass-through running for given cm-handle.
179 * @param datastoreName name of the datastore
180 * @param cmHandle cm handle identifier
181 * @param resourceIdentifier resource identifier
182 * @param requestBody the request body
183 * @param contentType content type of body
184 * @param authorization contents of Authorization header, or null if not present
185 * @return {@code ResponseEntity} response from dmi plugin
188 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String datastoreName,
189 final String cmHandle,
190 final String resourceIdentifier,
191 final Object requestBody,
192 final String contentType,
193 final String authorization) {
194 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
196 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
197 resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
198 return new ResponseEntity<>(HttpStatus.CREATED);
202 * Update resource data in datastore pass-through running for given cm-handle.
204 * @param datastoreName name of the datastore
205 * @param cmHandle cm handle identifier
206 * @param resourceIdentifier resource identifier
207 * @param requestBody the request body
208 * @param contentType content type of the body
209 * @param authorization contents of Authorization header, or null if not present
210 * @return response entity
214 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String datastoreName,
215 final String cmHandle,
216 final String resourceIdentifier,
217 final Object requestBody,
218 final String contentType,
219 final String authorization) {
220 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
222 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
223 resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
224 return new ResponseEntity<>(HttpStatus.OK);
228 * Delete resource data in datastore pass-through running for a given cm-handle.
230 * @param datastoreName name of the datastore
231 * @param cmHandle cm handle identifier
232 * @param resourceIdentifier resource identifier
233 * @param contentType content type of the body
234 * @param authorization contents of Authorization header, or null if not present
235 * @return response entity no content if request is successful
238 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
239 final String cmHandle,
240 final String resourceIdentifier,
241 final String contentType,
242 final String authorization) {
244 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
246 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
247 resourceIdentifier, DELETE, NO_BODY, contentType, authorization);
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.
336 * @param cmHandleId cm-handle identifier
337 * @param moduleName module name
338 * @param revision the revision of the module
339 * @return list of module definitions (module name, revision, yang resource content)
342 public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitions(final String cmHandleId,
343 final String moduleName,
344 final String revision) {
345 final Collection<ModuleDefinition> moduleDefinitions;
346 if (StringUtils.hasText(moduleName)) {
348 networkCmProxyDataService.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, revision);
350 moduleDefinitions = networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId);
351 if (StringUtils.hasText(revision)) {
352 log.warn("Ignoring revision filter as no module name is provided");
355 final List<RestModuleDefinition> response = new ArrayList<>();
356 for (final ModuleDefinition moduleDefinition: moduleDefinitions) {
357 response.add(ncmpRestInputMapper.toRestModuleDefinition(moduleDefinition));
359 return new ResponseEntity<>(response, HttpStatus.OK);
363 * Return module references for a cm handle.
365 * @param cmHandle the cm handle
366 * @return module references for cm handle. Namespace will be always blank because restConf does not include this.
368 public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
369 final List<RestModuleReference> restModuleReferences =
370 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
371 .map(ncmpRestInputMapper::toRestModuleReference)
372 .collect(Collectors.toList());
373 return new ResponseEntity<>(restModuleReferences, HttpStatus.OK);
377 * Set the data sync enabled flag, along with the data sync state for the specified cm handle.
379 * @param cmHandleId cm handle id
380 * @param dataSyncEnabledFlag data sync enabled flag
381 * @return response entity ok if request is successful
384 public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
385 final Boolean dataSyncEnabledFlag) {
386 networkCmProxyDataService.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag);
387 return new ResponseEntity<>(HttpStatus.OK);
391 private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) {
392 final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle();
393 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
394 final TrustLevel cmHandleCurrentTrustLevel = trustLevelPerCmHandle.get(ncmpServiceCmHandle.getCmHandleId());
395 restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId());
396 cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties());
397 restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties);
398 restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(
399 ncmpServiceCmHandle.getCompositeState()));
400 if (cmHandleCurrentTrustLevel != null) {
401 restOutputCmHandle.setTrustLevel(cmHandleCurrentTrustLevel.toString());
403 restOutputCmHandle.setModuleSetTag(ncmpServiceCmHandle.getModuleSetTag());
404 restOutputCmHandle.setAlternateId(ncmpServiceCmHandle.getAlternateId());
405 restOutputCmHandle.setDataProducerIdentifier(ncmpServiceCmHandle.getDataProducerIdentifier());
406 return restOutputCmHandle;
409 private void validateDataStore(final DatastoreType acceptableDataStoreType, final String requestedDatastoreName) {
410 final DatastoreType datastoreType = DatastoreType.fromDatastoreName(requestedDatastoreName);
412 if (acceptableDataStoreType != datastoreType) {
413 throw new InvalidDatastoreException(requestedDatastoreName + " is not supported");
417 private NcmpDatastoreRequestHandler getNcmpDatastoreRequestHandler(final String datastoreName) {
418 if (OPERATIONAL.equals(DatastoreType.fromDatastoreName(datastoreName))) {
419 return ncmpCachedResourceRequestHandler;
421 return ncmpPassthroughResourceRequestHandler;