2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Pantheon.tech
4 * Modifications Copyright (C) 2021-2022 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.DmiRequestBody.OperationEnum.CREATE;
27 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.DELETE;
28 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH;
29 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE;
31 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 includeDescendants 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 includeDescendants) {
94 final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler =
95 ncmpDatastoreResourceRequestHandlerFactory.getNcmpDatastoreResourceRequestHandler(
96 DatastoreType.fromDatastoreName(datastoreName));
98 return ncmpDatastoreRequestHandler.executeRequest(cmHandle, resourceIdentifier,
99 optionsParamInQuery, topicParamInQuery, includeDescendants);
103 * Query resource data from datastore.
105 * @param datastoreName name of the datastore
106 * @param cmHandle cm handle identifier
107 * @param cpsPath CPS Path
108 * @param optionsParamInQuery options query parameter
109 * @param topicParamInQuery topic query parameter
110 * @param includeDescendants whether include descendants
111 * @return {@code ResponseEntity} response from dmi plugin
115 public ResponseEntity<Object> queryResourceDataForCmHandle(final String datastoreName,
116 final String cmHandle,
117 final String cpsPath,
118 final String optionsParamInQuery,
119 final String topicParamInQuery,
120 final Boolean includeDescendants) {
121 validateDataStore(DatastoreType.OPERATIONAL, datastoreName);
122 final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler =
123 ncmpDatastoreResourceRequestHandlerFactory.getNcmpDatastoreResourceQueryHandler();
125 return ncmpDatastoreRequestHandler.executeRequest(cmHandle, cpsPath, optionsParamInQuery,
126 topicParamInQuery, includeDescendants);
130 * Patch resource data from passthrough-running.
132 * @param resourceIdentifier resource identifier
133 * @param datastoreName name of the datastore
134 * @param cmHandle cm handle identifier
135 * @param requestBody the request body
136 * @param contentType content type of body
137 * @return {@code ResponseEntity} response from dmi plugin
141 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
142 final String datastoreName,
143 final String cmHandle,
144 final Object requestBody,
145 final String contentType) {
147 validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
149 final Object responseObject = networkCmProxyDataService
150 .writeResourceDataPassThroughRunningForCmHandle(
151 cmHandle, resourceIdentifier, PATCH,
152 jsonObjectMapper.asJsonString(requestBody), contentType);
153 return ResponseEntity.ok(responseObject);
157 * Create resource data in datastore pass-through running for given cm-handle.
159 * @param resourceIdentifier resource identifier
160 * @param datastoreName name of the datastore
161 * @param cmHandle cm handle identifier
162 * @param requestBody the request body
163 * @param contentType content type of body
164 * @return {@code ResponseEntity} response from dmi plugin
167 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
168 final String datastoreName,
169 final String cmHandle,
170 final Object requestBody,
171 final String contentType) {
173 validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
175 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
176 resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType);
177 return new ResponseEntity<>(HttpStatus.CREATED);
181 * Update resource data in datastore pass-through running for given cm-handle.
183 * @param resourceIdentifier resource identifier
184 * @param datastoreName name of the datastore
185 * @param cmHandle cm handle identifier
186 * @param requestBody the request body
187 * @param contentType content type of the body
188 * @return response entity
192 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
193 final String datastoreName,
194 final String cmHandle,
195 final Object requestBody,
196 final String contentType) {
197 validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
199 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
200 resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType);
201 return new ResponseEntity<>(HttpStatus.OK);
205 * Delete resource data in datastore pass-through running for a given cm-handle.
207 * @param datastoreName name of the datastore
208 * @param cmHandle cm handle identifier
209 * @param resourceIdentifier resource identifier
210 * @param contentType content type of the body
211 * @return response entity no content if request is successful
214 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
215 final String cmHandle,
216 final String resourceIdentifier,
217 final String contentType) {
219 validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
221 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
222 resourceIdentifier, DELETE, NO_BODY, contentType);
223 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
227 * Query and return cm handles that match the given query parameters.
229 * @param cmHandleQueryParameters the cm handle query parameters
230 * @return collection of cm handles
233 @SuppressWarnings("deprecation") // mapOldConditionProperties method will be removed in Release 12
234 public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
235 final CmHandleQueryParameters cmHandleQueryParameters) {
236 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
237 deprecationHelper.mapOldConditionProperties(cmHandleQueryParameters);
238 final Set<NcmpServiceCmHandle> cmHandles = networkCmProxyDataService
239 .executeCmHandleSearch(cmHandleQueryApiParameters);
240 final List<RestOutputCmHandle> outputCmHandles =
241 cmHandles.stream().map(this::toRestOutputCmHandle).collect(Collectors.toList());
242 return ResponseEntity.ok(outputCmHandles);
246 * Query and return cm handle ids that match the given query parameters.
248 * @param cmHandleQueryParameters the cm handle query parameters
249 * @return collection of cm handle ids
252 public ResponseEntity<List<String>> searchCmHandleIds(
253 final CmHandleQueryParameters cmHandleQueryParameters) {
254 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
255 jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class);
256 final Set<String> cmHandleIds = networkCmProxyDataService.executeCmHandleIdSearch(cmHandleQueryApiParameters);
257 return ResponseEntity.ok(List.copyOf(cmHandleIds));
261 * Search for Cm Handle and Properties by Name.
263 * @param cmHandleId cm-handle identifier
264 * @return cm handle and its properties
267 public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
268 final NcmpServiceCmHandle ncmpServiceCmHandle = networkCmProxyDataService.getNcmpServiceCmHandle(cmHandleId);
269 final RestOutputCmHandle restOutputCmHandle = toRestOutputCmHandle(ncmpServiceCmHandle);
270 return ResponseEntity.ok(restOutputCmHandle);
274 * Get Cm Handle Properties by Cm Handle Id.
276 * @param cmHandleId cm-handle identifier
277 * @return cm handle properties
280 public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
281 final String cmHandleId) {
282 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
283 cmHandlePublicProperties.add(networkCmProxyDataService.getCmHandlePublicProperties(cmHandleId));
284 final RestOutputCmHandlePublicProperties restOutputCmHandlePublicProperties =
285 new RestOutputCmHandlePublicProperties();
286 restOutputCmHandlePublicProperties.setPublicCmHandleProperties(cmHandlePublicProperties);
287 return ResponseEntity.ok(restOutputCmHandlePublicProperties);
291 * Get Cm Handle State by Cm Handle Id.
293 * @param cmHandleId cm-handle identifier
294 * @return cm handle state
297 public ResponseEntity<RestOutputCmHandleCompositeState> getCmHandleStateByCmHandleId(
298 final String cmHandleId) {
299 final CompositeState cmHandleState = networkCmProxyDataService.getCmHandleCompositeState(cmHandleId);
300 final RestOutputCmHandleCompositeState restOutputCmHandleCompositeState =
301 new RestOutputCmHandleCompositeState();
302 restOutputCmHandleCompositeState.setState(
303 cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(cmHandleState));
304 return ResponseEntity.ok(restOutputCmHandleCompositeState);
308 * Return module definitions for a cm handle.
310 * @param cmHandleId cm-handle identifier
311 * @return list of module definitions (module name, revision, yang resource content)
314 public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
315 final List<RestModuleDefinition> restModuleDefinitions =
316 networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId).stream()
317 .map(ncmpRestInputMapper::toRestModuleDefinition)
318 .collect(Collectors.toList());
319 return new ResponseEntity<>(restModuleDefinitions, HttpStatus.OK);
323 * Return module references for a cm handle.
325 * @param cmHandle the cm handle
326 * @return module references for cm handle. Namespace will be always blank because restConf does not include this.
328 public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
329 final List<RestModuleReference> restModuleReferences =
330 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
331 .map(ncmpRestInputMapper::toRestModuleReference)
332 .collect(Collectors.toList());
333 return new ResponseEntity<>(restModuleReferences, HttpStatus.OK);
337 * Set the data sync enabled flag, along with the data sync state for the specified cm handle.
339 * @param cmHandleId cm handle id
340 * @param dataSyncEnabledFlag data sync enabled flag
341 * @return response entity ok if request is successful
344 public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
345 final Boolean dataSyncEnabledFlag) {
346 networkCmProxyDataService.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag);
347 return new ResponseEntity<>(HttpStatus.OK);
351 private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) {
352 final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle();
353 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
354 restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId());
355 cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties());
356 restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties);
357 restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(
358 ncmpServiceCmHandle.getCompositeState()));
359 return restOutputCmHandle;
362 private void validateDataStore(final DatastoreType acceptableDataStoreType, final String requestedDatastoreName) {
363 final DatastoreType datastoreType = DatastoreType.fromDatastoreName(requestedDatastoreName);
365 if (acceptableDataStoreType != datastoreType) {
366 throw new InvalidDatastoreException(requestedDatastoreName + " is not supported");