2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.adapter.iaas.provider.operation.impl;
25 import org.openecomp.appc.Constants;
26 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
27 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
28 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
29 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation;
30 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderOperation;
31 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderStackOperation;
32 import org.openecomp.appc.adapter.openstack.heat.SnapshotResource;
33 import org.openecomp.appc.adapter.openstack.heat.StackResource;
34 import org.openecomp.appc.adapter.openstack.heat.model.CreateSnapshotParams;
35 import org.openecomp.appc.adapter.openstack.heat.model.Snapshot;
36 import org.openecomp.appc.configuration.Configuration;
37 import org.openecomp.appc.configuration.ConfigurationFactory;
38 import org.openecomp.appc.exceptions.APPCException;
39 import org.openecomp.appc.i18n.Msg;
40 import com.att.cdp.exceptions.ResourceNotFoundException;
41 import com.att.cdp.exceptions.ZoneException;
42 import com.att.cdp.openstack.OpenStackContext;
43 import com.att.cdp.openstack.connectors.HeatConnector;
44 import com.att.cdp.openstack.util.ExceptionMapper;
45 import com.att.cdp.zones.Context;
46 import com.att.cdp.zones.model.ModelObject;
47 import com.att.cdp.zones.model.Stack;
48 import com.att.cdp.zones.spi.RequestState;
49 import com.att.eelf.configuration.EELFLogger;
50 import com.att.eelf.configuration.EELFManager;
51 import com.att.eelf.i18n.EELFResourceManager;
52 import org.openecomp.sdnc.sli.SvcLogicContext;
53 import com.woorea.openstack.base.client.OpenStackBaseException;
54 import com.woorea.openstack.heat.Heat;
55 import org.glassfish.grizzly.http.util.HttpStatus;
59 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
62 import java.text.DateFormat;
63 import java.text.SimpleDateFormat;
64 import java.util.Date;
65 import java.util.TimeZone;
69 * @since September 26, 2016
71 public class SnapshotStack extends ProviderStackOperation {
73 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SnapshotStack.class);
74 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
77 private Snapshot snapshotStack(@SuppressWarnings("unused") RequestContext rc, Stack stack) throws ZoneException, RequestFailedException {
78 Snapshot snapshot = new Snapshot();
79 Context context = stack.getContext();
81 OpenStackContext osContext = (OpenStackContext)context;
83 final HeatConnector heatConnector = osContext.getHeatConnector();
84 ((OpenStackContext)context).refreshIfStale(heatConnector);
86 trackRequest(context);
87 RequestState.put("SERVICE", "Orchestration");
88 RequestState.put("SERVICE_URL", heatConnector.getEndpoint());
90 Heat heat = heatConnector.getClient();
92 SnapshotResource snapshotResource = new SnapshotResource(heat);
95 * Set Time for Metrics Logger
97 long startTime = System.currentTimeMillis();
98 TimeZone tz = TimeZone.getTimeZone("UTC");
99 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
101 String startTimeStr = df.format(new Date());
102 long endTime = System.currentTimeMillis();
103 long duration = endTime - startTime;
104 String endTimeStr = String.valueOf(endTime);
105 String durationStr = String.valueOf(duration);
106 String endTimeStrUTC = df.format(new Date());
107 MDC.put("EndTimestamp", endTimeStrUTC);
108 MDC.put("ElapsedTime", durationStr);
109 MDC.put("TargetEntity", "cdp");
110 MDC.put("TargetServiceName", "snapshot stack");
111 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.SnapshotStack");
115 snapshot = snapshotResource.create(stack.getName(), stack.getId(), new CreateSnapshotParams()).execute();
117 // wait for the stack deletion
118 StackResource stackResource = new StackResource(heat);
119 if (!waitForStack(stack, stackResource, "SNAPSHOT_COMPLETE")) {
120 throw new RequestFailedException("Stack Snapshot failed.");
123 } catch (OpenStackBaseException e) {
124 ExceptionMapper.mapException(e);
131 public Stack snapshotStack(Map<String, String> params, SvcLogicContext ctx) throws IllegalArgumentException, APPCException {
133 RequestContext rc = new RequestContext(ctx);
136 ctx.setAttribute("SNAPSHOT_STATUS", "STACK_NOT_FOUND");
137 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
139 String vm_url = null;
140 Context context = null;
143 * Set Time for Metrics Logger
145 long startTime = System.currentTimeMillis();
146 TimeZone tz = TimeZone.getTimeZone("UTC");
147 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
149 String startTimeStr = df.format(new Date());
150 long endTime = System.currentTimeMillis();
151 long duration = endTime - startTime;
152 String endTimeStr = String.valueOf(endTime);
153 String durationStr = String.valueOf(duration);
154 String endTimeStrUTC = df.format(new Date());
155 MDC.put("EndTimestamp", endTimeStrUTC);
156 MDC.put("ElapsedTime", durationStr);
157 MDC.put("TargetEntity", "cdp");
158 MDC.put("TargetServiceName", "snapshot stack");
159 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.SnapshotStack");
164 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
165 ProviderAdapter.PROPERTY_PROVIDER_NAME, ProviderAdapter.PROPERTY_STACK_ID);
167 String stackId = params.get(ProviderAdapter.PROPERTY_STACK_ID);
168 vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
170 context = resolveContext(rc, params, appName, vm_url);
172 if (context != null) {
173 stack = lookupStack(rc, context, stackId);
174 logger.debug(Msg.STACK_FOUND, vm_url, context.getTenantName(), stack.getStatus().toString());
175 logger.info(EELFResourceManager.format(Msg.SNAPSHOTING_STACK, stack.getName()));
176 metricsLogger.info(EELFResourceManager.format(Msg.SNAPSHOTING_STACK, stack.getName()));
178 Snapshot snapshot = snapshotStack(rc, stack);
180 ctx.setAttribute(ProviderAdapter.DG_OUTPUT_PARAM_NAMESPACE +
181 ProviderAdapter.PROPERTY_SNAPSHOT_ID, snapshot.getId());
183 logger.info(EELFResourceManager.format(Msg.STACK_SNAPSHOTED, stack.getName(), snapshot.getId()));
184 metricsLogger.info(EELFResourceManager.format(Msg.STACK_SNAPSHOTED, stack.getName(), snapshot.getId()));
188 ctx.setAttribute(Constants.DG_ATTRIBUTE_STATUS, "failure");
191 } catch (ResourceNotFoundException e) {
192 String msg = EELFResourceManager.format(Msg.STACK_NOT_FOUND, e, vm_url);
194 metricsLogger.error(msg);
195 doFailure(rc, HttpStatus.NOT_FOUND_404, msg, e);
196 } catch (RequestFailedException e) {
197 logger.error(EELFResourceManager.format(Msg.MISSING_PARAMETER_IN_REQUEST, e.getReason(), "snapshotStack"));
198 metricsLogger.error(EELFResourceManager.format(Msg.MISSING_PARAMETER_IN_REQUEST, e.getReason(), "snapshotStack"));
199 doFailure(rc, e.getStatus(), e.getMessage(), e);
200 } catch (Throwable t) {
201 String msg = EELFResourceManager.format(Msg.STACK_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
202 "snapshotStack", vm_url, null == context ? "n/a" : context.getTenantName());
203 logger.error(msg, t);
204 metricsLogger.error(msg);
205 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg, t);
211 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
212 setMDC(Operation.SNAPSHOT_STACK.toString(), "App-C IaaS Adapter:Snapshot-Stack", ADAPTER_NAME);
213 logOperation(Msg.SNAPSHOTING_STACK, params, context);
216 * Set Time for Metrics Logger
218 long startTime = System.currentTimeMillis();
219 TimeZone tz = TimeZone.getTimeZone("UTC");
220 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
222 String startTimeStr = df.format(new Date());
223 long endTime = System.currentTimeMillis();
224 long duration = endTime - startTime;
225 String endTimeStr = String.valueOf(endTime);
226 String durationStr = String.valueOf(duration);
227 String endTimeStrUTC = df.format(new Date());
228 MDC.put("EndTimestamp", endTimeStrUTC);
229 MDC.put("ElapsedTime", durationStr);
230 MDC.put("TargetEntity", "cdp");
231 MDC.put("TargetServiceName", "snapshot stack");
232 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.SnapshotStack");
234 metricsLogger.info("Executing Provider Operation: Snapshot Stack");
236 return snapshotStack(params, context);