363f614554e5d9ae1c6beca5351fcb6b98dc349b
[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  * =============================================================================
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
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
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.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25 package org.onap.appc.adapter.iaas.provider.operation.impl;
26
27 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
28 import java.util.Map;
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;
55
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;
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) throws APPCException {
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         try {
83             VMURL vm = VMURL.parseURL(vmUrl);
84             Context context;
85             // to be used also in case of exception
86             if (validateVM(requestContext, appName, vmUrl, vm)) {
87                 return null;
88             }
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);
96             }
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);
108                 }
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);
116                 } else {
117                     ctx.setAttribute("REBOOT_STATUS", "FAILURE");
118                 }
119                 context.close();
120             } else {
121                 ctx.setAttribute("REBOOT_STATUS", "FAILURE");
122             }
123
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());
130             } else {
131                 doFailure(requestContext, HttpStatus.CONFLICT_409, ex.getMessage());
132             }
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());
138         }
139         return server;
140     }
141
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();
149         String msg;
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);
155             } else {
156                 server.waitForStateChange(pollInterval, timeout, desiredStates);
157                 if (Server.Status.RUNNING.equals(server.getStatus())
158                         || Server.Status.READY.equals(server.getStatus())) {
159                     status = true;
160                 }
161                 logger.info(server.getStatus() + " status ");
162             }
163             if (status) {
164                 logger.info("Done Trying " + rc.getAttempts() + " attempts");
165                 break;
166             } else {
167                 rc.delay();
168             }
169         }
170         if (rc.isFailed()) {
171             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, vmUrl);
172             logger.info("waitForStateChange Failed");
173             logger.error(msg);
174             throw new RequestFailedException("Waiting for State Change", msg, HttpStatus.BAD_GATEWAY_502, server);
175         }
176         if ((rc.getAttempts() == NO_OF_ATTEMPTS) && (!status)) {
177
178             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, Long.toString(rc.getRetryDelay()),
179                     Integer.toString(rc.getAttempts()), Integer.toString(rc.getRetryLimit()));
180             logger.error(msg);
181             throw new TimeoutException(msg);
182         }
183         rc.reset();
184         logger.info("Reboot server status flag --> " + status);
185         return status;
186     }
187 }