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