code fix for OS attach volumes and dettach 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 3d3b9b7..b62062d 100644 (file)
  * ============LICENSE_END=========================================================
  */
 package org.onap.appc.adapter.iaas.provider.operation.impl;
+
 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
 import java.util.Map;
+import java.util.List;
+import com.att.cdp.zones.ComputeService;
 import org.glassfish.grizzly.http.util.HttpStatus;
 import org.onap.appc.Constants;
 import org.onap.appc.adapter.iaas.ProviderAdapter;
@@ -49,6 +52,7 @@ import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operati
 
 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);
@@ -66,32 +70,34 @@ public class AttachVolumeServer extends ProviderServerOperation {
             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();
-            String msg;
             context = getContext(rc, vm_url, 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, tenantName, server.getStatus().toString());
-                    VolumeService vs = context.getVolumeService();
-                    vs.getVolumes(server);;
-                    Volume vol = new Volume();
-                    vol.setId(vol_id);
-                    logger.info("Server status: "+server.getStatus());
-                    Map volms = server.getVolumes();
-                    logger.info("list of attachments");
-                    logger.info(volms.size()+"initial volumes");
-                    logger.info(vol.getId());
-                    if(server.getVolumes().containsValue(vol_id))
-                    {
-                        logger.info("Alreday volumes exists:");
-                         logger.info( volms.size()+"volumes size if exists");
-                    }
-                    else
-                    {
-                    server.attachVolume(vol, device);
-                    logger.info( volms.size()+"volumes size after attaching volume");
+                logger.debug(Msg.SERVER_FOUND, vm_url, 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());
+                            doSuccess(rc);
+                        } 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);
+                        }
                     }
+                }
                 context.close();
                 doSuccess(rc);
                 ctx.setAttribute("VOLUME_STATUS", "SUCCESS");
@@ -112,6 +118,7 @@ public class AttachVolumeServer extends ProviderServerOperation {
         }
         return server;
     }
+
     @Override
     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
             throws APPCException {