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.NcmpDatastoreResourceRequestHandler;
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 NcmpDatastoreResourceRequestHandler ncmpDatastoreResourceRequestHandler =
95 ncmpDatastoreResourceRequestHandlerFactory.getNcmpDatastoreResourceRequestHandler(
96 DatastoreType.fromDatastoreName(datastoreName));
98 return ncmpDatastoreResourceRequestHandler.getResourceData(cmHandle, resourceIdentifier,
99 optionsParamInQuery, topicParamInQuery, includeDescendants);
103 * Patch resource data from passthrough-running.
105 * @param resourceIdentifier resource identifier
106 * @param datastoreName name of the datastore
107 * @param cmHandle cm handle identifier
108 * @param requestBody the request body
109 * @param contentType content type of body
110 * @return {@code ResponseEntity} response from dmi plugin
114 public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
115 final String datastoreName,
116 final String cmHandle,
117 final Object requestBody,
118 final String contentType) {
120 acceptPassthroughRunningOnly(datastoreName);
122 final Object responseObject = networkCmProxyDataService
123 .writeResourceDataPassThroughRunningForCmHandle(
124 cmHandle, resourceIdentifier, PATCH,
125 jsonObjectMapper.asJsonString(requestBody), contentType);
126 return ResponseEntity.ok(responseObject);
130 * Create resource data in datastore pass-through running for given cm-handle.
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
140 public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
141 final String datastoreName,
142 final String cmHandle,
143 final Object requestBody,
144 final String contentType) {
146 acceptPassthroughRunningOnly(datastoreName);
148 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
149 resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType);
150 return new ResponseEntity<>(HttpStatus.CREATED);
154 * Update resource data in datastore pass-through running for given cm-handle.
156 * @param resourceIdentifier resource identifier
157 * @param datastoreName name of the datastore
158 * @param cmHandle cm handle identifier
159 * @param requestBody the request body
160 * @param contentType content type of the body
161 * @return response entity
165 public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
166 final String datastoreName,
167 final String cmHandle,
168 final Object requestBody,
169 final String contentType) {
170 acceptPassthroughRunningOnly(datastoreName);
172 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
173 resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType);
174 return new ResponseEntity<>(HttpStatus.OK);
178 * Delete resource data in datastore pass-through running for a given cm-handle.
180 * @param datastoreName name of the datastore
181 * @param cmHandle cm handle identifier
182 * @param resourceIdentifier resource identifier
183 * @param contentType content type of the body
184 * @return response entity no content if request is successful
187 public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
188 final String cmHandle,
189 final String resourceIdentifier,
190 final String contentType) {
192 acceptPassthroughRunningOnly(datastoreName);
194 networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
195 resourceIdentifier, DELETE, NO_BODY, contentType);
196 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
200 * Query and return cm handles that match the given query parameters.
202 * @param cmHandleQueryParameters the cm handle query parameters
203 * @return collection of cm handles
206 @SuppressWarnings("deprecation") // mapOldConditionProperties method will be removed in Release 12
207 public ResponseEntity<List<RestOutputCmHandle>> searchCmHandles(
208 final CmHandleQueryParameters cmHandleQueryParameters) {
209 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
210 deprecationHelper.mapOldConditionProperties(cmHandleQueryParameters);
211 final Set<NcmpServiceCmHandle> cmHandles = networkCmProxyDataService
212 .executeCmHandleSearch(cmHandleQueryApiParameters);
213 final List<RestOutputCmHandle> outputCmHandles =
214 cmHandles.stream().map(this::toRestOutputCmHandle).collect(Collectors.toList());
215 return ResponseEntity.ok(outputCmHandles);
219 * Query and return cm handle ids that match the given query parameters.
221 * @param cmHandleQueryParameters the cm handle query parameters
222 * @return collection of cm handle ids
225 public ResponseEntity<List<String>> searchCmHandleIds(
226 final CmHandleQueryParameters cmHandleQueryParameters) {
227 final CmHandleQueryApiParameters cmHandleQueryApiParameters =
228 jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class);
229 final Set<String> cmHandleIds = networkCmProxyDataService.executeCmHandleIdSearch(cmHandleQueryApiParameters);
230 return ResponseEntity.ok(List.copyOf(cmHandleIds));
234 * Search for Cm Handle and Properties by Name.
236 * @param cmHandleId cm-handle identifier
237 * @return cm handle and its properties
240 public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
241 final NcmpServiceCmHandle ncmpServiceCmHandle = networkCmProxyDataService.getNcmpServiceCmHandle(cmHandleId);
242 final RestOutputCmHandle restOutputCmHandle = toRestOutputCmHandle(ncmpServiceCmHandle);
243 return ResponseEntity.ok(restOutputCmHandle);
247 * Get Cm Handle Properties by Cm Handle Id.
249 * @param cmHandleId cm-handle identifier
250 * @return cm handle properties
253 public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
254 final String cmHandleId) {
255 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
256 cmHandlePublicProperties.add(networkCmProxyDataService.getCmHandlePublicProperties(cmHandleId));
257 final RestOutputCmHandlePublicProperties restOutputCmHandlePublicProperties =
258 new RestOutputCmHandlePublicProperties();
259 restOutputCmHandlePublicProperties.setPublicCmHandleProperties(cmHandlePublicProperties);
260 return ResponseEntity.ok(restOutputCmHandlePublicProperties);
264 * Get Cm Handle State by Cm Handle Id.
266 * @param cmHandleId cm-handle identifier
267 * @return cm handle state
270 public ResponseEntity<RestOutputCmHandleCompositeState> getCmHandleStateByCmHandleId(
271 final String cmHandleId) {
272 final CompositeState cmHandleState = networkCmProxyDataService.getCmHandleCompositeState(cmHandleId);
273 final RestOutputCmHandleCompositeState restOutputCmHandleCompositeState =
274 new RestOutputCmHandleCompositeState();
275 restOutputCmHandleCompositeState.setState(
276 cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(cmHandleState));
277 return ResponseEntity.ok(restOutputCmHandleCompositeState);
281 * Return module definitions for a cm handle.
283 * @param cmHandleId cm-handle identifier
284 * @return list of module definitions (module name, revision, yang resource content)
287 public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
288 final List<RestModuleDefinition> restModuleDefinitions =
289 networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId).stream()
290 .map(ncmpRestInputMapper::toRestModuleDefinition)
291 .collect(Collectors.toList());
292 return new ResponseEntity<>(restModuleDefinitions, HttpStatus.OK);
296 * Return module references for a cm handle.
298 * @param cmHandle the cm handle
299 * @return module references for cm handle. Namespace will be always blank because restConf does not include this.
301 public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
302 final List<RestModuleReference> restModuleReferences =
303 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
304 .map(ncmpRestInputMapper::toRestModuleReference)
305 .collect(Collectors.toList());
306 return new ResponseEntity<>(restModuleReferences, HttpStatus.OK);
310 * Set the data sync enabled flag, along with the data sync state for the specified cm handle.
312 * @param cmHandleId cm handle id
313 * @param dataSyncEnabledFlag data sync enabled flag
314 * @return response entity ok if request is successful
317 public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
318 final Boolean dataSyncEnabledFlag) {
319 networkCmProxyDataService.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag);
320 return new ResponseEntity<>(HttpStatus.OK);
324 private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) {
325 final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle();
326 final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
327 restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId());
328 cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties());
329 restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties);
330 restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(
331 ncmpServiceCmHandle.getCompositeState()));
332 return restOutputCmHandle;
335 private void acceptPassthroughRunningOnly(final String datastoreName) {
336 final DatastoreType datastoreType = DatastoreType.fromDatastoreName(datastoreName);
338 if (DatastoreType.PASSTHROUGH_RUNNING != datastoreType) {
339 throw new InvalidDatastoreException(datastoreName + " is not supported");