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 org.openecomp.appc.Constants;
28 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
29 import org.openecomp.appc.adapter.iaas.impl.IdentityURL;
30 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
31 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
32 import org.openecomp.appc.adapter.iaas.impl.VMURL;
33 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Outcome;
34 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
35 import org.openecomp.appc.exceptions.UnknownProviderException;
36 import org.openecomp.appc.i18n.Msg;
37 import com.att.cdp.exceptions.ResourceNotFoundException;
38 import com.att.cdp.exceptions.ZoneException;
39 import com.att.cdp.zones.Context;
40 import com.att.cdp.zones.NetworkService;
41 import com.att.cdp.zones.model.ModelObject;
42 import com.att.cdp.zones.model.Network;
43 import com.att.cdp.zones.model.Port;
44 import com.att.cdp.zones.model.Server;
45 import com.att.cdp.zones.model.Subnet;
46 import com.att.eelf.configuration.EELFLogger;
47 import com.att.eelf.configuration.EELFManager;
48 import com.att.eelf.i18n.EELFResourceManager;
49 import org.openecomp.sdnc.sli.SvcLogicContext;
50 import org.glassfish.grizzly.http.util.HttpStatus;
53 import java.text.DateFormat;
54 import java.text.SimpleDateFormat;
55 import java.util.Date;
56 import java.util.List;
58 import java.util.TimeZone;
60 import static org.openecomp.appc.adapter.iaas.provider.operation.common.constants.Constants.MDC_SERVICE;
61 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.RESTART_SERVICE;
62 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
65 public class RestartServer extends ProviderServerOperation {
67 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestartServer.class);
68 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
72 * This method handles the case of restarting a server once we have found the server and have obtained the abstract
73 * representation of the server via the context (i.e., the "Server" object from the CDP-Zones abstraction).
76 * The request context that manages the state and recovery of the request for the life of its processing.
78 * The server object representing the server we want to operate on
79 * @throws ZoneException
81 @SuppressWarnings("nls")
82 private void restartServer(RequestContext rc, Server server, SvcLogicContext ctx)
83 throws ZoneException, RequestFailedException {
86 * Pending is a bit of a special case. If we find the server is in a
87 * pending state, then the provider is in the process of changing state
88 * of the server. So, lets try to wait a little bit and see if the state
89 * settles down to one we can deal with. If not, then we have to fail
93 if (server.getStatus().equals(Server.Status.PENDING)) {
94 waitForStateChange(rc, server, Server.Status.READY, Server.Status.RUNNING, Server.Status.ERROR,
95 Server.Status.SUSPENDED, Server.Status.PAUSED);
99 * Set Time for Metrics Logger
101 long startTime = System.currentTimeMillis();
102 TimeZone tz = TimeZone.getTimeZone("UTC");
103 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
105 String startTimeStr = df.format(new Date());
106 long endTime = System.currentTimeMillis();
107 long duration = endTime - startTime;
108 String endTimeStr = String.valueOf(endTime);
109 String durationStr = String.valueOf(duration);
110 String endTimeStrUTC = df.format(new Date());
111 MDC.put("EndTimestamp", endTimeStrUTC);
112 MDC.put("ElapsedTime", durationStr);
113 MDC.put("TargetEntity", "cdp");
114 MDC.put("TargetServiceName", "restart server");
115 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.RestartServer");
117 String skipHypervisorCheck = null;
119 skipHypervisorCheck = ctx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
123 // Always perform Virtual Machine/Hypervisor Status/Network checks
124 // unless the skip is set to true
125 if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
127 // Check of the Hypervisor for the VM Server is UP and reachable
129 checkHypervisor(server);
134 * We determine what to do based on the current state of the server
137 switch (server.getStatus()) {
139 // Nothing to do, the server is gone
140 msg = EELFResourceManager.format(Msg.SERVER_DELETED, server.getName(), server.getId(),
141 server.getTenantId(), "restarted");
142 generateEvent(rc, false, msg);
144 metricsLogger.error(msg);
148 // Attempt to stop and start the server
149 stopServer(rc, server);
150 startServer(rc, server);
151 generateEvent(rc, true, Outcome.SUCCESS.toString());
152 metricsLogger.info("Server status: RUNNING");
156 msg = EELFResourceManager.format(Msg.SERVER_ERROR_STATE, server.getName(), server.getId(),
157 server.getTenantId(), "rebuild");
158 generateEvent(rc, false, msg);
160 metricsLogger.error(msg);
161 throw new RequestFailedException("Rebuild Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
164 // Attempt to start the server
165 startServer(rc, server);
166 generateEvent(rc, true, Outcome.SUCCESS.toString());
167 metricsLogger.info("Server status: READY");
171 // if paused, un-pause it
172 unpauseServer(rc, server);
173 generateEvent(rc, true, Outcome.SUCCESS.toString());
174 metricsLogger.info("Server status: PAUSED");
178 // Attempt to resume the suspended server
179 resumeServer(rc, server);
180 generateEvent(rc, true, Outcome.SUCCESS.toString());
181 metricsLogger.info("Server status: SUSPENDED");
185 // Hmmm, unknown status, should never occur
186 msg = EELFResourceManager.format(Msg.UNKNOWN_SERVER_STATE, server.getName(), server.getId(),
187 server.getTenantId(), server.getStatus().name());
188 generateEvent(rc, false, msg);
190 metricsLogger.error(msg);
198 * This method is used to restart an existing virtual machine given the fully qualified URL of the machine.
200 * The fully qualified URL contains enough information to locate the appropriate server. The URL is of the form
202 * [scheme]://[host[:port]] / [path] / [tenant_id] / servers / [vm_id]
203 * </pre> Where the various parts of the URL can be parsed and extracted and used to locate the appropriate service
204 * in the provider service catalog. This then allows us to open a context using the CDP abstraction, obtain the
205 * server by its UUID, and then perform the restart.
208 * @throws UnknownProviderException
209 * If the provider cannot be found
210 * @throws IllegalArgumentException
211 * if the expected argument(s) are not defined or are invalid
212 * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#restartServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
214 @SuppressWarnings("nls")
215 private Server restartServer(Map<String, String> params, SvcLogicContext ctx)
216 throws UnknownProviderException, IllegalArgumentException {
217 Server server = null;
218 RequestContext rc = new RequestContext(ctx);
221 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
225 * Set Time for Metrics Logger
227 long startTime = System.currentTimeMillis();
228 TimeZone tz = TimeZone.getTimeZone("UTC");
229 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
231 String startTimeStr = df.format(new Date());
232 long endTime = System.currentTimeMillis();
233 long duration = endTime - startTime;
234 String endTimeStr = String.valueOf(endTime);
235 String durationStr = String.valueOf(duration);
236 String endTimeStrUTC = df.format(new Date());
237 MDC.put("EndTimestamp", endTimeStrUTC);
238 MDC.put("ElapsedTime", durationStr);
239 MDC.put("TargetEntity", "cdp");
240 MDC.put("TargetServiceName", "GET server status");
241 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.RestartServer");
243 ctx.setAttribute("RESTART_STATUS", "ERROR");
245 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
246 ProviderAdapter.PROPERTY_PROVIDER_NAME);
248 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
250 VMURL vm = VMURL.parseURL(vm_url);
251 if (validateVM(rc, appName, vm_url, vm)) return null;
253 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
254 String identStr = (ident == null) ? null : ident.toString();
256 Context context = null;
258 context = getContext(rc, vm_url, identStr);
259 if (context != null) {
261 server = lookupServer(rc, context, vm.getServerId());
262 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
264 restartServer(rc, server, ctx);
267 ctx.setAttribute("RESTART_STATUS", "SUCCESS");
268 String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "RestartServer", vm_url);
269 ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
271 } catch (RequestFailedException e) {
272 doFailure(rc, e.getStatus(), e.getMessage());
274 catch (ResourceNotFoundException e) {
275 String msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
277 metricsLogger.error(msg);
278 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
279 } catch (Throwable t) {
280 String msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
281 RESTART_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
282 logger.error(msg, t);
283 metricsLogger.error(msg, t);
284 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
286 } catch (RequestFailedException e) {
287 doFailure(rc, e.getStatus(), e.getMessage());
294 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws UnknownProviderException {
296 setMDC(RESTART_SERVICE.toString(), "App-C IaaS Adapter:Restart", ADAPTER_NAME);
297 logOperation(Msg.RESTARTING_SERVER, params, context);
300 * Set Time for Metrics Logger
302 long startTime = System.currentTimeMillis();
303 TimeZone tz = TimeZone.getTimeZone("UTC");
304 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
306 String startTimeStr = df.format(new Date());
307 long endTime = System.currentTimeMillis();
308 long duration = endTime - startTime;
309 String endTimeStr = String.valueOf(endTime);
310 String durationStr = String.valueOf(duration);
311 String endTimeStrUTC = df.format(new Date());
312 MDC.put("EndTimestamp", endTimeStrUTC);
313 MDC.put("ElapsedTime", durationStr);
314 MDC.put("TargetEntity", "cdp");
315 MDC.put("TargetServiceName", "execute restart");
316 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.RestartServer");
318 metricsLogger.info("Executing Provider Operation: Restart");
321 return restartServer(params, context);