get resource data for operational passthrough
[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.utils.DataMapUtils;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.bind.annotation.RestController;
43
44 @RestController
45 @RequestMapping("${rest.api.ncmp-base-path}")
46 public class NetworkCmProxyController implements NetworkCmProxyApi {
47
48     private static final Gson GSON = new GsonBuilder().create();
49
50     private final NetworkCmProxyDataService networkCmProxyDataService;
51
52     private final ObjectMapper objectMapper;
53
54     /**
55      * Constructor Injection for Dependencies.
56      * @param networkCmProxyDataService Data Service Interface
57      * @param objectMapper Object Mapper
58      */
59     public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService,
60         final ObjectMapper objectMapper) {
61         this.networkCmProxyDataService = networkCmProxyDataService;
62         this.objectMapper = objectMapper;
63     }
64
65     /**
66      * Create Node.
67      * @deprecated This Method is no longer used as part of NCMP.
68      */
69     @Override
70     @Deprecated(forRemoval = false)
71     public ResponseEntity<Void> createNode(final String cmHandle, @Valid final String jsonData,
72         @Valid final String parentNodeXpath) {
73         networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, jsonData);
74         return new ResponseEntity<>(HttpStatus.CREATED);
75     }
76
77     /**
78      * Add List-node Child Element.
79      * @deprecated This Method is no longer used as part of NCMP.
80      */
81     @Override
82     @Deprecated(forRemoval = false)
83     public ResponseEntity<Void> addListNodeElements(@NotNull @Valid final String parentNodeXpath,
84         final String cmHandle, @Valid final String jsonData) {
85         networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData);
86         return new ResponseEntity<>(HttpStatus.CREATED);
87     }
88
89     /**
90      * Get Node By CM Handle and X-Path.
91      * @deprecated This Method is no longer used as part of NCMP.
92      */
93     @Override
94     @Deprecated(forRemoval = false)
95     public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath,
96         @Valid final Boolean includeDescendants) {
97         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
98             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
99         final var dataNode = networkCmProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption);
100         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
101     }
102
103     /**
104      * Update DMI Plugin Registration (used for first registration also).
105      * @param restDmiPluginRegistration the registration data
106      */
107     @Override
108     public ResponseEntity<Void> updateDmiPluginRegistration(
109         final @Valid RestDmiPluginRegistration restDmiPluginRegistration) {
110         final DmiPluginRegistration dmiPluginRegistration =
111             convertRestObjectToJavaApiObject(restDmiPluginRegistration);
112         networkCmProxyDataService.updateDmiPluginRegistration(dmiPluginRegistration);
113         return new ResponseEntity<>(HttpStatus.CREATED);
114     }
115
116     /**
117      * Query Data Nodes.
118      * @deprecated This Method is no longer used as part of NCMP.
119      */
120     @Override
121     @Deprecated(forRemoval = false)
122     public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath,
123         @Valid final Boolean includeDescendants) {
124         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
125             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
126         final Collection<DataNode> dataNodes =
127             networkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption);
128         return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK);
129     }
130
131     /**
132      * Replace Node With Descendants.
133      * @deprecated This Method is no longer used as part of NCMP.
134      */
135     @Override
136     @Deprecated(forRemoval = false)
137     public ResponseEntity<Object> replaceNode(final String cmHandle, @Valid final String jsonData,
138         @Valid final String parentNodeXpath) {
139         networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, jsonData);
140         return new ResponseEntity<>(HttpStatus.OK);
141     }
142
143     /**
144      * Update Node Leaves.
145      * @deprecated This Method is no longer used as part of NCMP.
146      */
147     @Override
148     @Deprecated(forRemoval = false)
149     public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final String jsonData,
150         @Valid final String parentNodeXpath) {
151         networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, jsonData);
152         return new ResponseEntity<>(HttpStatus.OK);
153     }
154
155     /**
156      * Get resource data for for operational datastore.
157      *
158      * @param cmHandle cm handle identifier
159      * @param resourceIdentifier resource identifier
160      * @param accept accept header parameter
161      * @param fields fields query parameter
162      * @param depth depth query parameter
163      * @return {@code ResponseEntity} response from dmi plugin
164      */
165     @Override
166     public ResponseEntity<Object> getResourceDataOperationalForCmHandle(final String cmHandle,
167                                                                         final String resourceIdentifier,
168                                                                         final String accept,
169                                                                         final @Valid String fields,
170                                                                         final @Min(1) @Valid Integer depth) {
171         final var responseObject = networkCmProxyDataService.getResourceDataOperationalFoCmHandle(cmHandle,
172                 resourceIdentifier,
173                 accept,
174                 fields,
175                 depth);
176         return ResponseEntity.ok(responseObject);
177     }
178
179     private DmiPluginRegistration convertRestObjectToJavaApiObject(
180         final RestDmiPluginRegistration restDmiPluginRegistration) {
181         return objectMapper.convertValue(restDmiPluginRegistration, DmiPluginRegistration.class);
182     }
183
184 }