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