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