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.util.Date;
52 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.RESTART_SERVICE;
53 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
55 public class RestartServer extends ProviderServerOperation {
57 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestartServer.class);
58 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
61 * This method handles the case of restarting a server once we have found the
62 * server and have obtained the abstract representation of the server via the
63 * context (i.e., the "Server" object from the CDP-Zones abstraction).
66 * The request context that manages the state and recovery of the
67 * request for the life of its processing.
69 * The server object representing the server we want to operate on
70 * @throws ZoneException
72 * @throws RequestFailedException
73 * when server status is error.
75 @SuppressWarnings("nls")
76 private void restartServer(RequestContext rc, Server server, SvcLogicContext ctx)
77 throws ZoneException, RequestFailedException {
79 * Pending is a bit of a special case. If we find the server is in a pending
80 * state, then the provider is in the process of changing state of the server.
81 * So, lets try to wait a little bit and see if the state settles down to one we
82 * can deal with. If not, then we have to fail the request.
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);
89 setTimeForMetricsLogger("restart server");
90 String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
91 if (skipHypervisorCheck == null && ctx != null) {
92 skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
94 // Always perform Virtual Machine/Hypervisor Status/Network checks
95 // unless the skip is set to true
96 if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
97 // Check of the Hypervisor for the VM Server is UP and reachable
98 checkHypervisor(server);
101 * We determine what to do based on the current state of the server
103 switch (server.getStatus()) {
105 // Nothing to do, the server is gone
106 msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(), server.getTenantId(),
108 generateEvent(rc, false, msg);
110 metricsLogger.error(msg);
113 // Attempt to stop and start the server
114 stopServer(rc, server);
115 startServer(rc, server);
116 generateEvent(rc, true, Outcome.SUCCESS.toString());
117 metricsLogger.info("Server status: RUNNING");
120 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
121 server.getTenantId(), "restart");
122 generateEvent(rc, false, msg);
124 metricsLogger.error(msg);
125 throw new RequestFailedException("Restart Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
127 // Attempt to start the server
128 startServer(rc, server);
129 generateEvent(rc, true, Outcome.SUCCESS.toString());
130 metricsLogger.info("Server status: READY");
133 // if paused, un-pause it
134 unpauseServer(rc, server);
135 generateEvent(rc, true, Outcome.SUCCESS.toString());
136 metricsLogger.info("Server status: PAUSED");
139 // Attempt to resume the suspended server
140 resumeServer(rc, server);
141 generateEvent(rc, true, Outcome.SUCCESS.toString());
142 metricsLogger.info("Server status: SUSPENDED");
145 // Hmmm, unknown status, should never occur
146 msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
147 server.getTenantId(), server.getStatus().name());
148 generateEvent(rc, false, msg);
150 metricsLogger.error(msg);
156 * This method is used to restart an existing virtual machine given the fully
157 * qualified URL of the machine.
159 * The fully qualified URL contains enough information to locate the appropriate
160 * server. The URL is of the form
163 * [scheme]://[host[:port]] / [path] / [tenant_id] / servers / [vm_id]
166 * Where the various parts of the URL can be parsed and extracted and used to
167 * locate the appropriate service in the provider service catalog. This then
168 * allows us to open a context using the CDP abstraction, obtain the server by
169 * its UUID, and then perform the restart.
172 * @throws UnknownProviderException
173 * If the provider cannot be found
174 * @throws IllegalArgumentException
175 * if the expected argument(s) are not defined or are invalid
176 * @see org.onap.appc.adapter.iaas.ProviderAdapter#restartServer(java.util.Map,
177 * org.onap.ccsdk.sli.core.sli.SvcLogicContext)
179 @SuppressWarnings("nls")
180 private Server restartServer(Map<String, String> params, SvcLogicContext ctx)
181 throws UnknownProviderException, IllegalArgumentException {
182 Server server = null;
183 RequestContext rc = new RequestContext(ctx);
185 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
187 * Set Time for Metrics Logger
189 setTimeForMetricsLogger("GET server status");
190 ctx.setAttribute("RESTART_STATUS", "ERROR");
192 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
193 ProviderAdapter.PROPERTY_PROVIDER_NAME);
194 String vmUrl = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
195 VMURL vm = VMURL.parseURL(vmUrl);
196 if (validateVM(rc, appName, vmUrl, vm))
198 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
199 String identStr = (ident == null) ? null : ident.toString();
200 Context context = null;
201 String tenantName = "Unknown";// to be used also in case of exception
203 context = getContext(rc, vmUrl, identStr);
204 if (context != null) {
205 tenantName = context.getTenantName();// this varaible also is used in case of exception
207 server = lookupServer(rc, context, vm.getServerId());
208 logger.debug(Msg.SERVER_FOUND, vmUrl, tenantName, server.getStatus().toString());
210 restartServer(rc, server, ctx);
213 ctx.setAttribute("RESTART_STATUS", "SUCCESS");
214 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "RestartServer", vmUrl);
215 ctx.setAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
217 } catch (RequestFailedException e) {
218 doFailure(rc, e.getStatus(), e.getMessage());
219 } catch (ResourceNotFoundException e) {
220 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
222 metricsLogger.error(msg);
223 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
224 } catch (Exception e1) {
225 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1,
226 e1.getClass().getSimpleName(), RESTART_SERVICE.toString(), vmUrl, tenantName);
227 logger.error(msg, e1);
228 metricsLogger.error(msg, e1);
229 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
231 } catch (RequestFailedException e) {
232 doFailure(rc, e.getStatus(), e.getMessage());
238 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
239 throws UnknownProviderException {
240 setMDC(RESTART_SERVICE.toString(), "App-C IaaS Adapter:Restart", ADAPTER_NAME);
241 logOperation(Msg.RESTARTING_SERVER, params, context);
242 setTimeForMetricsLogger("execute restart");
243 metricsLogger.info("Executing Provider Operation: Restart");
244 return restartServer(params, context);
247 private void setTimeForMetricsLogger(String targetServiceName) {
248 String timestamp = LoggingUtils.generateTimestampStr(((Date) new Date()).toInstant());
249 MDC.put(LoggingConstants.MDCKeys.BEGIN_TIMESTAMP, timestamp);
250 MDC.put(LoggingConstants.MDCKeys.END_TIMESTAMP, timestamp);
251 MDC.put(LoggingConstants.MDCKeys.ELAPSED_TIME, "0");
252 MDC.put(LoggingConstants.MDCKeys.STATUS_CODE, LoggingConstants.StatusCodes.COMPLETE);
253 MDC.put(LoggingConstants.MDCKeys.TARGET_ENTITY, "cdp");
254 MDC.put(LoggingConstants.MDCKeys.TARGET_SERVICE_NAME, targetServiceName);
255 MDC.put(LoggingConstants.MDCKeys.CLASS_NAME,
256 "org.onap.appc.adapter.iaas.provider.operation.impl.RestartServer");