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 Ericsson
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=========================================================
25 package org.onap.appc.adapter.iaas.provider.operation.impl;
27 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
29 import java.util.concurrent.TimeoutException;
30 import org.glassfish.grizzly.http.util.HttpStatus;
31 import org.onap.appc.Constants;
32 import org.onap.appc.adapter.iaas.ProviderAdapter;
33 import org.onap.appc.adapter.iaas.impl.IdentityURL;
34 import org.onap.appc.adapter.iaas.impl.RequestContext;
35 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
36 import org.onap.appc.adapter.iaas.impl.VMURL;
37 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
38 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
39 import org.onap.appc.configuration.Configuration;
40 import org.onap.appc.configuration.ConfigurationFactory;
41 import org.onap.appc.exceptions.APPCException;
42 import org.onap.appc.i18n.Msg;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import com.att.cdp.exceptions.ResourceNotFoundException;
45 import com.att.cdp.exceptions.StateException;
46 import com.att.cdp.zones.ComputeService;
47 import com.att.cdp.zones.Context;
48 import com.att.cdp.zones.model.ModelObject;
49 import com.att.cdp.zones.model.Server;
50 import com.att.eelf.configuration.EELFLogger;
51 import com.att.eelf.configuration.EELFManager;
52 import com.att.eelf.i18n.EELFResourceManager;
53 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.REBOOT_SERVICE;
54 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
56 public class RebootServer extends ProviderServerOperation {
57 private final EELFLogger logger = EELFManager.getInstance().getLogger(RebootServer.class);
58 private static final Configuration config = ConfigurationFactory.getConfiguration();
59 private static final Integer NO_OF_ATTEMPTS = 30;
60 private static final Integer RETRY_INTERVAL = 10;
61 private static final int MILLI_SECONDS = 1000;
64 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
65 throws APPCException {
66 setMDC(Operation.REBOOT_SERVICE.toString(), "App-C IaaS Adapter:rebootServer", ADAPTER_NAME);
67 return rebootServer(params, context);
70 private Server rebootServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
72 RequestContext requestContext = new RequestContext(ctx);
73 requestContext.isAlive();
74 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
75 String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
76 String rebooType = params.get(ProviderAdapter.REBOOT_TYPE);
77 String tenantName = "Unknown";
78 if (rebooType.isEmpty()) {
81 logger.info("reboot type" + rebooType);
83 VMURL vm = VMURL.parseURL(vmUrl);
85 // to be used also in case of exception
86 if (validateVM(requestContext, appName, vmUrl, vm)) {
89 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
90 String identStr = (ident == null) ? null : ident.toString();
91 context = getContext(requestContext, vmUrl, identStr);
92 // Is the skip Hypervisor check attribute populated?
93 String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
94 if (skipHypervisorCheck == null && ctx != null) {
95 skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
97 if (context != null) {
98 tenantName = context.getTenantName();// this variable also is
99 // used in case of exception
100 requestContext.reset();
101 server = lookupServer(requestContext, context, vm.getServerId());
102 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
103 // Always perform Hypervisor check
104 // unless the skip is set to true
105 if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
106 // Check of the Hypervisor for the VM Server is UP and reachable
107 checkHypervisor(server);
109 Context contx = server.getContext();
110 ComputeService service = contx.getComputeService();
111 logger.info("performing reboot action for " + server.getId() + " rebootype " + rebooType);
112 service.rebootServer(server.getId(), rebooType);
113 if (waitForServerStatusChange(requestContext, server, vmUrl, Server.Status.RUNNING)) {
114 ctx.setAttribute("REBOOT_STATUS", "SUCCESS");
115 doSuccess(requestContext);
117 ctx.setAttribute("REBOOT_STATUS", "FAILURE");
121 ctx.setAttribute("REBOOT_STATUS", "FAILURE");
124 } catch (ResourceNotFoundException | StateException ex) {
125 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, ex, ex.getClass().getSimpleName(),
126 REBOOT_SERVICE.toString(), vmUrl, tenantName);
127 ctx.setAttribute("REBOOT_STATUS", "FAILURE");
128 if (ex instanceof ResourceNotFoundException) {
129 doFailure(requestContext, HttpStatus.NOT_FOUND_404, ex.getMessage());
131 doFailure(requestContext, HttpStatus.CONFLICT_409, ex.getMessage());
133 } catch (Exception ex) {
134 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, ex, ex.getClass().getSimpleName(),
135 REBOOT_SERVICE.toString(), vmUrl, tenantName);
136 ctx.setAttribute("REBOOT_STATUS", "FAILURE");
137 doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.getMessage());
142 private boolean waitForServerStatusChange(RequestContext rc, Server server, String vmUrl,
143 Server.Status... desiredStates) throws Exception {
144 int pollInterval = RETRY_INTERVAL.intValue();
145 int timeout = configuration.getIntegerProperty(Constants.PROPERTY_SERVER_STATE_CHANGE_TIMEOUT);
146 config.setProperty(Constants.PROPERTY_RETRY_DELAY, RETRY_INTERVAL.toString());
147 config.setProperty(Constants.PROPERTY_RETRY_LIMIT, NO_OF_ATTEMPTS.toString());
148 Context context = server.getContext();
150 boolean status = false;
151 while (rc.attempt()) {
152 if ( Server.Status.ERROR.equals(server.getStatus())) {
153 msg = "Device status " + Server.Status.ERROR + " cannot proceed further";
154 throw new RequestFailedException("Waiting for State Change", msg, HttpStatus.CONFLICT_409, server);
156 server.waitForStateChange(pollInterval, timeout, desiredStates);
157 if (Server.Status.RUNNING.equals(server.getStatus())
158 || Server.Status.READY.equals(server.getStatus())) {
161 logger.info(server.getStatus() + " status ");
164 logger.info("Done Trying " + rc.getAttempts() + " attempts");
171 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, vmUrl);
172 logger.info("waitForStateChange Failed");
174 throw new RequestFailedException("Waiting for State Change", msg, HttpStatus.BAD_GATEWAY_502, server);
176 if ((rc.getAttempts() == NO_OF_ATTEMPTS) && (!status)) {
178 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, Long.toString(rc.getRetryDelay()),
179 Integer.toString(rc.getAttempts()), Integer.toString(rc.getRetryLimit()));
181 throw new TimeoutException(msg);
184 logger.info("Reboot server status flag --> " + status);