2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.iaas.provider.operation.impl;
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;
58 import java.text.DateFormat;
59 import java.text.SimpleDateFormat;
60 import java.util.Date;
61 import java.util.List;
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;
67 public class RebuildServer extends ProviderServerOperation {
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();
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.
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
82 @SuppressWarnings("nls")
83 private void rebuildServer(RequestContext rc, Server server, String image) throws RequestFailedException {
84 logger.debug(Msg.REBUILD_SERVER, server.getId());
87 Context context = server.getContext();
88 Provider provider = context.getProvider();
89 ComputeService service = context.getComputeService();
92 * Set Time for Metrics Logger
94 setTimeForMetricsLogger();
97 while (rc.attempt()) {
99 server.rebuild(image);
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);
113 * We need to provide some time for OpenStack to start processing the request.
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");
121 } catch (ZoneException e) {
122 msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
125 metricsLogger.error(msg);
126 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
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).
134 waitForStateChange(rc, server, Server.Status.READY);
137 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
139 metricsLogger.error(msg);
140 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
146 * This method is called to rebuild the provided server.
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.
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
157 @SuppressWarnings("nls")
158 private void rebuildServer(RequestContext rc, Server server, SvcLogicContext ctx)
159 throws ZoneException, RequestFailedException {
160 ServerBootSource builtFrom = server.getBootSource();
163 * Set Time for Metrics Logger
165 setTimeForMetricsLogger();
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]",
172 generateEvent(rc, false, msg);
174 metricsLogger.error(msg);
175 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.FORBIDDEN_403, server);
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.
183 Context context = server.getContext();
184 Provider provider = context.getProvider();
185 ComputeService service = context.getComputeService();
186 if (server.getStatus().equals(Server.Status.PENDING)) {
188 waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
189 Server.Status.SUSPENDED, Server.Status.PAUSED);
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);
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);
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.
209 List<Image> snapshots = server.getSnapshots();
211 if (snapshots != null && !snapshots.isEmpty()) {
212 imageToUse = snapshots.get(0).getId();
214 imageToUse = server.getImage();
215 ImageService imageService = server.getContext().getImageService();
218 while (rc.attempt()) {
221 * We are just trying to make sure that the image exists. We arent interested in the details at
224 imageService.getImage(imageToUse);
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);
236 } catch (ZoneException e) {
237 msg = EELFResourceManager.format(Msg.IMAGE_NOT_FOUND, imageToUse, "rebuild");
238 generateEvent(rc, false, msg);
240 metricsLogger.error(msg);
241 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
245 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
247 metricsLogger.error(msg);
248 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.BAD_GATEWAY_502, server);
253 * We determine what to do based on the current state of the server
255 switch (server.getStatus()) {
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);
262 metricsLogger.error(msg);
263 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
266 // Attempt to stop the server, then rebuild it
267 stopServer(rc, server);
269 rebuildServer(rc, server, imageToUse);
271 startServer(rc, server);
272 generateEvent(rc, true, Outcome.SUCCESS.toString());
273 metricsLogger.info("Server status: RUNNING");
277 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
278 server.getTenantId(), "rebuild");
279 generateEvent(rc, false, msg);
281 metricsLogger.error(msg);
282 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
285 // Attempt to rebuild the server
286 rebuildServer(rc, server, imageToUse);
288 startServer(rc, server);
289 generateEvent(rc, true, Outcome.SUCCESS.toString());
290 metricsLogger.info("Server status: READY");
294 // if paused, un-pause it, stop it, and rebuild it
295 unpauseServer(rc, server);
297 stopServer(rc, server);
299 rebuildServer(rc, server, imageToUse);
301 startServer(rc, server);
302 generateEvent(rc, true, Outcome.SUCCESS.toString());
303 metricsLogger.info("Server status: PAUSED");
307 // Attempt to resume the suspended server, stop it, and rebuild it
308 resumeServer(rc, server);
310 stopServer(rc, server);
312 rebuildServer(rc, server, imageToUse);
314 startServer(rc, server);
315 generateEvent(rc, true, Outcome.SUCCESS.toString());
316 metricsLogger.info("Server status: SUSPENDED");
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);
325 metricsLogger.error(msg);
326 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
331 * @see org.onap.appc.adapter.iaas.ProviderAdapter#rebuildServer(java.util.Map,
332 * org.openecomp.sdnc.sli.SvcLogicContext)
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);
340 setTimeForMetricsLogger();
344 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
345 ProviderAdapter.PROPERTY_PROVIDER_NAME);
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))
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");
357 Context context = null;
359 context = getContext(rc, vm_url, identStr);
360 if (context != null) {
362 server = lookupServer(rc, context, vm.getServerId());
363 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
365 // Manually checking image service until new PAL release
366 if (hasImageAccess(rc, context)) {
367 rebuildServer(rc, server, ctx);
369 ctx.setAttribute("REBUILD_STATUS", "SUCCESS");
371 msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
372 "Accessing Image Service Failed");
374 metricsLogger.error(msg);
375 doFailure(rc, HttpStatus.FORBIDDEN_403, msg);
379 ctx.setAttribute("REBUILD_STATUS", "CONTEXT_NOT_FOUND");
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");
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);
398 } catch (RequestFailedException e) {
399 doFailure(rc, e.getStatus(), e.getMessage());
400 ctx.setAttribute("REBUILD_STATUS", "ERROR");
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);
412 setTimeForMetricsLogger();
414 metricsLogger.info("Executing Provider Operation: Rebuild");
416 return rebuildServer(params, context);
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");
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");