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=========================================================
24 package org.onap.appc.adapter.iaas.provider.operation.impl;
26 import com.att.cdp.exceptions.ResourceNotFoundException;
27 import com.att.cdp.exceptions.ZoneException;
28 import com.att.cdp.zones.Context;
29 import com.att.cdp.zones.model.ModelObject;
30 import com.att.cdp.zones.model.Server;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33 import com.att.eelf.i18n.EELFResourceManager;
34 import org.glassfish.grizzly.http.util.HttpStatus;
35 import org.onap.appc.Constants;
36 import org.onap.appc.adapter.iaas.ProviderAdapter;
37 import org.onap.appc.adapter.iaas.impl.IdentityURL;
38 import org.onap.appc.adapter.iaas.impl.RequestContext;
39 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
40 import org.onap.appc.adapter.iaas.impl.VMURL;
41 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
42 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Outcome;
43 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
44 import org.onap.appc.exceptions.UnknownProviderException;
45 import org.onap.appc.i18n.Msg;
46 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
48 import org.onap.appc.logging.LoggingConstants;
49 import org.onap.appc.logging.LoggingUtils;
50 import java.text.DateFormat;
51 import java.text.SimpleDateFormat;
52 import java.util.Date;
54 import java.util.TimeZone;
55 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.RESTART_SERVICE;
56 import static org.onap.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();
64 * This method handles the case of restarting a server once we have found the
65 * server and have obtained the abstract representation of the server via the
66 * context (i.e., the "Server" object from the CDP-Zones abstraction).
69 * The request context that manages the state and recovery of the
70 * request for the life of its processing.
72 * The server object representing the server we want to operate on
73 * @throws ZoneException
75 * @throws RequestFailedException
76 * when server status is error.
78 @SuppressWarnings("nls")
79 private void restartServer(RequestContext rc, Server server, SvcLogicContext ctx)
80 throws ZoneException, RequestFailedException {
82 * Pending is a bit of a special case. If we find the server is in a pending
83 * state, then the provider is in the process of changing state of the server.
84 * So, lets try to wait a little bit and see if the state settles down to one we
85 * can deal with. If not, then we have to fail the request.
88 if (server.getStatus().equals(Server.Status.PENDING)) {
89 waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
90 Server.Status.SUSPENDED, Server.Status.PAUSED);
92 setTimeForMetricsLogger("restart server");
93 String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
94 if (skipHypervisorCheck == null && ctx != null) {
95 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);
104 * We determine what to do based on the current state of the server
106 switch (server.getStatus()) {
108 // Nothing to do, the server is gone
109 msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(), server.getTenantId(),
111 generateEvent(rc, false, msg);
113 metricsLogger.error(msg);
116 // Attempt to stop and start the server
117 stopServer(rc, server);
118 startServer(rc, server);
119 generateEvent(rc, true, Outcome.SUCCESS.toString());
120 metricsLogger.info("Server status: RUNNING");
123 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
124 server.getTenantId(), "restart");
125 generateEvent(rc, false, msg);
127 metricsLogger.error(msg);
128 throw new RequestFailedException("Restart Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
130 // Attempt to start the server
131 startServer(rc, server);
132 generateEvent(rc, true, Outcome.SUCCESS.toString());
133 metricsLogger.info("Server status: READY");
136 // if paused, un-pause it
137 unpauseServer(rc, server);
138 generateEvent(rc, true, Outcome.SUCCESS.toString());
139 metricsLogger.info("Server status: PAUSED");
142 // Attempt to resume the suspended server
143 resumeServer(rc, server);
144 generateEvent(rc, true, Outcome.SUCCESS.toString());
145 metricsLogger.info("Server status: SUSPENDED");
148 // Hmmm, unknown status, should never occur
149 msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
150 server.getTenantId(), server.getStatus().name());
151 generateEvent(rc, false, msg);
153 metricsLogger.error(msg);
159 * This method is used to restart an existing virtual machine given the fully
160 * qualified URL of the machine.
162 * The fully qualified URL contains enough information to locate the appropriate
163 * server. The URL is of the form
166 * [scheme]://[host[:port]] / [path] / [tenant_id] / servers / [vm_id]
169 * Where the various parts of the URL can be parsed and extracted and used to
170 * locate the appropriate service in the provider service catalog. This then
171 * allows us to open a context using the CDP abstraction, obtain the server by
172 * its UUID, and then perform the restart.
175 * @throws UnknownProviderException
176 * If the provider cannot be found
177 * @throws IllegalArgumentException
178 * if the expected argument(s) are not defined or are invalid
179 * @see org.onap.appc.adapter.iaas.ProviderAdapter#restartServer(java.util.Map,
180 * org.onap.ccsdk.sli.core.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);
188 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
190 * Set Time for Metrics Logger
192 setTimeForMetricsLogger("GET server status");
193 ctx.setAttribute("RESTART_STATUS", "ERROR");
195 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
196 ProviderAdapter.PROPERTY_PROVIDER_NAME);
197 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
198 VMURL vm = VMURL.parseURL(vm_url);
199 if (validateVM(rc, appName, vm_url, vm))
201 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
202 String identStr = (ident == null) ? null : ident.toString();
203 Context context = null;
204 String tenantName = "Unknown";// to be used also in case of exception
206 context = getContext(rc, vm_url, identStr);
207 if (context != null) {
208 tenantName = context.getTenantName();// this varaible also is used in case of exception
210 server = lookupServer(rc, context, vm.getServerId());
211 logger.debug(Msg.SERVER_FOUND, vm_url, tenantName, server.getStatus().toString());
213 restartServer(rc, server, ctx);
216 ctx.setAttribute("RESTART_STATUS", "SUCCESS");
217 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "RestartServer", vm_url);
218 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
220 } catch (RequestFailedException e) {
221 doFailure(rc, e.getStatus(), e.getMessage());
222 } catch (ResourceNotFoundException e) {
223 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
225 metricsLogger.error(msg);
226 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
227 } catch (Exception e1) {
228 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1,
229 e1.getClass().getSimpleName(), RESTART_SERVICE.toString(), vm_url, tenantName);
230 logger.error(msg, e1);
231 metricsLogger.error(msg, e1);
232 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
234 } catch (RequestFailedException e) {
235 doFailure(rc, e.getStatus(), e.getMessage());
241 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
242 throws UnknownProviderException {
243 setMDC(RESTART_SERVICE.toString(), "App-C IaaS Adapter:Restart", ADAPTER_NAME);
244 logOperation(Msg.RESTARTING_SERVER, params, context);
245 setTimeForMetricsLogger("execute restart");
246 metricsLogger.info("Executing Provider Operation: Restart");
247 return restartServer(params, context);
250 private void setTimeForMetricsLogger(String targetServiceName) {
251 String timestamp = LoggingUtils.generateTimestampStr(((Date) new Date()).toInstant());
252 MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, timestamp);
253 MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, timestamp);
254 MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, "0");
255 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, LoggingConstants.StatusCodes.COMPLETE);
256 MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, "cdp");
257 MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, targetServiceName);
258 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME,
259 "org.onap.appc.adapter.iaas.provider.operation.impl.RestartServer");