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.ResourceNotFoundException;
28 import com.att.cdp.exceptions.ZoneException;
29 import com.att.cdp.openstack.OpenStackContext;
30 import com.att.cdp.openstack.connectors.HeatConnector;
31 import com.att.cdp.openstack.util.ExceptionMapper;
32 import com.att.cdp.zones.Context;
33 import com.att.cdp.zones.model.ModelObject;
34 import com.att.cdp.zones.model.Stack;
35 import com.att.cdp.zones.spi.RequestState;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import com.att.eelf.i18n.EELFResourceManager;
39 import com.woorea.openstack.base.client.OpenStackBaseException;
40 import com.woorea.openstack.heat.Heat;
41 import org.glassfish.grizzly.http.util.HttpStatus;
42 import org.onap.appc.Constants;
43 import org.onap.appc.adapter.iaas.ProviderAdapter;
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.provider.operation.common.enums.Operation;
47 import org.onap.appc.adapter.iaas.provider.operation.impl.base.ProviderStackOperation;
48 import org.onap.appc.adapter.openstack.heat.SnapshotResource;
49 import org.onap.appc.adapter.openstack.heat.StackResource;
50 import org.onap.appc.adapter.openstack.heat.model.CreateSnapshotParams;
51 import org.onap.appc.adapter.openstack.heat.model.Snapshot;
52 import org.onap.appc.exceptions.APPCException;
53 import org.onap.appc.i18n.Msg;
54 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
56 import java.text.DateFormat;
57 import java.text.SimpleDateFormat;
58 import java.util.Date;
60 import java.util.TimeZone;
61 import static org.onap.appc.adapter.utils.Constants.ADAPTER_NAME;
63 public class SnapshotStack extends ProviderStackOperation {
65 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SnapshotStack.class);
66 private static EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
69 private Snapshot snapshotStack(@SuppressWarnings("unused") RequestContext rc, Stack stack)
70 throws ZoneException, RequestFailedException {
71 Snapshot snapshot = new Snapshot();
72 Context context = stack.getContext();
74 OpenStackContext osContext = (OpenStackContext) context;
76 final HeatConnector heatConnector = osContext.getHeatConnector();
77 ((OpenStackContext) context).refreshIfStale(heatConnector);
79 trackRequest(context);
80 RequestState.put("SERVICE", "Orchestration");
81 RequestState.put("SERVICE_URL", heatConnector.getEndpoint());
83 Heat heat = heatConnector.getClient();
85 SnapshotResource snapshotResource = new SnapshotResource(heat);
87 setTimeForMetricsLogger();
91 snapshot = snapshotResource.create(stack.getName(), stack.getId(), new CreateSnapshotParams()).execute();
93 // wait for the stack deletion
94 StackResource stackResource = new StackResource(heat);
95 if (!waitForStack(stack, stackResource, "SNAPSHOT_COMPLETE")) {
96 throw new RequestFailedException("Stack Snapshot failed.");
99 } catch (OpenStackBaseException e) {
100 ExceptionMapper.mapException(e);
106 public Stack snapshotStack(Map<String, String> params, SvcLogicContext ctx)
107 throws IllegalArgumentException, APPCException {
109 RequestContext rc = new RequestContext(ctx);
111 ctx.setAttribute("SNAPSHOT_STATUS", "STACK_NOT_FOUND");
113 setTimeForMetricsLogger();
115 String vm_url = null;
116 Context context = null;
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 stack = lookupStack(rc, context, stackId);
128 logger.debug(Msg.STACK_FOUND, vm_url, context.getTenantName(), stack.getStatus().toString());
129 logger.info(EELFResourceManager.format(Msg.SNAPSHOTING_STACK, stack.getName()));
130 metricsLogger.info(EELFResourceManager.format(Msg.SNAPSHOTING_STACK, stack.getName()));
132 Snapshot snapshot = snapshotStack(rc, stack);
134 ctx.setAttribute(ProviderAdapter.DG_OUTPUT_PARAM_NAMESPACE + ProviderAdapter.PROPERTY_SNAPSHOT_ID,
137 logger.info(EELFResourceManager.format(Msg.STACK_SNAPSHOTED, stack.getName(), snapshot.getId()));
138 metricsLogger.info(EELFResourceManager.format(Msg.STACK_SNAPSHOTED, stack.getName(), snapshot.getId()));
142 ctx.setAttribute(Constants.DG_ATTRIBUTE_STATUS, "failure");
145 } catch (ResourceNotFoundException e) {
146 String msg = EELFResourceManager.format(Msg.STACK_NOT_FOUND, e, vm_url);
148 metricsLogger.error(msg);
149 doFailure(rc, HttpStatus.NOT_FOUND_404, msg, e);
150 } catch (RequestFailedException e) {
151 logger.error(EELFResourceManager.format(Msg.MISSING_PARAMETER_IN_REQUEST, e.getReason(), "snapshotStack"));
153 EELFResourceManager.format(Msg.MISSING_PARAMETER_IN_REQUEST, e.getReason(), "snapshotStack"));
154 doFailure(rc, e.getStatus(), e.getMessage(), e);
155 } catch (Exception e1) {
156 String msg = EELFResourceManager.format(Msg.STACK_OPERATION_EXCEPTION, e1, e1.getClass().getSimpleName(),
157 "snapshotStack", vm_url, null == context ? "n/a" : context.getTenantName());
158 logger.error(msg, e1);
159 metricsLogger.error(msg);
160 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg, e1);
166 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context)
167 throws APPCException {
168 setMDC(Operation.SNAPSHOT_STACK.toString(), "App-C IaaS Adapter:Snapshot-Stack", ADAPTER_NAME);
169 logOperation(Msg.SNAPSHOTING_STACK, params, context);
171 setTimeForMetricsLogger();
173 metricsLogger.info("Executing Provider Operation: Snapshot Stack");
175 return snapshotStack(params, context);
178 private void setTimeForMetricsLogger() {
179 long startTime = System.currentTimeMillis();
180 TimeZone tz = TimeZone.getTimeZone("UTC");
181 DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
183 long endTime = System.currentTimeMillis();
184 long duration = endTime - startTime;
185 String durationStr = String.valueOf(duration);
186 String endTimeStrUTC = df.format(new Date());
187 MDC.put("EndTimestamp", endTimeStrUTC);
188 MDC.put("ElapsedTime", durationStr);
189 MDC.put("TargetEntity", "cdp");
190 MDC.put("TargetServiceName", "snapshot stack");
191 MDC.put("ClassName", "org.onap.appc.adapter.iaas.provider.operation.impl.SnapshotStack");