2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.iaas.provider.operation.impl;
27 import org.onap.appc.Constants;
28 import org.onap.appc.adapter.iaas.ProviderAdapter;
29 import org.onap.appc.adapter.iaas.impl.IdentityURL;
30 import org.onap.appc.adapter.iaas.impl.RequestContext;
31 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
32 import org.onap.appc.adapter.iaas.impl.VMURL;
33 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
34 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
35 import org.onap.appc.exceptions.APPCException;
36 import org.onap.appc.i18n.Msg;
37 import com.att.cdp.exceptions.ResourceNotFoundException;
38 import com.att.cdp.zones.Context;
39 import com.att.cdp.zones.model.ModelObject;
40 import com.att.cdp.zones.model.Server;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import com.att.eelf.i18n.EELFResourceManager;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
45 import org.glassfish.grizzly.http.util.HttpStatus;
47 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.START_SERVICE;
48 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
50 public class StartServer extends ProviderServerOperation {
52 private static final EELFLogger logger = EELFManager.getInstance().getLogger(StartServer.class);
55 * @see org.onap.appc.adapter.iaas.ProviderAdapter#startServer(java.util.Map,
56 * org.openecomp.sdnc.sli.SvcLogicContext)
58 @SuppressWarnings("nls")
59 public Server startServer(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);
72 VMURL vm = VMURL.parseURL(vm_url);
73 if (validateVM(rc, appName, vm_url, vm))
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("START_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 pending state, then the
95 * provider is in the process of changing state of the server. So, lets try to wait a little bit and
96 * see if the state settles down to one we can deal with. If not, then we have to fail the request.
99 if (server.getStatus().equals(Server.Status.PENDING)) {
100 waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
101 Server.Status.SUSPENDED, Server.Status.PAUSED, Server.Status.DELETED);
104 switch (server.getStatus()) {
106 // Nothing to do, the server is gone
107 msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
108 server.getTenantId(), "started");
110 throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405,
114 // Nothing to do, the server is already running
115 logger.info("Server was already running");
119 // Server is in error state
120 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
121 server.getTenantId(), "start");
123 throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405,
127 // Server is stopped attempt to start the server
129 startServer(rc, server);
133 // if paused, un-pause it
135 unpauseServer(rc, server);
139 // Attempt to resume the suspended server
141 resumeServer(rc, server);
145 // Hmmm, unknown status, should never occur
146 msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
147 server.getTenantId(), server.getStatus().name());
148 generateEvent(rc, false, msg);
150 throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405,
155 ctx.setAttribute("START_STATUS", "SUCCESS");
157 ctx.setAttribute("START_STATUS", "CONTEXT_NOT_FOUND");
159 } catch (ResourceNotFoundException e) {
160 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
162 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
163 } catch (Exception e1) {
164 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1,
165 e1.getClass().getSimpleName(), START_SERVICE.toString(), vm_url,
166 context == null ? "Unknown" : context.getTenantName());
167 logger.error(msg, e1);
168 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
170 } catch (RequestFailedException e) {
171 doFailure(rc, e.getStatus(), e.getMessage());
178 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
179 throws APPCException {
180 setMDC(Operation.START_SERVICE.toString(), "App-C IaaS Adapter:Start", ADAPTER_NAME);
181 logOperation(Msg.STARTING_SERVER, params, context);
182 return startServer(params, context);