Realize the update PNF API.
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / wrapper / PnfManagerWrapper.java
index 5efa743..7b5cf29 100644 (file)
  */
 package org.onap.aai.esr.wrapper;
 
+import java.util.ArrayList;
+import java.util.List;
 import javax.ws.rs.core.Response;
 import org.onap.aai.esr.entity.aai.Pnf;
+import org.onap.aai.esr.entity.aai.PnfList;
 import org.onap.aai.esr.entity.rest.PnfRegisterInfo;
 import org.onap.aai.esr.exception.ExceptionUtil;
 import org.onap.aai.esr.exception.ExtsysException;
@@ -53,8 +56,32 @@ public class PnfManagerWrapper {
      * @return
      */
     public Response queryPnfList() {
-        // TODO Auto-generated method stub
-        return null;
+        List<PnfRegisterInfo> esrPnfList = new ArrayList<>();
+        PnfList pnfList = new PnfList();
+        try {
+            String pnflistStr = networkProxy.queryPnfList();
+            pnfList = new Gson().fromJson(pnflistStr, PnfList.class);
+            LOG.info("Response from AAI by query PNF list: " + pnflistStr);
+            esrPnfList = getEsrPnfList(pnfList);
+            return Response.ok(esrPnfList).build();
+        } catch (ExtsysException e) {
+            LOG.error("Query VNFM list failed !", e);
+            return Response.ok(esrPnfList).build();
+        }
+    }
+
+    /**
+     * @param pnfList
+     * @return
+     */
+    private List<PnfRegisterInfo> getEsrPnfList(PnfList pnfList) {
+        List<PnfRegisterInfo> esrPnfList = new ArrayList<>();
+        for (int i = 0; i < pnfList.getPnf().size(); i++) {
+            Pnf pnf = pnfList.getPnf().get(i);
+            PnfRegisterInfo pnfRegisterInfo = pnfManagerUtil.pnf2PnfRegisterInfo(pnf);
+            esrPnfList.add(pnfRegisterInfo);
+        }
+        return esrPnfList;
     }
 
     /**
@@ -85,8 +112,32 @@ public class PnfManagerWrapper {
      * @return
      */
     public Response delPnf(String pnfId) {
-        // TODO Auto-generated method stub
-        return null;
+        String resourceVersion = getResourceVersion(pnfId);
+        try {
+            networkProxy.deletePnf(pnfId, resourceVersion);
+            return Response.noContent().build();
+        } catch (ExtsysException e) {
+            LOG.error("Delete PNF from A&AI failed! PNF ID: " + pnfId + "resouce-version:" + resourceVersion, e);
+            throw ExceptionUtil.buildExceptionResponse(e.getMessage());
+        }
+    }
+
+    /**
+     * @param pnfId
+     * @return
+     */
+    private String getResourceVersion(String pnfId) {
+        String resourceVersion = null;
+        try {
+            String pnfStr = networkProxy.queryPNF(pnfId);
+            Pnf pnf = new Gson().fromJson(pnfStr, Pnf.class);
+            if (pnf != null && pnf.getResourceVersion() != null) {
+                resourceVersion = pnf.getResourceVersion();
+            }
+        } catch (ExtsysException e) {
+            LOG.error("Query PNF detail failed! PNF ID: " + pnfId, e);
+        }
+        return resourceVersion;
     }
 
     /**
@@ -94,9 +145,17 @@ public class PnfManagerWrapper {
      * @param pnfId
      * @return
      */
-    public Response updatePnf(PnfRegisterInfo pnf, String pnfId) {
-        // TODO Auto-generated method stub
-        return null;
+    public Response updatePnf(PnfRegisterInfo pnfRegisterInfo, String pnfId) {
+        String resourceVersion = getResourceVersion(pnfId);
+        Pnf pnf = pnfManagerUtil.pnfRegisterInfo2pnf(pnfRegisterInfo);
+        pnf.setResourceVersion(resourceVersion);
+        try {
+            networkProxy.registerPnf(pnfId, pnf);
+            return Response.ok().build();
+        } catch (ExtsysException e) {
+            LOG.error("Update PNF failed !", e);
+            throw ExceptionUtil.buildExceptionResponse(e.getMessage());
+        }
     }
 
     /**