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