modify deleteLink and add getPost/get uitl 59/66859/1
authorguochuyicmri <guochuyi@chinamobile.com>
Mon, 17 Sep 2018 02:23:12 +0000 (10:23 +0800)
committerguochuyicmri <guochuyi@chinamobile.com>
Mon, 17 Sep 2018 02:23:35 +0000 (10:23 +0800)
Change-Id: Ie0cb2ca4dd5c2aefcb881246f1c89bd007789ac0
Issue-ID: USECASEUI-141
Signed-off-by: guochuyicmri <guochuyi@chinamobile.com>
server/pom.xml
server/src/main/java/org/onap/usecaseui/server/constant/Constant.java
server/src/main/java/org/onap/usecaseui/server/controller/sotn/SotnController.java
server/src/main/java/org/onap/usecaseui/server/service/sotn/impl/SOTNServiceImpl.java
server/src/main/java/org/onap/usecaseui/server/util/HttpUtil.java [new file with mode: 0644]

index 026c208..9469516 100644 (file)
         <dependency>
             <groupId>com.google.guava</groupId>
             <artifactId>guava</artifactId>
-
+            <version>23.0</version>
         </dependency>
 
         <dependency>
index b2c79b5..f1babd6 100755 (executable)
@@ -29,7 +29,7 @@ public final class Constant
     \r
     public static Map<String,ServiceTemplateInput> netWorkMap  = new HashMap<String,ServiceTemplateInput>();\r
     \r
-    public static final String CONSTANT_SUCCESS="SUCCESS";\r
+    public static final String CONSTANT_SUCCESS="{\"status\":\"SUCCESS\"}";\r
     \r
-    public static final String CONSTANT_FAILED="FAILED";\r
+    public static final String CONSTANT_FAILED="{\"status\":\"FAILED\"}";\r
 }\r
index a8a9479..c6cb032 100644 (file)
@@ -25,18 +25,20 @@ import javax.servlet.http.HttpServletRequest;
 import org.onap.usecaseui.server.bean.sotn.NetWorkResource;
 import org.onap.usecaseui.server.bean.sotn.Pinterface;
 import org.onap.usecaseui.server.bean.sotn.Pnf;
+import org.onap.usecaseui.server.constant.Constant;
 import org.onap.usecaseui.server.service.sotn.SOTNService;
+import org.onap.usecaseui.server.util.HttpUtil;
 import org.onap.usecaseui.server.util.UuiCommonUtil;
-import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestController;
 
 import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
 
