aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoCommonUtils.java
blob: 7d6de317ad92d7ccddebc01939fd0a6fe9f79c19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.mso.openstack.utils;


import org.openecomp.mso.logger.MsoAlarmLogger;
import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.openstack.exceptions.MsoAdapterException;
import org.openecomp.mso.openstack.exceptions.MsoException;
import org.openecomp.mso.openstack.exceptions.MsoExceptionCategory;
import org.openecomp.mso.openstack.exceptions.MsoIOException;
import org.openecomp.mso.openstack.exceptions.MsoOpenstackException;
import org.openecomp.mso.properties.MsoJavaProperties;
import com.woorea.openstack.base.client.OpenStackBaseException;
import com.woorea.openstack.base.client.OpenStackConnectException;
import com.woorea.openstack.base.client.OpenStackRequest;
import com.woorea.openstack.base.client.OpenStackResponseException;
import com.woorea.openstack.heat.model.Explanation;
import com.woorea.openstack.keystone.model.Error;
import com.woorea.openstack.quantum.model.NeutronError;

public class MsoCommonUtils {

    private static MsoLogger logger = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
    protected static MsoAlarmLogger alarmLogger = new MsoAlarmLogger();
    protected static String retryDelayProp = "ecomp.mso.adapters.po.retryDelay";
    protected static String retryCountProp = "ecomp.mso.adapters.po.retryCount";
    protected static String retryCodesProp = "ecomp.mso.adapters.po.retryCodes";
    protected static int retryDelayDefault = 5;
    protected static int retryCountDefault = 3;
    protected static String retryCodesDefault = "504";
  
    /*
     * Method to execute an Openstack command and track its execution time.
     * For the metrics log, a category of "Openstack" is used along with a
     * sub-category that identifies the specific call (using the real
     * openstack-java-sdk classname of the OpenStackRequest<T> parameter).
     */
    
    protected static <T> T executeAndRecordOpenstackRequest (OpenStackRequest <T> request)
    {
    	return executeAndRecordOpenstackRequest (request, null);
    }
    protected static <T> T executeAndRecordOpenstackRequest (OpenStackRequest <T> request, MsoJavaProperties msoProps) {
    	
    	int limit;
        // Get the name and method name of the parent class, which triggered this method
        StackTraceElement[] classArr = new Exception ().getStackTrace ();
        if (classArr.length >=2) {
        	limit = 3;
        } else {
        	limit = classArr.length;
        }
    	String parentServiceMethodName = classArr[0].getClassName () + "." + classArr[0].getMethodName ();
    	for (int i = 1; i < limit; i++) {
            String className = classArr[i].getClassName ();
            if (!className.equals (MsoCommonUtils.class.getName ())) {
            	parentServiceMethodName = className + "." + classArr[i].getMethodName ();
            	break;
            }
        }

    	String requestType;
        if (request.getClass ().getEnclosingClass () != null) {
            requestType = request.getClass ().getEnclosingClass ().getSimpleName () + "."
                          + request.getClass ().getSimpleName ();
        } else {
            requestType = request.getClass ().getSimpleName ();
        }
        
        int retryDelay = retryDelayDefault;
        int retryCount = retryCountDefault;
        String retryCodes  = retryCodesDefault;
        if (msoProps != null) //extra check to avoid NPE
        {
        	retryDelay = msoProps.getIntProperty (retryDelayProp, retryDelayDefault);
        	retryCount = msoProps.getIntProperty (retryCountProp, retryCountDefault);
        	retryCodes = msoProps.getProperty (retryCodesProp, retryCodesDefault);
        }
    	
        // Run the actual command. All exceptions will be propagated
        while (true)
        {
        	try {
                return request.execute ();
        	} 
        	catch (OpenStackResponseException e) {
        		boolean retry = false;
        		if (retryCodes != null ) {
        			int code = e.getStatus();
                    logger.debug ("Config values RetryDelay:" + retryDelay + " RetryCount:" + retryCount + " RetryCodes:" + retryCodes + " ResponseCode:" + code);
        			for (String rCode : retryCodes.split (",")) {
        				try {
        					if (retryCount > 0 && code == Integer.parseInt (rCode))
        					{
        						retryCount--;
        						retry = true;
                                logger.debug ("OpenStackResponseException ResponseCode:" + code +  " at:" + parentServiceMethodName + " request:" + requestType +  " Retry indicated. Attempts remaining:" + retryCount);
        						break;
        					}
        				} catch (NumberFormatException e1) {
                            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");
        					throw e;
        				}
        			}
        		}
        		if (retry)
    			{
    				try {
    					Thread.sleep (retryDelay * 1000L);
    				} catch (InterruptedException e1) {
                        logger.debug ("Thread interrupted while sleeping", e1);
						Thread.currentThread().interrupt();
    				}
    			}
        		else
        			throw e; // exceeded retryCount or code is not retryable
        	}
        	catch (OpenStackConnectException e) {
        		// Connection to Openstack failed
        		if (retryCount > 0)
        		{
        			retryCount--;
                    logger.debug ("OpenstackConnectException at:" + parentServiceMethodName + " request:" + requestType + " Retry indicated. Attempts remaining:" + retryCount);
        			try {
        				Thread.sleep (retryDelay * 1000L);
        			} catch (InterruptedException e1) {
                        logger.debug ("Thread interrupted while sleeping", e1);
						Thread.currentThread().interrupt();
        			}
        		}
        		else
        			throw e;
        			
        	}
        }
    }
  
