c24619e3891308f0d0f9cf13ffb0648d60cb4768
[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/{pnfName:.+}"})
142     public String getPnfInfo(@PathVariable(value="pnfName") 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 = {"/getPinterfaceByVpnId/{vpnId:.+}"})
160     public String getPinterfaceByVpnId(@PathVariable(value="vpnId") String vpnId){
161         return sotnService.getPinterfaceByVpnId(vpnId);
162     }
163     
164     @GetMapping(value = {"/getServiceInstanceList"})
165     public String getServiceInstanceList(HttpServletRequest request){
166         String customerId = request.getParameter("customerId");
167         String serviceType = request.getParameter("serviceType");
168         return sotnService.getServiceInstances(customerId, serviceType);
169     }
170
171     @DeleteMapping(value = {"/deleteExtNetWork"})
172     public String deleteExtNetwork(@RequestParam String extNetworkId,@RequestParam(value="resourceVersion") String resourceVersion){
173         return sotnService.deleteExtNetwork(extNetworkId,resourceVersion);
174     }
175     
176     private void createJson(String json,List<NetWorkResource> list){
177
178         ObjectMapper mapper = new ObjectMapper();
179         
180         try {
181                         JsonNode node = mapper.readTree(json);
182                         JsonNode netNode = node.get("network-resource");
183                         for(int i=0;i<netNode.size();i++){
184                                 NetWorkResource netResource = new NetWorkResource();
185                                 List<Pnf> pnfs = new ArrayList<Pnf>();
186                                 String networkId=netNode.get(i).get("network-id").toString();
187                                 if(networkId.indexOf("\"")!=-1){
188                                         networkId = networkId.substring(1, networkId.length()-1);
189                                 }
190                                 netResource.setNetworkId(networkId);
191                                 if(UuiCommonUtil.isNotNullOrEmpty(netNode.get(i).get("relationship-list"))){
192                                         String relationJson = netNode.get(i).get("relationship-list").toString();
193                                         JsonNode relationNode = mapper.readTree(relationJson);
194                                         
195                                         JsonNode shipNode = relationNode.get("relationship");
196                                         for(int j=0;j<shipNode.size();j++){
197                                                 Pnf pnf = new Pnf();
198                                                 JsonNode shipDataNode = shipNode.get(j).get("relationship-data");
199                                                 String shipDataValue = shipDataNode.get(0).get("relationship-value").toString();
200                                                 String shipDataKey = shipDataNode.get(0).get("relationship-key").toString();
201                                                 if(shipDataKey.indexOf("\"")!=-1){
202                                                         shipDataKey = shipDataKey.substring(1, shipDataKey.length()-1);
203                                                 }
204                                                 if("ext-aai-network.aai-id".equals(shipDataKey)){
205                                                         netResource.setAaiId(shipDataKey);
206                                                         continue;
207                                                 }
208                                                 if(shipDataValue.indexOf("\"")!=-1){
209                                                         shipDataValue = shipDataValue.substring(1, shipDataValue.length()-1);
210                                                 }
211                                                 pnf.setPnfName(shipDataValue);
212                                                 pnfs.add(pnf);
213                                         }
214                                         netResource.setPnfs(pnfs);
215                                         list.add(netResource);
216                                 }
217                         }
218                 } catch (IOException e) {
219                         e.printStackTrace();
220                 }
221     }
222 }