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 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.iaas.provider.operation.impl;
26 import com.att.cdp.exceptions.ResourceNotFoundException;
27 import com.att.cdp.exceptions.ZoneException;
28 import com.att.cdp.openstack.OpenStackContext;
29 import com.att.cdp.openstack.connectors.HeatConnector;
30 import com.att.cdp.openstack.util.ExceptionMapper;
31 import com.att.cdp.zones.Context;
32 import com.att.cdp.zones.model.ModelObject;
33 import com.att.cdp.zones.model.Stack;
34 import com.att.cdp.zones.spi.RequestState;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.att.eelf.i18n.EELFResourceManager;
38 import com.woorea.openstack.base.client.OpenStackBaseException;
39 import com.woorea.openstack.heat.Heat;
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.RequestContext;
44 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
45 import org.onap.appc.adapter.iaas.provider.operation.common.enums.Operation;
46 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderStackOperation;
47 import org.onap.appc.adapter.openstack.heat.SnapshotResource;
48 import org.onap.appc.adapter.openstack.heat.StackResource;
49 import org.onap.appc.adapter.openstack.heat.model.CreateSnapshotParams;
50 import org.onap.appc.adapter.openstack.heat.model.Snapshot;
51 import org.onap.appc.exceptions.APPCException;
52 import org.onap.appc.i18n.Msg;
53 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
55 import java.text.DateFormat;
56 import java.text.SimpleDateFormat;
57 import java.util.Date;
59 import java.util.TimeZone;
60 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
62 public class SnapshotStack extends ProviderStackOperation {
64 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SnapshotStack.class);
65 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
68 private Snapshot snapshotStack(@SuppressWarnings("unused") RequestContext rc, Stack stack)
69 throws ZoneException, RequestFailedException {
70 Snapshot snapshot = new Snapshot();
71 Context context = stack.getContext();
73 OpenStackContext osContext = (OpenStackContext) context;
75 final HeatConnector heatConnector = osContext.getHeatConnector();
76 ((OpenStackContext) context).refreshIfStale(heatConnector);
78 trackRequest(context);
79 RequestState.put("SERVICE", "Orchestration");
80 RequestState.put("SERVICE_URL", heatConnector.getEndpoint());
82 Heat heat = heatConnector.getClient();
84 SnapshotResource snapshotResource = new SnapshotResource(heat);
86 setTimeForMetricsLogger();
90 snapshot = snapshotResource.create(stack.getName(), stack.getId(), new CreateSnapshotParams()).execute();
92 // wait for the stack deletion
93 StackResource stackResource = new StackResource(heat);
94 if (!waitForStack(stack, stackResource, "SNAPSHOT_COMPLETE")) {
95 throw new RequestFailedException("Stack Snapshot failed.");
98 } catch (OpenStackBaseException e) {
99 ExceptionMapper.mapException(e);
105 public Stack snapshotStack(Map<String, String> params, SvcLogicContext ctx)
106 throws IllegalArgumentException, APPCException {
108 RequestContext rc = new RequestContext(ctx);
110 ctx.setAttribute("SNAPSHOT_STATUS", "STACK_NOT_FOUND");
112 setTimeForMetricsLogger();
114 String vm_url = null;
115 Context context = null;
116 String tenantName = "Unknown";//to be used also in case of exception
118 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
119 ProviderAdapter.PROPERTY_PROVIDER_NAME, ProviderAdapter.PROPERTY_STACK_ID);
121 String stackId = params.get(ProviderAdapter.PROPERTY_STACK_ID);
122 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
123 vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
124 context = resolveContext(rc, params, appName, vm_url);
126 if (context != null) {
127 tenantName = context.getTenantName();//this varaible also is used in case of exception
128 stack = lookupStack(rc, context, stackId);
129 logger.debug(Msg.STACK_FOUND, vm_url, tenantName, stack.getStatus().toString());
130 logger.info(EELFResourceManager.format(Msg.SNAPSHOTING_STACK, stack.getName()));
131 metricsLogger.info(EELFResourceManager.format(Msg.SNAPSHOTING_STACK, stack.getName()));
133 Snapshot snapshot = snapshotStack(rc, stack);
135 ctx.setAttribute(ProviderAdapter.DG_OUTPUT_PARAM_NAMESPACE + ProviderAdapter.PROPERTY_SNAPSHOT_ID,
138 logger.info(EELFResourceManager.format(Msg.STACK_SNAPSHOTED, stack.getName(), snapshot.getId()));
139 metricsLogger.info(EELFResourceManager.format(Msg.STACK_SNAPSHOTED, stack.getName(), snapshot.getId()));
143 ctx.setAttribute(Constants.DG_ATTRIBUTE_STATUS, "failure");
146 } catch (ResourceNotFoundException e) {
147 String msg = EELFResourceManager.format(Msg.STACK_NOT_FOUND, e, vm_url);
149 metricsLogger.error(msg);
150 doFailure(rc, HttpStatus.NOT_FOUND_404, msg, e);
151 } catch (RequestFailedException e) {
152 logger.error(EELFResourceManager.format(Msg.MISSING_PARAMETER_IN_REQUEST, e.getReason(), "snapshotStack"));
154 EELFResourceManager.format(Msg.MISSING_PARAMETER_IN_REQUEST, e.getReason(), "snapshotStack"));
155 doFailure(rc, e.getStatus(), e.getMessage(), e);
156 } catch (Exception e1) {
157 String msg = EELFResourceManager.format(Msg.STACK_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
158 "snapshotStack", vm_url, tenantName);
159 logger.error(msg, e1);
160 metricsLogger.error(msg);
161 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg, e1);
167 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
168 throws APPCException {
169 setMDC(Operation.SNAPSHOT_STACK.toString(), "App-C IaaS Adapter:Snapshot-Stack", ADAPTER_NAME);
170 logOperation(Msg.SNAPSHOTING_STACK, params, context);
172 setTimeForMetricsLogger();
174 metricsLogger.info("Executing Provider Operation: Snapshot Stack");
176 return snapshotStack(params, context);
179 private void setTimeForMetricsLogger() {
180 long startTime = System.currentTimeMillis();
181 TimeZone tz = TimeZone.getTimeZone("UTC");
182 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
184 long endTime = System.currentTimeMillis();
185 long duration = endTime - startTime;
186 String durationStr = String.valueOf(duration);
187 String endTimeStrUTC = df.format(new Date());
188 MDC.put("EndTimestamp", endTimeStrUTC);
189 MDC.put("ElapsedTime", durationStr);
190 MDC.put("TargetEntity", "cdp");
191 MDC.put("TargetServiceName", "snapshot stack");
192 MDC.put("ClassName", "org.onap.appc.adapter.iaas.provider.operation.impl.SnapshotStack");