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