2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.ContextConnectionException;
28 import com.att.cdp.exceptions.ResourceNotFoundException;
29 import com.att.cdp.exceptions.ZoneException;
30 import com.att.cdp.zones.Context;
31 import com.att.cdp.zones.ImageService;
32 import com.att.cdp.zones.Provider;
33 import com.att.cdp.zones.model.Image;
34 import com.att.cdp.zones.model.ModelObject;
35 import com.att.cdp.zones.model.Server;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import com.att.eelf.i18n.EELFResourceManager;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40 import org.glassfish.grizzly.http.util.HttpStatus;
41 import org.onap.appc.Constants;
42 import org.onap.appc.adapter.iaas.ProviderAdapter;
43 import org.onap.appc.adapter.iaas.impl.IdentityURL;
44 import org.onap.appc.adapter.iaas.impl.RequestContext;
45 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
46 import org.onap.appc.adapter.iaas.impl.VMURL;
47 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
48 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderServerOperation;
49 import org.onap.appc.configuration.Configuration;
50 import org.onap.appc.configuration.ConfigurationFactory;
51 import org.onap.appc.exceptions.APPCException;
52 import org.onap.appc.i18n.Msg;
54 import java.text.DateFormat;
55 import java.text.SimpleDateFormat;
56 import java.util.Date;
58 import java.util.TimeZone;
59 import static org.onap.appc.adapter.iaas.provider.operation.common.constants.Constants.DATE_FORMAT;
60 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
62 public class CreateSnapshot extends ProviderServerOperation {
64 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CreateSnapshot.class);
65 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
66 private static final Configuration configuration = ConfigurationFactory.getConfiguration();
68 private String generateSnapshotName(String server) {
69 setTimeForMetricsLogger();
71 SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT);
72 metricsLogger.info("Snapshot Name Generated: Snapshot of %s at %s", server, df.format(new Date()));
74 return String.format("Snapshot of %s at %s", server, df.format(new Date()));
77 private Image createSnapshot(RequestContext rc, Server server) throws ZoneException, RequestFailedException {
78 Context context = server.getContext();
79 Provider provider = context.getProvider();
80 ImageService service = context.getImageService(); // Already checked access by this point
82 String snapshotName = generateSnapshotName(server.getName());
84 setTimeForMetricsLogger();
86 logger.info(String.format("Creating snapshot of server %s (%s) with name %s", server.getName(), server.getId(),
88 metricsLogger.info(String.format("Creating snapshot of server %s (%s) with name %s", server.getName(),
89 server.getId(), snapshotName));
93 while (rc.attempt()) {
95 server.createSnapshot(snapshotName);
97 } catch (ContextConnectionException e) {
98 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, provider.getName(), service.getURL(),
99 context.getTenant().getName(), context.getTenant().getId(), e.getMessage(),
100 Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
101 Integer.toString(rc.getRetryLimit()));
102 logger.error(msg, e);
103 metricsLogger.error(msg, e);
108 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
110 metricsLogger.error(msg);
111 throw new RequestFailedException("Stop Server", msg, HttpStatus.BAD_GATEWAY_502, server);
115 // Locate snapshot image
116 Image snapshot = null;
117 while (rc.attempt()) {
119 snapshot = service.getImageByName(snapshotName);
120 if (snapshot != null) {
123 } catch (ContextConnectionException e) {
124 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, provider.getName(), service.getURL(),
125 context.getTenant().getName(), context.getTenant().getId(), e.getMessage(),
126 Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
127 Integer.toString(rc.getRetryLimit()));
128 logger.error(msg, e);
129 metricsLogger.error(msg, e);
134 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), service.getURL());
136 metricsLogger.error(msg);
137 throw new RequestFailedException("Stop Server", msg, HttpStatus.BAD_GATEWAY_502, server);
141 // Wait for it to be ready
142 waitForStateChange(rc, snapshot, Image.Status.ACTIVE);
147 private Image createSnapshot(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
148 Image snapshot = null;
149 RequestContext rc = new RequestContext(ctx);
152 setTimeForMetricsLogger();
154 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
155 ProviderAdapter.PROPERTY_PROVIDER_NAME);
156 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
157 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
158 VMURL vm = VMURL.parseURL(vm_url);
159 if (validateVM(rc, appName, vm_url, vm))
162 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
163 String identStr = (ident == null) ? null : ident.toString();
164 snapshot = createSnapshotNested(snapshot, rc, vm, vm_url, identStr);
165 } catch (RequestFailedException e) {
166 doFailure(rc, e.getStatus(), e.getMessage());
171 private Image createSnapshotNested(Image SnapShot, RequestContext RcContext, VMURL vm, String vmUrl, String identStr)
172 throws APPCException {
176 Context context = null;
177 String tenantName = "Unknown";//this variable is also used in catch
179 context = getContext(RcContext, vmUrl, identStr);
180 if (context != null) {
181 tenantName = context.getTenantName();
182 Server server = lookupServer(RcContext, context, vm.getServerId());
184 logger.debug(Msg.SERVER_FOUND, vmUrl, tenantName, server.getStatus().toString());
186 if (hasImageAccess(RcContext, context)) {
187 SnapShot = createSnapshot(RcContext, server);
188 doSuccess(RcContext);
190 msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
191 "Accessing Image Service Failed");
193 metricsLogger.error(msg);
194 doFailure(RcContext, HttpStatus.FORBIDDEN_403, msg);
198 } catch (ResourceNotFoundException e) {
199 msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vmUrl);
201 metricsLogger.error(msg, e);
202 doFailure(RcContext, HttpStatus.NOT_FOUND_404, msg);
203 } catch (Exception e1) {
204 msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
205 Operation.SNAPSHOT_SERVICE.toString(), vmUrl,
207 logger.error(msg, e1);
208 doFailure(RcContext, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
214 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
215 throws APPCException {
217 setMDC(Operation.SNAPSHOT_SERVICE.toString(), "App-C IaaS Adapter:Snapshot", ADAPTER_NAME);
218 logOperation(Msg.SNAPSHOTING_SERVER, params, context);
219 setTimeForMetricsLogger();
221 metricsLogger.info("Executing Provider Operation: Create Snapshot");
223 return createSnapshot(params, context);
226 private void setTimeForMetricsLogger() {
227 long startTime = System.currentTimeMillis();
228 TimeZone tz = TimeZone.getTimeZone("UTC");
229 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
231 long endTime = System.currentTimeMillis();
232 long duration = endTime - startTime;
233 String durationStr = String.valueOf(duration);
234 String endTimeStrUTC = df.format(new Date());
235 MDC.put("EndTimestamp", endTimeStrUTC);
236 MDC.put("ElapsedTime", durationStr);
237 MDC.put("TargetEntity", "cdp");
238 MDC.put("TargetServiceName", "create snapshot");
239 MDC.put("ClassName", "org.onap.appc.adapter.iaas.provider.operation.impl.CreateSnapshot");