-@Controller
+@RestController
 @RequestMapping("/uui-sotn")
 public class SotnController {
        
@@ -51,8 +53,7 @@ public class SotnController {
                this.sotnService = sotnService;
        }
        
-    @ResponseBody
-    @RequestMapping(value = {"/getNetWorkResources"}, method = RequestMethod.GET , produces = "application/json")
+    @RequestMapping(value = {"/getNetWorkResources"}, method = RequestMethod.GET)
        public List<NetWorkResource> getNetWorkResources(){
        List<NetWorkResource> result = new ArrayList<NetWorkResource>();
        String json  = sotnService.getNetWorkResources();
@@ -72,68 +73,57 @@ public class SotnController {
                return result;
        }
     
-    @ResponseBody
-    @RequestMapping(value = {"/getPinterfaceByPnfName/{pnfName}"}, method = RequestMethod.GET , produces = "application/json")
+    @RequestMapping(value = {"/getPinterfaceByPnfName/{pnfName}"}, method = RequestMethod.GET)
     public List<Pinterface>  getPinterfaceByPnfName(@PathVariable(value="pnfName") String pnfName){
        return sotnService.getPinterfaceByPnfName(pnfName);
     }
     
-    @ResponseBody
-    @RequestMapping(value = {"/getLogicalLinks"}, method = RequestMethod.GET , produces = "application/json")
+    @RequestMapping(value = {"/getLogicalLinks"}, method = RequestMethod.GET)
     public String getLogicalLinks(){
        return sotnService.getLogicalLinks();
     }
     
-    @ResponseBody
-    @RequestMapping(value = {"/getSpecificLogicalLink/{linkName}"}, method = RequestMethod.GET , produces = "application/json")
+    @RequestMapping(value = {"/getSpecificLogicalLink/{linkName}"}, method = RequestMethod.GET)
     public String getSpecificLogicalLink(@PathVariable(value="linkName") String linkName){
        return sotnService.getSpecificLogicalLink(linkName);
     }
     
-    @ResponseBody
-    @RequestMapping(value = {"/getHostUrl/{aaiId}"}, method = RequestMethod.GET , produces = "application/json")
+    @RequestMapping(value = {"/getHostUrl/{aaiId}"}, method = RequestMethod.GET)
     public String getHostUrl(@PathVariable(value="aaiId") String aaiId){
        return sotnService.getHostUrl(aaiId);
     }
     
-    @ResponseBody
-    @RequestMapping(value = {"/createHostUrl/{aaiId}"}, method = RequestMethod.PUT , produces = "application/json")
+    @RequestMapping(value = {"/createHostUrl/{aaiId}"}, method = RequestMethod.PUT)
     public String createHostUrl(HttpServletRequest request,@PathVariable(value="aaiId") String aaiId){
        return sotnService.createTopoNetwork(request,aaiId);
     }
     
-    @ResponseBody
-    @RequestMapping(value = {"/createTopoNetwork/{networkId}"}, method = RequestMethod.PUT , produces = "application/json")
+    @RequestMapping(value = {"/createTopoNetwork/{networkId}"}, method = RequestMethod.PUT)
     public String createTopoNetwork(HttpServletRequest request,@PathVariable(value="networkId") String networkId){
        return sotnService.createTopoNetwork(request,networkId);
     }
     
-    @ResponseBody
-    @RequestMapping(value = {"/pnf/{pnfName}/p-interfaces/p-interface/{tp-id}/createTerminationPoint"}, method = RequestMethod.PUT , produces = "application/json")
+    @RequestMapping(value = {"/pnf/{pnfName}/p-interfaces/p-interface/{tp-id}/createTerminationPoint"}, method = RequestMethod.PUT)
     public String createTerminationPoint(HttpServletRequest request,@PathVariable(value="pnfName") String pnfName,@PathVariable(value="tp-id") String tpId){
        return sotnService.createTerminationPoint(request,pnfName,tpId);
     }
     
-    @ResponseBody
-    @RequestMapping(value = {"/createLink/{linkName}"}, method = RequestMethod.PUT , produces = "application/json")
+    @RequestMapping(value = {"/createLink/{linkName}"}, method = RequestMethod.PUT)
     public String createLink(HttpServletRequest request,@PathVariable(value="linkName") String linkName){
        return sotnService.createLink(request, linkName);
     }
     
-    @ResponseBody
-    @RequestMapping(value = {"/createPnf/{pnfName}"}, method = RequestMethod.PUT , produces = "application/json")
+    @RequestMapping(value = {"/createPnf/{pnfName}"}, method = RequestMethod.PUT)
     public String createPnf(HttpServletRequest request,@PathVariable(value="pnfName") String pnfName){
        return sotnService.createPnf(request, pnfName);
     }
     
-    @ResponseBody
-    @RequestMapping(value = {"/deleteLink/{linkName}/{resourceVersion}"}, method = RequestMethod.DELETE , produces = "application/json")
+    @RequestMapping(value = {"/deleteLink/{linkName}/{resourceVersion}"}, method = RequestMethod.DELETE)
     public String deleteLink(@PathVariable(value="linkName") String linkName,@PathVariable(value="resourceVersion") String resourceVersion){
        return sotnService.deleteLink(linkName,resourceVersion);
     }
     
-    @ResponseBody
-    @RequestMapping(value = {"/getServiceInstanceInfo"}, method = RequestMethod.GET , produces = "application/json")
+    @RequestMapping(value = {"/getServiceInstanceInfo"}, method = RequestMethod.GET)
     public String getServiceInstanceInfo(HttpServletRequest request){
         String customerId = request.getParameter("customerId");
         String serviceType = request.getParameter("serviceType");
@@ -141,14 +131,18 @@ public class SotnController {
        return sotnService.serviceInstanceInfo(customerId, serviceType, serviceId);
     }
     
-    @ResponseBody
-    @RequestMapping(value = {"/getServiceInstanceList"}, method = RequestMethod.GET , produces = "application/json")
+    @RequestMapping(value = {"/getServiceInstanceList"}, method = RequestMethod.GET)
     public String getServiceInstanceList(HttpServletRequest request){
         String customerId = request.getParameter("customerId");
         String serviceType = request.getParameter("serviceType");
        return sotnService.getServiceInstances(customerId, serviceType);
     }
     
+    @RequestMapping(value = {"/getOssInvenory"}, method = RequestMethod.GET)
+    public String getData(){
+               String result = HttpUtil.sendGet("http://10.73.242.244:8082/uui-sotn/getLogicalLinks", "");
+               return result;
+    }
     private void createJson(String json,List<NetWorkResource> list){
 
        ObjectMapper mapper = new ObjectMapper();
index 19dc76d..b98e785 100644 (file)
@@ -169,12 +169,10 @@ public class SOTNServiceImpl implements SOTNService{
             } else {
                result=Constant.CONSTANT_FAILED;
                 logger.error(String.format("Can not createHostUrl[code=%s, message=%s]", response.code(), response.message()));
-                throw new SOException("aai createHostUrl failed!");
             }
         } catch (Exception e) {
                result=Constant.CONSTANT_FAILED;
                logger.error("createHostUrl occur exception:"+e);
-            throw new SOException("aai createHostUrl is not available!", e);
         }
         return result;
        }
@@ -192,12 +190,10 @@ public class SOTNServiceImpl implements SOTNService{
             } else {
                result=Constant.CONSTANT_FAILED;
                 logger.error(String.format("Can not createTopoNetwork[code=%s, message=%s]", response.code(), response.message()));
-                throw new SOException("aai createTopoNetwork failed!");
             }
         } catch (Exception e) {
                result=Constant.CONSTANT_FAILED;
                logger.error("createTopoNetwork occur exception:"+e);
-            throw new SOException("aai createTopoNetwork is not available!", e);
         }
         return result;
        }
