19b9a09daa64c88f1a051987e7e2eaeb9a039512
[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 cmHandle,
137         final String resourceIdentifier, final String requestBody, final String contentType) {
138         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
139     }
140
141     /**
142      * Update Node Leaves.
143      * @deprecated This Method is no longer used as part of NCMP.
144      */
145     @Override
146     @Deprecated(forRemoval = false)
147     public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final String jsonData,
148         @Valid final String parentNodeXpath) {
149         networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, jsonData);
150         return new ResponseEntity<>(HttpStatus.OK);
151     }
152
153     /**
154      * Get resource data from operational datastore.
155      *
156      * @param cmHandle cm handle identifier
157      * @param resourceIdentifier resource identifier
158      * @param acceptParamInHeader accept header parameter
159      * @param optionsParamInQuery options query parameter
160      * @return {@code ResponseEntity} response from dmi plugin
161      */
162     @Override
163     public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandle,
164                                                                         final @NotNull @Valid String resourceIdentifier,
165                                                                         final String acceptParamInHeader,
166                                                                         final @Valid String optionsParamInQuery) {
167         final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle,
168                 resourceIdentifier,
169                 acceptParamInHeader,
170                 optionsParamInQuery);
171         return ResponseEntity.ok(responseObject);
172     }
173
174     /**
175      * Get resource data from pass-through running datastore.
176      *
177      * @param cmHandle cm handle identifier
178      * @param resourceIdentifier resource identifier
179      * @param acceptParamInHeader accept header parameter
180      * @param optionsParamInQuery options query parameter
181      * @return {@code ResponseEntity} response from dmi plugin
182      */
183     @Override
184     public ResponseEntity<Object> getResourceDataRunningForCmHandle(final String cmHandle,
185                                                                     final @NotNull @Valid String resourceIdentifier,
186                                                                     final String acceptParamInHeader,
187                                                                     final @Valid String optionsParamInQuery) {
188         final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle,
189                 resourceIdentifier,
190                 acceptParamInHeader,
191                 optionsParamInQuery);
192         return ResponseEntity.ok(responseObject);
193     }
194
195     /**
196      * Create resource data in datastore pass through running
197      * for given cm-handle.
198      *
199      * @param resourceIdentifier resource identifier
200      * @param cmHandle cm handle identifier
201      * @param requestBody requestBody
202      * @param contentType content type of body
203      * @return {@code ResponseEntity} response from dmi plugi
204      */
205     @Override
206     public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
207                                                                      final String cmHandle,
208                                                                      final String requestBody,
209                                                                      final String contentType) {
210         networkCmProxyDataService.createResourceDataPassThroughRunningForCmHandle(cmHandle,
211                 resourceIdentifier, requestBody, contentType);
212         return new ResponseEntity<>(HttpStatus.CREATED);
213     }
214
215     @Override
216     public ResponseEntity<CmHandles> executeCmHandleSearch(final Conditions conditions) {
217         final List<ConditionProperties> conditionProperties =
218             conditions.getConditions().stream().collect(Collectors.toList());
219         final CmHandles cmHandles = new CmHandles();
220         final Collection<String> cmHandleIdentifiers = processConditions(conditionProperties);
221         cmHandleIdentifiers.forEach(cmHandle -> cmHandles.setCmHandles(toCmHandleProperties(cmHandle)));
222         return ResponseEntity.ok(cmHandles);
223     }
224
225     @Override
226     public ResponseEntity<Object> getModuleReferencesByCmHandle(final String cmHandle) {
227         final Collection<ModuleReference>
228             moduleReferences = networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle);
229         return new ResponseEntity<>(new Gson().toJson(moduleReferences), HttpStatus.OK);
230     }
231
232     private Collection<String> processConditions(final List<ConditionProperties> conditionProperties) {
233         for (final ConditionProperties conditionProperty : conditionProperties) {
234             if (conditionProperty.getName().equals("hasAllModules")) {
235                 return executeCmHandleSearchesForModuleNames(conditionProperty);
236             } else {
237                 log.warn("Unrecognized condition name {}.", conditionProperty.getName());
238             }
239         }
240         log.warn("No valid conditions found {}.", conditionProperties);
241         return Collections.emptyList();
242     }
243
244     private Collection<String> executeCmHandleSearchesForModuleNames(final ConditionProperties conditionProperties) {
245         return networkCmProxyDataService
246             .executeCmHandleHasAllModulesSearch(getModuleNames(conditionProperties.getConditionParameters()));
247     }
248
249     private Collection<String> getModuleNames(final ModuleNamesAsJsonArray moduleNamesAsJsonArray) {
250         final Collection<String> moduleNames = new ArrayList<>(moduleNamesAsJsonArray.size());
251         for (final ModuleNameAsJsonObject moduleNameAsJsonObject : moduleNamesAsJsonArray) {
252             moduleNames.add(moduleNameAsJsonObject.getModuleName());
253         }
254         return moduleNames;
255     }
256
257     private CmHandleProperties toCmHandleProperties(final String cmHandleId) {
258         final CmHandleProperties cmHandleProperties = new CmHandleProperties();
259         final CmHandleProperty cmHandleProperty = new CmHandleProperty();
260         cmHandleProperty.setCmHandleId(cmHandleId);
261         cmHandleProperties.add(cmHandleProperty);
262         return cmHandleProperties;
263     }
264
265
266 }