Merge "Issue with CPSData API to add an item to an existing list node"
[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.NotNull;
31 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
32 import org.onap.cps.ncmp.api.models.DmiPluginRegistration;
33 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
34 import org.onap.cps.ncmp.rest.model.RestDmiPluginRegistration;
35 import org.onap.cps.spi.FetchDescendantsOption;
36 import org.onap.cps.spi.model.DataNode;
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     private final ObjectMapper objectMapper;
52
53     /**
54      * Constructor Injection for Dependencies.
55      * @param networkCmProxyDataService Data Service Interface
56      * @param objectMapper Object Mapper
57      */
58     public NetworkCmProxyController(final NetworkCmProxyDataService networkCmProxyDataService,
59         final ObjectMapper objectMapper) {
60         this.networkCmProxyDataService = networkCmProxyDataService;
61         this.objectMapper = objectMapper;
62     }
63
64     /**
65      * Create Node.
66      * @deprecated This Method is no longer used as part of NCMP.
67      */
68     @Override
69     @Deprecated(forRemoval = false)
70     public ResponseEntity<Void> createNode(final String cmHandle, @Valid final String jsonData,
71         @Valid final String parentNodeXpath) {
72         networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, jsonData);
73         return new ResponseEntity<>(HttpStatus.CREATED);
74     }
75
76     /**
77      * Add List-node Child Element.
78      * @deprecated This Method is no longer used as part of NCMP.
79      */
80     @Override
81     @Deprecated(forRemoval = false)
82     public ResponseEntity<Void> addListNodeElements(@NotNull @Valid final String parentNodeXpath,
83         final String cmHandle, @Valid final String jsonData) {
84         networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData);
85         return new ResponseEntity<>(HttpStatus.CREATED);
86     }
87
88     /**
89      * Get Node By CM Handle and X-Path.
90      * @deprecated This Method is no longer used as part of NCMP.
91      */
92     @Override
93     @Deprecated(forRemoval = false)
94     public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath,
95         @Valid final Boolean includeDescendants) {
96         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
97             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
98         final var dataNode = networkCmProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption);
99         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
100     }
101
102     /**
103      * Update DMI Plugin Registration (used for first registration also).
104      * @param restDmiPluginRegistration the registration data
105      */
106     @Override
107     public ResponseEntity<Void> updateDmiPluginRegistration(
108         final @Valid RestDmiPluginRegistration restDmiPluginRegistration) {
109         final DmiPluginRegistration dmiPluginRegistration =
110             convertRestObjectToJavaApiObject(restDmiPluginRegistration);
111         networkCmProxyDataService.updateDmiPluginRegistration(dmiPluginRegistration);
112         return new ResponseEntity<>(HttpStatus.CREATED);
113     }
114
115     /**
116      * Query Data Nodes.
117      * @deprecated This Method is no longer used as part of NCMP.
118      */
119     @Override
120     @Deprecated(forRemoval = false)
121     public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath,
122         @Valid final Boolean includeDescendants) {
123         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
124             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
125         final Collection<DataNode> dataNodes =
126             networkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption);
127         return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK);
128     }
129
130     /**
131      * Replace Node With Descendants.
132      * @deprecated This Method is no longer used as part of NCMP.
133      */
134     @Override
135     @Deprecated(forRemoval = false)
136     public ResponseEntity<Object> replaceNode(final String cmHandle, @Valid final String jsonData,
137         @Valid final String parentNodeXpath) {
138         networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, jsonData);
139         return new ResponseEntity<>(HttpStatus.OK);
140     }
141
142     /**
143      * Update Node Leaves.
144      * @deprecated This Method is no longer used as part of NCMP.
145      */
146     @Override
147     @Deprecated(forRemoval = false)
148     public ResponseEntity<Object> updateNodeLeaves(final String cmHandle, @Valid final String jsonData,
149         @Valid final String parentNodeXpath) {
150         networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, jsonData);
151         return new ResponseEntity<>(HttpStatus.OK);
152     }
153
154
155     private DmiPluginRegistration convertRestObjectToJavaApiObject(
156         final RestDmiPluginRegistration restDmiPluginRegistration) {
157         return objectMapper.convertValue(restDmiPluginRegistration, DmiPluginRegistration.class);
158     }
159
160 }