@@ -215,12 +211,10 @@ public class SOTNServiceImpl implements SOTNService{
             } else {
                result=Constant.CONSTANT_FAILED;
                 logger.error(String.format("Can not createTerminationPoint[code=%s, message=%s]", response.code(), response.message()));
-                throw new SOException("aai createTerminationPoint failed!");
             }
         } catch (Exception e) {
                result=Constant.CONSTANT_FAILED;
                logger.error("createTerminationPoint occur exception:"+e);
-            throw new SOException("aai createTerminationPoint is not available!", e);
         }
         return result;
        }
@@ -238,12 +232,10 @@ public class SOTNServiceImpl implements SOTNService{
             } else {
                result=Constant.CONSTANT_FAILED;
                 logger.error(String.format("Can not createLink[code=%s, message=%s]", response.code(), response.message()));
-                throw new SOException("aai createLink failed!");
             }
         } catch (Exception e) {
                result="FAILED";
                logger.error("createLink occur exception:"+e);
-            throw new SOException("aai createLink is not available!", e);
         }
         return result;
        }
@@ -261,7 +253,6 @@ public class SOTNServiceImpl implements SOTNService{
             } else {
                result=Constant.CONSTANT_FAILED;
                 logger.error(String.format("Can not createPnf[code=%s, message=%s]", response.code(), response.message()));
-                throw new SOException("aai createPnf failed!");
             }
         } catch (Exception e) {
                result=Constant.CONSTANT_FAILED;
@@ -282,12 +273,10 @@ public class SOTNServiceImpl implements SOTNService{
             } else {
                result=Constant.CONSTANT_FAILED;
                 logger.error(String.format("Can not deleteLink[code=%s, message=%s]", response.code(), response.message()));
-                throw new SOException("aai deleteLink failed!");
             }
         } catch (Exception e) {
                result=Constant.CONSTANT_FAILED;
                logger.error("deleteLink occur exception:"+e);
-            throw new SOException("aai deleteLink is not available!", e);
         }
         return result;
        }
