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.NcmpServiceCmHandle;
48 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
49 import org.onap.cps.ncmp.rest.controller.handlers.NcmpCachedResourceRequestHandler;
50 import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreRequestHandler;
51 import org.onap.cps.ncmp.rest.controller.handlers.NcmpPassthroughResourceRequestHandler;
52 import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper;
53 import org.onap.cps.ncmp.rest.mapper.DataOperationRequestMapper;
54 import org.onap.cps.ncmp.rest.model.CmHandlePublicProperties;
55 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
56 import org.onap.cps.ncmp.rest.model.DataOperationRequest;
57 import org.onap.cps.ncmp.rest.model.RestModuleDefinition;
58 import org.onap.cps.ncmp.rest.model.RestModuleReference;
59 import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
60 import org.onap.cps.ncmp.rest.model.RestOutputCmHandleCompositeState;
61 import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
62 import org.onap.cps.ncmp.rest.util.DeprecationHelper;
63 import org.onap.cps.spi.model.ModuleDefinition;
64 import org.onap.cps.utils.JsonObjectMapper;
65 import org.springframework.http.HttpStatus;
66 import org.springframework.http.ResponseEntity;
67 import org.springframework.util.StringUtils;
68 import org.springframework.web.bind.annotation.RequestMapping;
69 import org.springframework.web.bind.annotation.RestController;
73 @RequestMapping("${rest.api.ncmp-base-path}")
74 @RequiredArgsConstructor
75 public class NetworkCmProxyController implements NetworkCmProxyApi {
77 private static final String NO_BODY = null;
78 private final NetworkCmProxyDataService networkCmProxyDataService;
79 private final JsonObjectMapper jsonObjectMapper;
80 private final DeprecationHelper deprecationHelper;
81 private final NcmpRestInputMapper ncmpRestInputMapper;
82 private final CmHandleStateMapper cmHandleStateMapper;
83 private final NcmpCachedResourceRequestHandler ncmpCachedResourceRequestHandler;
84 private final NcmpPassthroughResourceRequestHandler ncmpPassthroughResourceRequestHandler;
85 private final DataOperationRequestMapper dataOperationRequestMapper;
86 private final Map<String, TrustLevel> trustLevelPerCmHandle;
89 * Get resource data from datastore.
91 * @param datastoreName name of the datastore
92 * @param cmHandle cm handle identifier
93 * @param resourceIdentifier resource identifier
94 * @param optionsParamInQuery options query parameter
95 * @param topicParamInQuery topic query parameter
96 * @param includeDescendants whether to include descendants or not
97 * @param authorization contents of Authorization header, or null if not present
98 * @return {@code ResponseEntity} response from dmi plugin
101 @Timed(value = "cps.ncmp.controller.get", description = "Time taken to get resource data from datastore")
102 public ResponseEntity<Object> getResourceDataForCmHandle(final String datastoreName,
103 final String cmHandle,
104 final String resourceIdentifier,
105 final String optionsParamInQuery,
106 final String topicParamInQuery,
107 final Boolean includeDescendants,
108 final String authorization) {
109 final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler = getNcmpDatastoreRequestHandler(datastoreName);
110 return ncmpDatastoreRequestHandler.executeRequest(datastoreName, cmHandle, resourceIdentifier,
111 optionsParamInQuery, topicParamInQuery, includeDescendants, authorization);
115 public ResponseEntity<Object> executeDataOperationForCmHandles(final String topicParamInQuery,
116 final DataOperationRequest dataOperationRequest,
117 final String authorization) {
118 return ncmpPassthroughResourceRequestHandler.executeRequest(topicParamInQuery,
119 dataOperationRequestMapper.toDataOperationRequest(dataOperationRequest), authorization);
123 * Query resource data from datastore.
125 * @param datastoreName name of the datastore
126 * @param cmHandle cm handle identifier
127 * @param cpsPath CPS Path
128 * @param optionsParamInQuery options query parameter
129 * @param topicParamInQuery topic query parameter
130 * @param includeDescendants whether to include descendants or not
131 * @return {@code ResponseEntity} response from dmi plugin
135 public ResponseEntity<Object> queryResourceDataForCmHandle(final String datastoreName,
136 final String cmHandle,
137 final String cpsPath,
138 final String optionsParamInQuery,
139 final String topicParamInQuery,
140 final Boolean includeDescendants) {
141 validateDataStore(OPERATIONAL, datastoreName);
142 return ncmpCachedResourceRequestHandler.executeRequest(cmHandle, cpsPath, includeDescendants);
146 * Patch resource data from passthrough-running.
148 * @param datastoreName name of the datastore
149 * @param cmHandle cm handle identifier
150 * @param resourceIdentifier resource identifier
151 * @param requestBody the request body
152 * @param contentType content type of body
153 * @param authorization contents of Authorization header, or null if not present
154 * @return {@code ResponseEntity} response from dmi plugin
158 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String datastoreName,
159 final String cmHandle,
160 final String resourceIdentifier,
161 final Object requestBody,
162 final String contentType,
163 final String authorization) {
165 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
167 final Object responseObject = networkCmProxyDataService
168 .writeResourceDataPassThroughRunningForCmHandle(
169 cmHandle, resourceIdentifier, PATCH,
170 jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
171 return ResponseEntity.ok(responseObject);
175 * Create resource data in datastore pass-through running for given cm-handle.
177 * @param datastoreName name of the datastore
178 * @param cmHandle cm handle identifier
179 * @param resourceIdentifier resource identifier
180 * @param requestBody the request body
181 * @param contentType content type of body
182 * @param authorization contents of Authorization header, or null if not present
183 * @return {@code ResponseEntity} response from dmi plugin
186 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String datastoreName,
187 final String cmHandle,
188 final String resourceIdentifier,
189 final Object requestBody,
190 final String contentType,
191 final String authorization) {
192 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
194 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
195 resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
196 return new ResponseEntity<>(HttpStatus.CREATED);
200 * Update resource data in datastore pass-through running for given cm-handle.
202 * @param datastoreName name of the datastore
203 * @param cmHandle cm handle identifier
204 * @param resourceIdentifier resource identifier
205 * @param requestBody the request body
206 * @param contentType content type of the body
207 * @param authorization contents of Authorization header, or null if not present
208 * @return response entity
212 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String datastoreName,
213 final String cmHandle,
214 final String resourceIdentifier,
215 final Object requestBody,
216 final String contentType,
217 final String authorization) {
218 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
220 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
221 resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
222 return new ResponseEntity<>(HttpStatus.OK);
226 * Delete resource data in datastore pass-through running for a given cm-handle.
228 * @param datastoreName name of the datastore
229 * @param cmHandle cm handle identifier
230 * @param resourceIdentifier resource identifier
231 * @param contentType content type of the body
232 * @param authorization contents of Authorization header, or null if not present
233 * @return response entity no content if request is successful
236 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
237 final String cmHandle,
238 final String resourceIdentifier,
239 final String contentType,
240 final String authorization) {
242 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
244 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
245 resourceIdentifier, DELETE, NO_BODY, contentType, authorization);
246 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
250 * Query and return cm handles that match the given query parameters.
252 * @param cmHandleQueryParameters the cm handle query parameters
253 * @return collection of cm handles
256 @SuppressWarnings("deprecation") // mapOldConditionProperties method will be removed in Release 12
257 public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
258 final CmHandleQueryParameters cmHandleQueryParameters) {
259 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
260 deprecationHelper.mapOldConditionProperties(cmHandleQueryParameters);
261 final Collection<NcmpServiceCmHandle> cmHandles = networkCmProxyDataService
262 .executeCmHandleSearch(cmHandleQueryApiParameters);
263 final List<RestOutputCmHandle> outputCmHandles =
264 cmHandles.stream().map(this::toRestOutputCmHandle).collect(Collectors.toList());
265 return ResponseEntity.ok(outputCmHandles);
269 * Query and return cm handle ids that match the given query parameters.
271 * @param cmHandleQueryParameters the cm handle query parameters
272 * @return collection of cm handle ids
275 public ResponseEntity<List<String>> searchCmHandleIds(
276 final CmHandleQueryParameters cmHandleQueryParameters) {
277 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
278 jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class);
279 final Collection<String> cmHandleIds
280 = networkCmProxyDataService.executeCmHandleIdSearch(cmHandleQueryApiParameters);
281 return ResponseEntity.ok(List.copyOf(cmHandleIds));
285 * Search for Cm Handle and Properties by Name.
287 * @param cmHandleId cm-handle identifier
288 * @return cm handle and its properties
291 public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
292 final NcmpServiceCmHandle ncmpServiceCmHandle = networkCmProxyDataService.getNcmpServiceCmHandle(cmHandleId);
293 final RestOutputCmHandle restOutputCmHandle = toRestOutputCmHandle(ncmpServiceCmHandle);
294 return ResponseEntity.ok(restOutputCmHandle);
298 * Get Cm Handle Properties by Cm Handle Id.
300 * @param cmHandleId cm-handle identifier
301 * @return cm handle properties
304 public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
305 final String cmHandleId) {
306 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
307 cmHandlePublicProperties.add(networkCmProxyDataService.getCmHandlePublicProperties(cmHandleId));
308 final RestOutputCmHandlePublicProperties restOutputCmHandlePublicProperties =
309 new RestOutputCmHandlePublicProperties();
310 restOutputCmHandlePublicProperties.setPublicCmHandleProperties(cmHandlePublicProperties);
311 return ResponseEntity.ok(restOutputCmHandlePublicProperties);
315 * Get Cm Handle State by Cm Handle Id.
317 * @param cmHandleId cm-handle identifier
318 * @return cm handle state
321 public ResponseEntity<RestOutputCmHandleCompositeState> getCmHandleStateByCmHandleId(
322 final String cmHandleId) {
323 final CompositeState cmHandleState = networkCmProxyDataService.getCmHandleCompositeState(cmHandleId);
324 final RestOutputCmHandleCompositeState restOutputCmHandleCompositeState =
325 new RestOutputCmHandleCompositeState();
326 restOutputCmHandleCompositeState.setState(
327 cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(cmHandleState));
328 return ResponseEntity.ok(restOutputCmHandleCompositeState);
332 * Return module definitions.
334 * @param cmHandleId cm-handle identifier
335 * @param moduleName module name
336 * @param revision the revision of the module
337 * @return list of module definitions (module name, revision, yang resource content)
340 public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitions(final String cmHandleId,
341 final String moduleName,
342 final String revision) {
343 final Collection<ModuleDefinition> moduleDefinitions;
344 if (StringUtils.hasText(moduleName)) {
346 networkCmProxyDataService.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, revision);
348 moduleDefinitions = networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId);
349 if (StringUtils.hasText(revision)) {
350 log.warn("Ignoring revision filter as no module name is provided");
353 final List<RestModuleDefinition> response = new ArrayList<>();
354 for (final ModuleDefinition moduleDefinition: moduleDefinitions) {
355 response.add(ncmpRestInputMapper.toRestModuleDefinition(moduleDefinition));
357 return new ResponseEntity<>(response, HttpStatus.OK);
361 * Return module references for a cm handle.
363 * @param cmHandle the cm handle
364 * @return module references for cm handle. Namespace will be always blank because restConf does not include this.
366 public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
367 final List<RestModuleReference> restModuleReferences =
368 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
369 .map(ncmpRestInputMapper::toRestModuleReference)
370 .collect(Collectors.toList());
371 return new ResponseEntity<>(restModuleReferences, HttpStatus.OK);
375 * Set the data sync enabled flag, along with the data sync state for the specified cm handle.
377 * @param cmHandleId cm handle id
378 * @param dataSyncEnabledFlag data sync enabled flag
379 * @return response entity ok if request is successful
382 public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
383 final Boolean dataSyncEnabledFlag) {
384 networkCmProxyDataService.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag);
385 return new ResponseEntity<>(HttpStatus.OK);
389 private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) {
390 final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle();
391 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
392 final TrustLevel cmHandleCurrentTrustLevel = trustLevelPerCmHandle.get(ncmpServiceCmHandle.getCmHandleId());
393 restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId());
394 cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties());
395 restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties);
396 restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(
397 ncmpServiceCmHandle.getCompositeState()));
398 if (cmHandleCurrentTrustLevel != null) {
399 restOutputCmHandle.setTrustLevel(cmHandleCurrentTrustLevel.toString());
401 restOutputCmHandle.setModuleSetTag(ncmpServiceCmHandle.getModuleSetTag());
402 restOutputCmHandle.setAlternateId(ncmpServiceCmHandle.getAlternateId());
403 restOutputCmHandle.setDataProducerIdentifier(ncmpServiceCmHandle.getDataProducerIdentifier());
404 return restOutputCmHandle;
407 private void validateDataStore(final DatastoreType acceptableDataStoreType, final String requestedDatastoreName) {
408 final DatastoreType datastoreType = DatastoreType.fromDatastoreName(requestedDatastoreName);
410 if (acceptableDataStoreType != datastoreType) {
411 throw new InvalidDatastoreException(requestedDatastoreName + " is not supported");
415 private NcmpDatastoreRequestHandler getNcmpDatastoreRequestHandler(final String datastoreName) {
416 if (OPERATIONAL.equals(DatastoreType.fromDatastoreName(datastoreName))) {
417 return ncmpCachedResourceRequestHandler;
419 return ncmpPassthroughResourceRequestHandler;