b6cf8cc58bcfcc6b1beda5bdd55b10f3d76c13be
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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
54 public class RebootServer extends ProviderServerOperation {
55     private final EELFLogger logger = EELFManager.getInstance().getLogger(RebootServer.class);
56     private static final Configuration config = ConfigurationFactory.getConfiguration();
57     private static final Integer NO_OF_ATTEMPTS=30;
58     private static final Integer RETRY_INTERVAL=10;
59
60     @Override
61     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
62             throws APPCException {
63         setMDC(Operation.REBOOT_SERVICE.toString(), "App-C IaaS Adapter:rebootServer", ADAPTER_NAME);
64         return rebootServer(params, context);
65     }
66
67     private Server rebootServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
68         Server server = null;
69         RequestContext requestContext = new RequestContext(ctx);
70         requestContext.isAlive();
71         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
72         String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
73         String rebooType = params.get(ProviderAdapter.REBOOT_TYPE);
74         String tenantName = "Unknown";
75         if (rebooType.isEmpty() ) {
76             rebooType = "SOFT";
77         }
78         logger.info("reboot type" + rebooType);
79         try {
80             VMURL vm = VMURL.parseURL(vmUrl);
81             Context context;
82             // to be used also in case of exception
83             if (validateVM(requestContext, appName, vmUrl, vm)) {
84                 return null;
85             }
86             IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
87             String identStr = (ident == null) ? null : ident.toString();
88             context = getContext(requestContext, vmUrl, identStr);
89             if (context != null) {
90                 tenantName = context.getTenantName();// this variable also is
91                 // used in case of exception
92                 requestContext.reset();
93                 server = lookupServer(requestContext, context, vm.getServerId());
94                 logger.debug(Msg.SERVER_FOUND, vmUrl, context.getTenantName(), server.getStatus().toString());
95                 Context contx = server.getContext();
96                 ComputeService service = contx.getComputeService();
97                 logger.info("performing reboot action for " + server.getId() + " rebootype " + rebooType);
98                 service.rebootServer(server.getId(), rebooType);
99                 if (waitForServerStatusChange(requestContext, server, vmUrl, Server.Status.RUNNING)) {
100                     ctx.setAttribute("REBOOT_STATUS", "SUCCESS");
101                     doSuccess(requestContext);
102                 } else {
103                     ctx.setAttribute("REBOOT_STATUS", "FAILURE");
104                 }
105                 context.close();
106             } else {
107                 ctx.setAttribute("REBOOT_STATUS", "FAILURE");
108             }
109
110         } catch (ResourceNotFoundException | StateException ex) {
111             logger.info(ex.getMessage());
112             ctx.setAttribute("REBOOT_STATUS", "FAILURE");
113             if (ex instanceof ResourceNotFoundException) {
114                 doFailure(requestContext, HttpStatus.NOT_FOUND_404, ex.getMessage());
115             } else {
116                 doFailure(requestContext, HttpStatus.CONFLICT_409, ex.getMessage());
117             }
118         } catch (Exception ex) {
119             logger.info(ex.getMessage());
120             ctx.setAttribute("REBOOT_STATUS", "FAILURE");
121             doFailure(requestContext, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.getMessage());
122         }
123         return server;
124     }
125
126     private boolean waitForServerStatusChange(RequestContext rc, Server server, String vmUrl,
127             Server.Status... desiredStates) throws Exception {
128         int pollInterval = RETRY_INTERVAL.intValue();
129         int timeout = configuration.getIntegerProperty(Constants.PROPERTY_SERVER_STATE_CHANGE_TIMEOUT);
130         config.setProperty(Constants.PROPERTY_RETRY_DELAY, RETRY_INTERVAL.toString());
131         config.setProperty(Constants.PROPERTY_RETRY_LIMIT, NO_OF_ATTEMPTS.toString());
132         Context context = server.getContext();
133         String msg;
134         boolean status = false;
135         while (rc.attempt()) {
136                 server.waitForStateChange(pollInterval, timeout, desiredStates);
137                 if ((server.getStatus().equals(Server.Status.RUNNING)) || (server.getStatus().equals(Server.Status.READY))) {
138                     status = true;
139                 }
140                 logger.info(server.getStatus() + " status ");
141             if (status) {
142                 logger.info("Done Trying " + rc.getAttempts() + " attempts");
143                 break;
144             } else {
145                 rc.delay();
146             }
147         }
148
149         if (rc.isFailed()) {
150             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, vmUrl);
151             logger.info("waitForStateChange Failed");
152             logger.error(msg);
153             throw new RequestFailedException("Waiting for State Change", msg, HttpStatus.BAD_GATEWAY_502, server);
154         }
155         if ((rc.getAttempts() == NO_OF_ATTEMPTS) && (!status)) {
156
157             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, Long.toString(rc.getRetryDelay()),
158                     Integer.toString(rc.getAttempts()), Integer.toString(rc.getRetryLimit()));
159             logger.error(msg);
160             throw new TimeoutException(msg);
161         }
162
163         rc.reset();
164         logger.info("Reboot server status flag --> " + status);
165         return status;
166     }
167 }