fix: UUI backend AAI restful api and implementation for jakarta ccvpn usecase
[usecase-ui/server.git] / server / src / main / java / org / onap / usecaseui / server / controller / sotn / SotnController.java
1 /*
2  * Copyright (C) 2018 CMCC, Inc. and others. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.usecaseui.server.controller.sotn;
17
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.annotation.Resource;
23 import javax.servlet.http.HttpServletRequest;
24
25 import org.onap.usecaseui.server.bean.sotn.NetWorkResource;
26 import org.onap.usecaseui.server.bean.sotn.Pinterface;
27 import org.onap.usecaseui.server.bean.sotn.Pnf;
28 import org.onap.usecaseui.server.service.sotn.SOTNService;
29 import org.onap.usecaseui.server.util.UuiCommonUtil;
30 import org.springframework.web.bind.annotation.CrossOrigin;
31 import org.springframework.web.bind.annotation.PathVariable;
32 import org.springframework.web.bind.annotation.RequestMapping;
33 import org.springframework.web.bind.annotation.GetMapping;
34 import org.springframework.web.bind.annotation.PutMapping;
35 import org.springframework.web.bind.annotation.DeleteMapping;
36 import org.springframework.web.bind.annotation.RequestParam;
37 import org.springframework.web.bind.annotation.RestController;
38
39 import com.fasterxml.jackson.databind.JsonNode;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41
42 @RestController
43 @CrossOrigin(origins="*")
44 @RequestMapping("/uui-sotn")
45 public class SotnController {
46         
47         @Resource(name="SOTNService")
48         public SOTNService sotnService;
49
50         public SOTNService getSotnService() {
51                 return sotnService;
52         }
53
54         public void setSotnService(SOTNService sotnService) {
55                 this.sotnService = sotnService;
56         }
57         
58     @GetMapping(value = {"/getNetWorkResources"})
59         public List<NetWorkResource> getNetWorkResources(){
60         List<NetWorkResource> result = new ArrayList<NetWorkResource>();
61         String json  = sotnService.getNetWorkResources();
62         if(json.indexOf("FAILED")!=-1){
63                 return result;
64         }
65         createJson(json,result);
66         for(NetWorkResource networks:result){
67                 List<Pinterface> pinterfaces = new ArrayList<Pinterface>();
68                 List<Pnf> pnfs = networks.getPnfs();
69                 for(Pnf pnf :pnfs){
70                         List<Pinterface> tps= sotnService.getPinterfaceByPnfName(pnf.getPnfName());
71                         pinterfaces.addAll(tps);
72                 }
73                 networks.setTps(pinterfaces);
74         }
75                 return result;
76         }
77     
78     @GetMapping(value = {"/getPinterfaceByPnfName/{pnfName:.+}"})
79     public List<Pinterface>  getPinterfaceByPnfName(@PathVariable(value="pnfName") String pnfName){
80         return sotnService.getPinterfaceByPnfName(pnfName);
81     }
82     
83     @GetMapping(value = {"/getLogicalLinks"})
84     public String getLogicalLinks(){
85         return sotnService.getLogicalLinks();
86     }
87     
88     @GetMapping(value = {"/getSpecificLogicalLink/{linkName:.+}"})
89     public String getSpecificLogicalLink(@PathVariable(value="linkName") String linkName){
90         return sotnService.getSpecificLogicalLink(linkName);
91     }
92     
93     @GetMapping(value = {"/getHostUrl/{aaiId:.+}"})
94     public String getHostUrl(@PathVariable(value="aaiId") String aaiId){
95         return sotnService.getHostUrl(aaiId);
96     }
97     
98     @GetMapping(value = {"/getExtAaiId/{aaiId:.+}"})
99     public String getExtAaiId(@PathVariable(value="aaiId") String aaiId){
100         return sotnService.getExtAaiId(aaiId);
101     }
102     
103     @PutMapping(value = {"/createHostUrl/{aaiId:.+}"})
104     public String createHostUrl(HttpServletRequest request,@PathVariable(value="aaiId") String aaiId){
105         return sotnService.createHostUrl(request, aaiId);
106     }
107     
108     @PutMapping(value = {"/createTopoNetwork/{networkId:.+}"})
109     public String createTopoNetwork(HttpServletRequest request,@PathVariable(value="networkId") String networkId){
110         return sotnService.createTopoNetwork(request,networkId);
111     }
112     
113     @PutMapping(value = {"/pnf/{pnfName:.+}/p-interfaces/p-interface/{tp-id:.+}/createTerminationPoint"})
114     public String createTerminationPoint(HttpServletRequest request,@PathVariable(value="pnfName") String pnfName,@PathVariable(value="tp-id") String tpId){
115         return sotnService.createTerminationPoint(request,pnfName,tpId);
116     }
117     
118     @PutMapping(value = {"/createLink/{linkName:.+}"})
119     public String createLink(HttpServletRequest request,@PathVariable(value="linkName") String linkName){
120         return sotnService.createLink(request, linkName);
121     }
122     
123     @PutMapping(value = {"/createPnf/{pnfName:.+}"})
124     public String createPnf(HttpServletRequest request,@PathVariable(value="pnfName") String pnfName){
125         return sotnService.createPnf(request, pnfName);
126     }
127     
128     @DeleteMapping(value = {"/deleteLink/{linkName:.+}/{resourceVersion:.+}"})
129     public String deleteLink(@PathVariable(value="linkName") String linkName,@PathVariable(value="resourceVersion") String resourceVersion){
130         return sotnService.deleteLink(linkName,resourceVersion);
131     }
132     
133     @GetMapping(value = {"/getServiceInstanceInfo"})
134     public String getServiceInstanceInfo(HttpServletRequest request){
135         String customerId = request.getParameter("customerId");
136         String serviceType = request.getParameter("serviceType");
137         String serviceId = request.getParameter("serviceId");
138         return sotnService.serviceInstanceInfo(customerId, serviceType, serviceId);
139     }
140     
141     @GetMapping(value = {"/getPnfInfo", "/getPnfInfo/{pnfName}"})
142     public String getPnfInfo(@PathVariable(required = false) String pnfName){
143         return sotnService.getPnfInfo(pnfName);
144     }
145     
146     @GetMapping(value = {"/getAllottedResources"})
147     public String getAllottedResources(HttpServletRequest request){
148         String customerId = request.getParameter("customerId");
149         String serviceType = request.getParameter("serviceType");
150         String serviceId = request.getParameter("serviceId");
151         return sotnService.getAllottedResources(customerId, serviceType,serviceId);
152     }
153     
154     @GetMapping(value = {"/getConnectivityInfo", "/getConnectivityInfo/{connectivityId}"})
155     public String getConnectivityInfo(@PathVariable(required = false) String connectivityId){
156         return sotnService.getConnectivityInfo(connectivityId);
157     }
158
159         @GetMapping(value = {"/getVpnBindingInfo", "/getVpnBindingInfo/{vpnId}"})
160         public String getVpnBindingInfo(@PathVariable(required = false) String vpnId){
161                 return sotnService.getVpnBindingInfo(vpnId);
162         }
163
164         @GetMapping(value = {"/getNetworkRouteInfo", "/getNetworkRouteInfo/{routeId}"})
165         public String getNetworkRouteInfo(@PathVariable(required = false) String routeId){
166                 return sotnService.getNetworkRouteInfo(routeId);
167         }
168
169     @GetMapping(value = {"/getPinterfaceByVpnId/{vpnId:.+}"})
170     public String getPinterfaceByVpnId(@PathVariable(value="vpnId") String vpnId){
171         return sotnService.getPinterfaceByVpnId(vpnId);
172     }
173     
174     @GetMapping(value = {"/getServiceInstanceList"})
175     public String getServiceInstanceList(HttpServletRequest request){
176         String customerId = request.getParameter("customerId");
177         String serviceType = request.getParameter("serviceType");
178         return sotnService.getServiceInstances(customerId, serviceType);
179     }
180
181     @DeleteMapping(value = {"/deleteExtNetWork"})
182     public String deleteExtNetwork(@RequestParam String extNetworkId,@RequestParam(value="resourceVersion") String resourceVersion){
183         return sotnService.deleteExtNetwork(extNetworkId,resourceVersion);
184     }
185     
186     private void createJson(String json,List<NetWorkResource> list){
187
188         ObjectMapper mapper = new ObjectMapper();
189         
190         try {
191                         JsonNode node = mapper.readTree(json);
192                         JsonNode netNode = node.get("network-resource");
193                         for(int i=0;i<netNode.size();i++){
194                                 NetWorkResource netResource = new NetWorkResource();
195                                 List<Pnf> pnfs = new ArrayList<Pnf>();
196                                 String networkId=netNode.get(i).get("network-id").toString();
197                                 if(networkId.indexOf("\"")!=-1){
198                                         networkId = networkId.substring(1, networkId.length()-1);
199                                 }
200                                 netResource.setNetworkId(networkId);
201                                 if(UuiCommonUtil.isNotNullOrEmpty(netNode.get(i).get("relationship-list"))){
202                                         String relationJson = netNode.get(i).get("relationship-list").toString();
203                                         JsonNode relationNode = mapper.readTree(relationJson);
204                                         
205                                         JsonNode shipNode = relationNode.get("relationship");
206                                         for(int j=0;j<shipNode.size();j++){
207                                                 Pnf pnf = new Pnf();
208                                                 JsonNode shipDataNode = shipNode.get(j).get("relationship-data");
209                                                 String shipDataValue = shipDataNode.get(0).get("relationship-value").toString();
210                                                 String shipDataKey = shipDataNode.get(0).get("relationship-key").toString();
211                                                 if(shipDataKey.indexOf("\"")!=-1){
212                                                         shipDataKey = shipDataKey.substring(1, shipDataKey.length()-1);
213                                                 }
214                                                 if("ext-aai-network.aai-id".equals(shipDataKey)){
215                                                         netResource.setAaiId(shipDataKey);
216                                                         continue;
217                                                 }
218                                                 if(shipDataValue.indexOf("\"")!=-1){
219                                                         shipDataValue = shipDataValue.substring(1, shipDataValue.length()-1);
220                                                 }
221                                                 pnf.setPnfName(shipDataValue);
222                                                 pnfs.add(pnf);
223                                         }
224                                         netResource.setPnfs(pnfs);
225                                         list.add(netResource);
226                                 }
227                         }
228                 } catch (IOException e) {
229                         e.printStackTrace();
230                 }
231     }
232 }