    /*
     * Convert an Openstack Exception on a Keystone call to an MsoException.
     * This method supports both OpenstackResponseException and OpenStackConnectException.
     */
    protected static MsoException keystoneErrorToMsoException (OpenStackBaseException e, String context) {
        MsoException me = null;

        if (e instanceof OpenStackResponseException) {
            OpenStackResponseException re = (OpenStackResponseException) e;

            try {
                // Failed Keystone calls return an Error entity body.
                Error error = re.getResponse ().getErrorEntity (Error.class);
                logger.error (MessageEnum.RA_CONNECTION_EXCEPTION, "Openstack Keystone Error on " + context + ": " + error, "Openstack", "", MsoLogger.ErrorCode.DataError, "Openstack Keystone Error on " + context);
				me = new MsoOpenstackException (error.getCode (), error.getTitle (), error.getMessage ());
            } catch (Exception e2) {
                // Can't parse the body as an "Error". Report the HTTP error
                logger.error (MessageEnum.RA_CONNECTION_EXCEPTION, "HTTP Error on " + context + ": " + re.getStatus() + "," + re.getMessage(), "Openstack", "", MsoLogger.ErrorCode.DataError, "HTTP Error on " + context, e2);
				me = new MsoOpenstackException (re.getStatus (), re.getMessage (), "");
            }

            // Add the context of the error
            me.addContext (context);

            // Generate an alarm for 5XX and higher errors.
            if (re.getStatus () >= 500) {
                alarmLogger.sendAlarm ("KeystoneError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
            }
        } else if (e instanceof OpenStackConnectException) {
            OpenStackConnectException ce = (OpenStackConnectException) e;

            me = new MsoIOException (ce.getMessage ());
            me.addContext (context);

            // Generate an alarm for all connection errors.
            logger.error(MessageEnum.RA_GENERAL_EXCEPTION_ARG, "Openstack Keystone connection error on " + context + ": " + e, "Openstack", "", MsoLogger.ErrorCode.DataError, "Openstack Keystone connection error on " + context);
			alarmLogger.sendAlarm ("KeystoneIOError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
        }

        return me;
    }

    /*
     * Convert an Openstack Exception on a Heat call to an MsoOpenstackException.
     * This method supports both OpenstackResponseException and OpenStackConnectException.
     */
    protected MsoException heatExceptionToMsoException (OpenStackBaseException e, String context) {
        MsoException me = null;

        if (e instanceof OpenStackResponseException) {
            OpenStackResponseException re = (OpenStackResponseException) e;

            try {
                // Failed Heat calls return an Explanation entity body.
                Explanation explanation = re.getResponse ().getErrorEntity (Explanation.class);
                logger.error (MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Error on " + context + ": " + explanation.toString(), "Openstack", "", MsoLogger.ErrorCode.DataError, "Exception - Openstack Error on " + context);
                String fullError = explanation.getExplanation() + ", error.type=" + explanation.getError().getType() + ", error.message=" + explanation.getError().getMessage();
                logger.debug(fullError);
				me = new MsoOpenstackException (explanation.getCode (),
                                                explanation.getTitle (),
                                                //explanation.getExplanation ());
                                                fullError);
            } catch (Exception e2) {
                // Couldn't parse the body as an "Explanation". Report the original HTTP error.
                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);
				me = new MsoOpenstackException (re.getStatus (), re.getMessage (), "");
            }

            // Add the context of the error
            me.addContext (context);

            // Generate an alarm for 5XX and higher errors.
            if (re.getStatus () >= 500) {
                alarmLogger.sendAlarm ("HeatError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
            }
        } else if (e instanceof OpenStackConnectException) {
            OpenStackConnectException ce = (OpenStackConnectException) e;

            me = new MsoIOException (ce.getMessage ());
            me.addContext (context);

            // Generate an alarm for all connection errors.
            alarmLogger.sendAlarm ("HeatIOError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
            logger.error(MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Heat connection error on " + context + ": " + e, "Openstack", "", MsoLogger.ErrorCode.DataError, "Openstack Heat connection error on " + context);
    	}

        return me;
    }

    /*
     * Convert an Openstack Exception on a Neutron call to an MsoOpenstackException.
     * This method supports both OpenstackResponseException and OpenStackConnectException.
     */
    protected MsoException neutronExceptionToMsoException (OpenStackBaseException e, String context) {
        MsoException me = null;

        if (e instanceof OpenStackResponseException) {
            OpenStackResponseException re = (OpenStackResponseException) e;

            try {
                // Failed Neutron calls return an NeutronError entity body
                NeutronError error = re.getResponse ().getErrorEntity (NeutronError.class);
                logger.error (MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Neutron Error on " + context + ": " + error, "Openstack", "", MsoLogger.ErrorCode.DataError, "Openstack Neutron Error on " + context);
				me = new MsoOpenstackException (re.getStatus (), error.getType (), error.getMessage ());
            } catch (Exception e2) {
                // Couldn't parse body as a NeutronError. Report the HTTP error.
                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);
				me = new MsoOpenstackException (re.getStatus (), re.getMessage (), null);
            }

            // Add the context of the error
            me.addContext (context);

            // Generate an alarm for 5XX and higher errors.
            if (re.getStatus () >= 500) {
                alarmLogger.sendAlarm ("NeutronError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
            }
        } else if (e instanceof OpenStackConnectException) {
            OpenStackConnectException ce = (OpenStackConnectException) e;

            me = new MsoIOException (ce.getMessage ());
            me.addContext (context);

            // Generate an alarm for all connection errors.
            alarmLogger.sendAlarm ("NeutronIOError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());
            logger.error(MessageEnum.RA_CONNECTION_EXCEPTION, "OpenStack", "Openstack Neutron Connection error on "+ context + ": " + e, "OpenStack", "", MsoLogger.ErrorCode.DataError, "Openstack Neutron Connection error on "+ context);
    	}

        return me;
    }

    /*
     * Convert a Java Runtime Exception to an MsoException.
     * All Runtime exceptions will be translated into an MsoAdapterException,
     * which captures internal errors.
     * Alarms will be generated on all such exceptions.
     */
    protected MsoException runtimeExceptionToMsoException (RuntimeException e, String context) {
        MsoAdapterException me = new MsoAdapterException (e.getMessage (), e);
        me.addContext (context);
        me.setCategory (MsoExceptionCategory.INTERNAL);

        // Always generate an alarm for internal exceptions
        logger.error(MessageEnum.RA_GENERAL_EXCEPTION_ARG, "An exception occured on  "+ context + ": " + e, "OpenStack", "", MsoLogger.ErrorCode.DataError, "An exception occured on  "+ context);
		alarmLogger.sendAlarm ("AdapterInternalError", MsoAlarmLogger.CRITICAL, me.getContextMessage ());

        return me;
    }

    public static boolean isNullOrEmpty (String s) {
        return s == null || s.isEmpty();
    }



}