refactor CreateSnapshot for nesting try 55/31155/4
authorTaka <tc012c@att.com>
Sat, 10 Feb 2018 23:13:14 +0000 (18:13 -0500)
committerPatrick Brady <pb071s@att.com>
Sun, 18 Feb 2018 21:19:46 +0000 (21:19 +0000)
Change-Id: Ia66842fe6b680fd5f6eb74e8ddebf09f89c6c1d0
Signed-off-by: Taka <tc012c@att.com>
Issue-ID: APPC-597

appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/CreateSnapshot.java

index 4513352..6aec5c6 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
  * =============================================================================
@@ -150,12 +150,9 @@ public class CreateSnapshot extends ProviderServerOperation {
         rc.isAlive();
 
         setTimeForMetricsLogger();
-
-        String msg;
         try {
             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
-
             String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
             VMURL vm = VMURL.parseURL(vm_url);
@@ -164,47 +161,55 @@ public class CreateSnapshot extends ProviderServerOperation {
 
             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
             String identStr = (ident == null) ? null : ident.toString();
-
-            Context context = null;
-            String tenantName = "Unknown";//this variable is also used in catch
-            try {
-                context = getContext(rc, vm_url, identStr);
-                if (context != null) {
-                    tenantName = context.getTenantName();
-                    Server server = lookupServer(rc, context, vm.getServerId());
-
-                    logger.debug(Msg.SERVER_FOUND, vm_url, tenantName, server.getStatus().toString());
-
-                    if (hasImageAccess(rc, context)) {
-                        snapshot = createSnapshot(rc, server);
-                        doSuccess(rc);
-                    } else {
-                        msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
-                                "Accessing Image Service Failed");
-                        logger.error(msg);
-                        metricsLogger.error(msg);
-                        doFailure(rc, HttpStatus.FORBIDDEN_403, msg);
-                    }
-                    context.close();
-                }
-            } catch (ResourceNotFoundException e) {
-                msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
-                logger.error(msg);
-                metricsLogger.error(msg, e);
-                doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
-            } catch (Exception e1) {
-                msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
-                        Operation.SNAPSHOT_SERVICE.toString(), vm_url,
-                        tenantName);
-                logger.error(msg, e1);
-                doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
-            }
+            snapshot = createSnapshotNested(snapshot, rc, vm, vm_url, identStr);
         } catch (RequestFailedException e) {
             doFailure(rc, e.getStatus(), e.getMessage());
         }
         return snapshot;
     }
 
+    private Image createSnapshotNested(Image SnapShot, RequestContext RcContext, VMURL vm, String vmUrl, String identStr) 
+            throws APPCException {
+
+        String msg;
+
+        Context context = null;
+        String tenantName = "Unknown";//this variable is also used in catch
+        try {
+            context = getContext(RcContext, vmUrl, identStr);
+            if (context != null) {
+                tenantName = context.getTenantName();
+                Server server = lookupServer(RcContext, context, vm.getServerId());
+
+                logger.debug(Msg.SERVER_FOUND, vmUrl, tenantName, server.getStatus().toString());
+
+                if (hasImageAccess(RcContext, context)) {
+                    SnapShot = createSnapshot(RcContext, server);
+                    doSuccess(RcContext);
+                } else {
+                    msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
+                            "Accessing Image Service Failed");
+                    logger.error(msg);
+                    metricsLogger.error(msg);
+                    doFailure(RcContext, HttpStatus.FORBIDDEN_403, msg);
+                }
+                context.close();
+            }
+        } catch (ResourceNotFoundException e) {
+            msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
+            logger.error(msg);
+            metricsLogger.error(msg, e);
+            doFailure(RcContext, HttpStatus.NOT_FOUND_404, msg);
+        } catch (Exception e1) {
+            msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
+                    Operation.SNAPSHOT_SERVICE.toString(), vmUrl,
+                    tenantName);
+            logger.error(msg, e1);
+            doFailure(RcContext, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
+        }
+        return SnapShot;
+    }
+
     @Override
     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
             throws APPCException {