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