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