2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.adapter.iaas.provider.operation.impl;
25 import org.openecomp.appc.Constants;
26 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
27 import org.openecomp.appc.adapter.iaas.impl.IdentityURL;
28 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
29 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
30 import org.openecomp.appc.adapter.iaas.impl.VMURL;
31 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation;
32 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
33 import org.openecomp.appc.configuration.Configuration;
34 import org.openecomp.appc.configuration.ConfigurationFactory;
35 import org.openecomp.appc.exceptions.APPCException;
36 import org.openecomp.appc.i18n.Msg;
37 import com.att.cdp.exceptions.ContextConnectionException;
38 import com.att.cdp.exceptions.ResourceNotFoundException;
39 import com.att.cdp.exceptions.ZoneException;
40 import com.att.cdp.zones.ComputeService;
41 import com.att.cdp.zones.Context;
42 import com.att.cdp.zones.model.ModelObject;
43 import com.att.cdp.zones.model.Server;
44 import com.att.eelf.configuration.EELFLogger;
45 import com.att.eelf.configuration.EELFManager;
46 import com.att.eelf.i18n.EELFResourceManager;
47 import org.openecomp.sdnc.sli.SvcLogicContext;
48 import org.glassfish.grizzly.http.util.HttpStatus;
50 import java.util.Arrays;
51 import java.util.Collection;
54 import static org.openecomp.appc.adapter.iaas.provider.operation.common.constants.Constants.MDC_SERVICE;
55 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.MIGRATE_SERVICE;
56 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
59 import java.text.DateFormat;
60 import java.text.SimpleDateFormat;
61 import java.util.Date;
62 import java.util.TimeZone;
66 * @since September 26, 2016
68 public class MigrateServer extends ProviderServerOperation {
70 private static final EELFLogger logger = EELFManager.getInstance().getLogger(EvacuateServer.class);
71 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
72 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
75 * A list of valid initial VM statuses for a migrate operations
77 private final Collection<Server.Status> migratableStatuses = Arrays.asList(Server.Status.READY, Server.Status.RUNNING, Server.Status.SUSPENDED);
80 private String getConnectionExceptionMessage(RequestContext rc, Context ctx, ContextConnectionException e)
81 throws ZoneException {
82 return EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, ctx.getProvider().getName(),
83 ctx.getComputeService().getURL(), ctx.getTenant().getName(), ctx.getTenant().getId(), e.getMessage(),
84 Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
85 Integer.toString(rc.getRetryLimit()));
88 private void migrateServer(RequestContext rc, Server server, SvcLogicContext svcCtx)
89 throws ZoneException, RequestFailedException {
91 Context ctx = server.getContext();
92 ComputeService service = ctx.getComputeService();
94 // Init status will equal final status
95 Server.Status initialStatus = server.getStatus();
97 if (initialStatus == null) {
98 throw new ZoneException("Failed to determine server's starting status");
101 // We can only migrate certain statuses
102 if (!migratableStatuses.contains(initialStatus)) {
103 throw new ZoneException(String.format("Cannot migrate server that is in %s state. Must be in one of [%s]",
104 initialStatus, migratableStatuses));
108 * Set Time for Metrics Logger
110 long startTime = System.currentTimeMillis();
111 TimeZone tz = TimeZone.getTimeZone("UTC");
112 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
114 String startTimeStr = df.format(new Date());
115 long endTime = System.currentTimeMillis();
116 long duration = endTime - startTime;
117 String endTimeStr = String.valueOf(endTime);
118 String durationStr = String.valueOf(duration);
119 String endTimeStrUTC = df.format(new Date());
120 MDC.put("EndTimestamp", endTimeStrUTC);
121 MDC.put("ElapsedTime", durationStr);
122 MDC.put("TargetEntity", "cdp");
123 MDC.put("TargetServiceName", "migrate server");
124 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.MigrateServer");
125 // Is the skip Hypervisor check attribute populated?
126 String skipHypervisorCheck = null;
127 if (svcCtx != null) {
128 skipHypervisorCheck = svcCtx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
132 // // Always perform Hypervisor check
133 // unless the skip is set to true
135 if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
137 // Check of the Hypervisor for the VM Server is UP and reachable
139 checkHypervisor(server);
143 boolean inConfirmPhase = false;
145 while (rc.attempt()) {
147 if (!inConfirmPhase) {
148 // Initial migrate request
149 service.migrateServer(server.getId());
150 // Wait for change to verify resize
151 waitForStateChange(rc, server, Server.Status.READY);
152 inConfirmPhase = true;
156 service.processResize(server);
157 // Wait for complete. will go back to init status
158 waitForStateChange(rc, server, initialStatus);
159 logger.info("Completed migrate request successfully");
160 metricsLogger.info("Completed migrate request successfully");
162 } catch (ContextConnectionException e) {
163 msg = getConnectionExceptionMessage(rc, ctx, e);
164 logger.error(msg, e);
165 metricsLogger.error(msg, e);
169 } catch (ZoneException e) {
170 String phase = inConfirmPhase ? "VERIFY MIGRATE" : "REQUEST MIGRATE";
171 msg = EELFResourceManager.format(Msg.MIGRATE_SERVER_FAILED, server.getName(), server.getId(), phase,
173 generateEvent(rc, false, msg);
174 logger.error(msg, e);
175 metricsLogger.error(msg, e);
176 throw new RequestFailedException("Migrate Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
182 * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#migrateServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
184 private Server migrateServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
185 Server server = null;
186 RequestContext rc = new RequestContext(ctx);
189 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
193 * Set Time for Metrics Logger
195 long startTime = System.currentTimeMillis();
196 TimeZone tz = TimeZone.getTimeZone("UTC");
197 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
199 String startTimeStr = df.format(new Date());
200 long endTime = System.currentTimeMillis();
201 long duration = endTime - startTime;
202 String endTimeStr = String.valueOf(endTime);
203 String durationStr = String.valueOf(duration);
204 String endTimeStrUTC = df.format(new Date());
205 MDC.put("EndTimestamp", endTimeStrUTC);
206 MDC.put("ElapsedTime", durationStr);
207 MDC.put("TargetEntity", "cdp");
208 MDC.put("TargetServiceName", "migrate server");
209 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.MigrateServer");
212 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
213 ProviderAdapter.PROPERTY_PROVIDER_NAME);
214 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
216 VMURL vm = VMURL.parseURL(vm_url);
217 if (validateVM(rc, appName, vm_url, vm)) return null;
219 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
220 String identStr = (ident == null) ? null : ident.toString();
222 Context context = null;
224 context = getContext(rc, vm_url, identStr);
225 if (context != null) {
226 server = lookupServer(rc, context, vm.getServerId());
227 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
228 migrateServer(rc, server, ctx);
229 server.refreshStatus();
233 } catch (RequestFailedException e) {
234 doFailure(rc, e.getStatus(), e.getMessage());
236 catch (ResourceNotFoundException e) {
237 msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
239 metricsLogger.error(msg);
240 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
241 } catch (Throwable t) {
242 msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
243 MIGRATE_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
244 logger.error(msg, t);
245 metricsLogger.error(msg);
246 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
248 } catch (RequestFailedException e) {
249 doFailure(rc, e.getStatus(), e.getMessage());
256 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
258 setMDC(Operation.MIGRATE_SERVICE.toString(), "App-C IaaS Adapter:Migrate", ADAPTER_NAME);
259 logOperation(Msg.MIGRATING_SERVER, params, context);
262 * Set Time for Metrics Logger
264 long startTime = System.currentTimeMillis();
265 TimeZone tz = TimeZone.getTimeZone("UTC");
266 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
268 String startTimeStr = df.format(new Date());
269 long endTime = System.currentTimeMillis();
270 long duration = endTime - startTime;
271 String endTimeStr = String.valueOf(endTime);
272 String durationStr = String.valueOf(duration);
273 String endTimeStrUTC = df.format(new Date());
274 MDC.put("EndTimestamp", endTimeStrUTC);
275 MDC.put("ElapsedTime", durationStr);
276 MDC.put("TargetEntity", "cdp");
277 MDC.put("TargetServiceName", "migrate server");
278 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.MigrateServer");
281 metricsLogger.info("Executing Provider Operation: Migrate");
283 return migrateServer(params,context);