d2cbf7a6da9372b1fceedb3baf7805348445bc0e
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / openecomp / appc / adapter / iaas / provider / operation / impl / TerminateStack.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.adapter.iaas.provider.operation.impl;
26
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.exceptions.APPCException;
34 import org.openecomp.appc.i18n.Msg;
35 import com.att.cdp.exceptions.ResourceNotFoundException;
36 import com.att.cdp.exceptions.ZoneException;
37 import com.att.cdp.zones.Context;
38 import com.att.cdp.zones.StackService;
39 import com.att.cdp.zones.model.ModelObject;
40 import com.att.cdp.zones.model.Stack;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import com.att.eelf.i18n.EELFResourceManager;
44 import org.openecomp.sdnc.sli.SvcLogicContext;
45 import org.glassfish.grizzly.http.util.HttpStatus;
46
47 import java.util.Map;
48
49 import static org.openecomp.appc.adapter.utils.Constants.ADAPTER_NAME;
50
51 /**
52  * @since September 26, 2016
53  */
54 public class TerminateStack extends ProviderStackOperation {
55
56     private static final EELFLogger logger = EELFManager.getInstance().getLogger(EvacuateServer.class);
57
58     private void deleteStack(RequestContext rc, Stack stack) throws ZoneException, RequestFailedException {
59         SvcLogicContext ctx = rc.getSvcLogicContext();
60         Context context = stack.getContext();
61         StackService stackService = context.getStackService();
62         logger.debug("Deleting Stack: " + "id:{ " + stack.getId() + "}");
63         stackService.deleteStack(stack);
64
65         // wait for the stack deletion
66         boolean success = waitForStackStatus(rc, stack, Stack.Status.DELETED);
67         if (success) {
68             ctx.setAttribute("TERMINATE_STATUS", "SUCCESS");
69         } else {
70             ctx.setAttribute("TERMINATE_STATUS", "ERROR");
71             throw new RequestFailedException("Delete Stack failure : " + Msg.STACK_OPERATION_EXCEPTION.toString());
72         }
73     }
74
75     @SuppressWarnings("nls")
76     public Stack terminateStack(Map<String, String> params, SvcLogicContext ctx) throws IllegalArgumentException, APPCException {
77         Stack stack = null;
78         RequestContext rc = new RequestContext(ctx);
79         rc.isAlive();
80
81         ctx.setAttribute("TERMINATE_STATUS", "STACK_NOT_FOUND");
82         String appName = configuration.getProperty(Constants.PROPERTY_APPLICATION_NAME);
83
84         try {
85
86             validateParametersExist(params, ProviderAdapter.PROPERTY_INSTANCE_URL,
87                     ProviderAdapter.PROPERTY_PROVIDER_NAME, ProviderAdapter.PROPERTY_STACK_ID);
88
89             String stackId = params.get(ProviderAdapter.PROPERTY_STACK_ID);
90             String vm_url = params.get(ProviderAdapter.PROPERTY_INSTANCE_URL);
91
92             Context context = resolveContext(rc, params, appName, vm_url);
93
94             try {
95                 if (context != null) {
96                     rc.reset();
97                     stack = lookupStack(rc, context, stackId);
98                     logger.debug(Msg.STACK_FOUND, vm_url, context.getTenantName(), stack.getStatus().toString());
99                     logger.info(EELFResourceManager.format(Msg.TERMINATING_STACK, stack.getName()));
100                     deleteStack(rc, stack);
101                     logger.info(EELFResourceManager.format(Msg.TERMINATE_STACK, stack.getName()));
102                     context.close();
103                     doSuccess(rc);
104                     String msg = EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "TerminateStack", vm_url);
105                     ctx.setAttribute(org.openecomp.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE, msg);
106                 }
107             } catch (ResourceNotFoundException e) {
108                 String msg = EELFResourceManager.format(Msg.STACK_NOT_FOUND, e, vm_url);
109                 logger.error(msg);
110                 doFailure(rc, HttpStatus.NOT_FOUND_404, msg);
111             } catch (Throwable t) {
112                 String msg = EELFResourceManager.format(Msg.STACK_OPERATION_EXCEPTION, t, t.getClass().getSimpleName(),
113                         Operation.TERMINATE_STACK.toString(), vm_url, context.getTenantName());
114                 logger.error(msg, t);
115                 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, msg);
116             }
117         } catch (RequestFailedException e) {
118             logger.error(EELFResourceManager.format(Msg.TERMINATE_STACK_FAILED, appName, "n/a", "n/a"));
119             doFailure(rc, e.getStatus(), e.getMessage());
120         }
121         return stack;
122     }
123
124     @Override
125     protected ModelObject executeProviderOperation(Map<String, String> params, SvcLogicContext context) throws APPCException {
126
127         setMDC(Operation.TERMINATE_STACK.toString(), "App-C IaaS Adapter:Terminate-Stack", ADAPTER_NAME);
128         logOperation(Msg.TERMINATING_STACK, params, context);
129         return terminateStack(params, context);
130     }
131 }