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