2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.adapter.iaas.provider.operation.impl;
27 import com.att.cdp.exceptions.ResourceNotFoundException;
28 import com.att.cdp.exceptions.ZoneException;
29 import com.att.cdp.zones.Context;
30 import com.att.cdp.zones.model.ModelObject;
31 import com.att.cdp.zones.model.Server;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34 import com.att.eelf.i18n.EELFResourceManager;
35 import org.glassfish.grizzly.http.util.HttpStatus;
36 import org.openecomp.appc.Constants;
37 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
38 import org.openecomp.appc.adapter.iaas.impl.IdentityURL;
39 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
40 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
41 import org.openecomp.appc.adapter.iaas.impl.VMURL;
42 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Outcome;
43 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
44 import org.openecomp.appc.exceptions.UnknownProviderException;
45 import org.openecomp.appc.i18n.Msg;
46 import org.openecomp.sdnc.sli.SvcLogicContext;
49 import java.text.DateFormat;
50 import java.text.SimpleDateFormat;
51 import java.util.Date;
53 import java.util.TimeZone;
55 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.RESTART_SERVICE;
56 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
58 public class RestartServer extends ProviderServerOperation {
60 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestartServer.class);
61 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
65 * This method handles the case of restarting a server once we have found the server and have obtained the abstract
66 * representation of the server via the context (i.e., the "Server" object from the CDP-Zones abstraction).
68 * @param rc The request context that manages the state and recovery of the request for the life of
70 * @param server The server object representing the server we want to operate on
71 * @throws ZoneException when error occurs.
72 * @throws RequestFailedException when server status is error.
74 @SuppressWarnings("nls")
75 private void restartServer(RequestContext rc, Server server, SvcLogicContext ctx)
76 throws ZoneException, RequestFailedException {
78 * Pending is a bit of a special case. If we find the server is in a
79 * pending state, then the provider is in the process of changing state
80 * of the server. So, lets try to wait a little bit and see if the state
81 * settles down to one we can deal with. If not, then we have to fail
85 if (server.getStatus().equals(Server.Status.PENDING)) {
86 waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
87 Server.Status.SUSPENDED, Server.Status.PAUSED);
90 setTimeForMetricsLogger("restart server");
92 String skipHypervisorCheck = null;
94 skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
97 // Always perform Virtual Machine/Hypervisor Status/Network checks
98 // unless the skip is set to true
99 if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
100 // Check of the Hypervisor for the VM Server is UP and reachable
101 checkHypervisor(server);
105 * We determine what to do based on the current state of the server
107 switch (server.getStatus()) {
109 // Nothing to do, the server is gone
110 msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
111 server.getTenantId(), "restarted");
112 generateEvent(rc, false, msg);
114 metricsLogger.error(msg);
118 // Attempt to stop and start the server
119 stopServer(rc, server);
120 startServer(rc, server);
121 generateEvent(rc, true, Outcome.SUCCESS.toString());
122 metricsLogger.info("Server status: RUNNING");
126 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
127 server.getTenantId(), "rebuild");
128 generateEvent(rc, false, msg);
130 metricsLogger.error(msg);
131 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
134 // Attempt to start the server
135 startServer(rc, server);
136 generateEvent(rc, true, Outcome.SUCCESS.toString());
137 metricsLogger.info("Server status: READY");
141 // if paused, un-pause it
142 unpauseServer(rc, server);
143 generateEvent(rc, true, Outcome.SUCCESS.toString());
144 metricsLogger.info("Server status: PAUSED");
148 // Attempt to resume the suspended server
149 resumeServer(rc, server);
150 generateEvent(rc, true, Outcome.SUCCESS.toString());
151 metricsLogger.info("Server status: SUSPENDED");
155 // Hmmm, unknown status, should never occur
156 msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
157 server.getTenantId(), server.getStatus().name());
158 generateEvent(rc, false, msg);
160 metricsLogger.error(msg);
168 * This method is used to restart an existing virtual machine given the fully qualified URL of the machine.
170 * The fully qualified URL contains enough information to locate the appropriate server. The URL is of the form
172 * [scheme]://[host[:port]] / [path] / [tenant_id] / servers / [vm_id]
173 * </pre> Where the various parts of the URL can be parsed and extracted and used to locate the appropriate service
174 * in the provider service catalog. This then allows us to open a context using the CDP abstraction, obtain the
175 * server by its UUID, and then perform the restart.
178 * @throws UnknownProviderException If the provider cannot be found
179 * @throws IllegalArgumentException if the expected argument(s) are not defined or are invalid
180 * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#restartServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
182 @SuppressWarnings("nls")
183 private Server restartServer(Map<String, String> params, SvcLogicContext ctx)
184 throws UnknownProviderException, IllegalArgumentException {
185 Server server = null;
186 RequestContext rc = new RequestContext(ctx);
189 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
192 * Set Time for Metrics Logger
194 setTimeForMetricsLogger("GET server status");
196 ctx.setAttribute("RESTART_STATUS", "ERROR");
198 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
199 ProviderAdapter.PROPERTY_PROVIDER_NAME);
201 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
203 VMURL vm = VMURL.parseURL(vm_url);
204 if (validateVM(rc, appName, vm_url, vm)) return null;
206 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
207 String identStr = (ident == null) ? null : ident.toString();
209 Context context = null;
211 context = getContext(rc, vm_url, identStr);
212 if (context != null) {
214 server = lookupServer(rc, context, vm.getServerId());
215 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
217 restartServer(rc, server, ctx);
220 ctx.setAttribute("RESTART_STATUS", "SUCCESS");
221 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE,
222 "RestartServer", vm_url);
223 ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
225 } catch (RequestFailedException e) {
226 doFailure(rc, e.getStatus(), e.getMessage());
227 } catch (ResourceNotFoundException e) {
228 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
230 metricsLogger.error(msg);
231 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
232 } catch (Exception e1) {
233 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
234 RESTART_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
235 logger.error(msg, e1);
236 metricsLogger.error(msg, e1);
237 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
239 } catch (RequestFailedException e) {
240 doFailure(rc, e.getStatus(), e.getMessage());
247 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
248 throws UnknownProviderException {
249 setMDC(RESTART_SERVICE.toString(), "App-C IaaS Adapter:Restart", ADAPTER_NAME);
250 logOperation(Msg.RESTARTING_SERVER, params, context);
252 setTimeForMetricsLogger("execute restart");
254 metricsLogger.info("Executing Provider Operation: Restart");
256 return restartServer(params, context);
259 private void setTimeForMetricsLogger(String targetServiceName) {
260 long startTime = System.currentTimeMillis();
261 TimeZone tz = TimeZone.getTimeZone("UTC");
262 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
264 long endTime = System.currentTimeMillis();
265 long duration = endTime - startTime;
266 String durationStr = String.valueOf(duration);
267 String endTimeStrUTC = df.format(new Date());
268 MDC.put("EndTimestamp", endTimeStrUTC);
269 MDC.put("ElapsedTime", durationStr);
270 MDC.put("TargetEntity", "cdp");
271 MDC.put("TargetServiceName", targetServiceName);
272 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.RestartServer");