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