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=========================================================
23 package org.onap.appc.adapter.iaas.provider.operation.impl;
25 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.REBOOT_SERVICE;
26 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
29 import java.util.concurrent.TimeoutException;
31 import org.onap.appc.configuration.Configuration;
32 import org.glassfish.grizzly.http.util.HttpStatus;
33 import org.onap.appc.Constants;
34 import org.onap.appc.adapter.iaas.ProviderAdapter;
35 import org.onap.appc.adapter.iaas.impl.IdentityURL;
36 import org.onap.appc.adapter.iaas.impl.RequestContext;
37 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
38 import org.onap.appc.adapter.iaas.impl.VMURL;
39 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
40 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
41 import org.onap.appc.configuration.ConfigurationFactory;
42 import org.onap.appc.exceptions.APPCException;
43 import org.onap.appc.i18n.Msg;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
45 import com.att.cdp.exceptions.ResourceNotFoundException;
46 import com.att.cdp.exceptions.StateException;
47 import com.att.cdp.zones.ComputeService;
48 import com.att.cdp.zones.Context;
49 import com.att.cdp.zones.model.ModelObject;
50 import com.att.cdp.zones.model.Server;
51 import com.att.eelf.configuration.EELFLogger;
52 import com.att.eelf.configuration.EELFManager;
53 import com.att.eelf.i18n.EELFResourceManager;
55 public class RebootServer extends ProviderServerOperation {
56 private final EELFLogger logger = EELFManager.getInstance().getLogger(RebootServer.class);
57 private static final Configuration config = ConfigurationFactory.getConfiguration();
58 private static final Integer NO_OF_ATTEMPTS=30;
59 private static final Integer RETRY_INTERVAL=10;
60 private static final int MILLI_SECONDS=1000;
63 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
64 throws APPCException {
65 setMDC(Operation.REBOOT_SERVICE.toString(), "App-C IaaS Adapter:rebootServer", ADAPTER_NAME);
66 // logOperation(Msg.REBOOT_SERVER, params, context);
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 if (context != null) {
93 tenantName = context.getTenantName();// this variable also is
94 // used in case of exception
95 requestContext.reset();
96 server = lookupServer(requestContext, context, vm.getServerId());
97 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
98 Context contx = server.getContext();
99 ComputeService service = contx.getComputeService();
100 logger.info("performing reboot action for " + server.getId() + " rebootype " + rebooType);
101 service.rebootServer(server.getId(), rebooType);
102 if (waitForServerStatusChange(requestContext, server, vmUrl, Server.Status.RUNNING)) {
103 ctx.setAttribute("REBOOT_STATUS", "SUCCESS");
104 doSuccess(requestContext);
106 ctx.setAttribute("REBOOT_STATUS", "FAILURE");
110 ctx.setAttribute("REBOOT_STATUS", "FAILURE");
113 } catch (ResourceNotFoundException | StateException ex) {
114 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, ex, ex.getClass().getSimpleName(),
115 REBOOT_SERVICE.toString(), vmUrl, tenantName);
116 logger.info(ex.getMessage());
117 ctx.setAttribute("REBOOT_STATUS", "FAILURE");
118 if (ex instanceof ResourceNotFoundException) {
119 doFailure(requestContext, HttpStatus.NOT_FOUND_404, ex.getMessage());
121 doFailure(requestContext, HttpStatus.CONFLICT_409, ex.getMessage());
123 } catch (Exception ex) {
124 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, ex, ex.getClass().getSimpleName(),
125 REBOOT_SERVICE.toString(), vmUrl, tenantName);
126 logger.info(ex.getMessage());
127 ctx.setAttribute("REBOOT_STATUS", "FAILURE");
128 doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.getMessage());
133 private boolean waitForServerStatusChange(RequestContext rc, Server server, String vmUrl,
134 Server.Status... desiredStates) throws Exception {
135 int pollInterval = RETRY_INTERVAL.intValue();
136 int timeout = configuration.getIntegerProperty(Constants.PROPERTY_SERVER_STATE_CHANGE_TIMEOUT);
137 config.setProperty(Constants.PROPERTY_RETRY_DELAY, RETRY_INTERVAL.toString());
138 config.setProperty(Constants.PROPERTY_RETRY_LIMIT, NO_OF_ATTEMPTS.toString());
139 Context context = server.getContext();
141 boolean status = false;
142 long endTime = System.currentTimeMillis() + (timeout * MILLI_SECONDS);
143 while (rc.attempt()) {
144 if (server.getStatus() != null && server.getStatus().equals(Server.Status.ERROR)) {
145 msg = "Device status " + Server.Status.ERROR + " cannot proceed further";
146 throw new RequestFailedException("Waiting for State Change", msg, HttpStatus.CONFLICT_409, server);
148 server.waitForStateChange(pollInterval, timeout, desiredStates);
149 if ((server.getStatus().equals(Server.Status.RUNNING)) || (server.getStatus().equals(Server.Status.READY))) {
152 logger.info(server.getStatus() + " status ");
155 logger.info("Done Trying " + rc.getAttempts() + " attempts");
163 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, vmUrl);
164 logger.info("waitForStateChange Failed");
166 throw new RequestFailedException("Waiting for State Change", msg, HttpStatus.BAD_GATEWAY_502, server);
168 if ((rc.getAttempts() == NO_OF_ATTEMPTS) && (!status)) {
170 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, Long.toString(rc.getRetryDelay()),
171 Integer.toString(rc.getAttempts()), Integer.toString(rc.getRetryLimit()));
173 throw new TimeoutException(msg);
177 logger.info("Reboot server status flag --> " + status);