cdff1673aaf2550b23f14a3d3cbf2c0c659f6be5
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / provider / operation / impl / StartServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.iaas.provider.operation.impl;
25
26 import org.onap.appc.Constants;
27 import org.onap.appc.adapter.iaas.ProviderAdapter;
28 import org.onap.appc.adapter.iaas.impl.IdentityURL;
29 import org.onap.appc.adapter.iaas.impl.RequestContext;
30 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
31 import org.onap.appc.adapter.iaas.impl.VMURL;
32 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
33 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
34 import org.onap.appc.exceptions.APPCException;
35 import org.onap.appc.i18n.Msg;
36 import com.att.cdp.exceptions.ResourceNotFoundException;
37 import com.att.cdp.zones.Context;
38 import com.att.cdp.zones.model.ModelObject;
39 import com.att.cdp.zones.model.Server;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42 import com.att.eelf.i18n.EELFResourceManager;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.glassfish.grizzly.http.util.HttpStatus;
45 import java.util.Map;
46 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.START_SERVICE;
47 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
48 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
49
50 public class StartServer extends ProviderServerOperation {
51
52     private static final EELFLogger logger = EELFManager.getInstance().getLogger(StartServer.class);
53
54     /**
55      * @see org.onap.appc.adapter.iaas.ProviderAdapter#startServer(java.util.Map,
56      *      org.onap.ccsdk.sli.core.sli.SvcLogicContext)
57      */
58     @SuppressWarnings("nls")
59     public Server startServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
60         Server server = null;
61         RequestContext rc = new RequestContext(ctx);
62         rc.isAlive();
63         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
64         try {
65             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
66                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
67             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
68             VMURL vm = VMURL.parseURL(vm_url);
69             if (validateVM(rc, appName, vm_url, vm))
70                 return null;
71             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
72             String identStr = (ident == null) ? null : ident.toString();
73             Context context = null;
74             String tenantName = "Unknown";// to be used also in case of exception
75             ctx.setAttribute("START_STATUS", "ERROR");
76             // Is the skip Hypervisor check attribute populated?
77             String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
78             if (skipHypervisorCheck == null && ctx != null) {
79                 skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
80             }
81             try {
82                 context = getContext(rc, vm_url, identStr);
83                 if (context != null) {
84                     tenantName = context.getTenantName();// this varaible also is used in case of exception
85                     rc.reset();
86                     server = lookupServer(rc, context, vm.getServerId());
87                     logger.debug(Msg.SERVER_FOUND, vm_url, tenantName, server.getStatus().toString());
88                     if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
89                         // Check of the Hypervisor for the VM Server is UP and reachable
90                         checkHypervisor(server);
91                     }
92                     String msg;
93                     /*
94                      * We determine what to do based on the current state of the server
95                      */
96
97                     /*
98                      * Pending is a bit of a special case. If we find the server is in a pending
99                      * state, then the provider is in the process of changing state of the server.
100                      * So, lets try to wait a little bit and see if the state settles down to one we
101                      * can deal with. If not, then we have to fail the request.
102                      */
103
104                     if (server.getStatus().equals(Server.Status.PENDING)) {
105                         waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
106                                 Server.Status.SUSPENDED, Server.Status.PAUSED, Server.Status.DELETED);
107                     }
108                     switch (server.getStatus()) {
109                     case DELETED:
110                         // Nothing to do, the server is gone
111                         msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
112                                 server.getTenantId(), "started");
113                         logger.error(msg);
114                         throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405,
115                                 server);
116                     case RUNNING:
117                         // Nothing to do, the server is already running
118                         logger.info("Server was already running");
119                         break;
120                     case ERROR:
121                         // Server is in error state
122                         msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
123                                 server.getTenantId(), "start");
124                         logger.error(msg);
125                         throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405,
126                                 server);
127
128                     case READY:
129                         // Server is stopped attempt to start the server
130                         rc.reset();
131                         startServer(rc, server);
132                         break;
133
134                     case PAUSED:
135                         // if paused, un-pause it
136                         rc.reset();
137                         unpauseServer(rc, server);
138                         break;
139
140                     case SUSPENDED:
141                         // Attempt to resume the suspended server
142                         rc.reset();
143                         resumeServer(rc, server);
144                         break;
145
146                     default:
147                         // Hmmm, unknown status, should never occur
148                         msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
149                                 server.getTenantId(), server.getStatus().name());
150                         generateEvent(rc, false, msg);
151                         logger.error(msg);
152                         throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405,
153                                 server);
154                     }
155                     context.close();
156                     doSuccess(rc);
157                     ctx.setAttribute("START_STATUS", "SUCCESS");
158                 } else {
159                     ctx.setAttribute("START_STATUS", "CONTEXT_NOT_FOUND");
160                 }
161             } catch (ResourceNotFoundException e) {
162                 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
163                 logger.error(msg);
164                 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
165             } catch (Exception e1) {
166                 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1,
167                         e1.getClass().getSimpleName(), START_SERVICE.toString(), vm_url, tenantName);
168                 logger.error(msg, e1);
169                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
170             }
171         } catch (RequestFailedException e) {
172             doFailure(rc, e.getStatus(), e.getMessage());
173         }
174         return server;
175     }
176
177     @Override
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);
183     }
184 }