Realize query thirdparty sdnc list API.
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / wrapper / ThirdpatySdncWrapper.java
index 603fcc3..20b8eab 100644 (file)
@@ -19,15 +19,21 @@ import java.util.ArrayList;
 
 import javax.ws.rs.core.Response;
 
-import org.onap.aai.esr.entity.rest.RegisterResponse;
+import org.onap.aai.esr.entity.aai.EsrThirdpartySdncDetail;
+import org.onap.aai.esr.entity.aai.EsrThirdpartySdncList;
+import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
 import org.onap.aai.esr.entity.rest.ThirdpartySdncRegisterInfo;
-//import org.slf4j.Logger;
-//import org.slf4j.LoggerFactory;
+import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
+import org.onap.aai.esr.util.ThirdpartySdncManagerUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.gson.Gson;
 
 public class ThirdpatySdncWrapper {
 
   private static ThirdpatySdncWrapper thirdpatySdncWrapper;
-//  private static final Logger LOG = LoggerFactory.getLogger(ThirdpatySdncWrapper.class);
+  private static final Logger LOG = LoggerFactory.getLogger(ThirdpatySdncWrapper.class);
 
   /**
    * get ThirdpatySdncWrapper instance.
@@ -41,9 +47,20 @@ public class ThirdpatySdncWrapper {
   }
   
   public Response registerThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
-    //TODO
-    RegisterResponse result = null;
-    return Response.ok(result).build();
+    CommonRegisterResponse result = new CommonRegisterResponse();
+    EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
+    esrSdncDetail = ThirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
+    String sdncId = esrSdncDetail.getThirdpartySdncId();
+    try {
+      ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
+      result.setId(sdncId);
+      return Response.ok(result).build();
+    } catch (Exception e) {
+      e.printStackTrace();
+      LOG.error("Register thirdParty SDNC failed !" + e.getMessage());
+      return Response.serverError().build();
+    }
+    
   }
 
   public Response updateThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
@@ -52,19 +69,62 @@ public class ThirdpatySdncWrapper {
   }
   
   public Response queryThirdpartySdncList() {
-    //TODO
-    ArrayList<ThirdpartySdncRegisterInfo> thirdpartySdncList = new ArrayList<ThirdpartySdncRegisterInfo>();
-    return Response.ok(thirdpartySdncList).build();
+    ArrayList<ThirdpartySdncRegisterInfo> sdncList = new ArrayList<ThirdpartySdncRegisterInfo>();
+    EsrThirdpartySdncList esrSdnc = new EsrThirdpartySdncList();
+    try {
+      String esrSdncStr = ExternalSystemProxy.querySdncList();
+      esrSdnc = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncList.class);
+      LOG.info("Response from AAI by query thirdparty SDNC list: " + esrSdnc);
+      sdncList = getSdncDetailList(esrSdnc);
+      return Response.ok(sdncList).build();
+    } catch (Exception e) {
+      e.printStackTrace();
+      LOG.error("Query thirdparty SDNC list failed !");
+      return Response.serverError().build();
+    }
   }
   
   public Response queryThirdpartySdncById(String thirdpartySdncId) {
     ThirdpartySdncRegisterInfo thirdpartySdnc = new ThirdpartySdncRegisterInfo();
-    //TODO
-    return Response.ok(thirdpartySdnc).build();
+    thirdpartySdnc = querySdncDetail(thirdpartySdncId);
+    if(thirdpartySdnc != null) {
+      return Response.ok(thirdpartySdnc).build();
+    } else {
+      return Response.serverError().build();
+    }
   }
   
   public Response delThirdpartySdnc(String thirdpartySdncId) {
     //TODO
     return Response.noContent().build();
   }
+  
+  private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
+    ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
+    EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
+    try {
+      String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
+      LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
+      esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
+      sdncRegisterInfo = ThirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
+      return sdncRegisterInfo;
+    } catch (Exception e) {
+      e.printStackTrace();
+      LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e.getMessage());
+      return null;
+    }
+  }
+  
+  private ArrayList<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
+    ArrayList<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<ThirdpartySdncRegisterInfo>();
+    ThirdpartySdncRegisterInfo sdncInfo = new ThirdpartySdncRegisterInfo();
+    for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
+      String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
+      sdncInfo = querySdncDetail(sdncId);
+      if (sdncInfo != null) {
+        sdncInfoList.add(sdncInfo);
+      }
+    }
+    return sdncInfoList;
+  }
 }