fix data from object to string
[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  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.rest.controller;
23
24
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import java.util.Collection;
29 import javax.validation.Valid;
30 import javax.validation.constraints.Min;
31 import javax.validation.constraints.NotNull;
32 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
33 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
34 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
35 import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration;
36 import org.onap.cps.spi.FetchDescendantsOption;
37 import org.onap.cps.spi.model.DataNode;
38 import org.onap.cps.spi.model.ModuleReference;
39 import org.onap.cps.utils.DataMapUtils;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.RequestMapping;
43 import org.springframework.web.bind.annotation.RestController;
44
45 @RestController
46 @RequestMapping("${rest.api.ncmp-base-path}")
47 public class NetworkCmProxyController implements NetworkCmProxyApi {
48
49     private static final Gson GSON = new GsonBuilder().create();
50
51     private final NetworkCmProxyDataService networkCmProxyDataService;
52
53     private final ObjectMapper objectMapper;
54
55     /**
56      * Constructor Injection for Dependencies.
57      * @param networkCmProxyDataService Data Service Interface
58      * @param objectMapper Object Mapper
59      */
60     public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService,
61         final ObjectMapper objectMapper) {
62         this.networkCmProxyDataService = networkCmProxyDataService;
63         this.objectMapper = objectMapper;
64     }
65
66     /**
67      * Create Node.
68      * @deprecated This Method is no longer used as part of NCMP.
69      */
70     @Override
71     @Deprecated(forRemoval = false)
72     public ResponseEntity<Void> createNode(final String cmHandle, @Valid final String jsonData,
73         @Valid final String parentNodeXpath) {
74         networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, jsonData);
75         return new ResponseEntity<>(HttpStatus.CREATED);
76     }
77
78     /**
79      * Add List-node Child Element.
80      * @deprecated This Method is no longer used as part of NCMP.
81      */
82     @Override
83     @Deprecated(forRemoval = false)
84     public ResponseEntity<Void> addListNodeElements(@NotNull @Valid final String parentNodeXpath,
85         final String cmHandle, @Valid final String jsonData) {
86         networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData);
87         return new ResponseEntity<>(HttpStatus.CREATED);
88     }
89
90     /**
91      * Get Node By CM Handle and X-Path.
92      * @deprecated This Method is no longer used as part of NCMP.
93      */
94     @Override
95     @Deprecated(forRemoval = false)
96     public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath,
97         @Valid final Boolean includeDescendants) {
98         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
99             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
100         final var dataNode = networkCmProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption);
101         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
102     }
103
104     /**
105      * Update DMI Plugin Registration (used for first registration also).
106      * @param restDmiPluginRegistration the registration data
107      */
108     @Override
109     public ResponseEntity<Void> updateDmiPluginRegistration(
110         final @Valid RestDmiPluginRegistration restDmiPluginRegistration) {
111         final DmiPluginRegistration dmiPluginRegistration =
112             convertRestObjectToJavaApiObject(restDmiPluginRegistration);
113         networkCmProxyDataService.updateDmiRegistrationAndSyncModule(dmiPluginRegistration);
114         return new ResponseEntity<>(HttpStatus.CREATED);
115     }
116
117     /**
118      * Query Data Nodes.
119      * @deprecated This Method is no longer used as part of NCMP.
120      */
121     @Override
122     @Deprecated(forRemoval = false)
123     public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath,
124         @Valid final Boolean includeDescendants) {
125         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
126             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
127         final Collection<DataNode> dataNodes =
128             networkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption);
129         return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK);
130     }
131
132     /**
133      * Replace Node With Descendants.
134      * @deprecated This Method is no longer used as part of NCMP.
135      */
136     @Override
137     @Deprecated(forRemoval = false)
138     public ResponseEntity<Object> replaceNode(final String cmHandle, @Valid final String jsonData,
139         @Valid final String parentNodeXpath) {
140         networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, jsonData);
141         return new ResponseEntity<>(HttpStatus.OK);
142     }
143
144     /**
145      * Update Node Leaves.
146      * @deprecated This Method is no longer used as part of NCMP.
147      */
148     @Override
149     @Deprecated(forRemoval = false)
150     public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final String jsonData,
151         @Valid final String parentNodeXpath) {
152         networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, jsonData);
153         return new ResponseEntity<>(HttpStatus.OK);
154     }
155
156     /**
157      * Get resource data from operational datastore.
158      *
159      * @param cmHandle cm handle identifier
160      * @param resourceIdentifier resource identifier
161      * @param accept accept header parameter
162      * @param fields fields query parameter
163      * @param depth depth query parameter
164      * @return {@code ResponseEntity} response from dmi plugin
165      */
166     @Override
167     public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandle,
168                                                                         final String resourceIdentifier,
169                                                                         final String accept,
170                                                                         final @Valid String fields,
171                                                                         final @Min(1) @Valid Integer depth) {
172         final Object responseObject = networkCmProxyDataService.getResourceDataOperationalForCmHandle(cmHandle,
173                 resourceIdentifier,
174                 accept,
175                 fields,
176                 depth);
177         return ResponseEntity.ok(responseObject);
178     }
179
180     /**
181      * Get resource data from pass-through running datastore.
182      *
183      * @param cmHandle cm handle identifier
184      * @param resourceIdentifier resource identifier
185      * @param accept accept header parameter
186      * @param fields fields query parameter
187      * @param depth depth query parameter
188      * @return {@code ResponseEntity} response from dmi plugin
189      */
190     @Override
191     public ResponseEntity<Object> getResourceDataRunningForCmHandle(final String cmHandle,
192                                                                     final String resourceIdentifier,
193                                                                     final String accept,
194                                                                     final @Valid String fields,
195                                                                     final @Min(1) @Valid Integer depth) {
196         final Object responseObject = networkCmProxyDataService.getResourceDataPassThroughRunningForCmHandle(cmHandle,
197                 resourceIdentifier,
198                 accept,
199                 fields,
200                 depth);
201         return ResponseEntity.ok(responseObject);
202     }
203
204     /**
205      * Create resource data in datastore pass through running
206      * for given cm-handle.
207      *
208      * @param cmHandle cm handle identifier
209      * @param resourceIdentifier resource identifier
210      * @param requestBody requestBody
211      * @param contentType content type of body
212      * @return {@code ResponseEntity} response from dmi plugi
213      */
214     @Override
215     public ResponseEntity<Void> createResourceDataRunningForCmHandle(final String cmHandle,
216                                                                      final String resourceIdentifier,
217                                                                      final String requestBody,
218                                                                      final String contentType) {
219         networkCmProxyDataService.createResourceDataPassThroughRunningForCmHandle(cmHandle,
220                 resourceIdentifier, requestBody, contentType);
221         return new ResponseEntity<>(HttpStatus.CREATED);
222     }
223
224     @Override
225     public ResponseEntity<Object> getModuleReferencesByCmHandle(final String cmHandle) {
226         final Collection<ModuleReference>
227             moduleReferences = networkCmProxyDataService.getYangResourcesModuleReferences(cmHandle);
228         return new ResponseEntity<>(new Gson().toJson(moduleReferences), HttpStatus.OK);
229     }
230
231     private DmiPluginRegistration convertRestObjectToJavaApiObject(
232         final RestDmiPluginRegistration restDmiPluginRegistration) {
233         return objectMapper.convertValue(restDmiPluginRegistration, DmiPluginRegistration.class);
234     }
235
236 }