2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (c) 2019 IBM
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.adapter.iaas.provider.operation.impl;
28 import org.onap.appc.Constants;
29 import org.onap.appc.adapter.iaas.ProviderAdapter;
30 import org.onap.appc.adapter.iaas.impl.IdentityURL;
31 import org.onap.appc.adapter.iaas.impl.RequestContext;
32 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
33 import org.onap.appc.adapter.iaas.impl.VMURL;
34 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Outcome;
35 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
36 import org.onap.appc.exceptions.APPCException;
37 import org.onap.appc.i18n.Msg;
38 import org.onap.appc.logging.LoggingConstants;
39 import org.onap.appc.logging.LoggingUtils;
40 import com.att.cdp.exceptions.ResourceNotFoundException;
41 import com.att.cdp.zones.Context;
42 import com.att.cdp.zones.model.ModelObject;
43 import com.att.cdp.zones.model.Server;
44 import com.att.eelf.configuration.EELFLogger;
45 import com.att.eelf.configuration.EELFManager;
46 import com.att.eelf.i18n.EELFResourceManager;
47 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
48 import org.glassfish.grizzly.http.util.HttpStatus;
49 import java.util.Date;
52 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.STOP_SERVICE;
53 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
54 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
56 public class StopServer extends ProviderServerOperation {
58 private final EELFLogger logger = EELFManager.getInstance().getLogger(StopServer.class);
59 private final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
62 * @see org.onap.appc.adapter.iaas.ProviderAdapter#stopServer(java.util.Map,
63 * org.openecomp.sdnc.sli.SvcLogicContext)
65 @SuppressWarnings("nls")
66 private Server stopServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
68 RequestContext rc = new RequestContext(ctx);
70 setTimeForMetricsLogger();
71 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
73 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
74 ProviderAdapter.PROPERTY_PROVIDER_NAME);
75 String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
76 ctx.setAttribute("STOP_STATUS", "SUCCESS");
77 VMURL vm = VMURL.parseURL(vmUrl);
78 if (validateVM(rc, appName, vmUrl, vm))
80 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
81 String identStr = (ident == null) ? null : ident.toString();
82 Context context = null;
83 ctx.setAttribute("STOP_STATUS", "ERROR");
84 // Is the skip Hypervisor check attribute populated?
85 String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
86 if (skipHypervisorCheck == null && ctx != null) {
87 skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
90 context = getContext(rc, vmUrl, identStr);
91 if (context != null) {
93 server = lookupServer(rc, context, vm.getServerId());
94 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
95 // Always perform Hypervisor check
96 // unless the skip is set to true
97 if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
98 // Check of the Hypervisor for the VM Server is UP and reachable
99 checkHypervisor(server);
103 * We determine what to do based on the current state of the server
106 * Pending is a bit of a special case. If we find the server is in a pending
107 * state, then the provider is in the process of changing state of the server.
108 * So, lets try to wait a little bit and see if the state settles down to one we
109 * can deal with. If not, then we have to fail the request.
111 if (server.getStatus().equals(Server.Status.PENDING)) {
112 waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
113 Server.Status.SUSPENDED, Server.Status.PAUSED, Server.Status.DELETED);
115 switch (server.getStatus()) {
117 // Nothing to do, the server is gone
118 msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
119 server.getTenantId(), "stopped");
120 generateEvent(rc, false, msg);
122 metricsLogger.error(msg);
123 throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
125 // Attempt to stop the server
127 stopServer(rc, server);
128 generateEvent(rc, true, Outcome.SUCCESS.toString());
131 // Server is in error state
132 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
133 server.getTenantId(), "stop");
134 generateEvent(rc, false, msg);
136 metricsLogger.error(msg);
137 throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
139 // Nothing to do, the server was already stopped
140 logger.info("Server was already stopped");
143 // if paused, un-pause it and then stop it
145 unpauseServer(rc, server);
147 stopServer(rc, server);
148 generateEvent(rc, true, Outcome.SUCCESS.toString());
151 // Attempt to resume the suspended server and after that stop it
153 resumeServer(rc, server);
155 stopServer(rc, server);
156 generateEvent(rc, true, Outcome.SUCCESS.toString());
159 // Hmmm, unknown status, should never occur
160 msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
161 server.getTenantId(), server.getStatus().name());
162 generateEvent(rc, false, msg);
164 metricsLogger.error(msg);
165 throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
169 ctx.setAttribute("STOP_STATUS", "SUCCESS");
170 msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "StopServer", vmUrl);
171 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
173 ctx.setAttribute("STOP_STATUS", "CONTEXT_NOT_FOUND");
175 } catch (ResourceNotFoundException e) {
176 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
178 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
179 } catch (Exception e1) {
180 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1,
181 e1.getClass().getSimpleName(), STOP_SERVICE.toString(), vmUrl,
182 context == null ? "Unknown" : context.getTenantName());
183 logger.error(msg, e1);
184 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
186 } catch (RequestFailedException e) {
187 logger.error(EELFResourceManager.format(Msg.STOP_SERVER_FAILED, appName, "n/a", "n/a", e.getMessage()));
188 doFailure(rc, e.getStatus(), e.getMessage());
194 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
195 throws APPCException {
196 setMDC(STOP_SERVICE.toString(), "App-C IaaS Adapter:Stop", ADAPTER_NAME);
197 logOperation(Msg.STOPPING_SERVER, params, context);
198 return stopServer(params, context);
201 private void setTimeForMetricsLogger() {
202 String timestamp = LoggingUtils.generateTimestampStr((new Date()).toInstant());
203 MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, timestamp);
204 MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, timestamp);
205 MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, "0");
206 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, LoggingConstants.StatusCodes.COMPLETE);
207 MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, "cdp");
208 MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, "stop server");
209 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME, "org.onap.appc.adapter.iaas.provider.operation.impl.StopServer");