b1879cee274b59a3b4cbc58ca80b121b102c1e88
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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  * Modifications Copyright (C) 2019 IBM
11  * =============================================================================
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  * 
16  *      http://www.apache.org/licenses/LICENSE-2.0
17  * 
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  * 
24  * ============LICENSE_END=========================================================
25  */
26 package org.onap.appc.adapter.iaas.provider.operation.impl;
27
28 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
29 import java.util.Map;
30 import java.util.concurrent.TimeoutException;
31 import org.glassfish.grizzly.http.util.HttpStatus;
32 import org.onap.appc.Constants;
33 import org.onap.appc.adapter.iaas.ProviderAdapter;
34 import org.onap.appc.adapter.iaas.impl.IdentityURL;
35 import org.onap.appc.adapter.iaas.impl.RequestContext;
36 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
37 import org.onap.appc.adapter.iaas.impl.VMURL;
38 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
39 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
40 import org.onap.appc.configuration.Configuration;
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;
54 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.REBOOT_SERVICE;
55 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
56
57 public class RebootServer extends ProviderServerOperation {
58     private final EELFLogger logger = EELFManager.getInstance().getLogger(RebootServer.class);
59     private static final Configuration config = ConfigurationFactory.getConfiguration();
60     private static final Integer NO_OF_ATTEMPTS = 30;
61     private static final Integer RETRY_INTERVAL = 10;
62
63     @Override
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);
68     }
69
70     private Server rebootServer(Map<String, String> params, SvcLogicContext ctx){
71         Server server = null;
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()) {
79             rebooType = "SOFT";
80         }
81         logger.info("reboot type" + rebooType);
82         if (ctx != null) {
83         try {
84             VMURL vm = VMURL.parseURL(vmUrl);
85             Context context;
86             // to be used also in case of exception
87             if (validateVM(requestContext, appName, vmUrl, vm)) {
88                 return null;
89             }
90             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
91             String identStr = (ident == null) ? null : ident.toString();
92             context = getContext(requestContext, vmUrl, identStr);
93             // Is the skip Hypervisor check attribute populated?
94             String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
95             if (skipHypervisorCheck == null && ctx != null) {
96                 skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
97             }
98             if (context != null) {
99                 tenantName = context.getTenantName();// this variable also is
100                 // used in case of exception
101                 requestContext.reset();
102                 server = lookupServer(requestContext, context, vm.getServerId());
103                 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
104                 // Always perform Hypervisor check
105                 // unless the skip is set to true
106                 if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
107                     // Check of the Hypervisor for the VM Server is UP and reachable
108                     checkHypervisor(server);
109                 }
110                 Context contx = server.getContext();
111                 ComputeService service = contx.getComputeService();
112                 logger.info("performing reboot action for " + server.getId() + " rebootype " + rebooType);
113                 service.rebootServer(server.getId(), rebooType);
114                 if (waitForServerStatusChange(requestContext, server, vmUrl, Server.Status.RUNNING)) {
115                     ctx.setAttribute("REBOOT_STATUS", "SUCCESS");
116                     doSuccess(requestContext);
117                 } else {
118                     ctx.setAttribute("REBOOT_STATUS", "FAILURE");
119                 }
120                 context.close();
121             } else {
122                 ctx.setAttribute("REBOOT_STATUS", "FAILURE");
123             }
124
125         } catch (ResourceNotFoundException | StateException ex) {
126             ctx.setAttribute("REBOOT_STATUS", "FAILURE");
127             if (ex instanceof ResourceNotFoundException) {
128                 doFailure(requestContext, HttpStatus.NOT_FOUND_404, ex.getMessage());
129             } else {
130                 doFailure(requestContext, HttpStatus.CONFLICT_409, ex.getMessage());
131             }
132         } catch (Exception ex) {
133             ctx.setAttribute("REBOOT_STATUS", "FAILURE");
134             doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.getMessage());
135         }
136         }
137         return server;
138     }
139
140     private boolean waitForServerStatusChange(RequestContext rc, Server server, String vmUrl,
141             Server.Status... desiredStates) throws Exception {
142         int pollInterval = RETRY_INTERVAL.intValue();
143         int timeout = configuration.getIntegerProperty(Constants.PROPERTY_SERVER_STATE_CHANGE_TIMEOUT);
144         config.setProperty(Constants.PROPERTY_RETRY_DELAY, RETRY_INTERVAL.toString());
145         config.setProperty(Constants.PROPERTY_RETRY_LIMIT, NO_OF_ATTEMPTS.toString());
146         Context context = server.getContext();
147         String msg;
148         boolean status = false;
149         while (rc.attempt()) {
150             if ( Server.Status.ERROR.equals(server.getStatus())) {
151                 msg = "Device status " + Server.Status.ERROR + " cannot proceed further";
152                 throw new RequestFailedException("Waiting for State Change", msg, HttpStatus.CONFLICT_409, server);
153             } else {
154                 server.waitForStateChange(pollInterval, timeout, desiredStates);
155                 if (Server.Status.RUNNING.equals(server.getStatus())
156                         || Server.Status.READY.equals(server.getStatus())) {
157                     status = true;
158                 }
159                 logger.info(server.getStatus() + " status ");
160             }
161             if (status) {
162                 logger.info("Done Trying " + rc.getAttempts() + " attempts");
163                 break;
164             } else {
165                 rc.delay();
166             }
167         }
168         if (rc.isFailed()) {
169             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, vmUrl);
170             logger.info("waitForStateChange Failed");
171             logger.error(msg);
172             throw new RequestFailedException("Waiting for State Change", msg, HttpStatus.BAD_GATEWAY_502, server);
173         }
174         if ((rc.getAttempts() == NO_OF_ATTEMPTS) && (!status)) {
175
176             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, Long.toString(rc.getRetryDelay()),
177                     Integer.toString(rc.getAttempts()), Integer.toString(rc.getRetryLimit()));
178             logger.error(msg);
179             throw new TimeoutException(msg);
180         }
181         rc.reset();
182         logger.info("Reboot server status flag --> " + status);
183         return status;
184     }
185 }