2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.iaas.provider.operation.impl.base;
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;
49 public abstract class ProviderStackOperation extends ProviderOperation {
51 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderStackOperation.class);
54 protected void trackRequest(Context context, AbstractService.State... states) {
59 for (AbstractService.State state : states) {
60 RequestState.put(state.getName(), state.getValue());
63 Thread currentThread = Thread.currentThread();
64 StackTraceElement[] stack = currentThread.getStackTrace();
65 if (stack != null && stack.length > 0) {
67 StackTraceElement element;
68 for (; index < stack.length; index++) {
69 element = stack[index];
70 if ("trackRequest".equals(element.getMethodName())) { //$NON-NLS-1$
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());
90 * Changed the 'pollInterval' type as long. Thread.sleep method needs 'long millis' as an argument
92 private boolean checkStatus(String expectedStatus, long pollInterval, String actualStatus) {
93 if (actualStatus.toUpperCase().equals(expectedStatus)) {
97 Thread.sleep(pollInterval * 1000);
98 } catch (InterruptedException ignored) {
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;
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"))
115 if (checkStatus(expectedStatus, pollInterval, stackStatus))
118 throw new TimeoutException("Timeout waiting for stack status change");
121 protected Stack lookupStack(RequestContext rc, Context context, String id)
122 throws ZoneException, RequestFailedException {
123 StackService stackService = context.getStackService();
126 Provider provider = context.getProvider();
127 while (rc.attempt()) {
129 List<Stack> stackList = stackService.getStacks();
130 for (Stack stackObj : stackList) {
131 if (stackObj.getId().equals(id)) {
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);
148 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), stackService.getURL());
150 doFailure(rc, HttpStatus.BAD_GATEWAY_502, msg);
151 throw new RequestFailedException("Lookup Stack", msg, HttpStatus.BAD_GATEWAY_502, stack);
155 throw new ResourceNotFoundException("Stack not found with Id : {" + id + "}");
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();
167 * Changed the 'pollInterval' type as long. Thread.sleep method needs 'long millis' as an argument
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) {
178 } else if (stackStatus == Stack.Status.FAILED) {
182 Thread.sleep(pollInterval * 1000);
183 } catch (InterruptedException e) {
184 logger.trace("Sleep threw interrupted exception, should never occur");
189 ctx.setAttribute("TERMINATE_STATUS", "ERROR");
190 throw new TimeoutException("Timeout waiting for stack status change");