2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.iaas.provider.operation.impl;
26 import com.att.cdp.exceptions.ContextConnectionException;
27 import com.att.cdp.exceptions.ResourceNotFoundException;
28 import com.att.cdp.exceptions.ZoneException;
29 import com.att.cdp.zones.ComputeService;
30 import com.att.cdp.zones.Context;
31 import com.att.cdp.zones.ImageService;
32 import com.att.cdp.zones.Provider;
33 import com.att.cdp.zones.model.Image;
34 import com.att.cdp.zones.model.ModelObject;
35 import com.att.cdp.zones.model.Server;
36 import com.att.cdp.zones.model.ServerBootSource;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39 import com.att.eelf.i18n.EELFResourceManager;
40 import org.glassfish.grizzly.http.util.HttpStatus;
41 import org.onap.appc.Constants;
42 import org.onap.appc.adapter.iaas.ProviderAdapter;
43 import org.onap.appc.adapter.iaas.impl.IdentityURL;
44 import org.onap.appc.adapter.iaas.impl.RequestContext;
45 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
46 import org.onap.appc.adapter.iaas.impl.VMURL;
47 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
48 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
49 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Outcome;
50 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
51 import org.onap.appc.configuration.Configuration;
52 import org.onap.appc.configuration.ConfigurationFactory;
53 import org.onap.appc.exceptions.APPCException;
54 import org.onap.appc.i18n.Msg;
55 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
57 import java.text.DateFormat;
58 import java.text.SimpleDateFormat;
59 import java.util.Date;
60 import java.util.List;
62 import java.util.TimeZone;
63 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.STOP_SERVICE;
64 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
66 public class RebuildServer extends ProviderServerOperation {
68 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RebuildServer.class);
69 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
70 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
71 //the sleep time used by thread.sleep to give "some time for OpenStack to start processing the request"
72 private long rebuildSleepTime = 10L * 1000L;
75 * Rebuild the indicated server with the indicated image. This method assumes the server has been determined to be
76 * in the correct state to do the rebuild.
78 * @param rc The request context that manages the state and recovery of the request for the life of its processing.
79 * @param server the server to be rebuilt
80 * @param image The image to be used (or snapshot)
81 * @throws RequestFailedException if the server does not change state in the allotted time
83 @SuppressWarnings("nls")
84 private void rebuildServer(RequestContext rc, Server server, String image) throws RequestFailedException {
85 logger.debug(Msg.REBUILD_SERVER, server.getId());
88 Context context = server.getContext();
89 Provider provider = context.getProvider();
90 ComputeService service = context.getComputeService();
93 * Set Time for Metrics Logger
95 setTimeForMetricsLogger();
98 while (rc.attempt()) {
100 server.rebuild(image);
102 } catch (ContextConnectionException e) {
103 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, provider.getName(), service.getURL(),
104 context.getTenant().getName(), context.getTenant().getId(), e.getMessage(),
105 Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
106 Integer.toString(rc.getRetryLimit()));
107 logger.error(msg, e);
108 metricsLogger.error(msg, e);
114 * We need to provide some time for OpenStack to start processing the request.
117 Thread.sleep(rebuildSleepTime);
118 } catch (InterruptedException e) {
119 logger.trace("Sleep threw interrupted exception, should never occur");
120 metricsLogger.trace("Sleep threw interrupted exception, should never occur");
122 } catch (ZoneException e) {
123 msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
126 metricsLogger.error(msg);
127 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
132 * Once we have started the process, now we wait for the final state of stopped. This should be the final state
133 * (since we started the rebuild with the server stopped).
135 waitForStateChange(rc, server, Server.Status.READY);
138 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
140 metricsLogger.error(msg);
141 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
147 * This method is called to rebuild the provided server.
149 * If the server was booted from a volume, then the request is failed immediately and no action is taken. Rebuilding
150 * a VM from a bootable volume, where the bootable volume itself is not rebuilt, serves no purpose.
153 * @param rc The request context that manages the state and recovery of the request for the life of its processing.
154 * @param server The server to be rebuilt
155 * @throws ZoneException When error occurs
156 * @throws RequestFailedException When server status is error
158 @SuppressWarnings("nls")
159 private void rebuildServer(RequestContext rc, Server server, SvcLogicContext ctx)
160 throws ZoneException, RequestFailedException {
161 ServerBootSource builtFrom = server.getBootSource();
164 * Set Time for Metrics Logger
166 setTimeForMetricsLogger();
169 // Throw exception for non image/snap boot source
170 if (ServerBootSource.VOLUME.equals(builtFrom)) {
171 msg = String.format("Rebuilding is currently not supported for servers built from bootable volumes [%s]",
173 generateEvent(rc, false, msg);
175 metricsLogger.error(msg);
176 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.FORBIDDEN_403, server);
180 * Pending is a bit of a special case. If we find the server is in a pending state, then the provider is in the
181 * process of changing state of the server. So, lets try to wait a little bit and see if the state settles down
182 * to one we can deal with. If not, then we have to fail the request.
184 Context context = server.getContext();
185 Provider provider = context.getProvider();
186 ComputeService service = context.getComputeService();
187 if (server.getStatus().equals(Server.Status.PENDING)) {
189 waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
190 Server.Status.SUSPENDED, Server.Status.PAUSED);
193 // Is the skip Hypervisor check attribute populated?
194 String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
195 if (skipHypervisorCheck == null && ctx != null) {
196 skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
199 // Always perform Hypervisor Status checks
200 // unless the skip is set to true
201 if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
202 // Check of the Hypervisor for the VM Server is UP and reachable
203 checkHypervisor(server);
207 * Get the image to use. This is determined by the presence or absence of snapshot images. If any snapshots
208 * exist, then the latest snapshot is used, otherwise the image used to construct the VM is used.
210 List<Image> snapshots = server.getSnapshots();
212 if (snapshots != null && !snapshots.isEmpty()) {
213 imageToUse = snapshots.get(0).getId();
215 imageToUse = server.getImage();
216 ImageService imageService = server.getContext().getImageService();
219 while (rc.attempt()) {
222 * We are just trying to make sure that the image exists. We arent interested in the details at
225 imageService.getImage(imageToUse);
227 } catch (ContextConnectionException e) {
228 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, provider.getName(),
229 imageService.getURL(), context.getTenant().getName(), context.getTenant().getId(),
230 e.getMessage(), Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
231 Integer.toString(rc.getRetryLimit()));
232 logger.error(msg, e);
233 metricsLogger.error(msg);
237 } catch (ZoneException e) {
238 msg = EELFResourceManager.format(Msg.IMAGE_NOT_FOUND, imageToUse, "rebuild");
239 generateEvent(rc, false, msg);
241 metricsLogger.error(msg);
242 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
246 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
248 metricsLogger.error(msg);
249 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
254 * We determine what to do based on the current state of the server
256 switch (server.getStatus()) {
258 // Nothing to do, the server is gone
259 msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
260 server.getTenantId(), "rebuilt");
261 generateEvent(rc, false, msg);
263 metricsLogger.error(msg);
264 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
267 // Attempt to stop the server, then rebuild it
268 stopServer(rc, server);
270 rebuildServer(rc, server, imageToUse);
272 startServer(rc, server);
273 generateEvent(rc, true, Outcome.SUCCESS.toString());
274 metricsLogger.info("Server status: RUNNING");
278 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
279 server.getTenantId(), "rebuild");
280 generateEvent(rc, false, msg);
282 metricsLogger.error(msg);
283 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
286 // Attempt to rebuild the server
287 rebuildServer(rc, server, imageToUse);
289 startServer(rc, server);
290 generateEvent(rc, true, Outcome.SUCCESS.toString());
291 metricsLogger.info("Server status: READY");
295 // if paused, un-pause it, stop it, and rebuild it
296 unpauseServer(rc, server);
298 stopServer(rc, server);
300 rebuildServer(rc, server, imageToUse);
302 startServer(rc, server);
303 generateEvent(rc, true, Outcome.SUCCESS.toString());
304 metricsLogger.info("Server status: PAUSED");
308 // Attempt to resume the suspended server, stop it, and rebuild it
309 resumeServer(rc, server);
311 stopServer(rc, server);
313 rebuildServer(rc, server, imageToUse);
315 startServer(rc, server);
316 generateEvent(rc, true, Outcome.SUCCESS.toString());
317 metricsLogger.info("Server status: SUSPENDED");
321 // Hmmm, unknown status, should never occur
322 msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
323 server.getTenantId(), server.getStatus().name());
324 generateEvent(rc, false, msg);
326 metricsLogger.error(msg);
327 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
332 * @see org.onap.appc.adapter.iaas.ProviderAdapter#rebuildServer(java.util.Map,
333 * org.onap.ccsdk.sli.core.sli.SvcLogicContext)
335 @SuppressWarnings("nls")
336 public Server rebuildServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
337 Server server = null;
338 RequestContext rc = new RequestContext(ctx);
341 setTimeForMetricsLogger();
345 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
346 ProviderAdapter.PROPERTY_PROVIDER_NAME);
348 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
349 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
350 VMURL vm = VMURL.parseURL(vm_url);
351 if (validateVM(rc, appName, vm_url, vm))
354 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
355 String identStr = (ident == null) ? null : ident.toString();
356 ctx.setAttribute("REBUILD_STATUS", "ERROR");
358 Context context = null;
359 String tenantName = "Unknown";//to be used also in case of exception
361 context = getContext(rc, vm_url, identStr);
362 if (context != null) {
363 tenantName = context.getTenantName();//this varaible also is used in case of exception
365 server = lookupServer(rc, context, vm.getServerId());
366 logger.debug(Msg.SERVER_FOUND, vm_url, tenantName, server.getStatus().toString());
368 // Manually checking image service until new PAL release
369 if (hasImageAccess(rc, context)) {
370 rebuildServer(rc, server, ctx);
372 ctx.setAttribute("REBUILD_STATUS", "SUCCESS");
374 msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
375 "Accessing Image Service Failed");
377 metricsLogger.error(msg);
378 doFailure(rc, HttpStatus.FORBIDDEN_403, msg);
382 ctx.setAttribute("REBUILD_STATUS", "CONTEXT_NOT_FOUND");
384 } catch (RequestFailedException e) {
385 doFailure(rc, e.getStatus(), e.getMessage());
386 ctx.setAttribute("REBUILD_STATUS", "ERROR");
387 } catch (ResourceNotFoundException e) {
388 msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
389 ctx.setAttribute("REBUILD_STATUS", "ERROR");
391 metricsLogger.error(msg);
392 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
393 } catch (Exception e1) {
394 msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
395 STOP_SERVICE.toString(), vm_url, tenantName);
396 ctx.setAttribute("REBUILD_STATUS", "ERROR");
397 logger.error(msg, e1);
398 metricsLogger.error(msg);
399 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
401 } catch (RequestFailedException e) {
402 doFailure(rc, e.getStatus(), e.getMessage());
403 ctx.setAttribute("REBUILD_STATUS", "ERROR");
410 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
411 throws APPCException {
412 setMDC(Operation.REBUILD_SERVICE.toString(), "App-C IaaS Adapter:Rebuild", ADAPTER_NAME);
413 logOperation(Msg.REBUILDING_SERVER, params, context);
415 setTimeForMetricsLogger();
417 metricsLogger.info("Executing Provider Operation: Rebuild");
419 return rebuildServer(params, context);
422 private void setTimeForMetricsLogger() {
423 long startTime = System.currentTimeMillis();
424 TimeZone tz = TimeZone.getTimeZone("UTC");
425 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
427 long endTime = System.currentTimeMillis();
428 long duration = endTime - startTime;
429 String durationStr = String.valueOf(duration);
430 String endTimeStrUTC = df.format(new Date());
431 MDC.put("EndTimestamp", endTimeStrUTC);
432 MDC.put("ElapsedTime", durationStr);
433 MDC.put("TargetEntity", "cdp");
434 MDC.put("TargetServiceName", "rebuild server");
435 MDC.put("ClassName", "org.onap.appc.adapter.iaas.provider.operation.impl.RebuildServer");
439 * Sets the sleep time used by thread.sleep to give
440 * "some time for OpenStack to start processing the request".
442 * @param millis Time to sleep in milliseconds
444 public void setRebuildSleepTime(long millis){
445 this.rebuildSleepTime = millis;