2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.mso.openstack.utils;
24 import org.openecomp.mso.logger.MsoAlarmLogger;
25 import org.openecomp.mso.logger.MsoLogger;
26 import org.openecomp.mso.logger.MessageEnum;
27 import org.openecomp.mso.openstack.exceptions.MsoAdapterException;
28 import org.openecomp.mso.openstack.exceptions.MsoException;
29 import org.openecomp.mso.openstack.exceptions.MsoExceptionCategory;
30 import org.openecomp.mso.openstack.exceptions.MsoIOException;
31 import org.openecomp.mso.openstack.exceptions.MsoOpenstackException;
32 import org.openecomp.mso.properties.MsoJavaProperties;
33 import com.woorea.openstack.base.client.OpenStackBaseException;
34 import com.woorea.openstack.base.client.OpenStackConnectException;
35 import com.woorea.openstack.base.client.OpenStackRequest;
36 import com.woorea.openstack.base.client.OpenStackResponseException;
37 import com.woorea.openstack.heat.model.Explanation;
38 import com.woorea.openstack.keystone.model.Error;
39 import com.woorea.openstack.quantum.model.NeutronError;
41 public class MsoCommonUtils {
43 private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
44 protected static MsoAlarmLogger alarmLogger = new MsoAlarmLogger();
46 protected static String retryDelayProp = "ecomp.mso.adapters.po.retryDelay";
47 protected static String retryCountProp = "ecomp.mso.adapters.po.retryCount";
48 protected static String retryCodesProp = "ecomp.mso.adapters.po.retryCodes";
50 protected static int retryDelayDefault = 5;
51 protected static int retryCountDefault = 3;
52 protected static String retryCodesDefault = "504";
55 * Method to execute an Openstack command and track its execution time.
56 * For the metrics log, a category of "Openstack" is used along with a
57 * sub-category that identifies the specific call (using the real
58 * openstack-java-sdk classname of the OpenStackRequest<T> parameter).
61 protected static <T> T executeAndRecordOpenstackRequest (OpenStackRequest <T> request)
63 return executeAndRecordOpenstackRequest (request, null);
65 protected static <T> T executeAndRecordOpenstackRequest (OpenStackRequest <T> request, MsoJavaProperties msoProps) {
68 // Get the name and method name of the parent class, which triggered this method
69 StackTraceElement[] classArr = new Exception ().getStackTrace ();
70 if (classArr.length >=2) {
73 limit = classArr.length;
75 String parentServiceMethodName = classArr[0].getClassName () + "." + classArr[0].getMethodName ();
76 for (int i = 1; i < limit; i++) {
77 String className = classArr[i].getClassName ();
78 if (!className.equals (MsoCommonUtils.class.getName ())) {
79 parentServiceMethodName = className + "." + classArr[i].getMethodName ();
84 long start = System.currentTimeMillis ();
86 if (request.getClass ().getEnclosingClass () != null) {
87 requestType = request.getClass ().getEnclosingClass ().getSimpleName () + "."
88 + request.getClass ().getSimpleName ();
90 requestType = request.getClass ().getSimpleName ();
93 int retryDelay = retryDelayDefault;
94 int retryCount = retryCountDefault;
95 String retryCodes = retryCodesDefault;
96 if (msoProps != null) //extra check to avoid NPE
98 retryDelay = msoProps.getIntProperty (retryDelayProp, retryDelayDefault);
99 retryCount = msoProps.getIntProperty (retryCountProp, retryCountDefault);
100 retryCodes = msoProps.getProperty (retryCodesProp, retryCodesDefault);
103 // Run the actual command. All exceptions will be propagated
107 return request.execute ();
109 catch (OpenStackResponseException e) {
110 boolean retry = false;
111 if (retryCodes != null ) {
112 int code = e.getStatus();
113 logger.debug ("Config values RetryDelay:" + retryDelay + " RetryCount:" + retryCount + " RetryCodes:" + retryCodes + " ResponseCode:" + code);
114 for (String rCode : retryCodes.split (",")) {
116 if (retryCount > 0 && code == Integer.parseInt (rCode))
120 logger.debug ("OpenStackResponseException ResponseCode:" + code + " at:" + parentServiceMethodName + " request:" + requestType + " Retry indicated. Attempts remaining:" + retryCount);
123 } catch (NumberFormatException e1) {
124 logger.error (MessageEnum.RA_CONFIG_EXC, "No retries. Exception in parsing retry code in config:" + rCode, "", "", MsoLogger.ErrorCode.SchemaError, "Exception in parsing retry code in config");
132 Thread.sleep (retryDelay * 1000L);
133 } catch (InterruptedException e1) {
134 logger.debug ("Thread interrupted while sleeping", e1);
138 throw e; // exceeded retryCount or code is not retryable
140 catch (OpenStackConnectException e) {
141 // Connection to Openstack failed
145 logger.debug ("OpenstackConnectException at:" + parentServiceMethodName + " request:" + requestType + " Retry indicated. Attempts remaining:" + retryCount);
147 Thread.sleep (retryDelay * 1000L);
148 } catch (InterruptedException e1) {
149 logger.debug ("Thread interrupted while sleeping", e1);
160 * Convert an Openstack Exception on a Keystone call to an MsoException.
161 * This method supports both OpenstackResponseException and OpenStackConnectException.
163 protected static MsoException keystoneErrorToMsoException (OpenStackBaseException e, String context) {
164 MsoException me = null;
166 if (e instanceof OpenStackResponseException) {
167 OpenStackResponseException re = (OpenStackResponseException) e;
170 // Failed Keystone calls return an Error entity body.
171 Error error = re.getResponse ().getErrorEntity (Error.class);
172 logger.error (MessageEnum.RA_CONNECTION_EXCEPTION, "Openstack Keystone Error on " + context + ": " + error, "Openstack", "", MsoLogger.ErrorCode.DataError, "Openstack Keystone Error on " + context);
173 me = new MsoOpenstackException (error.getCode (), error.getTitle (), error.getMessage ());
174 } catch (Exception e2) {
175 // Can't parse the body as an "Error". Report the HTTP error
176 logger.error (MessageEnum.RA_CONNECTION_EXCEPTION, "HTTP Error on " + context + ": " + re.getStatus() + "," + re.getMessage(), "Openstack", "", MsoLogger.ErrorCode.DataError, "HTTP Error on " + context, e2);
177 me = new MsoOpenstackException (re.getStatus (), re.getMessage (), "");
180 // Add the context of the error
181 me.addContext (context);
183 // Generate an alarm for 5XX and higher errors.
184 if (re.getStatus () >= 500) {
185 alarmLogger.sendAlarm ("KeystoneError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
187 } else if (e instanceof OpenStackConnectException) {
188 OpenStackConnectException ce = (OpenStackConnectException) e;
190 me = new MsoIOException (ce.getMessage ());
191 me.addContext (context);
193 // Generate an alarm for all connection errors.
194 logger.error(MessageEnum.RA_GENERAL_EXCEPTION_ARG, "Openstack Keystone connection error on " + context + ": " + e, "Openstack", "", MsoLogger.ErrorCode.DataError, "Openstack Keystone connection error on " + context);
195 alarmLogger.sendAlarm ("KeystoneIOError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
202 * Convert an Openstack Exception on a Heat call to an MsoOpenstackException.
203 * This method supports both OpenstackResponseException and OpenStackConnectException.
205 protected MsoException heatExceptionToMsoException (OpenStackBaseException e, String context) {
206 MsoException me = null;
208 if (e instanceof OpenStackResponseException) {
209 OpenStackResponseException re = (OpenStackResponseException) e;
212 // Failed Heat calls return an Explanation entity body.
213 Explanation explanation = re.getResponse ().getErrorEntity (Explanation.class);
214 logger.error (MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Error on " + context + ": " + explanation.toString(), "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception - Openstack Error on " + context);
215 String fullError = explanation.getExplanation() + ", error.type=" + explanation.getError().getType() + ", error.message=" + explanation.getError().getMessage();
216 logger.debug(fullError);
217 me = new MsoOpenstackException (explanation.getCode (),
218 explanation.getTitle (),
219 //explanation.getExplanation ());
221 } catch (Exception e2) {
222 // Couldn't parse the body as an "Explanation". Report the original HTTP error.
223 logger.error (MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "HTTP Error on " + context + ": " + re.getStatus() + "," + e.getMessage(), "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception - HTTP Error on " + context, e2);
224 me = new MsoOpenstackException (re.getStatus (), re.getMessage (), "");
227 // Add the context of the error
228 me.addContext (context);
230 // Generate an alarm for 5XX and higher errors.
231 if (re.getStatus () >= 500) {
232 alarmLogger.sendAlarm ("HeatError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
234 } else if (e instanceof OpenStackConnectException) {
235 OpenStackConnectException ce = (OpenStackConnectException) e;
237 me = new MsoIOException (ce.getMessage ());
238 me.addContext (context);
240 // Generate an alarm for all connection errors.
241 alarmLogger.sendAlarm ("HeatIOError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
242 logger.error(MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Heat connection error on " + context + ": " + e, "Openstack", "", MsoLogger.ErrorCode.DataError, "Openstack Heat connection error on " + context);
249 * Convert an Openstack Exception on a Neutron call to an MsoOpenstackException.
250 * This method supports both OpenstackResponseException and OpenStackConnectException.
252 protected MsoException neutronExceptionToMsoException (OpenStackBaseException e, String context) {
253 MsoException me = null;
255 if (e instanceof OpenStackResponseException) {
256 OpenStackResponseException re = (OpenStackResponseException) e;
259 // Failed Neutron calls return an NeutronError entity body
260 NeutronError error = re.getResponse ().getErrorEntity (NeutronError.class);
261 logger.error (MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Neutron Error on " + context + ": " + error, "Openstack", "", MsoLogger.ErrorCode.DataError, "Openstack Neutron Error on " + context);
262 me = new MsoOpenstackException (re.getStatus (), error.getType (), error.getMessage ());
263 } catch (Exception e2) {
264 // Couldn't parse body as a NeutronError. Report the HTTP error.
265 logger.error (MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "HTTP Error on " + context + ": " + re.getStatus() + "," + e.getMessage(), "Openstack", "", MsoLogger.ErrorCode.DataError, "Openstack HTTP Error on " + context, e2);
266 me = new MsoOpenstackException (re.getStatus (), re.getMessage (), null);
269 // Add the context of the error
270 me.addContext (context);
272 // Generate an alarm for 5XX and higher errors.
273 if (re.getStatus () >= 500) {
274 alarmLogger.sendAlarm ("NeutronError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
276 } else if (e instanceof OpenStackConnectException) {
277 OpenStackConnectException ce = (OpenStackConnectException) e;
279 me = new MsoIOException (ce.getMessage ());
280 me.addContext (context);
282 // Generate an alarm for all connection errors.
283 alarmLogger.sendAlarm ("NeutronIOError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
284 logger.error(MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Neutron Connection error on "+ context + ": " + e, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Openstack Neutron Connection error on "+ context);
291 * Convert a Java Runtime Exception to an MsoException.
292 * All Runtime exceptions will be translated into an MsoAdapterException,
293 * which captures internal errors.
294 * Alarms will be generated on all such exceptions.
296 protected MsoException runtimeExceptionToMsoException (RuntimeException e, String context) {
297 MsoAdapterException me = new MsoAdapterException (e.getMessage (), e);
298 me.addContext (context);
299 me.setCategory (MsoExceptionCategory.INTERNAL);
301 // Always generate an alarm for internal exceptions
302 logger.error(MessageEnum.RA_GENERAL_EXCEPTION_ARG, "An exception occured on "+ context + ": " + e, "OpenStack", "", MsoLogger.ErrorCode.DataError, "An exception occured on "+ context);
303 alarmLogger.sendAlarm ("AdapterInternalError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
308 public static boolean isNullOrEmpty (String s) {
309 return s == null || s.isEmpty();