933b5d8b46094c3a0e9d6a93677a81fe15ed6257
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / provider / operation / impl / StopServer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.Outcome;
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.STOP_SERVICE;
47 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
48
49 public class StopServer extends ProviderServerOperation {
50
51     private static final EELFLogger logger = EELFManager.getInstance().getLogger(StopServer.class);
52     private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
53
54     /**
55      * @see org.onap.appc.adapter.iaas.ProviderAdapter#stopServer(java.util.Map,
56      *      org.onap.ccsdk.sli.core.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))
75                 return null;
76
77             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
78             String identStr = (ident == null) ? null : ident.toString();
79
80             Context context = null;
81             String tenantName = "Unknown";//to be used also in case of exception
82             ctx.setAttribute("STOP_STATUS", "ERROR");
83             try {
84                 context = getContext(rc, vm_url, identStr);
85                 if (context != null) {
86                     tenantName = context.getTenantName();//this varaible also is used in case of exception
87                     rc.reset();
88                     server = lookupServer(rc, context, vm.getServerId());
89                     logger.debug(Msg.SERVER_FOUND, vm_url, tenantName, server.getStatus().toString());
90
91                     String msg;
92                     /*
93                      * We determine what to do based on the current state of the server
94                      */
95
96                     /*
97                      * Pending is a bit of a special case. If we find the server is in a pending state, then the
98                      * provider is in the process of changing state of the server. So, lets try to wait a little bit and
99                      * see if the state settles down to one we can deal with. If not, then we have to fail the request.
100                      */
101
102                     if (server.getStatus().equals(Server.Status.PENDING)) {
103                         waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
104                                 Server.Status.SUSPENDED, Server.Status.PAUSED, Server.Status.DELETED);
105                     }
106
107                     switch (server.getStatus()) {
108                         case DELETED:
109                             // Nothing to do, the server is gone
110                             msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
111                                     server.getTenantId(), "stopped");
112                             generateEvent(rc, false, msg);
113                             logger.error(msg);
114                             metricsLogger.error(msg);
115                             throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405,
116                                     server);
117
118                         case RUNNING:
119                             // Attempt to stop the server
120                             rc.reset();
121                             stopServer(rc, server);
122                             generateEvent(rc, true, Outcome.SUCCESS.toString());
123                             break;
124
125                         case ERROR:
126                             // Server is in error state
127                             msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
128                                     server.getTenantId(), "stop");
129                             generateEvent(rc, false, msg);
130                             logger.error(msg);
131                             metricsLogger.error(msg);
132                             throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405,
133                                     server);
134
135                         case READY:
136                             // Nothing to do, the server was already stopped
137                             logger.info("Server was already stopped");
138                             break;
139
140                         case PAUSED:
141                             // if paused, un-pause it and then stop it
142                             rc.reset();
143                             unpauseServer(rc, server);
144                             rc.reset();
145                             stopServer(rc, server);
146                             generateEvent(rc, true, Outcome.SUCCESS.toString());
147                             break;
148
149                         case SUSPENDED:
150                             // Attempt to resume the suspended server and after that stop it
151                             rc.reset();
152                             resumeServer(rc, server);
153                             rc.reset();
154                             stopServer(rc, server);
155                             generateEvent(rc, true, Outcome.SUCCESS.toString());
156                             break;
157
158                         default:
159                             // Hmmm, unknown status, should never occur
160                             msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
161                                     server.getTenantId(), server.getStatus().name());
162                             generateEvent(rc, false, msg);
163                             logger.error(msg);
164                             metricsLogger.error(msg);
165                             throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405,
166                                     server);
167                     }
168                     context.close();
169                     doSuccess(rc);
170                     ctx.setAttribute("STOP_STATUS", "SUCCESS");
171                     msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "StopServer", vm_url);
172                     ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
173
174                 } else {
175                     ctx.setAttribute("STOP_STATUS", "CONTEXT_NOT_FOUND");
176                 }
177             } catch (ResourceNotFoundException e) {
178                 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
179                 logger.error(msg);
180                 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
181             } catch (Exception e1) {
182                 String msg =
183                         EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
184                                 STOP_SERVICE.toString(), vm_url, tenantName);
185                 logger.error(msg, e1);
186                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
187             }
188         } catch (RequestFailedException e) {
189             logger.error(EELFResourceManager.format(Msg.STOP_SERVER_FAILED, appName, "n/a", "n/a", e.getMessage()));
190             doFailure(rc, e.getStatus(), e.getMessage());
191         }
192
193         return server;
194     }
195
196     @Override
197     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
198             throws APPCException {
199         setMDC(STOP_SERVICE.toString(), "App-C IaaS Adapter:Stop", ADAPTER_NAME);
200         logOperation(Msg.STOPPING_SERVER, params, context);
201         return stopServer(params, context);
202     }
203 }