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.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();
156 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
157 ProviderAdapter.PROPERTY_PROVIDER_NAME);
159 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
160 String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
161 VMURL vm = VMURL.parseURL(vm_url);
162 if (validateVM(rc, appName, vm_url, vm))
165 IdentityURL ident = IdentityURL.parseURL(params.get(ProviderAdapter.PROPERTY_IDENTITY_URL));
166 String identStr = (ident == null) ? null : ident.toString();
168 Context context = null;
170 context = getContext(rc, vm_url, identStr);
171 if (context != null) {
172 Server server = lookupServer(rc, context, vm.getServerId());
173 logger.debug(Msg.SERVER_FOUND, vm_url, context.getTenantName(), server.getStatus().toString());
175 if (hasImageAccess(rc, context)) {
176 snapshot = createSnapshot(rc, server);
179 msg = EELFResourceManager.format(Msg.REBUILD_SERVER_FAILED, server.getName(), server.getId(),
180 "Accessing Image Service Failed");
182 metricsLogger.error(msg);
183 doFailure(rc, HttpStatus.FORBIDDEN_403, msg);
187 } catch (ResourceNotFoundException e) {
188 msg = EELFResourceManager.format(Msg.SERVER_NOT_FOUND, e, vm_url);
190 metricsLogger.error(msg, e);
191 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
192 } catch (Exception e1) {
193 msg = EELFResourceManager.format(Msg.SERVER_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
194 Operation.SNAPSHOT_SERVICE.toString(), vm_url,
195 context == null ? "Unknown" : context.getTenantName());
196 logger.error(msg, e1);
197 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
199 } catch (RequestFailedException e) {
200 doFailure(rc, e.getStatus(), e.getMessage());
206 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
207 throws APPCException {
209 setMDC(Operation.SNAPSHOT_SERVICE.toString(), "App-C IaaS Adapter:Snapshot", ADAPTER_NAME);
210 logOperation(Msg.SNAPSHOTING_SERVER, params, context);
211 setTimeForMetricsLogger();
213 metricsLogger.info("Executing Provider Operation: Create Snapshot");
215 return createSnapshot(params, context);
218 private void setTimeForMetricsLogger() {
219 long startTime = System.currentTimeMillis();
220 TimeZone tz = TimeZone.getTimeZone("UTC");
221 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
223 long endTime = System.currentTimeMillis();
224 long duration = endTime - startTime;
225 String durationStr = String.valueOf(duration);
226 String endTimeStrUTC = df.format(new Date());
227 MDC.put("EndTimestamp", endTimeStrUTC);
228 MDC.put("ElapsedTime", durationStr);
229 MDC.put("TargetEntity", "cdp");
230 MDC.put("TargetServiceName", "create snapshot");
231 MDC.put("ClassName", "org.onap.appc.adapter.iaas.provider.operation.impl.CreateSnapshot");