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.Operation;
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.START_SERVICE;
47 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
49 public class StartServer extends ProviderServerOperation {
51 private static final EELFLogger logger = EELFManager.getInstance().getLogger(StartServer.class);
54 * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#startServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
56 @SuppressWarnings("nls")
57 public Server startServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
59 RequestContext rc = new RequestContext(ctx);
62 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
65 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
66 ProviderAdapter.PROPERTY_PROVIDER_NAME);
68 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
70 VMURL vm = VMURL.parseURL(vm_url);
71 if (validateVM(rc, appName, vm_url, vm)) return null;
73 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
74 String identStr = (ident == null) ? null : ident.toString();
76 Context context = null;
77 ctx.setAttribute("START_STATUS", "ERROR");
79 context = getContext(rc, vm_url, identStr);
80 if (context != null) {
82 server = lookupServer(rc, context, vm.getServerId());
83 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
87 * We determine what to do based on the current state of the server
91 * Pending is a bit of a special case. If we find the server is in a
92 * pending state, then the provider is in the process of changing state
93 * of the server. So, lets try to wait a little bit and see if the state
94 * settles down to one we can deal with. If not, then we have to fail
98 if (server.getStatus().equals(Server.Status.PENDING)) {
99 waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
100 Server.Status.SUSPENDED, Server.Status.PAUSED, Server.Status.DELETED);
103 switch (server.getStatus()) {
105 // Nothing to do, the server is gone
106 msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
107 server.getTenantId(), "started");
109 // metricsLogger.error(msg);
110 throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
113 // Nothing to do, the server is already running
114 logger.info("Server was already running");
118 // Server is in error state
119 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
120 server.getTenantId(), "start");
122 // metricsLogger.error(msg);
123 throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
126 // Server is stopped attempt to start the server
128 startServer(rc, server);
132 // if paused, un-pause it
134 unpauseServer(rc, server);
135 // metricsLogger.info("Server status: PAUSED");
139 // Attempt to resume the suspended server
141 resumeServer(rc, server);
142 // metricsLogger.info("Server status: SUSPENDED");
146 // Hmmm, unknown status, should never occur
147 msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
148 server.getTenantId(), server.getStatus().name());
149 generateEvent(rc, false, msg);
151 // metricsLogger.error(msg);
152 throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
156 ctx.setAttribute("START_STATUS", "SUCCESS");
160 ctx.setAttribute("START_STATUS", "CONTEXT_NOT_FOUND");
162 } catch (ResourceNotFoundException e) {
163 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
165 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
166 } catch (Throwable t) {
167 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
168 START_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
169 logger.error(msg, t);
170 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
172 } catch (RequestFailedException e) {
173 doFailure(rc, e.getStatus(), e.getMessage());
180 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
182 setMDC(Operation.START_SERVICE.toString(), "App-C IaaS Adapter:Start", ADAPTER_NAME);
183 logOperation(Msg.STARTING_SERVER, params, context);
184 return startServer(params, context);