diff --git a/server/src/main/java/org/onap/usecaseui/server/util/HttpUtil.java b/server/src/main/java/org/onap/usecaseui/server/util/HttpUtil.java
new file mode 100644 (file)
index 0000000..39a0795
--- /dev/null
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2018 CMCC, Inc. and others. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.usecaseui.server.util;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.List;
+import java.util.Map;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HttpUtil {
+       
+       private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
+    /**
+     * 向指定URL发送GET方法的请求
+     * 
+     * @param url
+     *            发送请求的URL
+     * @param param
+     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
+     * @return URL 所代表远程资源的响应结果
+     */
+    public static String sendGet(String url, String param) {
+        String result = "";
+        BufferedReader in = null;
+        try {
+            String urlNameString = url + "?" + param;
+            URL realUrl = new URL(urlNameString);
+            // 打开和URL之间的连接
+            URLConnection connection = realUrl.openConnection();
+            // 设置通用的请求属性
+            connection.setRequestProperty("accept", "*/*");
+            connection.setRequestProperty("connection", "Keep-Alive");
+            connection.setRequestProperty("user-agent",
+                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+            // 建立实际的连接
+            connection.connect();
+            // 获取所有响应头字段
+            Map<String, List<String>> map = connection.getHeaderFields();
+            // 遍历所有的响应头字段
+            for (String key : map.keySet()) {
+               logger.error(key + "--->" + map.get(key));
+            }
+            // 定义 BufferedReader输入流来读取URL的响应
+            in = new BufferedReader(new InputStreamReader(
+                    connection.getInputStream()));
+            String line;
+            while ((line = in.readLine()) != null) {
+                result += line;
+            }
+        } catch (Exception e) {
+               logger.error("发送GET请求出现异常!" + e);
+            e.printStackTrace();
+        }
+        // 使用finally块来关闭输入流
+        finally {
+            try {
+                if (in != null) {
+                    in.close();
+                }
+            } catch (Exception e2) {
+                e2.printStackTrace();
+            }
+        }
+        return result;
+    }
+
+    /**
+     * 向指定 URL 发送POST方法的请求
+     * 
+     * @param url
+     *            发送请求的 URL
+     * @param param
+     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
+     * @return 所代表远程资源的响应结果
+     */
+    public static String sendPost(String url, String param) {
+        PrintWriter out = null;
+        BufferedReader in = null;
+        String result = "";
+        try {
+            URL realUrl = new URL(url);
+            // 打开和URL之间的连接
+            URLConnection conn = realUrl.openConnection();
+            // 设置通用的请求属性
+            conn.setRequestProperty("accept", "*/*");
+            conn.setRequestProperty("connection", "Keep-Alive");
+            conn.setRequestProperty("user-agent",
+                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
+            // 发送POST请求必须设置如下两行
+            conn.setDoOutput(true);
+            conn.setDoInput(true);
+            // 获取URLConnection对象对应的输出流
+            out = new PrintWriter(conn.getOutputStream());
+            // 发送请求参数
+            out.print(param);
+            // flush输出流的缓冲
+            out.flush();
+            // 定义BufferedReader输入流来读取URL的响应
+            in = new BufferedReader(
+                    new InputStreamReader(conn.getInputStream()));
+            String line;
+            while ((line = in.readLine()) != null) {
+                result += line;
+            }
+        } catch (Exception e) {
+               logger.error("发送 POST 请求出现异常!"+e);
+            e.printStackTrace();
+        }
+        //使用finally块来关闭输出流、输入流
+        finally{
+            try{
+                if(out!=null){
+                    out.close();
+                }
+                if(in!=null){
+                    in.close();
+                }
+            }
+            catch(IOException ex){
+                ex.printStackTrace();
+            }
+        }
+        return result;
+    } 
+}