Remove compulsory body from delete passthrough request
[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     private static final String NO_BODY = null;
66
67     private final ModelMapper modelMapper = new ModelMapper();
68     private final NetworkCmProxyDataService networkCmProxyDataService;
69
70     /**
71      * Constructor Injection for Dependencies.
72      * @param networkCmProxyDataService Data Service Interface
73      */
74     public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService) {
75         this.networkCmProxyDataService = networkCmProxyDataService;
76     }
77
78     /**
79      * Create Node.
80      * @deprecated This Method is no longer used as part of NCMP.
81      */
82     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
83     @Override
84     @Deprecated(forRemoval = false)
85     public ResponseEntity<Void> createNode(final String cmHandle, @Valid final Object jsonData,
86         @Valid final String parentNodeXpath) {
87         networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
88         return new ResponseEntity<>(HttpStatus.CREATED);
89     }
90
91     /**
92      * Add List-node Child Element.
93      * @deprecated This Method is no longer used as part of NCMP.
94      */
95     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
96     @Override
97     @Deprecated(forRemoval = false)
98     public ResponseEntity<Void> addListNodeElements(@NotNull @Valid final String parentNodeXpath,
99         final String cmHandle, @Valid final Object jsonData) {
100         networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
101         return new ResponseEntity<>(HttpStatus.CREATED);
102     }
103
104     /**
105      * Get Node By CM Handle and X-Path.
106      * @deprecated This Method is no longer used as part of NCMP.
107      */
108     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
109     @Override
110     @Deprecated(forRemoval = false)
111     public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath,
112         @Valid final Boolean includeDescendants) {
113         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
114             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
115         final var dataNode = networkCmProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption);
116         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
117     }
118
119     /**
120      * Query Data Nodes.
121      * @deprecated This Method is no longer used as part of NCMP.
122      */
123     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
124     @Override
125     @Deprecated(forRemoval = false)
126     public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath,
127         @Valid final Boolean includeDescendants) {
128         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
129             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
130         final Collection<DataNode> dataNodes =
131             networkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption);
132         return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK);
133     }
134
135     /**
136      * Replace Node With Descendants.
137      * @deprecated This Method is no longer used as part of NCMP.
138      */
139     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
140     @Override
141     @Deprecated(forRemoval = false)
142     public ResponseEntity<Object> replaceNode(final String cmHandle, @Valid final Object jsonData,
143         @Valid final String parentNodeXpath) {
144         networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
145         return new ResponseEntity<>(HttpStatus.OK);
146     }
147
148     /**
149      * Update Node Leaves.
150      * @deprecated This Method is no longer used as part of NCMP.
151      */
152     // All deprecated APIs methods will be address into https://jira.onap.org/browse/CPS-642
153     @Override
154     @Deprecated(forRemoval = false)
155     public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final Object jsonData,
156         @Valid final String parentNodeXpath) {
157         networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, GSON.toJson(jsonData));
158         return new ResponseEntity<>(HttpStatus.OK);
159     }
160
161     /**
162      * Get resource data from operational datastore.
163      *
164      * @param cmHandle cm handle identifier
165      * @param resourceIdentifier resource identifier
166      * @param acceptParamInHeader accept header parameter
167      * @param optionsParamInQuery options query parameter
168      * @return {@code ResponseEntity} response from dmi plugin
169      */
170     @Override
171     public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandle,
172                                                                         final @NotNull @Valid String resourceIdentifier,
173                                                                         final String acceptParamInHeader,
174                                                                         final @Valid String optionsParamInQuery) {
175         final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle,
176                 resourceIdentifier,
177                 acceptParamInHeader,
178                 optionsParamInQuery);
179         return ResponseEntity.ok(responseObject);
180     }
181
182     /**
183      * Get resource data from pass-through running datastore.
184      *
185      * @param cmHandle cm handle identifier
186      * @param resourceIdentifier resource identifier
187      * @param acceptParamInHeader accept header parameter
188      * @param optionsParamInQuery options query parameter
189      * @return {@code ResponseEntity} response from dmi plugin
190      */
191     @Override
192     public ResponseEntity<Object> getResourceDataRunningForCmHandle(final String cmHandle,
193                                                                     final @NotNull @Valid String resourceIdentifier,
194                                                                     final String acceptParamInHeader,
195                                                                     final @Valid String optionsParamInQuery) {
196         final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle,
197                 resourceIdentifier,
198                 acceptParamInHeader,
199                 optionsParamInQuery);
200         return ResponseEntity.ok(responseObject);
201     }
202
203     @Override
204     public ResponseEntity<Object> patchResourceDataRunningForCmHandle(final String resourceIdentifier,
205         final String cmHandle,
206         final Object requestBody, final String contentType) {
207         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
208             resourceIdentifier, PATCH, GSON.toJson(requestBody), contentType);
209         return new ResponseEntity<>(HttpStatus.OK);
210     }
211
212     /**
213      * Create resource data in datastore pass-through running for given cm-handle.
214      *
215      * @param resourceIdentifier resource identifier
216      * @param cmHandle cm handle identifier
217      * @param requestBody the request body
218      * @param contentType content type of body
219      * @return {@code ResponseEntity} response from dmi plugin
220      */
221     @Override
222     public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
223         final String cmHandle, final Object requestBody, final String contentType) {
224         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
225                 resourceIdentifier, CREATE, GSON.toJson(requestBody), contentType);
226         return new ResponseEntity<>(HttpStatus.CREATED);
227     }
228
229     /**
230      * Update resource data in datastore pass-through running for given cm-handle.
231      *
232      * @param resourceIdentifier resource identifier
233      * @param cmHandle cm handle identifier
234      * @param requestBody the request body
235      * @param contentType content type of the body
236      * @return response entity
237      */
238     @Override
239     public ResponseEntity<Object> updateResourceDataRunningForCmHandle(final String resourceIdentifier,
240                                                                        final String cmHandle,
241                                                                        final Object requestBody,
242                                                                        final String contentType) {
243         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
244             resourceIdentifier, UPDATE, GSON.toJson(requestBody), contentType);
245         return new ResponseEntity<>(HttpStatus.OK);
246     }
247
248
249     /**
250      *  Delete resource data in datastore pass-through running for a given cm-handle.
251      *
252      * @param resourceIdentifier resource identifier
253      * @param cmHandle cm handle identifier
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 cmHandle,
259                                                                      final String resourceIdentifier,
260                                                                      final String contentType) {
261         networkCmProxyDataService.writeResourceDataPassThroughRunningForCmHandle(cmHandle,
262             resourceIdentifier, DELETE, NO_BODY, contentType);
263         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
264     }
265
266     /**
267      * Execute cm handle search.
268      *
269      * @param conditions the conditions
270      * @return cm handles returned from search.
271      */
272     @Override
273     public ResponseEntity<CmHandles> executeCmHandleSearch(final Conditions conditions) {
274         final List<ConditionProperties> conditionProperties =
275             conditions.getConditions().stream().collect(Collectors.toList());
276         final CmHandles cmHandles = new CmHandles();
277         cmHandles.setCmHandles(toCmHandleProperties(processConditions(conditionProperties)));
278         return ResponseEntity.ok(cmHandles);
279     }
280
281     /**
282      * Return module references for a cm handle.
283      *
284      * @param cmHandle the cm handle
285      * @return module references for cm handle
286      */
287     public ResponseEntity<List<ModuleReference>> getModuleReferencesByCmHandle(final String cmHandle) {
288         final List<ModuleReference> moduleReferences =
289             networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle).stream()
290             .map(moduleReference -> modelMapper.map(moduleReference, ModuleReference.class))
291                 .collect(Collectors.toList());
292         return new ResponseEntity<>(moduleReferences, HttpStatus.OK);
293     }
294
295     private Collection<String> processConditions(final List<ConditionProperties> conditionProperties) {
296         for (final ConditionProperties conditionProperty : conditionProperties) {
297             if (conditionProperty.getName().equals("hasAllModules")) {
298                 return executeCmHandleSearchesForModuleNames(conditionProperty);
299             } else {
300                 log.warn("Unrecognized condition name {}.", conditionProperty.getName());
301             }
302         }
303         log.warn("No valid conditions found {}.", conditionProperties);
304         return Collections.emptyList();
305     }
306
307     private Collection<String> executeCmHandleSearchesForModuleNames(final ConditionProperties conditionProperties) {
308         return networkCmProxyDataService
309             .executeCmHandleHasAllModulesSearch(getModuleNames(conditionProperties.getConditionParameters()));
310     }
311
312     private Collection<String> getModuleNames(final ModuleNamesAsJsonArray moduleNamesAsJsonArray) {
313         final Collection<String> moduleNames = new ArrayList<>(moduleNamesAsJsonArray.size());
314         for (final ModuleNameAsJsonObject moduleNameAsJsonObject : moduleNamesAsJsonArray) {
315             moduleNames.add(moduleNameAsJsonObject.getModuleName());
316         }
317         return moduleNames;
318     }
319
320     private CmHandleProperties toCmHandleProperties(final Collection<String> cmHandleIdentifiers) {
321         final CmHandleProperties cmHandleProperties = new CmHandleProperties();
322         for (final String cmHandleIdentifier : cmHandleIdentifiers) {
323             final CmHandleProperty cmHandleProperty = new CmHandleProperty();
324             cmHandleProperty.setCmHandleId(cmHandleIdentifier);
325             cmHandleProperties.add(cmHandleProperty);
326         }
327         return cmHandleProperties;
328     }
329 }