2366265177484a88d3c26b70b7c1d15849ad6db8
[appc.git] /
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.base;
24
25 import org.openecomp.appc.Constants;
26 import org.openecomp.appc.adapter.iaas.impl.RequestContext;
27 import org.openecomp.appc.adapter.iaas.impl.RequestFailedException;
28 import org.openecomp.appc.adapter.openstack.heat.StackResource;
29 import org.openecomp.appc.i18n.Msg;
30 import com.att.cdp.exceptions.ContextConnectionException;
31 import com.att.cdp.exceptions.ResourceNotFoundException;
32 import com.att.cdp.exceptions.TimeoutException;
33 import com.att.cdp.exceptions.ZoneException;
34 import com.att.cdp.zones.Context;
35 import com.att.cdp.zones.Provider;
36 import com.att.cdp.zones.StackService;
37 import com.att.cdp.zones.model.Stack;
38 import com.att.cdp.zones.spi.AbstractService;
39 import com.att.cdp.zones.spi.RequestState;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42 import com.att.eelf.i18n.EELFResourceManager;
43 import org.openecomp.sdnc.sli.SvcLogicContext;
44 import com.woorea.openstack.base.client.OpenStackBaseException;
45 import org.glassfish.grizzly.http.util.HttpStatus;
46
47 import java.util.List;
48
49 /**
50  * @since September 29, 2016
51  */
52 public abstract class ProviderStackOperation extends ProviderOperation{
53
54     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderStackOperation.class);
55
56
57     protected void trackRequest(Context context, AbstractService.State... states) {
58         RequestState.clear();
59
60         if (null == states) return;
61         for (AbstractService.State state : states) {
62             RequestState.put(state.getName(), state.getValue());
63         }
64
65         Thread currentThread = Thread.currentThread();
66         StackTraceElement[] stack = currentThread.getStackTrace();
67         if (stack != null && stack.length > 0) {
68             int index = 0;
69             StackTraceElement element;
70             for (; index < stack.length; index++) {
71                 element = stack[index];
72                 if ("trackRequest".equals(element.getMethodName())) {  //$NON-NLS-1$
73                     break;
74                 }
75             }
76             index++;
77
78             if (index < stack.length) {
79                 element = stack[index];
80                 RequestState.put(RequestState.METHOD, element.getMethodName());
81                 RequestState.put(RequestState.CLASS, element.getClassName());
82                 RequestState.put(RequestState.LINE_NUMBER, Integer.toString(element.getLineNumber()));
83                 RequestState.put(RequestState.THREAD, currentThread.getName());
84                 RequestState.put(RequestState.PROVIDER, context.getProvider().getName());
85                 RequestState.put(RequestState.TENANT, context.getTenantName());
86                 RequestState.put(RequestState.PRINCIPAL, context.getPrincipal());
87             }
88         }
89     }
90
91     private boolean checkStatus(String expectedStatus, int pollInterval, String actualStatus) {
92         if (actualStatus.toUpperCase().equals(expectedStatus)) {
93             return true;
94         } else {
95             try {
96                 Thread.sleep(pollInterval * 1000);
97             } catch (InterruptedException ignored) {
98             }
99         }
100         return false;
101     }
102
103     protected boolean waitForStack(Stack stack, StackResource stackResource, String expectedStatus)
104             throws OpenStackBaseException, TimeoutException {
105         int pollInterval = configuration.getIntegerProperty(Constants.PROPERTY_OPENSTACK_POLL_INTERVAL);
106         int timeout = configuration.getIntegerProperty(Constants.PROPERTY_STACK_STATE_CHANGE_TIMEOUT);
107         long maxTimeToWait = System.currentTimeMillis() + (long) timeout * 1000;
108
109         while (System.currentTimeMillis() < maxTimeToWait) {
110             String stackStatus = stackResource.show(stack.getName(), stack.getId()).execute().getStackStatus();
111             logger.debug("Stack status : " + stackStatus);
112             if (stackStatus.toUpperCase().contains("FAILED")) return false;
113             if(checkStatus(expectedStatus, pollInterval, stackStatus)) return true;
114         }
115         throw new TimeoutException("Timeout waiting for stack status change");
116     }
117
118     protected Stack lookupStack(RequestContext rc, Context context, String id)
119             throws ZoneException, RequestFailedException {
120         StackService stackService = context.getStackService();
121         Stack stack = null;
122         String msg;
123         Provider provider = context.getProvider();
124         while (rc.attempt()) {
125             try {
126                 List<Stack> stackList = stackService.getStacks();
127                 for (Stack stackObj : stackList) {
128                     if (stackObj.getId().equals(id)) {
129                         stack = stackObj;
130                         break;
131                     }
132                 }
133                 break;
134             } catch (ContextConnectionException e) {
135                 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, provider.getName(), stackService.getURL(),
136                         context.getTenant().getName(), context.getTenant().getId(), e.getMessage(),
137                         Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
138                         Integer.toString(rc.getRetryLimit()));
139                 logger.error(msg, e);
140                 rc.delay();
141             }
142
143         }
144         if (rc.isFailed()) {
145             msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), stackService.getURL());
146             logger.error(msg);
147             doFailure(rc, HttpStatus.BAD_GATEWAY_502, msg);
148             throw new RequestFailedException("Lookup Stack", msg, HttpStatus.BAD_GATEWAY_502, stack);
149         }
150
151         if (stack == null) {
152             throw new ResourceNotFoundException("Stack not found with Id : {" + id + "}");
153         }
154         return stack;
155     }
156
157
158     protected boolean waitForStackStatus(RequestContext rc, Stack stack, Stack.Status expectedStatus) throws ZoneException, RequestFailedException {
159         SvcLogicContext ctx = rc.getSvcLogicContext();
160         Context context = stack.getContext();
161         StackService stackService = context.getStackService();
162
163         int pollInterval = configuration.getIntegerProperty(Constants.PROPERTY_OPENSTACK_POLL_INTERVAL);
164         int timeout = configuration.getIntegerProperty(Constants.PROPERTY_STACK_STATE_CHANGE_TIMEOUT);
165         long maxTimeToWait = System.currentTimeMillis() + (long) timeout * 1000;
166         Stack.Status stackStatus;
167         while (System.currentTimeMillis() < maxTimeToWait) {
168             stackStatus = stackService.getStack(stack.getName(), stack.getId()).getStatus();
169             logger.debug("Stack status : " + stackStatus.toString());
170             if (stackStatus == expectedStatus) {
171                 return true;
172             } else if (stackStatus == Stack.Status.FAILED) {
173                 return false;
174             } else {
175                 try {
176                     Thread.sleep(pollInterval * 1000);
177                 } catch (InterruptedException e) {
178                     logger.trace("Sleep threw interrupted exception, should never occur");
179                 }
180             }
181         }
182
183         ctx.setAttribute("TERMINATE_STATUS", "ERROR");
184         throw new TimeoutException("Timeout waiting for stack status change");
185
186     }
187 }