2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.adapter.iaas.provider.operation.impl;
24 import org.openecomp.appc.Constants;
25 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
26 import org.openecomp.appc.adapter.iaas.impl.IdentityURL;
27 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
28 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
29 import org.openecomp.appc.adapter.iaas.impl.VMURL;
30 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Outcome;
31 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
32 import org.openecomp.appc.exceptions.APPCException;
33 import org.openecomp.appc.i18n.Msg;
34 import com.att.cdp.exceptions.ResourceNotFoundException;
35 import com.att.cdp.zones.Context;
36 import com.att.cdp.zones.model.ModelObject;
37 import com.att.cdp.zones.model.Server;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40 import com.att.eelf.i18n.EELFResourceManager;
41 import org.openecomp.sdnc.sli.SvcLogicContext;
42 import org.glassfish.grizzly.http.util.HttpStatus;
46 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.STOP_SERVICE;
47 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
50 public class StopServer extends ProviderServerOperation {
52 private static final EELFLogger logger = EELFManager.getInstance().getLogger(StopServer.class);
53 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
56 * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#stopServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
58 @SuppressWarnings("nls")
59 public Server stopServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
61 RequestContext rc = new RequestContext(ctx);
64 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
67 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
68 ProviderAdapter.PROPERTY_PROVIDER_NAME);
70 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
71 ctx.setAttribute("STOP_STATUS", "SUCCESS");
73 VMURL vm = VMURL.parseURL(vm_url);
74 if (validateVM(rc, appName, vm_url, vm)) return null;
76 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
77 String identStr = (ident == null) ? null : ident.toString();
79 Context context = null;
80 ctx.setAttribute("STOP_STATUS", "ERROR");
82 context = getContext(rc, vm_url, identStr);
83 if (context != null) {
85 server = lookupServer(rc, context, vm.getServerId());
86 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
90 * We determine what to do based on the current state of the server
94 * Pending is a bit of a special case. If we find the server is in a
95 * pending state, then the provider is in the process of changing state
96 * of the server. So, lets try to wait a little bit and see if the state
97 * settles down to one we can deal with. If not, then we have to fail
101 if (server.getStatus().equals(Server.Status.PENDING)) {
102 waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
103 Server.Status.SUSPENDED, Server.Status.PAUSED, Server.Status.DELETED);
106 switch (server.getStatus()) {
108 // Nothing to do, the server is gone
109 msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
110 server.getTenantId(), "stopped");
111 generateEvent(rc, false, msg);
113 metricsLogger.error(msg);
114 throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
117 // Attempt to stop the server
119 stopServer(rc, server);
120 generateEvent(rc, true, Outcome.SUCCESS.toString());
124 // Server is in error state
125 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
126 server.getTenantId(), "stop");
127 generateEvent(rc, false, msg);
129 metricsLogger.error(msg);
130 throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
133 // Nothing to do, the server was already stopped
134 logger.info("Server was already stopped");
138 // if paused, un-pause it and then stop it
140 unpauseServer(rc, server);
142 stopServer(rc, server);
143 generateEvent(rc, true, Outcome.SUCCESS.toString());
147 // Attempt to resume the suspended server and after that stop it
149 resumeServer(rc, server);
151 stopServer(rc, server);
152 generateEvent(rc, true, Outcome.SUCCESS.toString());
156 // Hmmm, unknown status, should never occur
157 msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
158 server.getTenantId(), server.getStatus().name());
159 generateEvent(rc, false, msg);
161 metricsLogger.error(msg);
162 throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
166 ctx.setAttribute("STOP_STATUS", "SUCCESS");
167 msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "StopServer", vm_url);
168 ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
171 ctx.setAttribute("STOP_STATUS", "CONTEXT_NOT_FOUND");
173 } catch (ResourceNotFoundException e) {
174 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
176 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
177 } catch (Throwable t) {
178 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
179 STOP_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
180 logger.error(msg, t);
181 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
183 } catch (RequestFailedException e) {
184 logger.error(EELFResourceManager.format(Msg.STOP_SERVER_FAILED, appName, "n/a", "n/a", e.getMessage()));
185 doFailure(rc, e.getStatus(), e.getMessage());
192 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
194 setMDC(STOP_SERVICE.toString(), "App-C IaaS Adapter:Stop", ADAPTER_NAME);
195 logOperation(Msg.STOPPING_SERVER, params, context);
196 return stopServer(params, context);