diff options
Diffstat (limited to 'appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/StopServer.java')
-rw-r--r-- | appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/StopServer.java | 162 |
1 files changed, 78 insertions, 84 deletions
diff --git a/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/StopServer.java b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/StopServer.java index 933b5d8b4..10236a80b 100644 --- a/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/StopServer.java +++ b/appc-adapters/appc-iaas-adapter/appc-iaas-adapter-bundle/src/main/java/org/onap/appc/adapter/iaas/provider/operation/impl/StopServer.java @@ -33,6 +33,8 @@ import org.onap.appc.adapter.iaas.provider.operation.common.enums.Outcome; import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation; import org.onap.appc.exceptions.APPCException; import org.onap.appc.i18n.Msg; +import org.onap.appc.logging.LoggingConstants; +import org.onap.appc.logging.LoggingUtils; import com.att.cdp.exceptions.ResourceNotFoundException; import com.att.cdp.zones.Context; import com.att.cdp.zones.model.ModelObject; @@ -42,135 +44,117 @@ import com.att.eelf.configuration.EELFManager; import com.att.eelf.i18n.EELFResourceManager; import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.glassfish.grizzly.http.util.HttpStatus; +import java.util.Date; import java.util.Map; +import org.slf4j.MDC; import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.STOP_SERVICE; import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME; public class StopServer extends ProviderServerOperation { - private static final EELFLogger logger = EELFManager.getInstance().getLogger(StopServer.class); - private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); + private final EELFLogger logger = EELFManager.getInstance().getLogger(StopServer.class); + private final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger(); /** * @see org.onap.appc.adapter.iaas.ProviderAdapter#stopServer(java.util.Map, - * org.onap.ccsdk.sli.core.sli.SvcLogicContext) + * org.openecomp.sdnc.sli.SvcLogicContext) */ @SuppressWarnings("nls") - public Server stopServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException { + private Server stopServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException { Server server = null; RequestContext rc = new RequestContext(ctx); rc.isAlive(); - + setTimeForMetricsLogger(); String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME); - try { validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL, ProviderAdapter.PROPERTY_PROVIDER_NAME); - String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL); ctx.setAttribute("STOP_STATUS", "SUCCESS"); - 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; - String tenantName = "Unknown";//to be used also in case of exception ctx.setAttribute("STOP_STATUS", "ERROR"); try { 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()); - + logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString()); String msg; /* * We determine what to do based on the current state of the server */ - /* - * Pending is a bit of a special case. If we find the server is in a pending state, then the - * provider is in the process of changing state of the server. So, lets try to wait a little bit and - * see if the state settles down to one we can deal with. If not, then we have to fail the request. + * Pending is a bit of a special case. If we find the server is in a pending + * state, then the provider is in the process of changing state of the server. + * So, lets try to wait a little bit and see if the state settles down to one we + * can deal with. If not, then we have to fail the request. */ - if (server.getStatus().equals(Server.Status.PENDING)) { waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR, Server.Status.SUSPENDED, Server.Status.PAUSED, Server.Status.DELETED); } - switch (server.getStatus()) { - case DELETED: - // Nothing to do, the server is gone - msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(), - server.getTenantId(), "stopped"); - generateEvent(rc, false, msg); - logger.error(msg); - metricsLogger.error(msg); - throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, - server); - - case RUNNING: - // Attempt to stop the server - rc.reset(); - stopServer(rc, server); - generateEvent(rc, true, Outcome.SUCCESS.toString()); - break; - - case ERROR: - // Server is in error state - msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(), - server.getTenantId(), "stop"); - generateEvent(rc, false, msg); - logger.error(msg); - metricsLogger.error(msg); - throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, - server); - - case READY: - // Nothing to do, the server was already stopped - logger.info("Server was already stopped"); - break; - - case PAUSED: - // if paused, un-pause it and then stop it - rc.reset(); - unpauseServer(rc, server); - rc.reset(); - stopServer(rc, server); - generateEvent(rc, true, Outcome.SUCCESS.toString()); - break; - - case SUSPENDED: - // Attempt to resume the suspended server and after that stop it - rc.reset(); - resumeServer(rc, server); - rc.reset(); - stopServer(rc, server); - generateEvent(rc, true, Outcome.SUCCESS.toString()); - break; - - default: - // Hmmm, unknown status, should never occur - msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(), - server.getTenantId(), server.getStatus().name()); - generateEvent(rc, false, msg); - logger.error(msg); - metricsLogger.error(msg); - throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, - server); + case DELETED: + // Nothing to do, the server is gone + msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(), + server.getTenantId(), "stopped"); + generateEvent(rc, false, msg); + logger.error(msg); + metricsLogger.error(msg); + throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server); + case RUNNING: + // Attempt to stop the server + rc.reset(); + stopServer(rc, server); + generateEvent(rc, true, Outcome.SUCCESS.toString()); + break; + case ERROR: + // Server is in error state + msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(), + server.getTenantId(), "stop"); + generateEvent(rc, false, msg); + logger.error(msg); + metricsLogger.error(msg); + throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server); + case READY: + // Nothing to do, the server was already stopped + logger.info("Server was already stopped"); + break; + case PAUSED: + // if paused, un-pause it and then stop it + rc.reset(); + unpauseServer(rc, server); + rc.reset(); + stopServer(rc, server); + generateEvent(rc, true, Outcome.SUCCESS.toString()); + break; + case SUSPENDED: + // Attempt to resume the suspended server and after that stop it + rc.reset(); + resumeServer(rc, server); + rc.reset(); + stopServer(rc, server); + generateEvent(rc, true, Outcome.SUCCESS.toString()); + break; + default: + // Hmmm, unknown status, should never occur + msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(), + server.getTenantId(), server.getStatus().name()); + generateEvent(rc, false, msg); + logger.error(msg); + metricsLogger.error(msg); + throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server); } context.close(); doSuccess(rc); ctx.setAttribute("STOP_STATUS", "SUCCESS"); msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "StopServer", vm_url); ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg); - } else { ctx.setAttribute("STOP_STATUS", "CONTEXT_NOT_FOUND"); } @@ -179,9 +163,9 @@ public class StopServer extends ProviderServerOperation { logger.error(msg); doFailure(rc, HttpStatus.NOT_FOUND_404, msg); } catch (Exception e1) { - String msg = - EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(), - STOP_SERVICE.toString(), vm_url, tenantName); + String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, + e1.getClass().getSimpleName(), STOP_SERVICE.toString(), vm_url, + context == null ? "Unknown" : context.getTenantName()); logger.error(msg, e1); doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg); } @@ -189,7 +173,6 @@ public class StopServer extends ProviderServerOperation { logger.error(EELFResourceManager.format(Msg.STOP_SERVER_FAILED, appName, "n/a", "n/a", e.getMessage())); doFailure(rc, e.getStatus(), e.getMessage()); } - return server; } @@ -200,4 +183,15 @@ public class StopServer extends ProviderServerOperation { logOperation(Msg.STOPPING_SERVER, params, context); return stopServer(params, context); } + private void setTimeForMetricsLogger() { + String timestamp = LoggingUtils.generateTimestampStr(((Date) new Date()).toInstant()); + MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, timestamp); + MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, timestamp); + MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, "0"); + MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, LoggingConstants.StatusCodes.COMPLETE); + MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, "cdp"); + MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, "stop server"); + MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, "org.onap.appc.adapter.iaas.provider.operation.impl.StopServer"); + } + } |