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