Modify iaas adapter provider files license headers
[appc.git] / appc-adapters / appc-iaas-adapter / appc-iaas-adapter-bundle / src / main / java / org / onap / appc / adapter / iaas / provider / operation / impl / base / ProviderStackOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.iaas.provider.operation.impl.base;
25
26 import org.onap.appc.Constants;
27 import org.onap.appc.adapter.iaas.impl.RequestContext;
28 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
29 import org.onap.appc.adapter.openstack.heat.StackResource;
30 import org.onap.appc.i18n.Msg;
31 import com.att.cdp.exceptions.ContextConnectionException;
32 import com.att.cdp.exceptions.ResourceNotFoundException;
33 import com.att.cdp.exceptions.TimeoutException;
34 import com.att.cdp.exceptions.ZoneException;
35 import com.att.cdp.zones.Context;
36 import com.att.cdp.zones.Provider;
37 import com.att.cdp.zones.StackService;
38 import com.att.cdp.zones.model.Stack;
39 import com.att.cdp.zones.spi.AbstractService;
40 import com.att.cdp.zones.spi.RequestState;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import com.att.eelf.i18n.EELFResourceManager;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
45 import com.woorea.openstack.base.client.OpenStackBaseException;
46 import org.glassfish.grizzly.http.util.HttpStatus;
47 import java.util.List;
48
49 public abstract class ProviderStackOperation extends ProviderOperation {
50
51     private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderStackOperation.class);
52
53
54     protected void trackRequest(Context context, AbstractService.State... states) {
55         RequestState.clear();
56
57         if (null == states)
58             return;
59         for (AbstractService.State state : states) {
60             RequestState.put(state.getName(), state.getValue());
61         }
62
63         Thread currentThread = Thread.currentThread();
64         StackTraceElement[] stack = currentThread.getStackTrace();
65         if (stack != null && stack.length > 0) {
66             int index = 0;
67             StackTraceElement element;
68             for (; index < stack.length; index++) {
69                 element = stack[index];
70                 if ("trackRequest".equals(element.getMethodName())) { //$NON-NLS-1$
71                     break;
72                 }
73             }
74             index++;
75
76             if (index < stack.length) {
77                 element = stack[index];
78                 RequestState.put(RequestState.METHOD, element.getMethodName());
79                 RequestState.put(RequestState.CLASS, element.getClassName());
80                 RequestState.put(RequestState.LINE_NUMBER, Integer.toString(element.getLineNumber()));
81                 RequestState.put(RequestState.THREAD, currentThread.getName());
82                 RequestState.put(RequestState.PROVIDER, context.getProvider().getName());
83                 RequestState.put(RequestState.TENANT, context.getTenantName());
84                 RequestState.put(RequestState.PRINCIPAL, context.getPrincipal());
85             }
86         }
87     }
88
89     /*
90      * Changed the 'pollInterval' type as long. Thread.sleep method needs 'long millis' as an argument
91      */
92     private boolean checkStatus(String expectedStatus, long pollInterval, String actualStatus) {
93         if (actualStatus.toUpperCase().equals(expectedStatus)) {
94             return true;
95         } else {
96             try {
97                 Thread.sleep(pollInterval * 1000);
98             } catch (InterruptedException ignored) {
99             }
100         }
101         return false;
102     }
103
104     protected boolean waitForStack(Stack stack, StackResource stackResource, String expectedStatus)
105             throws OpenStackBaseException, TimeoutException {
106         int pollInterval = configuration.getIntegerProperty(Constants.PROPERTY_OPENSTACK_POLL_INTERVAL);
107         int timeout = configuration.getIntegerProperty(Constants.PROPERTY_STACK_STATE_CHANGE_TIMEOUT);
108         long maxTimeToWait = System.currentTimeMillis() + (long) timeout * 1000;
109
110         while (System.currentTimeMillis() < maxTimeToWait) {
111             String stackStatus = stackResource.show(stack.getName(), stack.getId()).execute().getStackStatus();
112             logger.debug("Stack status : " + stackStatus);
113             if (stackStatus.toUpperCase().contains("FAILED"))
114                 return false;
115             if (checkStatus(expectedStatus, pollInterval, stackStatus))
116                 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)
162             throws ZoneException, RequestFailedException {
163         SvcLogicContext ctx = rc.getSvcLogicContext();
164         Context context = stack.getContext();
165         StackService stackService = context.getStackService();
166         /*
167          * Changed the 'pollInterval' type as long. Thread.sleep method needs 'long millis' as an argument
168          */
169         long pollInterval = configuration.getIntegerProperty(Constants.PROPERTY_OPENSTACK_POLL_INTERVAL);
170         int timeout = configuration.getIntegerProperty(Constants.PROPERTY_STACK_STATE_CHANGE_TIMEOUT);
171         long maxTimeToWait = System.currentTimeMillis() + (long) timeout * 1000;
172         Stack.Status stackStatus;
173         while (System.currentTimeMillis() < maxTimeToWait) {
174             stackStatus = stackService.getStack(stack.getName(), stack.getId()).getStatus();
175             logger.debug("Stack status : " + stackStatus.toString());
176             if (stackStatus == expectedStatus) {
177                 return true;
178             } else if (stackStatus == Stack.Status.FAILED) {
179                 return false;
180             } else {
181                 try {
182                     Thread.sleep(pollInterval * 1000);
183                 } catch (InterruptedException e) {
184                     logger.trace("Sleep threw interrupted exception, should never occur");
185                 }
186             }
187         }
188
189         ctx.setAttribute("TERMINATE_STATUS", "ERROR");
190         throw new TimeoutException("Timeout waiting for stack status change");
191
192     }
193 }