501cdc1f0a95175c33f7594054f416b8cd0984a5
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.adapter.iaas.provider.operation.impl;
24
25 import org.openecomp.appc.Constants;
26 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
27 import org.openecomp.appc.adapter.iaas.impl.IdentityURL;
28 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
29 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
30 import org.openecomp.appc.adapter.iaas.impl.VMURL;
31 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Outcome;
32 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
33 import org.openecomp.appc.exceptions.APPCException;
34 import org.openecomp.appc.i18n.Msg;
35 import com.att.cdp.exceptions.ResourceNotFoundException;
36 import com.att.cdp.zones.Context;
37 import com.att.cdp.zones.model.ModelObject;
38 import com.att.cdp.zones.model.Server;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.att.eelf.i18n.EELFResourceManager;
42 import org.openecomp.sdnc.sli.SvcLogicContext;
43 import org.glassfish.grizzly.http.util.HttpStatus;
44
45 import java.util.Map;
46
47 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.STOP_SERVICE;
48 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
49
50
51 public class StopServer extends ProviderServerOperation {
52
53     private static final EELFLogger logger = EELFManager.getInstance().getLogger(StopServer.class);
54     private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
55
56     /**
57      * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#stopServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
58      */
59     @SuppressWarnings("nls")
60     public Server stopServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
61         Server server = null;
62         RequestContext rc = new RequestContext(ctx);
63         rc.isAlive();
64
65         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
66
67         try {
68             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
69                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
70
71             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
72             ctx.setAttribute("STOP_STATUS", "SUCCESS");
73
74             VMURL vm = VMURL.parseURL(vm_url);
75             if (validateVM(rc, appName, vm_url, vm)) 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             ctx.setAttribute("STOP_STATUS", "ERROR");
82             try {
83                 context = getContext(rc, vm_url, identStr);
84                 if (context != null) {
85                     rc.reset();
86                     server = lookupServer(rc, context, vm.getServerId());
87                     logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
88                     
89                     String msg;
90                     /*
91                          * We determine what to do based on the current state of the server
92                          */
93
94                         /*
95                          * Pending is a bit of a special case. If we find the server is in a
96                          * pending state, then the provider is in the process of changing state
97                          * of the server. So, lets try to wait a little bit and see if the state
98                          * settles down to one we can deal with. If not, then we have to fail
99                          * 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, server);
116
117                         case RUNNING:
118                             // Attempt to stop the server
119                             rc.reset();
120                             stopServer(rc, server);
121                             generateEvent(rc, true, Outcome.SUCCESS.toString());
122                             break;
123
124                         case ERROR:
125                             // Server is in error state
126                             msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
127                                     server.getTenantId(), "stop");
128                             generateEvent(rc, false, msg);
129                             logger.error(msg);
130                             metricsLogger.error(msg);
131                             throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
132
133                         case READY:
134                             // Nothing to do, the server was already stopped
135                             logger.info("Server was already stopped");
136                             break;
137
138                         case PAUSED:
139                             // if paused, un-pause it and then stop it
140                             rc.reset();
141                             unpauseServer(rc, server);
142                             rc.reset();
143                             stopServer(rc, server);
144                             generateEvent(rc, true, Outcome.SUCCESS.toString());
145                             break;
146
147                         case SUSPENDED:
148                             // Attempt to resume the suspended server and after that stop it
149                             rc.reset();
150                             resumeServer(rc, server);
151                             rc.reset();
152                             stopServer(rc, server);
153                             generateEvent(rc, true, Outcome.SUCCESS.toString());
154                             break;
155
156                         default:
157                             // Hmmm, unknown status, should never occur
158                             msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
159                                     server.getTenantId(), server.getStatus().name());
160                             generateEvent(rc, false, msg);
161                             logger.error(msg);
162                             metricsLogger.error(msg);
163                             throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
164                     }
165                     context.close();
166                     doSuccess(rc);
167                     ctx.setAttribute("STOP_STATUS", "SUCCESS");
168                     msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "StopServer", vm_url);
169                     ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
170
171                 }else{
172                     ctx.setAttribute("STOP_STATUS", "CONTEXT_NOT_FOUND");
173                 }
174             } catch (ResourceNotFoundException e) {
175                 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
176                 logger.error(msg);
177                 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
178             } catch (Throwable t) {
179                 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
180                         STOP_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
181                 logger.error(msg, t);
182                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
183             }
184         } catch (RequestFailedException e) {
185             logger.error(EELFResourceManager.format(Msg.STOP_SERVER_FAILED, appName, "n/a", "n/a", e.getMessage()));
186             doFailure(rc, e.getStatus(), e.getMessage());
187         }
188
189         return server;
190     }
191
192     @Override
193     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
194
195         setMDC(STOP_SERVICE.toString(), "App-C IaaS Adapter:Stop", ADAPTER_NAME);
196         logOperation(Msg.STOPPING_SERVER, params, context);
197         return stopServer(params, context);
198     }
199 }