Merge of new rebased code
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / openecomp / appc / adapter / iaas / provider / operation / impl / StartServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.adapter.iaas.provider.operation.impl;
23
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;
43
44 import java.util.Map;
45
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;
48
49 public class StartServer extends ProviderServerOperation {
50
51     private static final EELFLogger logger = EELFManager.getInstance().getLogger(StartServer.class);
52
53     /**
54      * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#startServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
55      */
56     @SuppressWarnings("nls")
57     public Server startServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
58         Server server = null;
59         RequestContext rc = new RequestContext(ctx);
60         rc.isAlive();
61
62         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
63
64         try {
65             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
66                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
67
68             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
69
70             VMURL vm = VMURL.parseURL(vm_url);
71             if (validateVM(rc, appName, vm_url, vm)) return null;
72
73             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
74             String identStr = (ident == null) ? null : ident.toString();
75
76             Context context = null;
77             ctx.setAttribute("START_STATUS", "ERROR");
78             try {
79                 context = getContext(rc, vm_url, identStr);
80                 if (context != null) {
81                     rc.reset();
82                     server = lookupServer(rc, context, vm.getServerId());
83                     logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
84                     String msg;
85
86                         /*
87                          * We determine what to do based on the current state of the server
88                          */
89
90                         /*
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
95                          * the request.
96                          */
97
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);
101                     }
102
103                     switch (server.getStatus()) {
104                         case DELETED:
105                             // Nothing to do, the server is gone
106                             msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
107                                     server.getTenantId(), "started");
108                             logger.error(msg);
109                             //                                  metricsLogger.error(msg);
110                             throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
111
112                         case RUNNING:
113                             // Nothing to do, the server is already running
114                             logger.info("Server was already running");
115                             break;
116
117                         case ERROR:
118                             // Server is in error state
119                             msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
120                                     server.getTenantId(), "start");
121                             logger.error(msg);
122                             //                                  metricsLogger.error(msg);
123                             throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
124
125                         case READY:
126                             // Server is stopped attempt to start the server
127                             rc.reset();
128                             startServer(rc, server);
129                             break;
130
131                         case PAUSED:
132                             // if paused, un-pause it
133                             rc.reset();
134                             unpauseServer(rc, server);
135                             //                                  metricsLogger.info("Server status: PAUSED");
136                             break;
137
138                         case SUSPENDED:
139                             // Attempt to resume the suspended server
140                             rc.reset();
141                             resumeServer(rc, server);
142                             //                                  metricsLogger.info("Server status: SUSPENDED");
143                             break;
144
145                         default:
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);
150                             logger.error(msg);
151                             //                                  metricsLogger.error(msg);
152                             throw new RequestFailedException("Start Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
153                     }
154                     context.close();
155                     doSuccess(rc);
156                     ctx.setAttribute("START_STATUS", "SUCCESS");
157                 }
158                 else
159                 {
160                     ctx.setAttribute("START_STATUS", "CONTEXT_NOT_FOUND");
161                 }
162             } catch (ResourceNotFoundException e) {
163                 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
164                 logger.error(msg);
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);
171             }
172         } catch (RequestFailedException e) {
173             doFailure(rc, e.getStatus(), e.getMessage());
174         }
175
176         return server;
177     }
178
179     @Override
180     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
181
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);
185     }
186 }