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.onap.appc.adapter.iaas.provider.operation.impl;
27 import com.att.cdp.exceptions.ZoneException;
28 import com.att.cdp.zones.ComputeService;
29 import com.att.cdp.zones.Context;
30 import com.att.cdp.zones.model.ModelObject;
31 import com.att.cdp.zones.model.Server;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34 import com.att.eelf.i18n.EELFResourceManager;
35 import org.glassfish.grizzly.http.util.HttpStatus;
36 import org.onap.appc.Constants;
37 import org.onap.appc.adapter.iaas.ProviderAdapter;
38 import org.onap.appc.adapter.iaas.impl.IdentityURL;
39 import org.onap.appc.adapter.iaas.impl.RequestContext;
40 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
41 import org.onap.appc.adapter.iaas.impl.VMURL;
42 import org.onap.appc.adapter.iaas.provider.operation.common.constants.Property;
43 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
44 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
45 import org.onap.appc.configuration.Configuration;
46 import org.onap.appc.configuration.ConfigurationFactory;
47 import org.onap.appc.exceptions.APPCException;
48 import org.onap.appc.i18n.Msg;
49 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
51 import java.io.IOException;
52 import java.text.DateFormat;
53 import java.text.SimpleDateFormat;
54 import java.util.Arrays;
55 import java.util.Collection;
56 import java.util.Date;
58 import java.util.TimeZone;
60 import static org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation.MIGRATE_SERVICE;
61 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
63 public class MigrateServer extends ProviderServerOperation {
65 private static final EELFLogger logger = EELFManager.getInstance().getLogger(EvacuateServer.class);
66 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
67 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
70 * A list of valid initial VM statuses for a migrate operations
72 private final Collection<Server.Status> migratableStatuses =
73 Arrays.asList(Server.Status.READY, Server.Status.RUNNING, Server.Status.SUSPENDED);
76 private String getConnectionExceptionMessage(RequestContext rc, Context ctx, ZoneException e)
77 throws ZoneException {
78 return EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, ctx.getProvider().getName(),
79 ctx.getComputeService().getURL(), ctx.getTenant().getName(), ctx.getTenant().getId(), e.getMessage(),
80 Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
81 Integer.toString(rc.getRetryLimit()));
84 private void migrateServer(RequestContext rc, Server server, SvcLogicContext svcCtx)
85 throws ZoneException, RequestFailedException {
87 Context ctx = server.getContext();
88 ComputeService service = ctx.getComputeService();
90 // Init status will equal final status
91 Server.Status initialStatus = server.getStatus();
93 if (initialStatus == null) {
94 throw new ZoneException("Failed to determine server's starting status");
97 // We can only migrate certain statuses
98 if (!migratableStatuses.contains(initialStatus)) {
99 throw new ZoneException(String.format("Cannot migrate server that is in %s state. Must be in one of [%s]",
100 initialStatus, migratableStatuses));
103 setTimeForMetricsLogger();
105 // Is the skip Hypervisor check attribute populated?
106 String skipHypervisorCheck = configuration.getProperty(Property.SKIP_HYPERVISOR_CHECK);
107 if (skipHypervisorCheck == null && svcCtx != null) {
108 skipHypervisorCheck = svcCtx.getAttribute(ProviderAdapter.SKIP_HYPERVISOR_CHECK);
111 // Always perform Hypervisor check
112 // unless the skip is set to true
113 if (skipHypervisorCheck == null || (!skipHypervisorCheck.equalsIgnoreCase("true"))) {
114 // Check of the Hypervisor for the VM Server is UP and reachable
115 checkHypervisor(server);
118 boolean inConfirmPhase = false;
120 while (rc.attempt()) {
122 if (!inConfirmPhase) {
123 // Initial migrate request
124 service.migrateServer(server.getId());
125 // Wait for change to verify resize
126 waitForStateChange(rc, server, Server.Status.READY);
127 inConfirmPhase = true;
131 service.processResize(server);
132 // Wait for complete. will go back to init status
133 waitForStateChange(rc, server, initialStatus);
134 logger.info("Completed migrate request successfully");
135 metricsLogger.info("Completed migrate request successfully");
139 } catch (ZoneException e) {
141 msg = getConnectionExceptionMessage(rc, ctx, e);
142 } catch (ZoneException e1) {
143 String phase = inConfirmPhase ? "VERIFY MIGRATE" : "REQUEST MIGRATE";
144 msg = EELFResourceManager.format(Msg.MIGRATE_SERVER_FAILED, server.getName(), server.getId(), phase,
146 generateEvent(rc, false, msg);
147 logger.error(msg, e1);
148 metricsLogger.error(msg, e1);
149 throw new RequestFailedException("Migrate Server", msg, HttpStatus.METHOD_NOT_ALLOWED_405, server);
151 logger.error(msg, e);
152 metricsLogger.error(msg, e);
159 * @see org.onap.appc.adapter.iaas.ProviderAdapter#migrateServer(java.util.Map,
160 * org.onap.ccsdk.sli.core.sli.SvcLogicContext)
162 private Server migrateServer(Map<String, String> params, SvcLogicContext ctx) {
163 Server server = null;
164 RequestContext rc = new RequestContext(ctx);
167 setTimeForMetricsLogger();
170 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
171 ProviderAdapter.PROPERTY_PROVIDER_NAME);
172 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
174 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
175 VMURL vm = VMURL.parseURL(vm_url);
177 if (validateVM(rc, appName, vm_url, vm))
180 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
181 String identStr = (ident == null) ? null : ident.toString();
183 server = conductServerMigration(rc, vm_url, identStr, ctx);
185 } catch (RequestFailedException e) {
186 doFailure(rc, e.getStatus(), e.getMessage());
193 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
194 throws APPCException {
195 setMDC(Operation.MIGRATE_SERVICE.toString(), "App-C IaaS Adapter:Migrate", ADAPTER_NAME);
196 logOperation(Msg.MIGRATING_SERVER, params, context);
198 setTimeForMetricsLogger();
200 metricsLogger.info("Executing Provider Operation: Migrate");
202 return migrateServer(params, context);
205 private void setTimeForMetricsLogger() {
206 long startTime = System.currentTimeMillis();
207 TimeZone tz = TimeZone.getTimeZone("UTC");
208 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
210 long endTime = System.currentTimeMillis();
211 long duration = endTime - startTime;
212 String durationStr = String.valueOf(duration);
213 String endTimeStrUTC = df.format(new Date());
214 MDC.put("EndTimestamp", endTimeStrUTC);
215 MDC.put("ElapsedTime", durationStr);
216 MDC.put("TargetEntity", "cdp");
217 MDC.put("TargetServiceName", "migrate server");
218 MDC.put("ClassName", "org.onap.appc.adapter.iaas.provider.operation.impl.MigrateServer");
221 private Server conductServerMigration(RequestContext rc, String vm_url, String identStr, SvcLogicContext ctx) throws RequestFailedException {
223 Context context = getContext(rc, vm_url, identStr);
224 VMURL vm = VMURL.parseURL(vm_url);
225 Server server = null;
228 if (context != null) {
229 server = lookupServer(rc, context, vm.getServerId());
230 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
231 migrateServer(rc, server, ctx);
232 server.refreshStatus();
236 } catch (IOException | ZoneException e1) {
237 msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
238 MIGRATE_SERVICE.toString(), vm_url, context.getTenantName());
239 logger.error(msg, e1);
240 metricsLogger.error(msg);
241 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);