Misleading err msg failed to rebuild for snapshot.
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / provider / operation / impl / CreateSnapshot.java
index 5165b0c..2bf060a 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
  * =============================================================================
@@ -18,7 +18,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  * ============LICENSE_END=========================================================
  */
 
@@ -50,6 +49,7 @@ import org.onap.appc.configuration.Configuration;
 import org.onap.appc.configuration.ConfigurationFactory;
 import org.onap.appc.exceptions.APPCException;
 import org.onap.appc.i18n.Msg;
+import org.onap.appc.logging.LoggingConstants;
 import org.slf4j.MDC;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
@@ -67,10 +67,8 @@ public class CreateSnapshot extends ProviderServerOperation {
 
     private String generateSnapshotName(String server) {
         setTimeForMetricsLogger();
-
         SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
         metricsLogger.info("Snapshot Name Generated: Snapshot of %s at %s", server, df.format(new Date()));
-
         return String.format("Snapshot of %s at %s", server, df.format(new Date()));
     }
 
@@ -78,16 +76,12 @@ public class CreateSnapshot extends ProviderServerOperation {
         Context context = server.getContext();
         Provider provider = context.getProvider();
         ImageService service = context.getImageService(); // Already checked access by this point
-
         String snapshotName = generateSnapshotName(server.getName());
-
         setTimeForMetricsLogger();
-
         logger.info(String.format("Creating snapshot of server %s (%s) with name %s", server.getName(), server.getId(),
                 snapshotName));
         metricsLogger.info(String.format("Creating snapshot of server %s (%s) with name %s", server.getName(),
                 server.getId(), snapshotName));
-
         // Request Snapshot
         String msg;
         while (rc.attempt()) {
@@ -111,7 +105,6 @@ public class CreateSnapshot extends ProviderServerOperation {
             throw new RequestFailedException("Stop Server", msg, HttpStatus.BAD_GATEWAY_502, server);
         }
         rc.reset();
-
         // Locate snapshot image
         Image snapshot = null;
         while (rc.attempt()) {
@@ -137,10 +130,8 @@ public class CreateSnapshot extends ProviderServerOperation {
             throw new RequestFailedException("Stop Server", msg, HttpStatus.BAD_GATEWAY_502, server);
         }
         rc.reset();
-
         // Wait for it to be ready
         waitForStateChange(rc, snapshot, Image.Status.ACTIVE);
-
         return snapshot;
     }
 
@@ -148,86 +139,75 @@ public class CreateSnapshot extends ProviderServerOperation {
         Image snapshot = null;
         RequestContext rc = new RequestContext(ctx);
         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);
             if (validateVM(rc, appName, vm_url, vm))
                 return null;
-
             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
             String identStr = (ident == null) ? null : ident.toString();
-
-            Context context = null;
-            try {
-                context = getContext(rc, vm_url, identStr);
-                if (context != null) {
-                    Server server = lookupServer(rc, context, vm.getServerId());
-                    logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), 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,
-                        context == null ? "Unknown" : context.getTenantName());
-                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.IMAGE_SERVICE_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 {
-
         setMDC(Operation.SNAPSHOT_SERVICE.toString(), "App-C IaaS Adapter:Snapshot", ADAPTER_NAME);
         logOperation(Msg.SNAPSHOTING_SERVER, params, context);
         setTimeForMetricsLogger();
-
         metricsLogger.info("Executing Provider Operation: Create Snapshot");
-
         return createSnapshot(params, context);
     }
 
     private void setTimeForMetricsLogger() {
-        long startTime = System.currentTimeMillis();
-        TimeZone tz = TimeZone.getTimeZone("UTC");
-        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
-        df.setTimeZone(tz);
-        long endTime = System.currentTimeMillis();
-        long duration = endTime - startTime;
-        String durationStr = String.valueOf(duration);
-        String endTimeStrUTC = df.format(new Date());
-        MDC.put("EndTimestamp", endTimeStrUTC);
-        MDC.put("ElapsedTime", durationStr);
-        MDC.put("TargetEntity", "cdp");
-        MDC.put("TargetServiceName", "create snapshot");
-        MDC.put("ClassName", "org.onap.appc.adapter.iaas.provider.operation.impl.CreateSnapshot");
+        MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, "cdp");
+        MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, "create snapshot");
+        MDC.put(LoggingConstants.MDCKeys.CLASS_NAME,
+                "org.onap.appc.adapter.iaas.provider.operation.impl.CreateSnapshot");
     }
 }