Second part of onap rename
[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 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.onap.appc.adapter.iaas.provider.operation.impl;
26
27 import org.onap.appc.Constants;
28 import org.onap.appc.adapter.iaas.ProviderAdapter;
29 import org.onap.appc.adapter.iaas.impl.IdentityURL;
30 import org.onap.appc.adapter.iaas.impl.RequestContext;
31 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
32 import org.onap.appc.adapter.iaas.impl.VMURL;
33 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Outcome;
34 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
35 import org.onap.appc.exceptions.APPCException;
36 import org.onap.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.onap.ccsdk.sli.core.sli.SvcLogicContext;
45 import org.glassfish.grizzly.http.util.HttpStatus;
46 import java.util.Map;
47 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.STOP_SERVICE;
48 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
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.onap.appc.adapter.iaas.ProviderAdapter#stopServer(java.util.Map,
57      *      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))
76                 return null;
77
78             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
79             String identStr = (ident == null) ? null : ident.toString();
80
81             Context context = null;
82             ctx.setAttribute("STOP_STATUS", "ERROR");
83             try {
84                 context = getContext(rc, vm_url, identStr);
85                 if (context != null) {
86                     rc.reset();
87                     server = lookupServer(rc, context, vm.getServerId());
88                     logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
89
90                     String msg;
91                     /*
92                      * We determine what to do based on the current state of the server
93                      */
94
95                     /*
96                      * Pending is a bit of a special case. If we find the server is in a pending state, then the
97                      * provider is in the process of changing state of the server. So, lets try to wait a little bit and
98                      * see if the state settles down to one we can deal with. If not, then we have to fail 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,
115                                     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,
132                                     server);
133
134                         case READY:
135                             // Nothing to do, the server was already stopped
136                             logger.info("Server was already stopped");
137                             break;
138
139                         case PAUSED:
140                             // if paused, un-pause it and then stop it
141                             rc.reset();
142                             unpauseServer(rc, server);
143                             rc.reset();
144                             stopServer(rc, server);
145                             generateEvent(rc, true, Outcome.SUCCESS.toString());
146                             break;
147
148                         case SUSPENDED:
149                             // Attempt to resume the suspended server and after that stop it
150                             rc.reset();
151                             resumeServer(rc, server);
152                             rc.reset();
153                             stopServer(rc, server);
154                             generateEvent(rc, true, Outcome.SUCCESS.toString());
155                             break;
156
157                         default:
158                             // Hmmm, unknown status, should never occur
159                             msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
160                                     server.getTenantId(), server.getStatus().name());
161                             generateEvent(rc, false, msg);
162                             logger.error(msg);
163                             metricsLogger.error(msg);
164                             throw new RequestFailedException("Stop Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405,
165                                     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.onap.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 (Exception e1) {
181                 String msg =
182                         EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
183                                 STOP_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
184                 logger.error(msg, e1);
185                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
186             }
187         } catch (RequestFailedException e) {
188             logger.error(EELFResourceManager.format(Msg.STOP_SERVER_FAILED, appName, "n/a", "n/a", e.getMessage()));
189             doFailure(rc, e.getStatus(), e.getMessage());
190         }
191
192         return server;
193     }
194
195     @Override
196     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
197             throws APPCException {
198         setMDC(STOP_SERVICE.toString(), "App-C IaaS Adapter:Stop", ADAPTER_NAME);
199         logOperation(Msg.STOPPING_SERVER, params, context);
200         return stopServer(params, context);
201     }
202 }