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;
37 import java.util.stream.Collectors;
38 import lombok.RequiredArgsConstructor;
39 import lombok.extern.slf4j.Slf4j;
40 import org.onap.cps.ncmp.api.impl.NetworkCmProxyFacade;
41 import org.onap.cps.ncmp.api.impl.exception.InvalidDatastoreException;
42 import org.onap.cps.ncmp.api.impl.operations.DatastoreType;
43 import org.onap.cps.ncmp.api.inventory.NetworkCmProxyInventoryFacade;
44 import org.onap.cps.ncmp.api.inventory.models.CmHandleQueryApiParameters;
45 import org.onap.cps.ncmp.api.inventory.models.CompositeState;
46 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle;
47 import org.onap.cps.ncmp.api.models.CmResourceAddress;
48 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
49 import org.onap.cps.ncmp.rest.model.CmHandlePublicProperties;
50 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
51 import org.onap.cps.ncmp.rest.model.DataOperationRequest;
52 import org.onap.cps.ncmp.rest.model.RestModuleDefinition;
53 import org.onap.cps.ncmp.rest.model.RestModuleReference;
54 import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
55 import org.onap.cps.ncmp.rest.model.RestOutputCmHandleCompositeState;
56 import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
57 import org.onap.cps.ncmp.rest.util.CmHandleStateMapper;
58 import org.onap.cps.ncmp.rest.util.DataOperationRequestMapper;
59 import org.onap.cps.ncmp.rest.util.DeprecationHelper;
60 import org.onap.cps.ncmp.rest.util.NcmpRestInputMapper;
61 import org.onap.cps.spi.model.DataNode;
62 import org.onap.cps.spi.model.ModuleDefinition;
63 import org.onap.cps.utils.JsonObjectMapper;
64 import org.springframework.http.HttpStatus;
65 import org.springframework.http.ResponseEntity;
66 import org.springframework.util.StringUtils;
67 import org.springframework.web.bind.annotation.RequestMapping;
68 import org.springframework.web.bind.annotation.RestController;
72 @RequestMapping("${rest.api.ncmp-base-path}")
73 @RequiredArgsConstructor
74 public class NetworkCmProxyController implements NetworkCmProxyApi {
76 private static final String NO_BODY = null;
77 private final NetworkCmProxyFacade networkCmProxyFacade;
78 private final NetworkCmProxyInventoryFacade networkCmProxyInventoryFacade;
79 private final JsonObjectMapper jsonObjectMapper;
80 private final DeprecationHelper deprecationHelper;
81 private final NcmpRestInputMapper ncmpRestInputMapper;
82 private final CmHandleStateMapper cmHandleStateMapper;
83 private final DataOperationRequestMapper dataOperationRequestMapper;
86 * Get resource data from datastore.
88 * @param datastoreName name of the datastore
89 * @param cmHandle cm handle identifier
90 * @param resourceIdentifier resource identifier
91 * @param optionsParamInQuery options query parameter
92 * @param topicParamInQuery topic query parameter
93 * @param includeDescendants whether to include descendants or not
94 * @param authorization contents of Authorization header, or null if not present
95 * @return {@code ResponseEntity} response from dmi plugin
98 @Timed(value = "cps.ncmp.controller.get", description = "Time taken to get resource data from datastore")
99 public ResponseEntity<Object> getResourceDataForCmHandle(final String datastoreName,
100 final String cmHandle,
101 final String resourceIdentifier,
102 final String optionsParamInQuery,
103 final String topicParamInQuery,
104 final Boolean includeDescendants,
105 final String authorization) {
106 final CmResourceAddress cmResourceAddress = new CmResourceAddress(datastoreName, cmHandle, resourceIdentifier);
107 final Object result = networkCmProxyFacade.getResourceDataForCmHandle(cmResourceAddress, optionsParamInQuery,
108 topicParamInQuery, includeDescendants, authorization);
109 return ResponseEntity.ok(result);
113 public ResponseEntity<Object> executeDataOperationForCmHandles(final String topicParamInQuery,
114 final DataOperationRequest dataOperationRequest,
115 final String authorization) {
116 final Object result = networkCmProxyFacade.executeDataOperationForCmHandles(topicParamInQuery,
117 dataOperationRequestMapper.toDataOperationRequest(dataOperationRequest), authorization);
118 return ResponseEntity.ok(result);
122 * Query resource data from datastore.
124 * @param datastoreName name of the datastore
125 * @param cmHandle cm handle identifier
126 * @param cpsPath CPS Path
127 * @param optionsParamInQuery options query parameter
128 * @param topicParamInQuery topic query parameter
129 * @param includeDescendants whether to include descendants or not
130 * @return {@code ResponseEntity} response. Body contains a collection of DataNodes
134 public ResponseEntity<Object> queryResourceDataForCmHandle(final String datastoreName,
135 final String cmHandle,
136 final String cpsPath,
137 final String optionsParamInQuery,
138 final String topicParamInQuery,
139 final Boolean includeDescendants) {
140 validateDataStore(OPERATIONAL, datastoreName);
141 final Collection<DataNode> dataNodes = networkCmProxyFacade.queryResourceDataForCmHandle(cmHandle, cpsPath,
143 return ResponseEntity.ok(dataNodes);
147 * Patch resource data from passthrough-running.
149 * @param datastoreName name of the datastore
150 * @param cmHandle cm handle identifier
151 * @param resourceIdentifier resource identifier
152 * @param requestBody the request body
153 * @param contentType content type of body
154 * @param authorization contents of Authorization header, or null if not present
155 * @return {@code ResponseEntity} response from dmi plugin
159 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String datastoreName,
160 final String cmHandle,
161 final String resourceIdentifier,
162 final Object requestBody,
163 final String contentType,
164 final String authorization) {
166 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
168 final Object responseObject = networkCmProxyFacade
169 .writeResourceDataPassThroughRunningForCmHandle(
170 cmHandle, resourceIdentifier, PATCH,
171 jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
172 return ResponseEntity.ok(responseObject);
176 * Create resource data in datastore pass-through running for given cm-handle.
178 * @param datastoreName name of the datastore
179 * @param cmHandle cm handle identifier
180 * @param resourceIdentifier resource identifier
181 * @param requestBody the request body
182 * @param contentType content type of body
183 * @param authorization contents of Authorization header, or null if not present
184 * @return {@code ResponseEntity} response from dmi plugin
187 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String datastoreName,
188 final String cmHandle,
189 final String resourceIdentifier,
190 final Object requestBody,
191 final String contentType,
192 final String authorization) {
193 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
195 networkCmProxyFacade.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
196 resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
197 return new ResponseEntity<>(HttpStatus.CREATED);
201 * Update resource data in datastore pass-through running for given cm-handle.
203 * @param datastoreName name of the datastore
204 * @param cmHandle cm handle identifier
205 * @param resourceIdentifier resource identifier
206 * @param requestBody the request body
207 * @param contentType content type of the body
208 * @param authorization contents of Authorization header, or null if not present
209 * @return response entity
213 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String datastoreName,
214 final String cmHandle,
215 final String resourceIdentifier,
216 final Object requestBody,
217 final String contentType,
218 final String authorization) {
219 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
221 networkCmProxyFacade.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
222 resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType, authorization);
223 return new ResponseEntity<>(HttpStatus.OK);
227 * Delete resource data in datastore pass-through running for a given cm-handle.
229 * @param datastoreName name of the datastore
230 * @param cmHandle cm handle identifier
231 * @param resourceIdentifier resource identifier
232 * @param contentType content type of the body
233 * @param authorization contents of Authorization header, or null if not present
234 * @return response entity no content if request is successful
237 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
238 final String cmHandle,
239 final String resourceIdentifier,
240 final String contentType,
241 final String authorization) {
243 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
245 networkCmProxyFacade.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
246 resourceIdentifier, DELETE, NO_BODY, contentType, authorization);
247 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
251 * Query and return cm handles that match the given query parameters.
253 * @param cmHandleQueryParameters the cm handle query parameters
254 * @return collection of cm handles
257 @SuppressWarnings("deprecation") // mapOldConditionProperties method will be removed in Release 12
258 public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
259 final CmHandleQueryParameters cmHandleQueryParameters) {
260 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
261 deprecationHelper.mapOldConditionProperties(cmHandleQueryParameters);
262 final Collection<NcmpServiceCmHandle> cmHandles = networkCmProxyInventoryFacade
263 .executeCmHandleSearch(cmHandleQueryApiParameters);
264 final List<RestOutputCmHandle> restOutputCmHandles =
265 cmHandles.stream().map(this::toRestOutputCmHandle).collect(Collectors.toList());
266 return ResponseEntity.ok(restOutputCmHandles);
270 * Query and return cm handle ids that match the given query parameters.
272 * @param cmHandleQueryParameters the cm handle query parameters
273 * @return collection of cm handle ids
276 public ResponseEntity<List<String>> searchCmHandleIds(
277 final CmHandleQueryParameters cmHandleQueryParameters) {
278 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
279 jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class);
280 final Collection<String> cmHandleIds
281 = networkCmProxyInventoryFacade.executeCmHandleIdSearch(cmHandleQueryApiParameters);
282 return ResponseEntity.ok(List.copyOf(cmHandleIds));
286 * Search for Cm Handle and Properties by Name.
288 * @param cmHandleId cm-handle identifier
289 * @return cm handle and its properties
292 public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
293 final NcmpServiceCmHandle ncmpServiceCmHandle
294 = networkCmProxyInventoryFacade.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(networkCmProxyInventoryFacade.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 = networkCmProxyInventoryFacade.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 networkCmProxyInventoryFacade.getModuleDefinitionsByCmHandleAndModule(cmHandleId, moduleName, revision);
350 moduleDefinitions = networkCmProxyInventoryFacade.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 networkCmProxyInventoryFacade.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 networkCmProxyInventoryFacade.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag);
387 return new ResponseEntity<>(HttpStatus.OK);
390 private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) {
391 final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle();
392 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
393 restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId());
394 cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties());
395 restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties);
396 restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(
397 ncmpServiceCmHandle.getCompositeState()));
398 if (ncmpServiceCmHandle.getCurrentTrustLevel() != null) {
399 restOutputCmHandle.setTrustLevel(ncmpServiceCmHandle.getCurrentTrustLevel().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");