bugfix for error handling while detaching volumes
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / provider / operation / impl / AttachVolumeServer.java
index 0a35373..e27b159 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP : APPC
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Copyright (C) 2017 Amdocs
  * =============================================================================
  */
 package org.onap.appc.adapter.iaas.provider.operation.impl;
 
+import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.ATTACHVOLUME_SERVICE;
 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
-import java.util.Map;
-import java.util.List;
+import com.att.cdp.exceptions.ZoneException;
 import com.att.cdp.zones.ComputeService;
+import com.att.cdp.zones.Context;
+import com.att.cdp.zones.VolumeService;
+import com.att.cdp.zones.model.ModelObject;
+import com.att.cdp.zones.model.Server;
+import com.att.cdp.zones.model.Volume;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+import com.att.eelf.i18n.EELFResourceManager;
+import java.util.List;
+import java.util.Map;
 import org.glassfish.grizzly.http.util.HttpStatus;
 import org.onap.appc.Constants;
 import org.onap.appc.adapter.iaas.ProviderAdapter;
@@ -39,84 +49,82 @@ import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOpe
 import org.onap.appc.exceptions.APPCException;
 import org.onap.appc.i18n.Msg;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
-import com.att.cdp.exceptions.ZoneException;
-import com.att.cdp.zones.Context;
-import com.att.cdp.zones.VolumeService;
-import com.att.cdp.zones.model.ModelObject;
-import com.att.cdp.zones.model.Server;
-import com.att.cdp.zones.model.Volume;
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
-import com.att.eelf.i18n.EELFResourceManager;
-import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.ATTACHVOLUME_SERVICE;;
 
 public class AttachVolumeServer extends ProviderServerOperation {
+
     private final EELFLogger logger = EELFManager.getInstance().getLogger(AttachVolumeServer.class);
 
     private Server attachVolume(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
         Server server = null;
-        RequestContext rc = new RequestContext(ctx);
-        rc.isAlive();
+        RequestContext requestContext = new RequestContext(ctx);
+        requestContext.isAlive();
         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
-        String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
-        String volumeid = params.get(ProviderAdapter.VOLUME_ID);
+        String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
+        String volumeId = params.get(ProviderAdapter.VOLUME_ID);
         String device = params.get(ProviderAdapter.DEVICE);
-        VMURL vm = VMURL.parseURL(vm_url);
-        Context context = null;
+        VMURL vm = VMURL.parseURL(vmUrl);
+        Context context;
         String tenantName = "Unknown";// to be used also in case of exception
         try {
-            if (validateVM(rc, appName, vm_url, vm))
+            if (validateVM(requestContext, appName, vmUrl, vm)) {
                 return null;
+            }
             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
             String identStr = (ident == null) ? null : ident.toString();
-            String vol_id = (volumeid == null) ? null : volumeid.toString();
-            context = getContext(rc, vm_url, identStr);
+            context = getContext(requestContext, vmUrl, identStr);
             if (context != null) {
-                tenantName = context.getTenantName();// this varaible also is used in case of exception
-                rc.reset();
-                server = lookupServer(rc, context, vm.getServerId());
-                logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
+                tenantName = context.getTenantName();// this variable also is
+                                                        // used in case of
+                                                        // exception
+                requestContext.reset();
+                server = lookupServer(requestContext, context, vm.getServerId());
+                logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
                 Context contx = server.getContext();
                 ComputeService service = contx.getComputeService();
-                VolumeService vs = contx.getVolumeService();
-                logger.info("collecting volume status for volume -id:" + vol_id);
-                List<Volume> volList = vs.getVolumes();
-                logger.info("Size of volume list :" + volList.size());
-                if (volList != null && !volList.isEmpty()) {
-                    for (Volume v : volList) {
-                        logger.info("list of volumesif exists" + v.getId());
-                        if (!v.getId().equals(vol_id)) {
-                            v.setId(vol_id);
-                            logger.info("Ready to Attach Volume to the server:" + Volume.Status.ATTACHING);
-                            service.attachVolume(server, v, device);
-                            logger.info("Volume status after performing attach:" + v.getStatus());
-                            if (validateAttach(vs, vol_id)) {
-                                doSuccess(rc);
-                            }
+                VolumeService volumeService = contx.getVolumeService();
+                logger.info("collecting volume status for volume -id:" + volumeId);
+                List<Volume> volumes = volumeService.getVolumes();
+                Volume volume = new Volume();
+                logger.info("Size of volume list :" + volumes.size());
+                if (volumes != null && !volumes.isEmpty()) {
+                    if (!(volumes.contains(volumeId))) {
+                        volume.setId(volumeId);
+                        logger.info("Ready to Attach Volume to the server:");
+                        service.attachVolume(server, volume, device);
+                        logger.info("Volume status after performing attach:" + volume.getStatus());
+                        if (validateAttach(volumeService, volumeId)) {
+                            ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
+                            doSuccess(requestContext);
                         } else {
-                            String msg = "Volume with volume id " + vol_id + " cannot be attached as it already exists";
-                            logger.info("Alreday volumes exists:");
-                            doFailure(rc, HttpStatus.NOT_IMPLEMENTED_501, msg);
+                            String msg = "Failed to attach Volume";
+                            logger.info("Volume with " + volumeId + " unable to attach");
+                            ctx.setAttribute("VOLUME_STATUS", "FAILURE");
+                            doFailure(requestContext, HttpStatus.NOT_IMPLEMENTED_501, msg);
                         }
+                    } else {
+                        String msg = "Volume with volume id " + volumeId + " cannot be attached as it already exists";
+                        logger.info("Alreday volumes exists:");
+                        ctx.setAttribute("VOLUME_STATUS", "FAILURE");
+                        doFailure(requestContext, HttpStatus.NOT_IMPLEMENTED_501, msg);
                     }
                 }
                 context.close();
-                doSuccess(rc);
-                ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
             } else {
                 ctx.setAttribute("VOLUME_STATUS", "CONTEXT_NOT_FOUND");
             }
         } catch (ZoneException e) {
-            String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
+            String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
             logger.error(msg);
-            doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
+            ctx.setAttribute("VOLUME_STATUS", "FAILURE");
+            doFailure(requestContext, HttpStatus.NOT_FOUND_404, msg);
         } catch (RequestFailedException e) {
-            doFailure(rc, e.getStatus(), e.getMessage());
+            ctx.setAttribute("VOLUME_STATUS", "FAILURE");
+            doFailure(requestContext, e.getStatus(), e.getMessage());
         } catch (Exception ex) {
             String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, ex, ex.getClass().getSimpleName(),
-                    ATTACHVOLUME_SERVICE.toString(), vm_url, tenantName);
-            logger.error(msg, ex);
-            doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
+                    ATTACHVOLUME_SERVICE.toString(), vmUrl, tenantName);
+            ctx.setAttribute("VOLUME_STATUS", "FAILURE");
+            doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
         }
         return server;
     }
@@ -129,19 +137,16 @@ public class AttachVolumeServer extends ProviderServerOperation {
         return attachVolume(params, context);
     }
 
-    protected boolean validateAttach(VolumeService vs, String volId) throws RequestFailedException, ZoneException {
+    protected boolean validateAttach(VolumeService volumeService, String volumeId)
+            throws RequestFailedException, ZoneException {
         boolean flag = false;
-        List<Volume> volList = vs.getVolumes();
-        for (Volume v : volList) {
-            if (v.getId().equals(volId)) {
-                logger.info("Volume with " + volId + "attached successsfully");
-                flag = true;
-            } else {
-                logger.info("failed to attach volume with id" + volId);
-                flag = false;
-            }
+        List<Volume> volumeList = volumeService.getVolumes();
+        if (volumeList.contains(volumeId)) {
+            flag = true;
+        } else {
+            flag = false;
         }
+        logger.info("validateAttach flag-->" + flag);
         return flag;
     }
-
 }