added skip hpervisor check and other adapterchange
[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-2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modifications Copyright (C) 2019 IBM
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14
15 *      http://www.apache.org/licenses/LICENSE-2.0
16
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22
23 * ============LICENSE_END=========================================================
24 */
25
26 package org.onap.appc.adapter.iaas.provider.operation.impl;
27
28 import com.att.cdp.exceptions.ContextConnectionException;
29 import com.att.cdp.exceptions.ResourceNotFoundException;
30 import com.att.cdp.exceptions.ZoneException;
31 import com.att.cdp.zones.ComputeService;
32 import com.att.cdp.zones.Context;
33 import com.att.cdp.zones.ImageService;
34 import com.att.cdp.zones.Provider;
35 import com.att.cdp.zones.model.Image;
36 import com.att.cdp.zones.model.ModelObject;
37 import com.att.cdp.zones.model.Server;
38 import com.att.cdp.zones.model.ServerBootSource;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.att.eelf.i18n.EELFResourceManager;
42 import org.glassfish.grizzly.http.util.HttpStatus;
43 import org.onap.appc.Constants;
44 import com.fasterxml.jackson.databind.JsonNode;
45 import com.fasterxml.jackson.databind.ObjectMapper;
46 import org.onap.appc.adapter.iaas.ProviderAdapter;
47 import org.onap.appc.adapter.iaas.impl.IdentityURL;
48 import org.onap.appc.adapter.iaas.impl.RequestContext;
49 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
50 import org.onap.appc.adapter.iaas.impl.VMURL;
51 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
52 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
53 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Outcome;
54 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
55 import org.onap.appc.configuration.Configuration;
56 import org.onap.appc.configuration.ConfigurationFactory;
57 import org.onap.appc.exceptions.APPCException;
58 import org.onap.appc.i18n.Msg;
59 import org.onap.appc.logging.LoggingConstants;
60 import org.onap.appc.logging.LoggingUtils;
61 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
62 import org.slf4j.MDC;
63 import java.text.DateFormat;
64 import java.text.SimpleDateFormat;
65 import java.util.Date;
66 import java.util.List;
67 import java.util.Map;
68 import java.util.TimeZone;
69 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.STOP_SERVICE;
70 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
71 import com.att.cdp.exceptions.StateException;
72
73 public class RebuildServer extends ProviderServerOperation {
74
75     private static final EELFLogger logger = EELFManager.getInstance().getLogger(RebuildServer.class);
76     private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
77     private static final Configuration configuration = ConfigurationFactory.getConfiguration();
78     // the sleep time used by thread.sleep to give "some time for OpenStack to start
79     // processing the request"
80     private long rebuildSleepTime = 10L * 1000L;
81
82     /**
83      * Rebuild the indicated server with the indicated image. This method assumes
84      * the server has been determined to be in the correct state to do the rebuild.
85      *
86      * @param rc
87      *            The request context that manages the state and recovery of the
88      *            request for the life of its processing.
89      * @param server
90      *            the server to be rebuilt
91      * @param image
92      *            The image to be used (or snapshot)
93      * @throws RequestFailedException
94      *             if the server does not change state in the allotted time
95      */
96     @SuppressWarnings("nls")
97     private void rebuildServer(RequestContext rc, Server server, String image) throws RequestFailedException {
98         logger.debug(Msg.REBUILD_SERVER, server.getId());
99         String msg;
100         Context context = server.getContext();
101         Provider provider = context.getProvider();
102         ComputeService service = context.getComputeService();
103         /*
104          * Set Time for Metrics Logger
105          */
106         setTimeForMetricsLogger();
107         try {
108             while (rc.attempt()) {
109                 try {
110                     server.rebuild(image);
111                     break;
112                 } catch (ContextConnectionException e) {
113                     msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, provider.getName(), service.getURL(),
114                             context.getTenant().getName(), context.getTenant().getId(), e.getMessage(),
115                             Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
116                             Integer.toString(rc.getRetryLimit()));
117                     logger.error(msg, e);
118                     metricsLogger.error(msg, e);
119                     rc.delay();
120                 }
121             }
122             /*
123              * We need to provide some time for OpenStack to start processing the request.
124              */
125             try {
126                 Thread.sleep(rebuildSleepTime);
127             } catch (InterruptedException e) {
128                 logger.trace("Sleep threw interrupted exception, should never occur");
129                 metricsLogger.trace("Sleep threw interrupted exception, should never occur");
130                 Thread.currentThread().interrupt();
131             }
132         } catch (ZoneException e) {
133             msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
134                     e.getMessage());
135             logger.error(msg);
136             metricsLogger.error(msg);
137             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
138         }
139         rc.reset();
140         /*
141          * Once we have started the process, now we wait for the final state of stopped.
142          * This should be the final state (since we started the rebuild with the server
143          * stopped).
144          */
145         waitForStateChange(rc, server, Server.Status.READY);
146         if (rc.isFailed()) {
147             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
148             logger.error(msg);
149             metricsLogger.error(msg);
150             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
151         }
152         rc.reset();
153     }
154
155     /**
156      * This method is called to rebuild the provided server.
157      * <p>
158      * If the server was booted from a volume, then the request is failed
159      * immediately and no action is taken. Rebuilding a VM from a bootable volume,
160      * where the bootable volume itself is not rebuilt, serves no purpose.
161      * </p>
162      *
163      * @param rc
164      *            The request context that manages the state and recovery of the
165      *            request for the life of its processing.
166      * @param server
167      *            The server to be rebuilt
168      * @throws ZoneException
169      *             When error occurs
170      * @throws RequestFailedException
171      *             When server status is error
172      */
173     @SuppressWarnings("nls")
174     private void rebuildServer(RequestContext rc, Server server, SvcLogicContext ctx)
175             throws ZoneException, RequestFailedException {
176         ServerBootSource builtFrom = server.getBootSource();
177         /*
178          * Set Time for Metrics Logger
179          */
180         setTimeForMetricsLogger();
181         String msg;
182         // Throw error if boot source is unknown
183         if (ServerBootSource.UNKNOWN.equals(builtFrom)) {
184             logger.debug("Boot Source Unknown");
185             msg = String.format("Error occured when retrieving server boot source [%s]!!!", server.getId());
186             logger.error(msg);
187             generateEvent(rc, false, msg);
188             metricsLogger.error(msg);
189             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.INTERNAL_SERVER_ERROR_500, server);
190         }
191
192         // Throw exception for non image/snap boot source
193         if (ServerBootSource.VOLUME.equals(builtFrom)) {
194             logger.debug("Boot Source Not Supported built from bootable volume");
195             msg = String.format("Rebuilding is currently not supported for servers built from bootable volumes [%s]",
196                     server.getId());
197             generateEvent(rc, false, msg);
198             logger.error(msg);
199             metricsLogger.error(msg);
200             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.FORBIDDEN_403, server);
201         }
202         /*
203          * Pending is a bit of a special case. If we find the server is in a pending
204          * state, then the provider is in the process of changing state of the server.
205          * So, lets try to wait a little bit and see if the state settles down to one we
206          * can deal with. If not, then we have to fail the request.
207          */
208         Context context = server.getContext();
209         Provider provider = context.getProvider();
210         ComputeService service = context.getComputeService();
211         if (server.getStatus().equals(Server.Status.PENDING)) {
212             rc.reset();
213             waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
214                     Server.Status.SUSPENDED, Server.Status.PAUSED);
215         }
216         // Is the skip Hypervisor check attribute populated?
217         String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
218         if (skipHypervisorCheck == null && ctx != null) {
219             skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
220         }
221         // Always perform Hypervisor Status checks
222         // unless the skip is set to true
223         if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
224             // Check of the Hypervisor for the VM Server is UP and reachable
225             checkHypervisor(server);
226         }
227         /*
228          * Get the image to use in this priority order: (1) If snapshot-id provided in
229          * the request, use this (2) If any snapshots exist, then the latest snapshot is
230          * used (3) Otherwise the image used to construct the VM is used.
231          */
232         String imageToUse = "";
233         try {
234             ObjectMapper mapper = new ObjectMapper();
235             String payloadStr = configuration.getProperty(Property.PAYLOAD);
236             if (payloadStr == null || payloadStr.isEmpty()) {
237                 payloadStr = ctx.getAttribute(ProviderAdapter.PAYLOAD);
238             }
239             JsonNode payloadNode = mapper.readTree(payloadStr);
240             imageToUse = payloadNode.get(ProviderAdapter.PROPERTY_REQUEST_SNAPSHOT_ID).textValue();
241             logger.debug("Pulled snapshot-id " + imageToUse + " from the payload");
242         } catch (Exception e) {
243             logger.debug("Exception attempting to pull snapshot-id from the payload: " + e.toString());
244         }
245         List<Image> snapshots = server.getSnapshots();
246         ImageService imageService = server.getContext().getImageService();
247         List<Image> imageList = imageService.listImages();
248         if (!imageToUse.isEmpty()) {
249             logger.debug("Using snapshot-id " + imageToUse + " for the rebuild request");
250             boolean imgFound = validateSnapshotId(imageToUse, snapshots, imageList);
251
252             if (!imgFound) {
253                 logger.debug("Image Snapshot Not Found");
254                 msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
255                         "Invalid Snapshot-Id");
256                 logger.error(msg);
257                 metricsLogger.error(msg);
258                 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.FORBIDDEN_403, server);
259             }
260         } else if (snapshots != null && !snapshots.isEmpty()) {
261             logger.debug("Using snapshot-id when image is Empty" + imageToUse + " for the rebuild request");
262             imageToUse = snapshots.get(0).getId();
263         } else {
264             imageToUse = server.getImage();
265             rc.reset();
266             try {
267                 while (rc.attempt()) {
268                     try {
269                         /*
270                          * We are just trying to make sure that the image exists. We arent interested in
271                          * the details at this point.
272                          */
273                         imageService.getImage(imageToUse);
274                         break;
275                     } catch (ContextConnectionException e) {
276                         msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, provider.getName(),
277                                 imageService.getURL(), context.getTenant().getName(), context.getTenant().getId(),
278                                 e.getMessage(), Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
279                                 Integer.toString(rc.getRetryLimit()));
280                         logger.error(msg, e);
281                         metricsLogger.error(msg);
282                         rc.delay();
283                     }
284                 }
285             } catch (ZoneException e) {
286                 msg = EELFResourceManager.format(Msg.IMAGE_NOT_FOUND, imageToUse, "rebuild");
287                 generateEvent(rc, false, msg);
288                 logger.error(msg);
289                 metricsLogger.error(msg);
290                 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
291             }
292         }
293         if (rc.isFailed()) {
294             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
295             logger.error(msg);
296             metricsLogger.error(msg);
297             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
298         }
299         rc.reset();
300         /*
301          * We determine what to do based on the current state of the server
302          */
303         switch (server.getStatus()) {
304         case DELETED:
305             // Nothing to do, the server is gone
306             msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(), server.getTenantId(),
307                     "rebuilt");
308             generateEvent(rc, false, msg);
309             logger.error(msg);
310             metricsLogger.error(msg);
311             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
312         case RUNNING:
313             // Attempt to stop the server, then rebuild it
314             stopServer(rc, server);
315             rc.reset();
316             rebuildServer(rc, server, imageToUse);
317             rc.reset();
318             startServer(rc, server);
319             generateEvent(rc, true, Outcome.SUCCESS.toString());
320             metricsLogger.info("Server status: RUNNING");
321             break;
322         case ERROR:
323             msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
324                     server.getTenantId(), "rebuild");
325             generateEvent(rc, false, msg);
326             logger.error(msg);
327             metricsLogger.error(msg);
328             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
329         case READY:
330             // Attempt to rebuild the server
331             rebuildServer(rc, server, imageToUse);
332             rc.reset();
333             startServer(rc, server);
334             generateEvent(rc, true, Outcome.SUCCESS.toString());
335             metricsLogger.info("Server status: READY");
336             break;
337         case PAUSED:
338             // if paused, un-pause it, stop it, and rebuild it
339             unpauseServer(rc, server);
340             rc.reset();
341             stopServer(rc, server);
342             rc.reset();
343             rebuildServer(rc, server, imageToUse);
344             rc.reset();
345             startServer(rc, server);
346             generateEvent(rc, true, Outcome.SUCCESS.toString());
347             metricsLogger.info("Server status: PAUSED");
348             break;
349         case SUSPENDED:
350             // Attempt to resume the suspended server, stop it, and rebuild it
351             resumeServer(rc, server);
352             rc.reset();
353             stopServer(rc, server);
354             rc.reset();
355             rebuildServer(rc, server, imageToUse);
356             rc.reset();
357             startServer(rc, server);
358             generateEvent(rc, true, Outcome.SUCCESS.toString());
359             metricsLogger.info("Server status: SUSPENDED");
360             break;
361         default:
362             // Hmmm, unknown status, should never occur
363             msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
364                     server.getTenantId(), server.getStatus().name());
365             generateEvent(rc, false, msg);
366             logger.error(msg);
367             metricsLogger.error(msg);
368             throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
369         }
370     }
371
372     /**
373      * @see org.onap.appc.adapter.iaas.ProviderAdapter#rebuildServer(java.util.Map,
374      *      org.onap.ccsdk.sli.core.sli.SvcLogicContext)
375      */
376     @SuppressWarnings("nls")
377     public Server rebuildServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
378         Server server = null;
379         RequestContext rc = new RequestContext(ctx);
380         rc.isAlive();
381         setTimeForMetricsLogger();
382         String msg;
383         try {
384             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
385                     ProviderAdapter.PROPERTY_PROVIDER_NAME);
386             String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
387             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
388             VMURL vm = VMURL.parseURL(vm_url);
389             if (validateVM(rc, appName, vm_url, vm))
390                 return null;
391             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
392             String identStr = (ident == null) ? null : ident.toString();
393             ctx.setAttribute("REBUILD_STATUS", "ERROR");
394             Context context = null;
395             String tenantName = "Unknown";// to be used also in case of exception
396             try {
397                 context = getContext(rc, vm_url, identStr);
398                 if (context != null) {
399                     tenantName = context.getTenantName();// this varaible also is used in case of exception
400                     rc.reset();
401                     server = lookupServer(rc, context, vm.getServerId());
402                     logger.debug(Msg.SERVER_FOUND, vm_url, tenantName, server.getStatus().toString());
403                     // Manually checking image service until new PAL release
404                     if (hasImageAccess(rc, context)) {
405                         rebuildServer(rc, server, ctx);
406                         doSuccess(rc);
407                         ctx.setAttribute("REBUILD_STATUS", "SUCCESS");
408                     } else {
409                         msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
410                                 "Accessing Image Service Failed");
411                         logger.error(msg);
412                         metricsLogger.error(msg);
413                         doFailure(rc, HttpStatus.FORBIDDEN_403, msg);
414                     }
415                     context.close();
416                 } else {
417                     ctx.setAttribute("REBUILD_STATUS", "CONTEXT_NOT_FOUND");
418                 }
419             } catch (StateException ex) {
420                 logger.error(ex.getMessage());
421                 ctx.setAttribute("REBUILD_STATUS", "ERROR");
422                 doFailure(rc, HttpStatus.CONFLICT_409, ex.getMessage());
423             } catch (RequestFailedException e) {
424                 doFailure(rc, e.getStatus(), e.getMessage());
425                 ctx.setAttribute("REBUILD_STATUS", "ERROR");
426             } catch (ResourceNotFoundException e) {
427                 msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
428                 ctx.setAttribute("REBUILD_STATUS", "ERROR");
429                 logger.error(msg);
430                 metricsLogger.error(msg);
431                 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
432             } catch (Exception e1) {
433                 msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
434                         STOP_SERVICE.toString(), vm_url, tenantName);
435                 ctx.setAttribute("REBUILD_STATUS", "ERROR");
436                 logger.error(msg, e1);
437                 metricsLogger.error(msg);
438                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
439             }
440         } catch (RequestFailedException e) {
441             ctx.setAttribute("REBUILD_STATUS", "ERROR");
442             doFailure(rc, e.getStatus(), e.getMessage());
443         }
444         return server;
445     }
446
447     @Override
448     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
449             throws APPCException {
450         setMDC(Operation.REBUILD_SERVICE.toString(), "App-C IaaS Adapter:Rebuild", ADAPTER_NAME);
451         logOperation(Msg.REBUILDING_SERVER, params, context);
452         setTimeForMetricsLogger();
453         metricsLogger.info("Executing Provider Operation: Rebuild");
454         return rebuildServer(params, context);
455     }
456
457     private void setTimeForMetricsLogger() {
458         String timestamp = LoggingUtils.generateTimestampStr(((Date) new Date()).toInstant());
459         MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, timestamp);
460         MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, timestamp);
461         MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, "0");
462         MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, LoggingConstants.StatusCodes.COMPLETE);
463         MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, "cdp");
464         MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, "rebuild server");
465         MDC.put(LoggingConstants.MDCKeys.CLASS_NAME,
466                 "org.onap.appc.adapter.iaas.provider.operation.impl.RebuildServer");
467
468     }
469
470     /**
471      * Sets the sleep time used by thread.sleep to give "some time for OpenStack to
472      * start processing the request".
473      *
474      * @param millis
475      *            Time to sleep in milliseconds
476      */
477     public void setRebuildSleepTime(long millis) {
478         this.rebuildSleepTime = millis;
479     }
480
481     private boolean validateSnapshotId(String imageToUse, List<Image> snapshotList, List<Image> imageList) {
482
483         logger.debug("Validating snapshot-id " + imageToUse + " for the rebuild request from payload");
484         boolean imageFound = false;
485         // If image is empty , the validation si not required . Hence return false.
486         // Ideally function should not be called with empty image Id
487         if (imageToUse.isEmpty()) {
488             return imageFound;
489         } else {
490             // The supplied snapshot id can be a snapshot id or an image Id. Check both
491             // available for the vnf.
492             // Check against snapshot id list and image list
493             return findImageExists(snapshotList, imageToUse, "snapshotidList")
494                     || findImageExists(imageList, imageToUse, "imageidList");
495         }
496     }
497
498     boolean findImageExists(List<Image> list, String imageToUse, String source) {
499         boolean imageExists = false;
500         logger.debug("Available Image-ids from :" + source + "Start\n");
501         for (Image img : list) {
502             String imgId = img.getId();
503             logger.debug("Image Id - " + imgId + "\n");
504             if (imgId.equals(imageToUse)) {
505                 logger.debug("Image found in available " + source);
506                 imageExists = true;
507                 break;
508             }
509         }
510         return imageExists;
511
512     }
513 }