2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.adapter.iaas.provider.operation.impl.base;
27 import org.onap.appc.Constants;
28 import org.onap.appc.adapter.iaas.impl.RequestContext;
29 import org.onap.appc.adapter.iaas.impl.RequestFailedException;
30 import org.onap.appc.adapter.openstack.heat.StackResource;
31 import org.onap.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 import java.util.List;
50 public abstract class ProviderStackOperation extends ProviderOperation {
52 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderStackOperation.class);
55 protected void trackRequest(Context context, AbstractService.State... states) {
60 for (AbstractService.State state : states) {
61 RequestState.put(state.getName(), state.getValue());
64 Thread currentThread = Thread.currentThread();
65 StackTraceElement[] stack = currentThread.getStackTrace();
66 if (stack != null && stack.length > 0) {
68 StackTraceElement element;
69 for (; index < stack.length; index++) {
70 element = stack[index];
71 if ("trackRequest".equals(element.getMethodName())) { //$NON-NLS-1$
77 if (index < stack.length) {
78 element = stack[index];
79 RequestState.put(RequestState.METHOD, element.getMethodName());
80 RequestState.put(RequestState.CLASS, element.getClassName());
81 RequestState.put(RequestState.LINE_NUMBER, Integer.toString(element.getLineNumber()));
82 RequestState.put(RequestState.THREAD, currentThread.getName());
83 RequestState.put(RequestState.PROVIDER, context.getProvider().getName());
84 RequestState.put(RequestState.TENANT, context.getTenantName());
85 RequestState.put(RequestState.PRINCIPAL, context.getPrincipal());
91 * Changed the 'pollInterval' type as long. Thread.sleep method needs 'long millis' as an argument
93 private boolean checkStatus(String expectedStatus, long pollInterval, String actualStatus) {
94 if (actualStatus.toUpperCase().equals(expectedStatus)) {
98 Thread.sleep(pollInterval * 1000);
99 } catch (InterruptedException ignored) {
105 protected boolean waitForStack(Stack stack, StackResource stackResource, String expectedStatus)
106 throws OpenStackBaseException, TimeoutException {
107 int pollInterval = configuration.getIntegerProperty(Constants.PROPERTY_OPENSTACK_POLL_INTERVAL);
108 int timeout = configuration.getIntegerProperty(Constants.PROPERTY_STACK_STATE_CHANGE_TIMEOUT);
109 long maxTimeToWait = System.currentTimeMillis() + (long) timeout * 1000;
111 while (System.currentTimeMillis() < maxTimeToWait) {
112 String stackStatus = stackResource.show(stack.getName(), stack.getId()).execute().getStackStatus();
113 logger.debug("Stack status : " + stackStatus);
114 if (stackStatus.toUpperCase().contains("FAILED"))
116 if (checkStatus(expectedStatus, pollInterval, stackStatus))
119 throw new TimeoutException("Timeout waiting for stack status change");
122 protected Stack lookupStack(RequestContext rc, Context context, String id)
123 throws ZoneException, RequestFailedException {
124 StackService stackService = context.getStackService();
127 Provider provider = context.getProvider();
128 while (rc.attempt()) {
130 List<Stack> stackList = stackService.getStacks();
131 for (Stack stackObj : stackList) {
132 if (stackObj.getId().equals(id)) {
138 } catch (ContextConnectionException e) {
139 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED_RETRY, provider.getName(), stackService.getURL(),
140 context.getTenant().getName(), context.getTenant().getId(), e.getMessage(),
141 Long.toString(rc.getRetryDelay()), Integer.toString(rc.getAttempts()),
142 Integer.toString(rc.getRetryLimit()));
143 logger.error(msg, e);
149 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), stackService.getURL());
151 doFailure(rc, HttpStatus.BAD_GATEWAY_502, msg);
152 throw new RequestFailedException("Lookup Stack", msg, HttpStatus.BAD_GATEWAY_502, stack);
156 throw new ResourceNotFoundException("Stack not found with Id : {" + id + "}");
162 protected boolean waitForStackStatus(RequestContext rc, Stack stack, Stack.Status expectedStatus)
163 throws ZoneException, RequestFailedException {
164 SvcLogicContext ctx = rc.getSvcLogicContext();
165 Context context = stack.getContext();
166 StackService stackService = context.getStackService();
168 * Changed the 'pollInterval' type as long. Thread.sleep method needs 'long millis' as an argument
170 long pollInterval = configuration.getIntegerProperty(Constants.PROPERTY_OPENSTACK_POLL_INTERVAL);
171 int timeout = configuration.getIntegerProperty(Constants.PROPERTY_STACK_STATE_CHANGE_TIMEOUT);
172 long maxTimeToWait = System.currentTimeMillis() + (long) timeout * 1000;
173 Stack.Status stackStatus;
174 while (System.currentTimeMillis() < maxTimeToWait) {
175 stackStatus = stackService.getStack(stack.getName(), stack.getId()).getStatus();
176 logger.debug("Stack status : " + stackStatus.toString());
177 if (stackStatus == expectedStatus) {
179 } else if (stackStatus == Stack.Status.FAILED) {
183 Thread.sleep(pollInterval * 1000);
184 } catch (InterruptedException e) {
185 logger.trace("Sleep threw interrupted exception, should never occur");
190 ctx.setAttribute("TERMINATE_STATUS", "ERROR");
191 throw new TimeoutException("Timeout waiting for stack status change");