Merge "Add get cm handles by modules names - persistence 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.Collection;
28 import javax.validation.Valid;
29 import javax.validation.constraints.NotNull;
30 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
31 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
32 import org.onap.cps.ncmp.rest.model.CmHandles;
33 import org.onap.cps.ncmp.rest.model.Conditions;
34 import org.onap.cps.spi.FetchDescendantsOption;
35 import org.onap.cps.spi.model.DataNode;
36 import org.onap.cps.spi.model.ModuleReference;
37 import org.onap.cps.utils.DataMapUtils;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.bind.annotation.RequestMapping;
41 import org.springframework.web.bind.annotation.RestController;
42
43 @RestController
44 @RequestMapping("${rest.api.ncmp-base-path}")
45 public class NetworkCmProxyController implements NetworkCmProxyApi {
46
47     private static final Gson GSON = new GsonBuilder().create();
48
49     private final NetworkCmProxyDataService networkCmProxyDataService;
50
51     /**
52      * Constructor Injection for Dependencies.
53      * @param networkCmProxyDataService Data Service Interface
54      */
55     public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService) {
56         this.networkCmProxyDataService = networkCmProxyDataService;
57     }
58
59     /**
60      * Create Node.
61      * @deprecated This Method is no longer used as part of NCMP.
62      */
63     @Override
64     @Deprecated(forRemoval = false)
65     public ResponseEntity<Void> createNode(final String cmHandle, @Valid final String jsonData,
66         @Valid final String parentNodeXpath) {
67         networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, jsonData);
68         return new ResponseEntity<>(HttpStatus.CREATED);
69     }
70
71     /**
72      * Add List-node Child Element.
73      * @deprecated This Method is no longer used as part of NCMP.
74      */
75     @Override
76     @Deprecated(forRemoval = false)
77     public ResponseEntity<Void> addListNodeElements(@NotNull @Valid final String parentNodeXpath,
78         final String cmHandle, @Valid final String jsonData) {
79         networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData);
80         return new ResponseEntity<>(HttpStatus.CREATED);
81     }
82
83     /**
84      * Get Node By CM Handle and X-Path.
85      * @deprecated This Method is no longer used as part of NCMP.
86      */
87     @Override
88     @Deprecated(forRemoval = false)
89     public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath,
90         @Valid final Boolean includeDescendants) {
91         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
92             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
93         final var dataNode = networkCmProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption);
94         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
95     }
96
97     /**
98      * Query Data Nodes.
99      * @deprecated This Method is no longer used as part of NCMP.
100      */
101     @Override
102     @Deprecated(forRemoval = false)
103     public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath,
104         @Valid final Boolean includeDescendants) {
105         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
106             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
107         final Collection<DataNode> dataNodes =
108             networkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption);
109         return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK);
110     }
111
112     /**
113      * Replace Node With Descendants.
114      * @deprecated This Method is no longer used as part of NCMP.
115      */
116     @Override
117     @Deprecated(forRemoval = false)
118     public ResponseEntity<Object> replaceNode(final String cmHandle, @Valid final String jsonData,
119         @Valid final String parentNodeXpath) {
120         networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, jsonData);
121         return new ResponseEntity<>(HttpStatus.OK);
122     }
123
124     /**
125      * Update Node Leaves.
126      * @deprecated This Method is no longer used as part of NCMP.
127      */
128     @Override
129     @Deprecated(forRemoval = false)
130     public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final String jsonData,
131         @Valid final String parentNodeXpath) {
132         networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, jsonData);
133         return new ResponseEntity<>(HttpStatus.OK);
134     }
135
136     /**
137      * Get resource data from operational datastore.
138      *
139      * @param cmHandle cm handle identifier
140      * @param resourceIdentifier resource identifier
141      * @param acceptParamInHeader accept header parameter
142      * @param optionsParamInQuery options query parameter
143      * @return {@code ResponseEntity} response from dmi plugin
144      */
145     @Override
146     public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandle,
147                                                                         final @NotNull @Valid String resourceIdentifier,
148                                                                         final String acceptParamInHeader,
149                                                                         final @Valid String optionsParamInQuery) {
150         final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle,
151                 resourceIdentifier,
152                 acceptParamInHeader,
153                 optionsParamInQuery);
154         return ResponseEntity.ok(responseObject);
155     }
156
157     /**
158      * Get resource data from pass-through running datastore.
159      *
160      * @param cmHandle cm handle identifier
161      * @param resourceIdentifier resource identifier
162      * @param acceptParamInHeader accept header parameter
163      * @param optionsParamInQuery options query parameter
164      * @return {@code ResponseEntity} response from dmi plugin
165      */
166     @Override
167     public ResponseEntity<Object> getResourceDataRunningForCmHandle(final String cmHandle,
168                                                                     final @NotNull @Valid String resourceIdentifier,
169                                                                     final String acceptParamInHeader,
170                                                                     final @Valid String optionsParamInQuery) {
171         final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle,
172                 resourceIdentifier,
173                 acceptParamInHeader,
174                 optionsParamInQuery);
175         return ResponseEntity.ok(responseObject);
176     }
177
178     /**
179      * Create resource data in datastore pass through running
180      * for given cm-handle.
181      *
182      * @param resourceIdentifier resource identifier
183      * @param cmHandle cm handle identifier
184      * @param requestBody requestBody
185      * @param contentType content type of body
186      * @return {@code ResponseEntity} response from dmi plugi
187      */
188     @Override
189     public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String resourceIdentifier,
190                                                                      final String cmHandle,
191                                                                      final String requestBody,
192                                                                      final String contentType) {
193         networkCmProxyDataService.createResourceDataPassThroughRunningForCmHandle(cmHandle,
194                 resourceIdentifier, requestBody, contentType);
195         return new ResponseEntity<>(HttpStatus.CREATED);
196     }
197
198     @Override
199     public ResponseEntity<CmHandles> executeCmHandleSearch(final Conditions conditions) {
200         return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
201     }
202
203     @Override
204     public ResponseEntity<Object> getModuleReferencesByCmHandle(final String cmHandle) {
205         final Collection<ModuleReference>
206             moduleReferences = networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle);
207         return new ResponseEntity<>(new Gson().toJson(moduleReferences), HttpStatus.OK);
208     }
209
210 }