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.ContextConnectionException;
28 import com.att.cdp.exceptions.ResourceNotFoundException;
29 import com.att.cdp.exceptions.ZoneException;
30 import com.att.cdp.zones.ComputeService;
31 import com.att.cdp.zones.Context;
32 import com.att.cdp.zones.model.ModelObject;
33 import com.att.cdp.zones.model.Server;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36 import com.att.eelf.i18n.EELFResourceManager;
37 import org.glassfish.grizzly.http.util.HttpStatus;
38 import org.openecomp.appc.Constants;
39 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
40 import org.openecomp.appc.adapter.iaas.impl.IdentityURL;
41 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
42 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
43 import org.openecomp.appc.adapter.iaas.impl.VMURL;
44 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation;
45 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
46 import org.openecomp.appc.configuration.Configuration;
47 import org.openecomp.appc.configuration.ConfigurationFactory;
48 import org.openecomp.appc.exceptions.APPCException;
49 import org.openecomp.appc.i18n.Msg;
50 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
53 import java.text.DateFormat;
54 import java.text.SimpleDateFormat;
55 import java.util.Arrays;
56 import java.util.Collection;
57 import java.util.Date;
59 import java.util.TimeZone;
61 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.MIGRATE_SERVICE;
62 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
64 public class MigrateServer extends ProviderServerOperation {
66 private static final EELFLogger logger = EELFManager.getInstance().getLogger(EvacuateServer.class);
67 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
68 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
71 * A list of valid initial VM statuses for a migrate operations
73 private final Collection<Server.Status> migratableStatuses =
74 Arrays.asList(Server.Status.READY, Server.Status.RUNNING, Server.Status.SUSPENDED);
77 private String getConnectionExceptionMessage(RequestContext rc, Context ctx, ContextConnectionException e)
78 throws ZoneException {
79 return EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, ctx.getProvider().getName(),
80 ctx.getComputeService().getURL(), ctx.getTenant().getName(), ctx.getTenant().getId(), e.getMessage(),
81 Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
82 Integer.toString(rc.getRetryLimit()));
85 private void migrateServer(RequestContext rc, Server server, SvcLogicContext svcCtx)
86 throws ZoneException, RequestFailedException {
88 Context ctx = server.getContext();
89 ComputeService service = ctx.getComputeService();
91 // Init status will equal final status
92 Server.Status initialStatus = server.getStatus();
94 if (initialStatus == null) {
95 throw new ZoneException("Failed to determine server's starting status");
98 // We can only migrate certain statuses
99 if (!migratableStatuses.contains(initialStatus)) {
100 throw new ZoneException(String.format("Cannot migrate server that is in %s state. Must be in one of [%s]",
101 initialStatus, migratableStatuses));
104 setTimeForMetricsLogger();
106 // Is the skip Hypervisor check attribute populated?
107 String skipHypervisorCheck = null;
108 if (svcCtx != null) {
109 skipHypervisorCheck = svcCtx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
112 // Always perform Hypervisor check
113 // unless the skip is set to true
114 if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
115 // Check of the Hypervisor for the VM Server is UP and reachable
116 checkHypervisor(server);
119 boolean inConfirmPhase = false;
121 while (rc.attempt()) {
123 if (!inConfirmPhase) {
124 // Initial migrate request
125 service.migrateServer(server.getId());
126 // Wait for change to verify resize
127 waitForStateChange(rc, server, Server.Status.READY);
128 inConfirmPhase = true;
132 service.processResize(server);
133 // Wait for complete. will go back to init status
134 waitForStateChange(rc, server, initialStatus);
135 logger.info("Completed migrate request successfully");
136 metricsLogger.info("Completed migrate request successfully");
138 } catch (ContextConnectionException e) {
139 msg = getConnectionExceptionMessage(rc, ctx, e);
140 logger.error(msg, e);
141 metricsLogger.error(msg, e);
145 } catch (ZoneException e) {
146 String phase = inConfirmPhase ? "VERIFY MIGRATE" : "REQUEST MIGRATE";
147 msg = EELFResourceManager.format(Msg.MIGRATE_SERVER_FAILED, server.getName(), server.getId(), phase,
149 generateEvent(rc, false, msg);
150 logger.error(msg, e);
151 metricsLogger.error(msg, e);
152 throw new RequestFailedException("Migrate Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
157 * @see org.openecomp.appc.adapter.iaas.ProviderAdapter#migrateServer(java.util.Map, org.openecomp.sdnc.sli.SvcLogicContext)
159 private Server migrateServer(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
160 Server server = null;
161 RequestContext rc = new RequestContext(ctx);
164 setTimeForMetricsLogger();
168 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
169 ProviderAdapter.PROPERTY_PROVIDER_NAME);
170 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
172 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
173 VMURL vm = VMURL.parseURL(vm_url);
174 if (validateVM(rc, appName, vm_url, vm)) return null;
176 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
177 String identStr = (ident == null) ? null : ident.toString();
179 Context context = null;
181 context = getContext(rc, vm_url, identStr);
182 if (context != null) {
183 server = lookupServer(rc, context, vm.getServerId());
184 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
185 migrateServer(rc, server, ctx);
186 server.refreshStatus();
190 } catch (RequestFailedException e) {
191 doFailure(rc, e.getStatus(), e.getMessage());
192 } catch (ResourceNotFoundException e) {
193 msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
195 metricsLogger.error(msg);
196 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
197 } catch (Exception e1) {
198 msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
199 MIGRATE_SERVICE.toString(), vm_url, context == null ? "Unknown" : context.getTenantName());
200 logger.error(msg, e1);
201 metricsLogger.error(msg);
202 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
204 } catch (RequestFailedException e) {
205 doFailure(rc, e.getStatus(), e.getMessage());
212 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
213 throws APPCException {
214 setMDC(Operation.MIGRATE_SERVICE.toString(), "App-C IaaS Adapter:Migrate", ADAPTER_NAME);
215 logOperation(Msg.MIGRATING_SERVER, params, context);
217 setTimeForMetricsLogger();
219 metricsLogger.info("Executing Provider Operation: Migrate");
221 return migrateServer(params, context);
224 private void setTimeForMetricsLogger() {
225 long startTime = System.currentTimeMillis();
226 TimeZone tz = TimeZone.getTimeZone("UTC");
227 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
229 long endTime = System.currentTimeMillis();
230 long duration = endTime - startTime;
231 String durationStr = String.valueOf(duration);
232 String endTimeStrUTC = df.format(new Date());
233 MDC.put("EndTimestamp", endTimeStrUTC);
234 MDC.put("ElapsedTime", durationStr);
235 MDC.put("TargetEntity", "cdp");
236 MDC.put("TargetServiceName", "migrate server");
237 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.MigrateServer");