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.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 java.util.Collection;
34 import java.util.List;
36 import java.util.stream.Collectors;
37 import lombok.RequiredArgsConstructor;
38 import lombok.extern.slf4j.Slf4j;
39 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
40 import org.onap.cps.ncmp.api.impl.exception.InvalidDatastoreException;
41 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
42 import org.onap.cps.ncmp.api.impl.operations.DatastoreType;
43 import org.onap.cps.ncmp.api.impl.trustlevel.TrustLevel;
44 import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters;
45 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
46 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
47 import org.onap.cps.ncmp.rest.controller.handlers.NcmpCachedResourceRequestHandler;
48 import org.onap.cps.ncmp.rest.controller.handlers.NcmpDatastoreRequestHandler;
49 import org.onap.cps.ncmp.rest.controller.handlers.NcmpPassthroughResourceRequestHandler;
50 import org.onap.cps.ncmp.rest.mapper.CmHandleStateMapper;
51 import org.onap.cps.ncmp.rest.mapper.DataOperationRequestMapper;
52 import org.onap.cps.ncmp.rest.model.CmHandlePublicProperties;
53 import org.onap.cps.ncmp.rest.model.CmHandleQueryParameters;
54 import org.onap.cps.ncmp.rest.model.DataOperationRequest;
55 import org.onap.cps.ncmp.rest.model.RestModuleDefinition;
56 import org.onap.cps.ncmp.rest.model.RestModuleReference;
57 import org.onap.cps.ncmp.rest.model.RestOutputCmHandle;
58 import org.onap.cps.ncmp.rest.model.RestOutputCmHandleCompositeState;
59 import org.onap.cps.ncmp.rest.model.RestOutputCmHandlePublicProperties;
60 import org.onap.cps.ncmp.rest.util.DeprecationHelper;
61 import org.onap.cps.utils.JsonObjectMapper;
62 import org.springframework.http.HttpStatus;
63 import org.springframework.http.ResponseEntity;
64 import org.springframework.web.bind.annotation.RequestMapping;
65 import org.springframework.web.bind.annotation.RestController;
69 @RequestMapping("${rest.api.ncmp-base-path}")
70 @RequiredArgsConstructor
71 public class NetworkCmProxyController implements NetworkCmProxyApi {
73 private static final String NO_BODY = null;
74 private final NetworkCmProxyDataService networkCmProxyDataService;
75 private final JsonObjectMapper jsonObjectMapper;
76 private final DeprecationHelper deprecationHelper;
77 private final NcmpRestInputMapper ncmpRestInputMapper;
78 private final CmHandleStateMapper cmHandleStateMapper;
79 private final NcmpCachedResourceRequestHandler ncmpCachedResourceRequestHandler;
80 private final NcmpPassthroughResourceRequestHandler ncmpPassthroughResourceRequestHandler;
81 private final DataOperationRequestMapper dataOperationRequestMapper;
82 private final Map<String, TrustLevel> trustLevelPerCmHandle;
85 * Get resource data from datastore.
87 * @param datastoreName name of the datastore
88 * @param cmHandle cm handle identifier
89 * @param resourceIdentifier resource identifier
90 * @param optionsParamInQuery options query parameter
91 * @param topicParamInQuery topic query parameter
92 * @param includeDescendants whether to include descendants or not
93 * @return {@code ResponseEntity} response from dmi plugin
97 public ResponseEntity<Object> getResourceDataForCmHandle(final String datastoreName,
98 final String cmHandle,
99 final String resourceIdentifier,
100 final String optionsParamInQuery,
101 final String topicParamInQuery,
102 final Boolean includeDescendants) {
105 final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler = getNcmpDatastoreRequestHandler(datastoreName);
106 return ncmpDatastoreRequestHandler.executeRequest(datastoreName, cmHandle, resourceIdentifier,
107 optionsParamInQuery, topicParamInQuery, includeDescendants);
111 public ResponseEntity<Object> executeDataOperationForCmHandles(final String topicParamInQuery,
112 final DataOperationRequest
113 dataOperationRequest) {
114 return ncmpPassthroughResourceRequestHandler.executeRequest(topicParamInQuery,
115 dataOperationRequestMapper.toDataOperationRequest(dataOperationRequest));
119 * Query resource data from datastore.
121 * @param datastoreName name of the datastore
122 * @param cmHandle cm handle identifier
123 * @param cpsPath CPS Path
124 * @param optionsParamInQuery options query parameter
125 * @param topicParamInQuery topic query parameter
126 * @param includeDescendants whether to include descendants or not
127 * @return {@code ResponseEntity} response from dmi plugin
131 public ResponseEntity<Object> queryResourceDataForCmHandle(final String datastoreName,
132 final String cmHandle,
133 final String cpsPath,
134 final String optionsParamInQuery,
135 final String topicParamInQuery,
136 final Boolean includeDescendants) {
137 validateDataStore(OPERATIONAL, datastoreName);
138 return ncmpCachedResourceRequestHandler.executeRequest(cmHandle, cpsPath, includeDescendants);
142 * Patch resource data from passthrough-running.
144 * @param datastoreName name of the datastore
145 * @param cmHandle cm handle identifier
146 * @param resourceIdentifier resource identifier
147 * @param requestBody the request body
148 * @param contentType content type of body
149 * @return {@code ResponseEntity} response from dmi plugin
153 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String datastoreName,
154 final String cmHandle,
155 final String resourceIdentifier,
156 final Object requestBody,
157 final String contentType) {
159 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
161 final Object responseObject = networkCmProxyDataService
162 .writeResourceDataPassThroughRunningForCmHandle(
163 cmHandle, resourceIdentifier, PATCH,
164 jsonObjectMapper.asJsonString(requestBody), contentType);
165 return ResponseEntity.ok(responseObject);
169 * Create resource data in datastore pass-through running for given cm-handle.
171 * @param datastoreName name of the datastore
172 * @param cmHandle cm handle identifier
173 * @param resourceIdentifier resource identifier
174 * @param requestBody the request body
175 * @param contentType content type of body
176 * @return {@code ResponseEntity} response from dmi plugin
179 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String datastoreName,
180 final String cmHandle,
181 final String resourceIdentifier,
182 final Object requestBody,
183 final String contentType) {
185 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
187 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
188 resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType);
189 return new ResponseEntity<>(HttpStatus.CREATED);
193 * Update resource data in datastore pass-through running for given cm-handle.
195 * @param datastoreName name of the datastore
196 * @param cmHandle cm handle identifier
197 * @param resourceIdentifier resource identifier
198 * @param requestBody the request body
199 * @param contentType content type of the body
200 * @return response entity
204 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String datastoreName,
205 final String cmHandle,
206 final String resourceIdentifier,
207 final Object requestBody,
208 final String contentType) {
209 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
211 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
212 resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType);
213 return new ResponseEntity<>(HttpStatus.OK);
217 * Delete resource data in datastore pass-through running for a given cm-handle.
219 * @param datastoreName name of the datastore
220 * @param cmHandle cm handle identifier
221 * @param resourceIdentifier resource identifier
222 * @param contentType content type of the body
223 * @return response entity no content if request is successful
226 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
227 final String cmHandle,
228 final String resourceIdentifier,
229 final String contentType) {
231 validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
233 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
234 resourceIdentifier, DELETE, NO_BODY, contentType);
235 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
239 * Query and return cm handles that match the given query parameters.
241 * @param cmHandleQueryParameters the cm handle query parameters
242 * @return collection of cm handles
245 @SuppressWarnings("deprecation") // mapOldConditionProperties method will be removed in Release 12
246 public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
247 final CmHandleQueryParameters cmHandleQueryParameters) {
248 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
249 deprecationHelper.mapOldConditionProperties(cmHandleQueryParameters);
250 final Collection<NcmpServiceCmHandle> cmHandles = networkCmProxyDataService
251 .executeCmHandleSearch(cmHandleQueryApiParameters);
252 final List<RestOutputCmHandle> outputCmHandles =
253 cmHandles.stream().map(this::toRestOutputCmHandle).collect(Collectors.toList());
254 return ResponseEntity.ok(outputCmHandles);
258 * Query and return cm handle ids that match the given query parameters.
260 * @param cmHandleQueryParameters the cm handle query parameters
261 * @return collection of cm handle ids
264 public ResponseEntity<List<String>> searchCmHandleIds(
265 final CmHandleQueryParameters cmHandleQueryParameters) {
266 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
267 jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class);
268 final Collection<String> cmHandleIds
269 = networkCmProxyDataService.executeCmHandleIdSearch(cmHandleQueryApiParameters);
270 return ResponseEntity.ok(List.copyOf(cmHandleIds));
274 * Search for Cm Handle and Properties by Name.
276 * @param cmHandleId cm-handle identifier
277 * @return cm handle and its properties
280 public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
281 final NcmpServiceCmHandle ncmpServiceCmHandle = networkCmProxyDataService.getNcmpServiceCmHandle(cmHandleId);
282 final RestOutputCmHandle restOutputCmHandle = toRestOutputCmHandle(ncmpServiceCmHandle);
283 return ResponseEntity.ok(restOutputCmHandle);
287 * Get Cm Handle Properties by Cm Handle Id.
289 * @param cmHandleId cm-handle identifier
290 * @return cm handle properties
293 public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
294 final String cmHandleId) {
295 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
296 cmHandlePublicProperties.add(networkCmProxyDataService.getCmHandlePublicProperties(cmHandleId));
297 final RestOutputCmHandlePublicProperties restOutputCmHandlePublicProperties =
298 new RestOutputCmHandlePublicProperties();
299 restOutputCmHandlePublicProperties.setPublicCmHandleProperties(cmHandlePublicProperties);
300 return ResponseEntity.ok(restOutputCmHandlePublicProperties);
304 * Get Cm Handle State by Cm Handle Id.
306 * @param cmHandleId cm-handle identifier
307 * @return cm handle state
310 public ResponseEntity<RestOutputCmHandleCompositeState> getCmHandleStateByCmHandleId(
311 final String cmHandleId) {
312 final CompositeState cmHandleState = networkCmProxyDataService.getCmHandleCompositeState(cmHandleId);
313 final RestOutputCmHandleCompositeState restOutputCmHandleCompositeState =
314 new RestOutputCmHandleCompositeState();
315 restOutputCmHandleCompositeState.setState(
316 cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(cmHandleState));
317 return ResponseEntity.ok(restOutputCmHandleCompositeState);
321 * Return module definitions for a cm handle.
323 * @param cmHandleId cm-handle identifier
324 * @return list of module definitions (module name, revision, yang resource content)
327 public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
328 final List<RestModuleDefinition> restModuleDefinitions =
329 networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId).stream()
330 .map(ncmpRestInputMapper::toRestModuleDefinition)
331 .collect(Collectors.toList());
332 return new ResponseEntity<>(restModuleDefinitions, HttpStatus.OK);
336 * Return module references for a cm handle.
338 * @param cmHandle the cm handle
339 * @return module references for cm handle. Namespace will be always blank because restConf does not include this.
341 public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
342 final List<RestModuleReference> restModuleReferences =
343 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
344 .map(ncmpRestInputMapper::toRestModuleReference)
345 .collect(Collectors.toList());
346 return new ResponseEntity<>(restModuleReferences, HttpStatus.OK);
350 * Set the data sync enabled flag, along with the data sync state for the specified cm handle.
352 * @param cmHandleId cm handle id
353 * @param dataSyncEnabledFlag data sync enabled flag
354 * @return response entity ok if request is successful
357 public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
358 final Boolean dataSyncEnabledFlag) {
359 networkCmProxyDataService.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag);
360 return new ResponseEntity<>(HttpStatus.OK);
364 private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) {
365 final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle();
366 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
367 final TrustLevel cmHandleCurrentTrustLevel = trustLevelPerCmHandle.get(ncmpServiceCmHandle.getCmHandleId());
368 restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId());
369 cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties());
370 restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties);
371 restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(
372 ncmpServiceCmHandle.getCompositeState()));
373 if (cmHandleCurrentTrustLevel != null) {
374 restOutputCmHandle.setTrustLevel(cmHandleCurrentTrustLevel.toString());
376 return restOutputCmHandle;
379 private void validateDataStore(final DatastoreType acceptableDataStoreType, final String requestedDatastoreName) {
380 final DatastoreType datastoreType = DatastoreType.fromDatastoreName(requestedDatastoreName);
382 if (acceptableDataStoreType != datastoreType) {
383 throw new InvalidDatastoreException(requestedDatastoreName + " is not supported");
387 private NcmpDatastoreRequestHandler getNcmpDatastoreRequestHandler(final String datastoreName) {
388 if (OPERATIONAL.equals(DatastoreType.fromDatastoreName(datastoreName))) {
389 return ncmpCachedResourceRequestHandler;
391 return ncmpPassthroughResourceRequestHandler;