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