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.ProviderStackOperation;
31 import org.openecomp.appc.adapter.openstack.heat.SnapshotResource;
32 import org.openecomp.appc.adapter.openstack.heat.StackResource;
33 import org.openecomp.appc.exceptions.APPCException;
34 import org.openecomp.appc.exceptions.UnknownProviderException;
35 import org.openecomp.appc.i18n.Msg;
36 import com.att.cdp.exceptions.ResourceNotFoundException;
37 import com.att.cdp.exceptions.ZoneException;
38 import com.att.cdp.openstack.OpenStackContext;
39 import com.att.cdp.openstack.connectors.HeatConnector;
40 import com.att.cdp.openstack.util.ExceptionMapper;
41 import com.att.cdp.zones.Context;
42 import com.att.cdp.zones.model.ModelObject;
43 import com.att.cdp.zones.model.Stack;
44 import com.att.cdp.zones.spi.RequestState;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47 import com.att.eelf.i18n.EELFResourceManager;
48 import org.openecomp.sdnc.sli.SvcLogicContext;
49 import com.woorea.openstack.base.client.OpenStackBaseException;
50 import com.woorea.openstack.heat.Heat;
51 import org.glassfish.grizzly.http.util.HttpStatus;
55 import static org.openecomp.appc.adapter.iaas.provider.operation.common.enums.Operation.RESTART_SERVICE;
56 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
59 public class RestoreStack extends ProviderStackOperation {
61 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestoreStack.class);
63 private void restoreStack(Stack stack, String snapshotId) throws ZoneException, RequestFailedException {
64 Context context = stack.getContext();
66 OpenStackContext osContext = (OpenStackContext)context;
68 final HeatConnector heatConnector = osContext.getHeatConnector();
69 ((OpenStackContext)context).refreshIfStale(heatConnector);
71 trackRequest(context);
72 RequestState.put("SERVICE", "Orchestration");
73 RequestState.put("SERVICE_URL", heatConnector.getEndpoint());
75 Heat heat = heatConnector.getClient();
77 SnapshotResource snapshotResource = new SnapshotResource(heat);
81 snapshotResource.restore(stack.getName(), stack.getId(), snapshotId).execute();
83 // wait for the snapshot restore
84 StackResource stackResource = new StackResource(heat);
85 if (!waitForStack(stack, stackResource, "RESTORE_COMPLETE")) {
86 throw new RequestFailedException("Snapshot restore failed.");
89 } catch (OpenStackBaseException e) {
90 ExceptionMapper.mapException(e);
95 public Stack restoreStack(Map<String, String> params, SvcLogicContext ctx) throws IllegalArgumentException, APPCException {
97 RequestContext rc = new RequestContext(ctx);
100 ctx.setAttribute("SNAPSHOT_STATUS", "STACK_NOT_FOUND");
101 String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
103 String vm_url = null;
104 Context context = null;
108 validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL, ProviderAdapter.PROPERTY_PROVIDER_NAME,
109 ProviderAdapter.PROPERTY_STACK_ID, ProviderAdapter.PROPERTY_INPUT_SNAPSHOT_ID);
111 String stackId = params.get(ProviderAdapter.PROPERTY_STACK_ID);
112 vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
113 String snapshotId = params.get(ProviderAdapter.PROPERTY_INPUT_SNAPSHOT_ID);
115 context = resolveContext(rc, params, appName, vm_url);
118 if (context != null) {
119 stack = lookupStack(rc, context, stackId);
120 logger.debug(Msg.STACK_FOUND, vm_url, context.getTenantName(), stack.getStatus().toString());
121 logger.info(EELFResourceManager.format(Msg.TERMINATING_STACK, stack.getName()));
122 restoreStack(stack, snapshotId);
123 logger.info(EELFResourceManager.format(Msg.TERMINATE_STACK, stack.getName()));
127 ctx.setAttribute(Constants.DG_ATTRIBUTE_STATUS, "failure");
130 } catch (ResourceNotFoundException e) {
131 String msg = EELFResourceManager.format(Msg.STACK_NOT_FOUND, e, vm_url);
133 doFailure(rc, HttpStatus.NOT_FOUND_404, msg, e);
134 } catch (RequestFailedException e) {
135 logger.error(EELFResourceManager.format(Msg.MISSING_PARAMETER_IN_REQUEST, e.getReason(), "restoreStack"));
136 doFailure(rc, e.getStatus(), e.getMessage(), e);
137 } catch (Throwable t) {
138 String msg = EELFResourceManager.format(Msg.STACK_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
139 "restoreStack", vm_url, null == context ? "n/a" : context.getTenantName());
140 logger.error(msg, t);
141 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg, t);
147 protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
149 setMDC(Operation.RESTORE_STACK.toString(), "App-C IaaS Adapter:Restore-Stack", ADAPTER_NAME);
150 logOperation(Msg.RESTORING_STACK, params, context);
151 return restoreStack(params, context);