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 / StopServer.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.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;
43
44 import java.util.Map;
45
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;
48
49
50 public class StopServer extends ProviderServerOperation {
51
52     private static final EELFLogger logger = EELFManager.getInstance().getLogger(StopServer.class);
53     private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
54
55     /**
56      * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#stopServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
57      */
58     @SuppressWarnings("nls")
59     public Server stopServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
60         Server server = null;
61         RequestContext rc = new RequestContext(ctx);
62         rc.isAlive();
63
64         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
65
66         try {
67             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
68                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
69
70             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
71             ctx.setAttribute("STOP_STATUS", "SUCCESS");
72
73             VMURL vm = VMURL.parseURL(vm_url);
74             if (validateVM(rc, appName, vm_url, vm)) return null;
75
76             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
77             String identStr = (ident == null) ? null : ident.toString();
78
79             Context context = null;
80             ctx.setAttribute("STOP_STATUS", "ERROR");
81             try {
82                 context = getContext(rc, vm_url, identStr);
83                 if (context != null) {
84                     rc.reset();
85                     server = lookupServer(rc, context, vm.getServerId());
86                     logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
87                     
88                     String msg;
89                     /*
90                          * We determine what to do based on the current state of the server
91                          */
92
93                         /*
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
98                          * the request.
99                          */
100
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);
104                     }
105
106                     switch (server.getStatus()) {
107                         case DELETED:
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);
112                             logger.error(msg);
113                             metricsLogger.error(msg);
114                             throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
115
116                         case RUNNING:
117                             // Attempt to stop the server
118                             rc.reset();
119                             stopServer(rc, server);
120                             generateEvent(rc, true, Outcome.SUCCESS.toString());
121                             break;
122
123                         case ERROR:
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);
128                             logger.error(msg);
129                             metricsLogger.error(msg);
130                             throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
131
132                         case READY:
133                             // Nothing to do, the server was already stopped
134                             logger.info("Server was already stopped");
135                             break;
136
137                         case PAUSED:
138                             // if paused, un-pause it and then stop it
139                             rc.reset();
140                             unpauseServer(rc, server);
141                             rc.reset();
142                             stopServer(rc, server);
143                             generateEvent(rc, true, Outcome.SUCCESS.toString());
144                             break;
145
146                         case SUSPENDED:
147                             // Attempt to resume the suspended server and after that stop it
148                             rc.reset();
149                             resumeServer(rc, server);
150                             rc.reset();
151                             stopServer(rc, server);
152                             generateEvent(rc, true, Outcome.SUCCESS.toString());
153                             break;
154
155                         default:
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);
160                             logger.error(msg);
161                             metricsLogger.error(msg);
162                             throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
163                     }
164                     context.close();
165                     doSuccess(rc);
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);
169
170                 }else{
171                     ctx.setAttribute("STOP_STATUS", "CONTEXT_NOT_FOUND");
172                 }
173             } catch (ResourceNotFoundException e) {
174                 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
175                 logger.error(msg);
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);
182             }
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());
186         }
187
188         return server;
189     }
190
191     @Override
192     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
193
194         setMDC(STOP_SERVICE.toString(), "App-C IaaS Adapter:Stop", ADAPTER_NAME);
195         logOperation(Msg.STOPPING_SERVER, params, context);
196         return stopServer(params, context);
197     }
198 }