Applying license changes to all files
[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  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.adapter.iaas.provider.operation.impl;
26
27 import org.openecomp.appc.Constants;
28 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
29 import org.openecomp.appc.adapter.iaas.impl.IdentityURL;
30 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
31 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
32 import org.openecomp.appc.adapter.iaas.impl.VMURL;
33 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Outcome;
34 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
35 import org.openecomp.appc.exceptions.APPCException;
36 import org.openecomp.appc.i18n.Msg;
37 import com.att.cdp.exceptions.ResourceNotFoundException;
38 import com.att.cdp.zones.Context;
39 import com.att.cdp.zones.model.ModelObject;
40 import com.att.cdp.zones.model.Server;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import com.att.eelf.i18n.EELFResourceManager;
44 import org.openecomp.sdnc.sli.SvcLogicContext;
45 import org.glassfish.grizzly.http.util.HttpStatus;
46
47 import java.util.Map;
48
49 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.STOP_SERVICE;
50 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
51
52
53 public class StopServer extends ProviderServerOperation {
54
55     private static final EELFLogger logger = EELFManager.getInstance().getLogger(StopServer.class);
56     private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
57
58     /**
59      * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#stopServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
60      */
61     @SuppressWarnings("nls")
62     public Server stopServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
63         Server server = null;
64         RequestContext rc = new RequestContext(ctx);
65         rc.isAlive();
66
67         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
68
69         try {
70             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
71                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
72
73             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
74             ctx.setAttribute("STOP_STATUS", "SUCCESS");
75
76             VMURL vm = VMURL.parseURL(vm_url);
77             if (validateVM(rc, appName, vm_url, vm)) return null;
78
79             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
80             String identStr = (ident == null) ? null : ident.toString();
81
82             Context context = null;
83             ctx.setAttribute("STOP_STATUS", "ERROR");
84             try {
85                 context = getContext(rc, vm_url, identStr);
86                 if (context != null) {
87                     rc.reset();
88                     server = lookupServer(rc, context, vm.getServerId());
89                     logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), 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
98                          * pending state, then the provider is in the process of changing state
99                          * of the server. So, lets try to wait a little bit and see if the state
100                          * settles down to one we can deal with. If not, then we have to fail
101                          * 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
109                     switch (server.getStatus()) {
110                         case DELETED:
111                             // Nothing to do, the server is gone
112                             msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
113                                     server.getTenantId(), "stopped");
114                             generateEvent(rc, false, msg);
115                             logger.error(msg);
116                             metricsLogger.error(msg);
117                             throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
118
119                         case RUNNING:
120                             // Attempt to stop the server
121                             rc.reset();
122                             stopServer(rc, server);
123                             generateEvent(rc, true, Outcome.SUCCESS.toString());
124                             break;
125
126                         case ERROR:
127                             // Server is in error state
128                             msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
129                                     server.getTenantId(), "stop");
130                             generateEvent(rc, false, msg);
131                             logger.error(msg);
132                             metricsLogger.error(msg);
133                             throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, 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, server);
166                     }
167                     context.close();
168                     doSuccess(rc);
169                     ctx.setAttribute("STOP_STATUS", "SUCCESS");
170                     msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "StopServer", vm_url);
171                     ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
172
173                 }else{
174                     ctx.setAttribute("STOP_STATUS", "CONTEXT_NOT_FOUND");
175                 }
176             } catch (ResourceNotFoundException e) {
177                 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
178                 logger.error(msg);
179                 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
180             } catch (Throwable t) {
181                 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
182                         STOP_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
183                 logger.error(msg, t);
184                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
185             }
186         } catch (RequestFailedException e) {
187             logger.error(EELFResourceManager.format(Msg.STOP_SERVER_FAILED, appName, "n/a", "n/a", e.getMessage()));
188             doFailure(rc, e.getStatus(), e.getMessage());
189         }
190
191         return server;
192     }
193
194     @Override
195     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
196
197         setMDC(STOP_SERVICE.toString(), "App-C IaaS Adapter:Stop", ADAPTER_NAME);
198         logOperation(Msg.STOPPING_SERVER, params, context);
199         return stopServer(params, context);
200     }
201 }