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();
171 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
172 ProviderAdapter.PROPERTY_PROVIDER_NAME);
173 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
175 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
176 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 Context context = getContext(rc, vm_url, identStr);
185 if (context != null) {
186 server = lookupServer(rc, context, vm.getServerId());
187 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
188 migrateServer(rc, server, ctx);
189 server.refreshStatus();
193 } catch (IOException | ZoneException e1) {
194 msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
195 MIGRATE_SERVICE.toString(), vm_url, context.getTenantName());
196 logger.error(msg, e1);
197 metricsLogger.error(msg);
198 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
200 } catch (RequestFailedException e) {
201 doFailure(rc, e.getStatus(), e.getMessage());
208 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
209 throws APPCException {
210 setMDC(Operation.MIGRATE_SERVICE.toString(), "App-C IaaS Adapter:Migrate", ADAPTER_NAME);
211 logOperation(Msg.MIGRATING_SERVER, params, context);
213 setTimeForMetricsLogger();
215 metricsLogger.info("Executing Provider Operation: Migrate");
217 return migrateServer(params, context);
220 private void setTimeForMetricsLogger() {
221 long startTime = System.currentTimeMillis();
222 TimeZone tz = TimeZone.getTimeZone("UTC");
223 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
225 long endTime = System.currentTimeMillis();
226 long duration = endTime - startTime;
227 String durationStr = String.valueOf(duration);
228 String endTimeStrUTC = df.format(new Date());
229 MDC.put("EndTimestamp", endTimeStrUTC);
230 MDC.put("ElapsedTime", durationStr);
231 MDC.put("TargetEntity", "cdp");
232 MDC.put("TargetServiceName", "migrate server");
233 MDC.put("ClassName", "org.onap.appc.adapter.iaas.provider.operation.impl.MigrateServer");