Maven stage yaml for montreal
[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.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;
32
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.Map;
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;
66
67 @Slf4j
68 @RestController
69 @RequestMapping("${rest.api.ncmp-base-path}")
70 @RequiredArgsConstructor
71 public class NetworkCmProxyController implements NetworkCmProxyApi {
72
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;
83
84     /**
85      * Get resource data from datastore.
86      *
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
94      */
95
96     @Override
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) {
103
104
105         final NcmpDatastoreRequestHandler ncmpDatastoreRequestHandler = getNcmpDatastoreRequestHandler(datastoreName);
106         return ncmpDatastoreRequestHandler.executeRequest(datastoreName, cmHandle, resourceIdentifier,
107                 optionsParamInQuery, topicParamInQuery, includeDescendants);
108     }
109
110     @Override
111     public ResponseEntity<Object> executeDataOperationForCmHandles(final String topicParamInQuery,
112                                                                   final DataOperationRequest
113                                                                           dataOperationRequest) {
114         return ncmpPassthroughResourceRequestHandler.executeRequest(topicParamInQuery,
115                 dataOperationRequestMapper.toDataOperationRequest(dataOperationRequest));
116     }
117
118     /**
119      * Query resource data from datastore.
120      *
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
128      */
129
130     @Override
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);
139     }
140
141     /**
142      * Patch resource data from passthrough-running.
143      *
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
150      */
151
152     @Override
153     public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String datastoreName,
154                                                                       final String cmHandle,
155                                                                       final String resourceIdentifier,
156                                                                       final Object requestBody,
157                                                                       final String contentType) {
158
159         validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
160
161         final Object responseObject = networkCmProxyDataService
162                 .writeResourceDataPassThroughRunningForCmHandle(
163                         cmHandle, resourceIdentifier, PATCH,
164                         jsonObjectMapper.asJsonString(requestBody), contentType);
165         return ResponseEntity.ok(responseObject);
166     }
167
168     /**
169      * Create resource data in datastore pass-through running for given cm-handle.
170      *
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
177      */
178     @Override
179     public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String datastoreName,
180                                                                      final String cmHandle,
181                                                                      final String resourceIdentifier,
182                                                                      final Object requestBody,
183                                                                      final String contentType) {
184
185         validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
186
187         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
188                 resourceIdentifier, CREATE, jsonObjectMapper.asJsonString(requestBody), contentType);
189         return new ResponseEntity<>(HttpStatus.CREATED);
190     }
191
192     /**
193      * Update resource data in datastore pass-through running for given cm-handle.
194      *
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
201      */
202
203     @Override
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);
210
211         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
212                 resourceIdentifier, UPDATE, jsonObjectMapper.asJsonString(requestBody), contentType);
213         return new ResponseEntity<>(HttpStatus.OK);
214     }
215
216     /**
217      * Delete resource data in datastore pass-through running for a given cm-handle.
218      *
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
224      */
225     @Override
226     public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String datastoreName,
227                                                                      final String cmHandle,
228                                                                      final String resourceIdentifier,
229                                                                      final String contentType) {
230
231         validateDataStore(PASSTHROUGH_RUNNING, datastoreName);
232
233         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
234                 resourceIdentifier, DELETE, NO_BODY, contentType);
235         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
236     }
237
238     /**
239      * Query and return cm handles that match the given query parameters.
240      *
241      * @param cmHandleQueryParameters the cm handle query parameters
242      * @return collection of cm handles
243      */
244     @Override
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);
255     }
256
257     /**
258      * Query and return cm handle ids that match the given query parameters.
259      *
260      * @param cmHandleQueryParameters the cm handle query parameters
261      * @return collection of cm handle ids
262      */
263     @Override
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));
271     }
272
273     /**
274      * Search for Cm Handle and Properties by Name.
275      *
276      * @param cmHandleId cm-handle identifier
277      * @return cm handle and its properties
278      */
279     @Override
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);
284     }
285
286     /**
287      * Get Cm Handle Properties by Cm Handle Id.
288      *
289      * @param cmHandleId cm-handle identifier
290      * @return cm handle properties
291      */
292     @Override
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);
301     }
302
303     /**
304      * Get Cm Handle State by Cm Handle Id.
305      *
306      * @param cmHandleId cm-handle identifier
307      * @return cm handle state
308      */
309     @Override
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);
318     }
319
320     /**
321      * Return module definitions for a cm handle.
322      *
323      * @param cmHandleId cm-handle identifier
324      * @return list of module definitions (module name, revision, yang resource content)
325      */
326     @Override
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);
333     }
334
335     /**
336      * Return module references for a cm handle.
337      *
338      * @param cmHandle the cm handle
339      * @return module references for cm handle. Namespace will be always blank because restConf does not include this.
340      */
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);
347     }
348
349     /**
350      * Set the data sync enabled flag, along with the data sync state for the specified cm handle.
351      *
352      * @param cmHandleId          cm handle id
353      * @param dataSyncEnabledFlag data sync enabled flag
354      * @return response entity ok if request is successful
355      */
356     @Override
357     public ResponseEntity<Object> setDataSyncEnabledFlagForCmHandle(final String cmHandleId,
358                                                                     final Boolean dataSyncEnabledFlag) {
359         networkCmProxyDataService.setDataSyncEnabled(cmHandleId, dataSyncEnabledFlag);
360         return new ResponseEntity<>(HttpStatus.OK);
361     }
362
363
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());
375         }
376         return restOutputCmHandle;
377     }
378
379     private void validateDataStore(final DatastoreType acceptableDataStoreType, final String requestedDatastoreName) {
380         final DatastoreType datastoreType = DatastoreType.fromDatastoreName(requestedDatastoreName);
381
382         if (acceptableDataStoreType != datastoreType) {
383             throw new InvalidDatastoreException(requestedDatastoreName + " is not supported");
384         }
385     }
386
387     private NcmpDatastoreRequestHandler getNcmpDatastoreRequestHandler(final String datastoreName) {
388         if (OPERATIONAL.equals(DatastoreType.fromDatastoreName(datastoreName))) {
389             return ncmpCachedResourceRequestHandler;
390         }
391         return ncmpPassthroughResourceRequestHandler;
392     }
393
394
395 }
396