Merge "Update CPSarchitecture release notes"
[cps.git] / cps-ncmp-rest / src / main / java / org / onap / cps / ncmp / rest / controller / NetworkCmProxyController.java
1 /*
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
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.ncmp.rest.controller;
25
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;
30
31 import java.util.Collection;
32 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;
59
60 @Slf4j
61 @RestController
62 @RequestMapping("${rest.api.ncmp-base-path}")
63 @RequiredArgsConstructor
64 public class NetworkCmProxyController implements NetworkCmProxyApi {
65
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;
73
74     /**
75      * Get resource data from datastore.
76      *
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
84      */
85
86     @Override
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) {
93
94         final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler =
95                 ncmpDatastoreResourceRequestHandlerFactory.getNcmpDatastoreResourceRequestHandler(
96                         DatastoreType.fromDatastoreName(datastoreName));
97
98         return ncmpDatastoreRequestHandler.executeRequest(cmHandle, resourceIdentifier,
99                 optionsParamInQuery, topicParamInQuery, includeDescendants);
100     }
101
102     /**
103      * Query resource data from datastore.
104      *
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
112      */
113
114     @Override
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();
124
125         return ncmpDatastoreRequestHandler.executeRequest(cmHandle, cpsPath, optionsParamInQuery,
126             topicParamInQuery, includeDescendants);
127     }
128
129     /**
130      * Patch resource data from passthrough-running.
131      *
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
138      */
139
140     @Override
141     public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
142                                                                       final String datastoreName,
143                                                                       final String cmHandle,
144                                                                       final Object requestBody,
145                                                                       final String contentType) {
146
147         validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
148
149         final Object responseObject = networkCmProxyDataService
150                 .writeResourceDataPassThroughRunningForCmHandle(
151                         cmHandle, resourceIdentifier, PATCH,
152                         jsonObjectMapper.asJsonString(requestBody), contentType);
153         return ResponseEntity.ok(responseObject);
154     }
155
156     /**
157      * Create resource data in datastore pass-through running for given cm-handle.
158      *
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
165      */
166     @Override
167     public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
168                                                                      final String datastoreName,
169                                                                      final String cmHandle,
170                                                                      final Object requestBody,
171                                                                      final String contentType) {
172
173         validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
174
175         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
176                 resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType);
177         return new ResponseEntity<>(HttpStatus.CREATED);
178     }
179
180     /**
181      * Update resource data in datastore pass-through running for given cm-handle.
182      *
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
189      */
190
191     @Override
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);
198
199         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
200                 resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType);
201         return new ResponseEntity<>(HttpStatus.OK);
202     }
203
204     /**
205      * Delete resource data in datastore pass-through running for a given cm-handle.
206      *
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
212      */
213     @Override
214     public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
215                                                                      final String cmHandle,
216                                                                      final String resourceIdentifier,
217                                                                      final String contentType) {
218
219         validateDataStore(DatastoreType.PASSTHROUGH_RUNNING, datastoreName);
220
221         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
222                 resourceIdentifier, DELETE, NO_BODY, contentType);
223         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
224     }
225
226     /**
227      * Query and return cm handles that match the given query parameters.
228      *
229      * @param cmHandleQueryParameters the cm handle query parameters
230      * @return collection of cm handles
231      */
232     @Override
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 Collection<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);
243     }
244
245     /**
246      * Query and return cm handle ids that match the given query parameters.
247      *
248      * @param cmHandleQueryParameters the cm handle query parameters
249      * @return collection of cm handle ids
250      */
251     @Override
252     public ResponseEntity<List<String>> searchCmHandleIds(
253             final CmHandleQueryParameters cmHandleQueryParameters) {
254         final CmHandleQueryApiParameters cmHandleQueryApiParameters =
255                 jsonObjectMapper.convertToValueType(cmHandleQueryParameters, CmHandleQueryApiParameters.class);
256         final Collection<String> cmHandleIds
257             = networkCmProxyDataService.executeCmHandleIdSearch(cmHandleQueryApiParameters);
258         return ResponseEntity.ok(List.copyOf(cmHandleIds));
259     }
260
261     /**
262      * Search for Cm Handle and Properties by Name.
263      *
264      * @param cmHandleId cm-handle identifier
265      * @return cm handle and its properties
266      */
267     @Override
268     public ResponseEntity<RestOutputCmHandle> retrieveCmHandleDetailsById(final String cmHandleId) {
269         final NcmpServiceCmHandle ncmpServiceCmHandle = networkCmProxyDataService.getNcmpServiceCmHandle(cmHandleId);
270         final RestOutputCmHandle restOutputCmHandle = toRestOutputCmHandle(ncmpServiceCmHandle);
271         return ResponseEntity.ok(restOutputCmHandle);
272     }
273
274     /**
275      * Get Cm Handle Properties by Cm Handle Id.
276      *
277      * @param cmHandleId cm-handle identifier
278      * @return cm handle properties
279      */
280     @Override
281     public ResponseEntity<RestOutputCmHandlePublicProperties> getCmHandlePublicPropertiesByCmHandleId(
282             final String cmHandleId) {
283         final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
284         cmHandlePublicProperties.add(networkCmProxyDataService.getCmHandlePublicProperties(cmHandleId));
285         final RestOutputCmHandlePublicProperties restOutputCmHandlePublicProperties =
286                 new RestOutputCmHandlePublicProperties();
287         restOutputCmHandlePublicProperties.setPublicCmHandleProperties(cmHandlePublicProperties);
288         return ResponseEntity.ok(restOutputCmHandlePublicProperties);
289     }
290
291     /**
292      * Get Cm Handle State by Cm Handle Id.
293      *
294      * @param cmHandleId cm-handle identifier
295      * @return cm handle state
296      */
297     @Override
298     public ResponseEntity<RestOutputCmHandleCompositeState> getCmHandleStateByCmHandleId(
299             final String cmHandleId) {
300         final CompositeState cmHandleState = networkCmProxyDataService.getCmHandleCompositeState(cmHandleId);
301         final RestOutputCmHandleCompositeState restOutputCmHandleCompositeState =
302                 new RestOutputCmHandleCompositeState();
303         restOutputCmHandleCompositeState.setState(
304                 cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(cmHandleState));
305         return ResponseEntity.ok(restOutputCmHandleCompositeState);
306     }
307
308     /**
309      * Return module definitions for a cm handle.
310      *
311      * @param cmHandleId cm-handle identifier
312      * @return list of module definitions (module name, revision, yang resource content)
313      */
314     @Override
315     public ResponseEntity<List<RestModuleDefinition>> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
316         final List<RestModuleDefinition> restModuleDefinitions =
317                 networkCmProxyDataService.getModuleDefinitionsByCmHandleId(cmHandleId).stream()
318                         .map(ncmpRestInputMapper::toRestModuleDefinition)
319                         .collect(Collectors.toList());
320         return new ResponseEntity<>(restModuleDefinitions, HttpStatus.OK);
321     }
322
323     /**
324      * Return module references for a cm handle.
325      *
326      * @param cmHandle the cm handle
327      * @return module references for cm handle. Namespace will be always blank because restConf does not include this.
328      */
329     public ResponseEntity<List<RestModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
330         final List<RestModuleReference> restModuleReferences =
331                 networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
332                         .map(ncmpRestInputMapper::toRestModuleReference)
333                         .collect(Collectors.toList());
334         return new ResponseEntity<>(restModuleReferences, HttpStatus.OK);
335     }
336
337     /**
338      * Set the data sync enabled flag, along with the data sync state for the specified cm handle.
339      *
340      * @param cmHandleId          cm handle id
341      * @param dataSyncEnabledFlag data sync enabled flag
342      * @return response entity ok if request is successful
343      */
344     @Override
345     public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
346                                                                     final Boolean dataSyncEnabledFlag) {
347         networkCmProxyDataService.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag);
348         return new ResponseEntity<>(HttpStatus.OK);
349     }
350
351
352     private RestOutputCmHandle toRestOutputCmHandle(final NcmpServiceCmHandle ncmpServiceCmHandle) {
353         final RestOutputCmHandle restOutputCmHandle = new RestOutputCmHandle();
354         final CmHandlePublicProperties cmHandlePublicProperties = new CmHandlePublicProperties();
355         restOutputCmHandle.setCmHandle(ncmpServiceCmHandle.getCmHandleId());
356         cmHandlePublicProperties.add(ncmpServiceCmHandle.getPublicProperties());
357         restOutputCmHandle.setPublicCmHandleProperties(cmHandlePublicProperties);
358         restOutputCmHandle.setState(cmHandleStateMapper.toCmHandleCompositeStateExternalLockReason(
359                 ncmpServiceCmHandle.getCompositeState()));
360         return restOutputCmHandle;
361     }
362
363     private void validateDataStore(final DatastoreType acceptableDataStoreType, final String requestedDatastoreName) {
364         final DatastoreType datastoreType = DatastoreType.fromDatastoreName(requestedDatastoreName);
365
366         if (acceptableDataStoreType != datastoreType) {
367             throw new InvalidDatastoreException(requestedDatastoreName + " is not supported");
368         }
369     }
370 }
371