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