Fix Sonar Qube Violations - Recurring task
[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 (C) 2021 Nordix Foundation
5  *  Modification Copyright (C) 2021 highstreet technologies GmbH
6  *  Modifications (C) 2021 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  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.ncmp.rest.controller;
24
25 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE;
26 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.DELETE;
27 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH;
28 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE;
29
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.stream.Collectors;
37 import javax.validation.Valid;
38 import javax.validation.constraints.NotNull;
39 import lombok.extern.slf4j.Slf4j;
40 import org.modelmapper.ModelMapper;
41 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
42 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
43 import org.onap.cps.ncmp.rest.model.CmHandleProperties;
44 import org.onap.cps.ncmp.rest.model.CmHandleProperty;
45 import org.onap.cps.ncmp.rest.model.CmHandles;
46 import org.onap.cps.ncmp.rest.model.ConditionProperties;
47 import org.onap.cps.ncmp.rest.model.Conditions;
48 import org.onap.cps.ncmp.rest.model.ModuleNameAsJsonObject;
49 import org.onap.cps.ncmp.rest.model.ModuleNamesAsJsonArray;
50 import org.onap.cps.ncmp.rest.model.ModuleReference;
51 import org.onap.cps.spi.FetchDescendantsOption;
52 import org.onap.cps.spi.model.DataNode;
53 import org.onap.cps.utils.DataMapUtils;
54 import org.springframework.http.HttpStatus;
55 import org.springframework.http.ResponseEntity;
56 import org.springframework.web.bind.annotation.RequestMapping;
57 import org.springframework.web.bind.annotation.RestController;
58
59 @Slf4j
60 @RestController
61 @RequestMapping("${rest.api.ncmp-base-path}")
62 public class NetworkCmProxyController implements NetworkCmProxyApi {
63
64     private static final Gson GSON = new GsonBuilder().create();
65
66     private final ModelMapper modelMapper = new ModelMapper();
67     private final NetworkCmProxyDataService networkCmProxyDataService;
68
69     /**
70      * Constructor Injection for Dependencies.
71      * @param networkCmProxyDataService Data Service Interface
72      */
73     public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService) {
74         this.networkCmProxyDataService = networkCmProxyDataService;
75     }
76
77     /**
78      * Create Node.
79      * @deprecated This Method is no longer used as part of NCMP.
80      */
81     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
82     @Override
83     @Deprecated(forRemoval = false)
84     public ResponseEntity<Void> createNode(final String cmHandle, @Valid final Object jsonData,
85         @Valid final String parentNodeXpath) {
86         networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
87         return new ResponseEntity<>(HttpStatus.CREATED);
88     }
89
90     /**
91      * Add List-node Child Element.
92      * @deprecated This Method is no longer used as part of NCMP.
93      */
94     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
95     @Override
96     @Deprecated(forRemoval = false)
97     public ResponseEntity<Void> addListNodeElements(@NotNull @Valid final String parentNodeXpath,
98         final String cmHandle, @Valid final Object jsonData) {
99         networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
100         return new ResponseEntity<>(HttpStatus.CREATED);
101     }
102
103     /**
104      * Get Node By CM Handle and X-Path.
105      * @deprecated This Method is no longer used as part of NCMP.
106      */
107     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
108     @Override
109     @Deprecated(forRemoval = false)
110     public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath,
111         @Valid final Boolean includeDescendants) {
112         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
113             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
114         final var dataNode = networkCmProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption);
115         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
116     }
117
118     /**
119      * Query Data Nodes.
120      * @deprecated This Method is no longer used as part of NCMP.
121      */
122     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
123     @Override
124     @Deprecated(forRemoval = false)
125     public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath,
126         @Valid final Boolean includeDescendants) {
127         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
128             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
129         final Collection<DataNode> dataNodes =
130             networkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption);
131         return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK);
132     }
133
134     /**
135      * Replace Node With Descendants.
136      * @deprecated This Method is no longer used as part of NCMP.
137      */
138     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
139     @Override
140     @Deprecated(forRemoval = false)
141     public ResponseEntity<Object> replaceNode(final String cmHandle, @Valid final Object jsonData,
142         @Valid final String parentNodeXpath) {
143         networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
144         return new ResponseEntity<>(HttpStatus.OK);
145     }
146
147     /**
148      * Update Node Leaves.
149      * @deprecated This Method is no longer used as part of NCMP.
150      */
151     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
152     @Override
153     @Deprecated(forRemoval = false)
154     public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final Object jsonData,
155         @Valid final String parentNodeXpath) {
156         networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
157         return new ResponseEntity<>(HttpStatus.OK);
158     }
159
160     /**
161      * Get resource data from operational datastore.
162      *
163      * @param cmHandle cm handle identifier
164      * @param resourceIdentifier resource identifier
165      * @param acceptParamInHeader accept header parameter
166      * @param optionsParamInQuery options query parameter
167      * @return {@code ResponseEntity} response from dmi plugin
168      */
169     @Override
170     public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandle,
171                                                                         final @NotNull @Valid String resourceIdentifier,
172                                                                         final String acceptParamInHeader,
173                                                                         final @Valid String optionsParamInQuery) {
174         final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle,
175                 resourceIdentifier,
176                 acceptParamInHeader,
177                 optionsParamInQuery);
178         return ResponseEntity.ok(responseObject);
179     }
180
181     /**
182      * Get resource data from pass-through running datastore.
183      *
184      * @param cmHandle cm handle identifier
185      * @param resourceIdentifier resource identifier
186      * @param acceptParamInHeader accept header parameter
187      * @param optionsParamInQuery options query parameter
188      * @return {@code ResponseEntity} response from dmi plugin
189      */
190     @Override
191     public ResponseEntity<Object> getResourceDataRunningForCmHandle(final String cmHandle,
192                                                                     final @NotNull @Valid String resourceIdentifier,
193                                                                     final String acceptParamInHeader,
194                                                                     final @Valid String optionsParamInQuery) {
195         final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle,
196                 resourceIdentifier,
197                 acceptParamInHeader,
198                 optionsParamInQuery);
199         return ResponseEntity.ok(responseObject);
200     }
201
202     @Override
203     public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
204         final String cmHandle,
205         final Object requestBody, final String contentType) {
206         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
207             resourceIdentifier, PATCH, GSON.toJson(requestBody), contentType);
208         return new ResponseEntity<>(HttpStatus.OK);
209     }
210
211     /**
212      * Create resource data in datastore pass-through running for given cm-handle.
213      *
214      * @param resourceIdentifier resource identifier
215      * @param cmHandle cm handle identifier
216      * @param requestBody the request body
217      * @param contentType content type of body
218      * @return {@code ResponseEntity} response from dmi plugin
219      */
220     @Override
221     public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
222         final String cmHandle, final Object requestBody, final String contentType) {
223         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
224                 resourceIdentifier, CREATE, GSON.toJson(requestBody), contentType);
225         return new ResponseEntity<>(HttpStatus.CREATED);
226     }
227
228     /**
229      * Update resource data in datastore pass-through running for given cm-handle.
230      *
231      * @param resourceIdentifier resource identifier
232      * @param cmHandle cm handle identifier
233      * @param requestBody the request body
234      * @param contentType content type of the body
235      * @return response entity
236      */
237     @Override
238     public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
239                                                                        final String cmHandle,
240                                                                        final Object requestBody,
241                                                                        final String contentType) {
242         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
243             resourceIdentifier, UPDATE, GSON.toJson(requestBody), contentType);
244         return new ResponseEntity<>(HttpStatus.OK);
245     }
246
247
248     /**
249      *  Delete resource data in datastore pass-through running for a given cm-handle.
250      *
251      * @param resourceIdentifier resource identifier
252      * @param cmHandle cm handle identifier
253      * @param requestBody the request body
254      * @param contentType content type of the body
255      * @return response entity no content if request is successful
256      */
257     @Override
258     public ResponseEntity<Void> deleteResourceDataRunningForCmHandle(final String resourceIdentifier,
259                                                                      final String cmHandle,
260                                                                      final Object requestBody,
261                                                                      final String contentType) {
262
263         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
264             resourceIdentifier, DELETE, GSON.toJson(requestBody), contentType);
265         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
266     }
267
268     /**
269      * Execute cm handle search.
270      *
271      * @param conditions the conditions
272      * @return cm handles returned from search.
273      */
274     @Override
275     public ResponseEntity<CmHandles> executeCmHandleSearch(final Conditions conditions) {
276         final List<ConditionProperties> conditionProperties =
277             conditions.getConditions().stream().collect(Collectors.toList());
278         final CmHandles cmHandles = new CmHandles();
279         cmHandles.setCmHandles(toCmHandleProperties(processConditions(conditionProperties)));
280         return ResponseEntity.ok(cmHandles);
281     }
282
283     /**
284      * Return module references for a cm handle.
285      *
286      * @param cmHandle the cm handle
287      * @return module references for cm handle
288      */
289     public ResponseEntity<List<ModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
290         final List<ModuleReference> moduleReferences =
291             networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
292             .map(moduleReference -> modelMapper.map(moduleReference, ModuleReference.class))
293                 .collect(Collectors.toList());
294         return new ResponseEntity<>(moduleReferences, HttpStatus.OK);
295     }
296
297     private Collection<String> processConditions(final List<ConditionProperties> conditionProperties) {
298         for (final ConditionProperties conditionProperty : conditionProperties) {
299             if (conditionProperty.getName().equals("hasAllModules")) {
300                 return executeCmHandleSearchesForModuleNames(conditionProperty);
301             } else {
302                 log.warn("Unrecognized condition name {}.", conditionProperty.getName());
303             }
304         }
305         log.warn("No valid conditions found {}.", conditionProperties);
306         return Collections.emptyList();
307     }
308
309     private Collection<String> executeCmHandleSearchesForModuleNames(final ConditionProperties conditionProperties) {
310         return networkCmProxyDataService
311             .executeCmHandleHasAllModulesSearch(getModuleNames(conditionProperties.getConditionParameters()));
312     }
313
314     private Collection<String> getModuleNames(final ModuleNamesAsJsonArray moduleNamesAsJsonArray) {
315         final Collection<String> moduleNames = new ArrayList<>(moduleNamesAsJsonArray.size());
316         for (final ModuleNameAsJsonObject moduleNameAsJsonObject : moduleNamesAsJsonArray) {
317             moduleNames.add(moduleNameAsJsonObject.getModuleName());
318         }
319         return moduleNames;
320     }
321
322     private CmHandleProperties toCmHandleProperties(final Collection<String> cmHandleIdentifiers) {
323         final CmHandleProperties cmHandleProperties = new CmHandleProperties();
324         for (final String cmHandleIdentifier : cmHandleIdentifiers) {
325             final CmHandleProperty cmHandleProperty = new CmHandleProperty();
326             cmHandleProperty.setCmHandleId(cmHandleIdentifier);
327             cmHandleProperties.add(cmHandleProperty);
328         }
329         return cmHandleProperties;
330     }
331 }