d5be609b76b64cff83e568e1770539958695afb0
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / provider / operation / impl / RebuildServer.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 com.att.cdp.exceptions.ContextConnectionException;
27 import com.att.cdp.exceptions.ResourceNotFoundException;
28 import com.att.cdp.exceptions.ZoneException;
29 import com.att.cdp.zones.ComputeService;
30 import com.att.cdp.zones.Context;
31 import com.att.cdp.zones.ImageService;
32 import com.att.cdp.zones.Provider;
33 import com.att.cdp.zones.model.Image;
34 import com.att.cdp.zones.model.ModelObject;
35 import com.att.cdp.zones.model.Server;
36 import com.att.cdp.zones.model.ServerBootSource;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39 import com.att.eelf.i18n.EELFResourceManager;
40 import org.glassfish.grizzly.http.util.HttpStatus;
41 import org.onap.appc.Constants;
42 import org.onap.appc.adapter.iaas.ProviderAdapter;
43 import org.onap.appc.adapter.iaas.impl.IdentityURL;
44 import org.onap.appc.adapter.iaas.impl.RequestContext;
45 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
46 import org.onap.appc.adapter.iaas.impl.VMURL;
47 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
48 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
49 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Outcome;
50 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
51 import org.onap.appc.configuration.Configuration;
52 import org.onap.appc.configuration.ConfigurationFactory;
53 import org.onap.appc.exceptions.APPCException;
54 import org.onap.appc.i18n.Msg;
55 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
56 import org.slf4j.MDC;
57 import java.text.DateFormat;
58 import java.text.SimpleDateFormat;
59 import java.util.Date;
60 import java.util.List;
61 import java.util.Map;
62 import java.util.TimeZone;
63 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.STOP_SERVICE;
64 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
65
66 public class RebuildServer extends ProviderServerOperation {
67
68     private static final EELFLogger logger = EELFManager.getInstance().getLogger(RebuildServer.class);
69     private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
70     private static final Configuration configuration = ConfigurationFactory.getConfiguration();
71     //the sleep time used by thread.sleep to give "some time for OpenStack to start processing the request"
72     private long rebuildSleepTime = 10L * 1000L;
73
74     /**
75      * Rebuild the indicated server with the indicated image. This method assumes the server has been determined to be
76      * in the correct state to do the rebuild.
77      *
78      * @param rc The request context that manages the state and recovery of the request for the life of its processing.
79      * @param server the server to be rebuilt
80      * @param image The image to be used (or snapshot)
81      * @throws RequestFailedException if the server does not change state in the allotted time
82      */
83     @SuppressWarnings("nls")
84     private void rebuildServer(RequestContext rc, Server server, String image) throws RequestFailedException {
85         logger.debug(Msg.REBUILD_SERVER, server.getId());
86
87         String msg;
88         Context context = server.getContext();
89         Provider provider = context.getProvider();
90         ComputeService service = context.getComputeService();
91
92         /*
93          * Set Time for Metrics Logger
94          */
95         setTimeForMetricsLogger();
96
97         try {
98             while (rc.attempt()) {
99                 try {
100                     server.rebuild(image);
101                     break;
102                 } catch (ContextConnectionException e) {
103                     msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, provider.getName(), service.getURL(),
104                             context.getTenant().getName(), context.getTenant().getId(), e.getMessage(),
105                             Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
106                             Integer.toString(rc.getRetryLimit()));
107                     logger.error(msg, e);
108                     metricsLogger.error(msg, e);
109                     rc.delay();
110                 }
111             }
112
113             /*
114              * We need to provide some time for OpenStack to start processing the request.
115              */
116             try {
117                 Thread.sleep(rebuildSleepTime);
118             } catch (InterruptedException e) {
119                 logger.trace("Sleep threw interrupted exception, should never occur");
120                 metricsLogger.trace("Sleep threw interrupted exception, should never occur");
121             }
122         } catch (ZoneException e) {
123             msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
124                     e.getMessage());
125             logger.error(msg);
126             metricsLogger.error(msg);
127             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
128         }
129
130         rc.reset();
131         /*
132          * Once we have started the process, now we wait for the final state of stopped. This should be the final state
133          * (since we started the rebuild with the server stopped).
134          */
135         waitForStateChange(rc, server, Server.Status.READY);
136
137         if (rc.isFailed()) {
138             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
139             logger.error(msg);
140             metricsLogger.error(msg);
141             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
142         }
143         rc.reset();
144     }
145
146     /**
147      * This method is called to rebuild the provided server.
148      * <p>
149      * If the server was booted from a volume, then the request is failed immediately and no action is taken. Rebuilding
150      * a VM from a bootable volume, where the bootable volume itself is not rebuilt, serves no purpose.
151      * </p>
152      *
153      * @param rc The request context that manages the state and recovery of the request for the life of its processing.
154      * @param server The server to be rebuilt
155      * @throws ZoneException When error occurs
156      * @throws RequestFailedException When server status is error
157      */
158     @SuppressWarnings("nls")
159     private void rebuildServer(RequestContext rc, Server server, SvcLogicContext ctx)
160             throws ZoneException, RequestFailedException {
161         ServerBootSource builtFrom = server.getBootSource();
162
163         /*
164          * Set Time for Metrics Logger
165          */
166         setTimeForMetricsLogger();
167
168         String msg;
169         // Throw exception for non image/snap boot source
170         if (ServerBootSource.VOLUME.equals(builtFrom)) {
171             msg = String.format("Rebuilding is currently not supported for servers built from bootable volumes [%s]",
172                     server.getId());
173             generateEvent(rc, false, msg);
174             logger.error(msg);
175             metricsLogger.error(msg);
176             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.FORBIDDEN_403, server);
177         }
178
179         /*
180          * Pending is a bit of a special case. If we find the server is in a pending state, then the provider is in the
181          * process of changing state of the server. So, lets try to wait a little bit and see if the state settles down
182          * to one we can deal with. If not, then we have to fail the request.
183          */
184         Context context = server.getContext();
185         Provider provider = context.getProvider();
186         ComputeService service = context.getComputeService();
187         if (server.getStatus().equals(Server.Status.PENDING)) {
188             rc.reset();
189             waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
190                     Server.Status.SUSPENDED, Server.Status.PAUSED);
191         }
192
193         // Is the skip Hypervisor check attribute populated?
194         String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
195         if (skipHypervisorCheck == null && ctx != null) {
196             skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
197         }
198
199         // Always perform Hypervisor Status checks
200         // unless the skip is set to true
201         if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
202             // Check of the Hypervisor for the VM Server is UP and reachable
203             checkHypervisor(server);
204         }
205
206         /*
207          * Get the image to use. This is determined by the presence or absence of snapshot images. If any snapshots
208          * exist, then the latest snapshot is used, otherwise the image used to construct the VM is used.
209          */
210         List<Image> snapshots = server.getSnapshots();
211         String imageToUse;
212         if (snapshots != null && !snapshots.isEmpty()) {
213             imageToUse = snapshots.get(0).getId();
214         } else {
215             imageToUse = server.getImage();
216             ImageService imageService = server.getContext().getImageService();
217             rc.reset();
218             try {
219                 while (rc.attempt()) {
220                     try {
221                         /*
222                          * We are just trying to make sure that the image exists. We arent interested in the details at
223                          * this point.
224                          */
225                         imageService.getImage(imageToUse);
226                         break;
227                     } catch (ContextConnectionException e) {
228                         msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, provider.getName(),
229                                 imageService.getURL(), context.getTenant().getName(), context.getTenant().getId(),
230                                 e.getMessage(), Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
231                                 Integer.toString(rc.getRetryLimit()));
232                         logger.error(msg, e);
233                         metricsLogger.error(msg);
234                         rc.delay();
235                     }
236                 }
237             } catch (ZoneException e) {
238                 msg = EELFResourceManager.format(Msg.IMAGE_NOT_FOUND, imageToUse, "rebuild");
239                 generateEvent(rc, false, msg);
240                 logger.error(msg);
241                 metricsLogger.error(msg);
242                 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
243             }
244         }
245         if (rc.isFailed()) {
246             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
247             logger.error(msg);
248             metricsLogger.error(msg);
249             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
250         }
251         rc.reset();
252
253         /*
254          * We determine what to do based on the current state of the server
255          */
256         switch (server.getStatus()) {
257             case DELETED:
258                 // Nothing to do, the server is gone
259                 msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
260                         server.getTenantId(), "rebuilt");
261                 generateEvent(rc, false, msg);
262                 logger.error(msg);
263                 metricsLogger.error(msg);
264                 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
265
266             case RUNNING:
267                 // Attempt to stop the server, then rebuild it
268                 stopServer(rc, server);
269                 rc.reset();
270                 rebuildServer(rc, server, imageToUse);
271                 rc.reset();
272                 startServer(rc, server);
273                 generateEvent(rc, true, Outcome.SUCCESS.toString());
274                 metricsLogger.info("Server status: RUNNING");
275                 break;
276
277             case ERROR:
278                 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
279                         server.getTenantId(), "rebuild");
280                 generateEvent(rc, false, msg);
281                 logger.error(msg);
282                 metricsLogger.error(msg);
283                 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
284
285             case READY:
286                 // Attempt to rebuild the server
287                 rebuildServer(rc, server, imageToUse);
288                 rc.reset();
289                 startServer(rc, server);
290                 generateEvent(rc, true, Outcome.SUCCESS.toString());
291                 metricsLogger.info("Server status: READY");
292                 break;
293
294             case PAUSED:
295                 // if paused, un-pause it, stop it, and rebuild it
296                 unpauseServer(rc, server);
297                 rc.reset();
298                 stopServer(rc, server);
299                 rc.reset();
300                 rebuildServer(rc, server, imageToUse);
301                 rc.reset();
302                 startServer(rc, server);
303                 generateEvent(rc, true, Outcome.SUCCESS.toString());
304                 metricsLogger.info("Server status: PAUSED");
305                 break;
306
307             case SUSPENDED:
308                 // Attempt to resume the suspended server, stop it, and rebuild it
309                 resumeServer(rc, server);
310                 rc.reset();
311                 stopServer(rc, server);
312                 rc.reset();
313                 rebuildServer(rc, server, imageToUse);
314                 rc.reset();
315                 startServer(rc, server);
316                 generateEvent(rc, true, Outcome.SUCCESS.toString());
317                 metricsLogger.info("Server status: SUSPENDED");
318                 break;
319
320             default:
321                 // Hmmm, unknown status, should never occur
322                 msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
323                         server.getTenantId(), server.getStatus().name());
324                 generateEvent(rc, false, msg);
325                 logger.error(msg);
326                 metricsLogger.error(msg);
327                 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
328         }
329     }
330
331     /**
332      * @see org.onap.appc.adapter.iaas.ProviderAdapter#rebuildServer(java.util.Map,
333      *      org.onap.ccsdk.sli.core.sli.SvcLogicContext)
334      */
335     @SuppressWarnings("nls")
336     public Server rebuildServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
337         Server server = null;
338         RequestContext rc = new RequestContext(ctx);
339         rc.isAlive();
340
341         setTimeForMetricsLogger();
342
343         String msg;
344         try {
345             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
346                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
347
348             String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
349             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
350             VMURL vm = VMURL.parseURL(vm_url);
351             if (validateVM(rc, appName, vm_url, vm))
352                 return null;
353
354             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
355             String identStr = (ident == null) ? null : ident.toString();
356             ctx.setAttribute("REBUILD_STATUS", "ERROR");
357
358             Context context = null;
359             String tenantName = "Unknown";//to be used also in case of exception
360             try {
361                 context = getContext(rc, vm_url, identStr);
362                 if (context != null) {
363                     tenantName = context.getTenantName();//this varaible also is used in case of exception
364                     rc.reset();
365                     server = lookupServer(rc, context, vm.getServerId());
366                     logger.debug(Msg.SERVER_FOUND, vm_url, tenantName, server.getStatus().toString());
367
368                     // Manually checking image service until new PAL release
369                     if (hasImageAccess(rc, context)) {
370                         rebuildServer(rc, server, ctx);
371                         doSuccess(rc);
372                         ctx.setAttribute("REBUILD_STATUS", "SUCCESS");
373                     } else {
374                         msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
375                                 "Accessing Image Service Failed");
376                         logger.error(msg);
377                         metricsLogger.error(msg);
378                         doFailure(rc, HttpStatus.FORBIDDEN_403, msg);
379                     }
380                     context.close();
381                 } else {
382                     ctx.setAttribute("REBUILD_STATUS", "CONTEXT_NOT_FOUND");
383                 }
384             } catch (RequestFailedException e) {
385                 doFailure(rc, e.getStatus(), e.getMessage());
386                 ctx.setAttribute("REBUILD_STATUS", "ERROR");
387             } catch (ResourceNotFoundException e) {
388                 msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
389                 ctx.setAttribute("REBUILD_STATUS", "ERROR");
390                 logger.error(msg);
391                 metricsLogger.error(msg);
392                 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
393             } catch (Exception e1) {
394                 msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
395                         STOP_SERVICE.toString(), vm_url, tenantName);
396                 ctx.setAttribute("REBUILD_STATUS", "ERROR");
397                 logger.error(msg, e1);
398                 metricsLogger.error(msg);
399                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
400             }
401         } catch (RequestFailedException e) {
402             doFailure(rc, e.getStatus(), e.getMessage());
403             ctx.setAttribute("REBUILD_STATUS", "ERROR");
404         }
405
406         return server;
407     }
408
409     @Override
410     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
411             throws APPCException {
412         setMDC(Operation.REBUILD_SERVICE.toString(), "App-C IaaS Adapter:Rebuild", ADAPTER_NAME);
413         logOperation(Msg.REBUILDING_SERVER, params, context);
414
415         setTimeForMetricsLogger();
416
417         metricsLogger.info("Executing Provider Operation: Rebuild");
418
419         return rebuildServer(params, context);
420     }
421
422     private void setTimeForMetricsLogger() {
423         long startTime = System.currentTimeMillis();
424         TimeZone tz = TimeZone.getTimeZone("UTC");
425         DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
426         df.setTimeZone(tz);
427         long endTime = System.currentTimeMillis();
428         long duration = endTime - startTime;
429         String durationStr = String.valueOf(duration);
430         String endTimeStrUTC = df.format(new Date());
431         MDC.put("EndTimestamp", endTimeStrUTC);
432         MDC.put("ElapsedTime", durationStr);
433         MDC.put("TargetEntity", "cdp");
434         MDC.put("TargetServiceName", "rebuild server");
435         MDC.put("ClassName", "org.onap.appc.adapter.iaas.provider.operation.impl.RebuildServer");
436     }
437     
438     /**
439      * Sets the sleep time used by thread.sleep to give
440      * "some time for OpenStack to start processing the request".
441      *
442      * @param millis Time to sleep in milliseconds
443      */
444     public void setRebuildSleepTime(long millis){
445         this.rebuildSleepTime = millis;
446     }
447 }