2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 package org.openecomp.appc.adapter.iaas.provider.operation.impl.base;
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;
47 import java.util.List;
50 * @since September 29, 2016
52 public abstract class ProviderStackOperation extends ProviderOperation{
54 private static final EELFLogger logger = EELFManager.getInstance().getLogger(ProviderStackOperation.class);
57 protected void trackRequest(Context context, AbstractService.State... states) {
60 if (null == states) return;
61 for (AbstractService.State state : states) {
62 RequestState.put(state.getName(), state.getValue());
65 Thread currentThread = Thread.currentThread();
66 StackTraceElement[] stack = currentThread.getStackTrace();
67 if (stack != null && stack.length > 0) {
69 StackTraceElement element;
70 for (; index < stack.length; index++) {
71 element = stack[index];
72 if ("trackRequest".equals(element.getMethodName())) { //$NON-NLS-1$
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());
91 private boolean checkStatus(String expectedStatus, int pollInterval, String actualStatus) {
92 if (actualStatus.toUpperCase().equals(expectedStatus)) {
96 Thread.sleep(pollInterval * 1000);
97 } catch (InterruptedException ignored) {
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;
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;
115 throw new TimeoutException("Timeout waiting for stack status change");
118 protected Stack lookupStack(RequestContext rc, Context context, String id)
119 throws ZoneException, RequestFailedException {
120 StackService stackService = context.getStackService();
123 Provider provider = context.getProvider();
124 while (rc.attempt()) {
126 List<Stack> stackList = stackService.getStacks();
127 for (Stack stackObj : stackList) {
128 if (stackObj.getId().equals(id)) {
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);
145 msg = EELFResourceManager.format(Msg.CONNECTION_FAILED, provider.getName(), stackService.getURL());
147 doFailure(rc, HttpStatus.BAD_GATEWAY_502, msg);
148 throw new RequestFailedException("Lookup Stack", msg, HttpStatus.BAD_GATEWAY_502, stack);
152 throw new ResourceNotFoundException("Stack not found with Id : {" + id + "}");
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();
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) {
172 } else if (stackStatus == Stack.Status.FAILED) {
176 Thread.sleep(pollInterval * 1000);
177 } catch (InterruptedException e) {
178 logger.trace("Sleep threw interrupted exception, should never occur");
183 ctx.setAttribute("TERMINATE_STATUS", "ERROR");
184 throw new TimeoutException("Timeout waiting for stack status change");