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.openecomp.appc.adapter.iaas.provider.operation.impl;
27 import org.openecomp.appc.Constants;
28 import org.openecomp.appc.adapter.iaas.ProviderAdapter;
29 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
30 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
31 import org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation;
32 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderOperation;
33 import org.openecomp.appc.adapter.iaas.provider.operation.impl.base.ProviderStackOperation;
34 import org.openecomp.appc.adapter.openstack.heat.SnapshotResource;
35 import org.openecomp.appc.adapter.openstack.heat.StackResource;
36 import org.openecomp.appc.adapter.openstack.heat.model.CreateSnapshotParams;
37 import org.openecomp.appc.adapter.openstack.heat.model.Snapshot;
38 import org.openecomp.appc.configuration.Configuration;
39 import org.openecomp.appc.configuration.ConfigurationFactory;
40 import org.openecomp.appc.exceptions.APPCException;
41 import org.openecomp.appc.i18n.Msg;
42 import com.att.cdp.exceptions.ResourceNotFoundException;
43 import com.att.cdp.exceptions.ZoneException;
44 import com.att.cdp.openstack.OpenStackContext;
45 import com.att.cdp.openstack.connectors.HeatConnector;
46 import com.att.cdp.openstack.util.ExceptionMapper;
47 import com.att.cdp.zones.Context;
48 import com.att.cdp.zones.model.ModelObject;
49 import com.att.cdp.zones.model.Stack;
50 import com.att.cdp.zones.spi.RequestState;
51 import com.att.eelf.configuration.EELFLogger;
52 import com.att.eelf.configuration.EELFManager;
53 import com.att.eelf.i18n.EELFResourceManager;
54 import org.openecomp.sdnc.sli.SvcLogicContext;
55 import com.woorea.openstack.base.client.OpenStackBaseException;
56 import com.woorea.openstack.heat.Heat;
57 import org.glassfish.grizzly.http.util.HttpStatus;
61 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
64 import java.text.DateFormat;
65 import java.text.SimpleDateFormat;
66 import java.util.Date;
67 import java.util.TimeZone;
71 * @since September 26, 2016
73 public class SnapshotStack extends ProviderStackOperation {
75 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SnapshotStack.class);
76 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
79 private Snapshot snapshotStack(@SuppressWarnings("unused") RequestContext rc, Stack stack) throws ZoneException, RequestFailedException {
80 Snapshot snapshot = new Snapshot();
81 Context context = stack.getContext();
83 OpenStackContext osContext = (OpenStackContext)context;
85 final HeatConnector heatConnector = osContext.getHeatConnector();
86 ((OpenStackContext)context).refreshIfStale(heatConnector);
88 trackRequest(context);
89 RequestState.put("SERVICE", "Orchestration");
90 RequestState.put("SERVICE_URL", heatConnector.getEndpoint());
92 Heat heat = heatConnector.getClient();
94 SnapshotResource snapshotResource = new SnapshotResource(heat);
97 * Set Time for Metrics Logger
99 long startTime = System.currentTimeMillis();
100 TimeZone tz = TimeZone.getTimeZone("UTC");
101 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
103 String startTimeStr = df.format(new Date());
104 long endTime = System.currentTimeMillis();
105 long duration = endTime - startTime;
106 String endTimeStr = String.valueOf(endTime);
107 String durationStr = String.valueOf(duration);
108 String endTimeStrUTC = df.format(new Date());
109 MDC.put("EndTimestamp", endTimeStrUTC);
110 MDC.put("ElapsedTime", durationStr);
111 MDC.put("TargetEntity", "cdp");
112 MDC.put("TargetServiceName", "snapshot stack");
113 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.SnapshotStack");
117 snapshot = snapshotResource.create(stack.getName(), stack.getId(), new CreateSnapshotParams()).execute();
119 // wait for the stack deletion
120 StackResource stackResource = new StackResource(heat);
121 if (!waitForStack(stack, stackResource, "SNAPSHOT_COMPLETE")) {
122 throw new RequestFailedException("Stack Snapshot failed.");
125 } catch (OpenStackBaseException e) {
126 ExceptionMapper.mapException(e);
133 public Stack snapshotStack(Map<String, String> params, SvcLogicContext ctx) throws IllegalArgumentException, APPCException {
135 RequestContext rc = new RequestContext(ctx);
138 ctx.setAttribute("SNAPSHOT_STATUS", "STACK_NOT_FOUND");
139 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
141 String vm_url = null;
142 Context context = null;
145 * Set Time for Metrics Logger
147 long startTime = System.currentTimeMillis();
148 TimeZone tz = TimeZone.getTimeZone("UTC");
149 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
151 String startTimeStr = df.format(new Date());
152 long endTime = System.currentTimeMillis();
153 long duration = endTime - startTime;
154 String endTimeStr = String.valueOf(endTime);
155 String durationStr = String.valueOf(duration);
156 String endTimeStrUTC = df.format(new Date());
157 MDC.put("EndTimestamp", endTimeStrUTC);
158 MDC.put("ElapsedTime", durationStr);
159 MDC.put("TargetEntity", "cdp");
160 MDC.put("TargetServiceName", "snapshot stack");
161 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.SnapshotStack");
166 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
167 ProviderAdapter.PROPERTY_PROVIDER_NAME, ProviderAdapter.PROPERTY_STACK_ID);
169 String stackId = params.get(ProviderAdapter.PROPERTY_STACK_ID);
170 vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
172 context = resolveContext(rc, params, appName, vm_url);
174 if (context != null) {
175 stack = lookupStack(rc, context, stackId);
176 logger.debug(Msg.STACK_FOUND, vm_url, context.getTenantName(), stack.getStatus().toString());
177 logger.info(EELFResourceManager.format(Msg.SNAPSHOTING_STACK, stack.getName()));
178 metricsLogger.info(EELFResourceManager.format(Msg.SNAPSHOTING_STACK, stack.getName()));
180 Snapshot snapshot = snapshotStack(rc, stack);
182 ctx.setAttribute(ProviderAdapter.DG_OUTPUT_PARAM_NAMESPACE +
183 ProviderAdapter.PROPERTY_SNAPSHOT_ID, snapshot.getId());
185 logger.info(EELFResourceManager.format(Msg.STACK_SNAPSHOTED, stack.getName(), snapshot.getId()));
186 metricsLogger.info(EELFResourceManager.format(Msg.STACK_SNAPSHOTED, stack.getName(), snapshot.getId()));
190 ctx.setAttribute(Constants.DG_ATTRIBUTE_STATUS, "failure");
193 } catch (ResourceNotFoundException e) {
194 String msg = EELFResourceManager.format(Msg.STACK_NOT_FOUND, e, vm_url);
196 metricsLogger.error(msg);
197 doFailure(rc, HttpStatus.NOT_FOUND_404, msg, e);
198 } catch (RequestFailedException e) {
199 logger.error(EELFResourceManager.format(Msg.MISSING_PARAMETER_IN_REQUEST, e.getReason(), "snapshotStack"));
200 metricsLogger.error(EELFResourceManager.format(Msg.MISSING_PARAMETER_IN_REQUEST, e.getReason(), "snapshotStack"));
201 doFailure(rc, e.getStatus(), e.getMessage(), e);
202 } catch (Throwable t) {
203 String msg = EELFResourceManager.format(Msg.STACK_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
204 "snapshotStack", vm_url, null == context ? "n/a" : context.getTenantName());
205 logger.error(msg, t);
206 metricsLogger.error(msg);
207 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg, t);
213 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
214 setMDC(Operation.SNAPSHOT_STACK.toString(), "App-C IaaS Adapter:Snapshot-Stack", ADAPTER_NAME);
215 logOperation(Msg.SNAPSHOTING_STACK, params, context);
218 * Set Time for Metrics Logger
220 long startTime = System.currentTimeMillis();
221 TimeZone tz = TimeZone.getTimeZone("UTC");
222 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
224 String startTimeStr = df.format(new Date());
225 long endTime = System.currentTimeMillis();
226 long duration = endTime - startTime;
227 String endTimeStr = String.valueOf(endTime);
228 String durationStr = String.valueOf(duration);
229 String endTimeStrUTC = df.format(new Date());
230 MDC.put("EndTimestamp", endTimeStrUTC);
231 MDC.put("ElapsedTime", durationStr);
232 MDC.put("TargetEntity", "cdp");
233 MDC.put("TargetServiceName", "snapshot stack");
234 MDC.put("ClassName", "org.openecomp.appc.adapter.iaas.provider.operation.impl.SnapshotStack");
236 metricsLogger.info("Executing Provider Operation: Snapshot Stack");
238 return snapshotStack(params, context);