2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.adapter.iaas.provider.operation.impl;
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;
63 import java.text.DateFormat;
64 import java.text.SimpleDateFormat;
65 import java.util.Date;
66 import java.util.List;
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;
73 public class RebuildServer extends ProviderServerOperation {
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;
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.
87 * The request context that manages the state and recovery of the
88 * request for the life of its processing.
90 * the server to be rebuilt
92 * The image to be used (or snapshot)
93 * @throws RequestFailedException
94 * if the server does not change state in the allotted time
96 @SuppressWarnings("nls")
97 private void rebuildServer(RequestContext rc, Server server, String image) throws RequestFailedException {
98 logger.debug(Msg.REBUILD_SERVER, server.getId());
100 Context context = server.getContext();
101 Provider provider = context.getProvider();
102 ComputeService service = context.getComputeService();
104 * Set Time for Metrics Logger
106 setTimeForMetricsLogger();
108 while (rc.attempt()) {
110 server.rebuild(image);
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);
123 * We need to provide some time for OpenStack to start processing the request.
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();
132 } catch (ZoneException e) {
133 msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
136 metricsLogger.error(msg);
137 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
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
145 waitForStateChange(rc, server, Server.Status.READY);
147 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
149 metricsLogger.error(msg);
150 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
156 * This method is called to rebuild the provided server.
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.
164 * The request context that manages the state and recovery of the
165 * request for the life of its processing.
167 * The server to be rebuilt
168 * @throws ZoneException
170 * @throws RequestFailedException
171 * When server status is error
173 @SuppressWarnings("nls")
174 private void rebuildServer(RequestContext rc, Server server, SvcLogicContext ctx)
175 throws ZoneException, RequestFailedException {
176 ServerBootSource builtFrom = server.getBootSource();
178 * Set Time for Metrics Logger
180 setTimeForMetricsLogger();
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());
187 generateEvent(rc, false, msg);
188 metricsLogger.error(msg);
189 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.INTERNAL_SERVER_ERROR_500, server);
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]",
197 generateEvent(rc, false, msg);
199 metricsLogger.error(msg);
200 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.FORBIDDEN_403, server);
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.
208 Context context = server.getContext();
209 Provider provider = context.getProvider();
210 ComputeService service = context.getComputeService();
211 if (server.getStatus().equals(Server.Status.PENDING)) {
213 waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
214 Server.Status.SUSPENDED, Server.Status.PAUSED);
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);
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);
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.
232 String imageToUse = "";
234 ObjectMapper mapper = new ObjectMapper();
235 String payloadStr = configuration.getProperty(Property.PAYLOAD);
236 if (payloadStr == null || payloadStr.isEmpty()) {
237 payloadStr = ctx.getAttribute(ProviderAdapter.PAYLOAD);
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());
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);
253 logger.debug("Image Snapshot Not Found");
254 msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
255 "Invalid Snapshot-Id");
257 metricsLogger.error(msg);
258 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.FORBIDDEN_403, server);
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();
264 imageToUse = server.getImage();
267 while (rc.attempt()) {
270 * We are just trying to make sure that the image exists. We arent interested in
271 * the details at this point.
273 imageService.getImage(imageToUse);
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);
285 } catch (ZoneException e) {
286 msg = EELFResourceManager.format(Msg.IMAGE_NOT_FOUND, imageToUse, "rebuild");
287 generateEvent(rc, false, msg);
289 metricsLogger.error(msg);
290 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
294 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
296 metricsLogger.error(msg);
297 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
301 * We determine what to do based on the current state of the server
303 switch (server.getStatus()) {
305 // Nothing to do, the server is gone
306 msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(), server.getTenantId(),
308 generateEvent(rc, false, msg);
310 metricsLogger.error(msg);
311 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
313 // Attempt to stop the server, then rebuild it
314 stopServer(rc, server);
316 rebuildServer(rc, server, imageToUse);
318 startServer(rc, server);
319 generateEvent(rc, true, Outcome.SUCCESS.toString());
320 metricsLogger.info("Server status: RUNNING");
323 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
324 server.getTenantId(), "rebuild");
325 generateEvent(rc, false, msg);
327 metricsLogger.error(msg);
328 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
330 // Attempt to rebuild the server
331 rebuildServer(rc, server, imageToUse);
333 startServer(rc, server);
334 generateEvent(rc, true, Outcome.SUCCESS.toString());
335 metricsLogger.info("Server status: READY");
338 // if paused, un-pause it, stop it, and rebuild it
339 unpauseServer(rc, server);
341 stopServer(rc, server);
343 rebuildServer(rc, server, imageToUse);
345 startServer(rc, server);
346 generateEvent(rc, true, Outcome.SUCCESS.toString());
347 metricsLogger.info("Server status: PAUSED");
350 // Attempt to resume the suspended server, stop it, and rebuild it
351 resumeServer(rc, server);
353 stopServer(rc, server);
355 rebuildServer(rc, server, imageToUse);
357 startServer(rc, server);
358 generateEvent(rc, true, Outcome.SUCCESS.toString());
359 metricsLogger.info("Server status: SUSPENDED");
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);
367 metricsLogger.error(msg);
368 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
373 * @see org.onap.appc.adapter.iaas.ProviderAdapter#rebuildServer(java.util.Map,
374 * org.onap.ccsdk.sli.core.sli.SvcLogicContext)
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);
381 setTimeForMetricsLogger();
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))
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
397 context = getContext(rc, vm_url, identStr);
398 if (context != null) {
399 tenantName = context.getTenantName();// this varaible also is used in case of exception
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);
407 ctx.setAttribute("REBUILD_STATUS", "SUCCESS");
409 msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
410 "Accessing Image Service Failed");
412 metricsLogger.error(msg);
413 doFailure(rc, HttpStatus.FORBIDDEN_403, msg);
417 ctx.setAttribute("REBUILD_STATUS", "CONTEXT_NOT_FOUND");
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");
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);
440 } catch (RequestFailedException e) {
441 ctx.setAttribute("REBUILD_STATUS", "ERROR");
442 doFailure(rc, e.getStatus(), e.getMessage());
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);
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");
471 * Sets the sleep time used by thread.sleep to give "some time for OpenStack to
472 * start processing the request".
475 * Time to sleep in milliseconds
477 public void setRebuildSleepTime(long millis) {
478 this.rebuildSleepTime = millis;
481 private boolean validateSnapshotId(String imageToUse, List<Image> snapshotList, List<Image> imageList) {
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()) {
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");
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);