Create list-node elements (part3): NCMP REST and service layers
[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 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import java.util.Collection;
27 import javax.validation.Valid;
28 import org.onap.cps.ncmp.api.NetworkCmProxyDataService;
29 import org.onap.cps.ncmp.rest.api.NetworkCmProxyApi;
30 import org.onap.cps.spi.FetchDescendantsOption;
31 import org.onap.cps.spi.model.DataNode;
32 import org.onap.cps.utils.DataMapUtils;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RestController;
38
39
40 @RestController
41 @RequestMapping("${rest.api.ncmp-base-path}")
42 public class NetworkCmProxyController implements NetworkCmProxyApi {
43
44     private static final Gson GSON = new GsonBuilder().create();
45
46     @Autowired
47     private NetworkCmProxyDataService networkCmProxyDataService;
48
49     @Override
50     public ResponseEntity<String> createNode(final String jsonData, final String cmHandle,
51         final String parentNodeXpath) {
52         networkCmProxyDataService.createDataNode(cmHandle, parentNodeXpath, jsonData);
53         return new ResponseEntity<>(HttpStatus.CREATED);
54     }
55
56     @Override
57     public ResponseEntity<String> addListNodeElements(final String jsonData, final String parentNodeXpath,
58         final String cmHandle) {
59         networkCmProxyDataService.addListNodeElements(cmHandle, parentNodeXpath, jsonData);
60         return new ResponseEntity<>(HttpStatus.CREATED);
61     }
62
63     @Override
64     public ResponseEntity<Object> getNodeByCmHandleAndXpath(final String cmHandle, @Valid final String xpath,
65         @Valid final Boolean includeDescendants) {
66         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
67             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
68         final var dataNode = networkCmProxyDataService.getDataNode(cmHandle, xpath, fetchDescendantsOption);
69         return new ResponseEntity<>(DataMapUtils.toDataMap(dataNode), HttpStatus.OK);
70     }
71
72     @Override
73     public ResponseEntity<Object> queryNodesByCmHandleAndCpsPath(final String cmHandle, @Valid final String cpsPath,
74         @Valid final Boolean includeDescendants) {
75         final FetchDescendantsOption fetchDescendantsOption = Boolean.TRUE.equals(includeDescendants)
76             ? FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS : FetchDescendantsOption.OMIT_DESCENDANTS;
77         final Collection<DataNode> dataNodes =
78             networkCmProxyDataService.queryDataNodes(cmHandle, cpsPath, fetchDescendantsOption);
79         return new ResponseEntity<>(GSON.toJson(dataNodes), HttpStatus.OK);
80     }
81
82     @Override
83     public ResponseEntity<Object> replaceNode(@Valid final String jsonData, final String cmHandle,
84         @Valid final String parentNodeXpath) {
85         networkCmProxyDataService.replaceNodeTree(cmHandle, parentNodeXpath, jsonData);
86         return new ResponseEntity<>(HttpStatus.OK);
87     }
88
89     @Override
90     public ResponseEntity<Object> updateNodeLeaves(@Valid final String jsonData, final String cmHandle,
91         @Valid final String parentNodeXpath) {
92         networkCmProxyDataService.updateNodeLeaves(cmHandle, parentNodeXpath, jsonData);
93         return new ResponseEntity<>(HttpStatus.OK);
94     }
95 }