aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-adapter-utils/src/main/java/org/onap/so/openstack/utils/MsoCommonUtils.java
blob: c9a548d5f1e7cffe34ecf35d99d5f9f8a8d6471e (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2018 Intel Corp. All rights reserved.
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * 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.onap.so.openstack.utils;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
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.CreateStackParam;
import com.woorea.openstack.heat.model.Explanation;
import com.woorea.openstack.keystone.model.Error;
import com.woorea.openstack.quantum.model.NeutronError;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.onap.so.config.beans.PoConfig;
import org.onap.so.logger.ErrorCode;
import org.onap.so.logger.MessageEnum;
import org.onap.so.openstack.exceptions.MsoAdapterException;
import org.onap.so.openstack.exceptions.MsoException;
import org.onap.so.openstack.exceptions.MsoExceptionCategory;
import org.onap.so.openstack.exceptions.MsoIOException;
import org.onap.so.openstack.exceptions.MsoOpenstackException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("CommonUtils")
public class MsoCommonUtils {

    private static Logger logger = LoggerFactory.getLogger(MsoCommonUtils.class);


    @Autowired
    private PoConfig poConfig;
    /*
     * 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 <T> T executeAndRecordOpenstackRequest(OpenStackRequest<T> request) {

        int limit;

        long start = System.currentTimeMillis();
        String requestType;
        if (request.getClass().getEnclosingClass() != null) {
            requestType =
                    request.getClass().getEnclosingClass().getSimpleName() + "." + request.getClass().getSimpleName();
        } else {
            requestType = request.getClass().getSimpleName();
        }

        int retryDelay = poConfig.getRetryDelay();
        int retryCount = poConfig.getRetryCount();
        String retryCodes = poConfig.getRetryCodes();

        // 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:{} RetryCount:{}  RetryCodes:{} ResponseCode:{}", retryDelay,
                            retryCount, retryCodes, code);
                    for (String rCode : retryCodes.split(",")) {
                        try {
                            if (retryCount > 0 && code == Integer.parseInt(rCode)) {
                                retryCount--;
                                retry = true;
                                logger.debug(
                                        "OpenStackResponseException ResponseCode: {} request:{} Retry indicated. Attempts remaining:{}",
                                        code, requestType, retryCount);
                                break;
                            }
                        } catch (NumberFormatException e1) {
                            logger.error("{} No retries. Exception in parsing retry code in config:{} {} {}",
                                    MessageEnum.RA_CONFIG_EXC, rCode, ErrorCode.SchemaError.getValue(),
                                    "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(" request: {} Retry indicated. Attempts remaining:{}", requestType, 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 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("{} {} Openstack Keystone Error on {}: {}", MessageEnum.RA_CONNECTION_EXCEPTION,
                        ErrorCode.DataError.getValue(), context, error);
                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("{} {} HTTP Error on {}: {}, {}", MessageEnum.RA_CONNECTION_EXCEPTION,
                        ErrorCode.DataError.getValue(), context, re.getStatus(), re.getMessage(), 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) {

            }
        } 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("{} {} Openstack Keystone connection error on {}: ", MessageEnum.RA_GENERAL_EXCEPTION_ARG,
                    ErrorCode.DataError.getValue(), context, e);
        }

        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("{} {} Exception - Openstack Error on {} : {}", MessageEnum.RA_CONNECTION_EXCEPTION,
                        ErrorCode.DataError.getValue(), context, explanation.toString());
                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("{} {} Exception - HTTP Error on {}: {}, ", MessageEnum.RA_CONNECTION_EXCEPTION,
                        ErrorCode.DataError.getValue(), context, re.getStatus(), e.getMessage(), 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) {

            }
        } 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("{} {} Openstack Heat connection error on {}: ", MessageEnum.RA_CONNECTION_EXCEPTION,
                    ErrorCode.DataError.getValue(), context, e);
        }

        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("{} {} Openstack Neutron Error on {} {}", MessageEnum.RA_CONNECTION_EXCEPTION,
                        ErrorCode.DataError.getValue(), context, error);
                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("{} {} Openstack HTTP Error on {}: {}, {}", MessageEnum.RA_CONNECTION_EXCEPTION,
                        ErrorCode.DataError.getValue(), context, re.getStatus(), e.getMessage(), 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) {

            }
        } 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("{} {} Openstack Neutron Connection error on {}: ", MessageEnum.RA_CONNECTION_EXCEPTION,
                    ErrorCode.DataError.getValue(), context, e);
        }

        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("{} {} An exception occured on {}: ", MessageEnum.RA_GENERAL_EXCEPTION_ARG,
                ErrorCode.DataError.getValue(), context, e);

        return me;
    }

    protected MsoException ioExceptionToMsoException(IOException 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("{} {} An exception occured on {}: ", MessageEnum.RA_GENERAL_EXCEPTION_ARG,
                ErrorCode.DataError.getValue(), context, e);

        return me;
    }

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


    protected CreateStackParam createStackParam(String stackName, String heatTemplate, Map<String, ?> stackInputs,
            int timeoutMinutes, String environment, Map<String, Object> files, Map<String, Object> heatFiles) {

        // Create local variables checking to see if we have an environment, nested, get_files
        // Could later add some checks to see if it's valid.
        boolean haveEnvtVariable = true;
        if (environment == null || "".equalsIgnoreCase(environment.trim())) {
            haveEnvtVariable = false;
            logger.debug("createStackParam called with no environment variable");
        } else {
            logger.debug("createStackParam called with an environment variable: {}", environment);
        }

        boolean haveFiles = true;
        if (files == null || files.isEmpty()) {
            haveFiles = false;
            logger.debug("createStackParam called with no files / child template ids");
        } else {
            logger.debug("createStackParam called with {} files / child template ids", files.size());
        }

        boolean haveHeatFiles = true;
        if (heatFiles == null || heatFiles.isEmpty()) {
            haveHeatFiles = false;
            logger.debug("createStackParam called with no heatFiles");
        } else {
            logger.debug("createStackParam called with {} heatFiles", heatFiles.size());
        }

        // force entire stackInput object to generic Map<String, Object> for openstack compatibility
        ObjectMapper mapper = new ObjectMapper();
        Map<String, Object> normalized = new HashMap<>();
        try {
            normalized = mapper.readValue(mapper.writeValueAsString(stackInputs),
                    new TypeReference<HashMap<String, Object>>() {});
        } catch (IOException e1) {
            logger.debug("could not map json", e1);
        }

        // Build up the stack to create
        // Disable auto-rollback, because error reason is lost. Always rollback in the code.
        CreateStackParam stack = new CreateStackParam();
        stack.setStackName(stackName);
        stack.setTimeoutMinutes(timeoutMinutes);
        stack.setParameters(normalized);
        stack.setTemplate(heatTemplate);
        stack.setDisableRollback(true);
        // TJM New for PO Adapter - add envt variable
        if (haveEnvtVariable) {
            logger.debug("Found an environment variable - value: {}", environment);
            stack.setEnvironment(environment);
        }
        // Now handle nested templates or get_files - have to combine if we have both
        // as they're both treated as "files:" on the stack.
        if (haveFiles && haveHeatFiles) {
            // Let's do this here - not in the bean
            logger.debug("Found files AND heatFiles - combine and add!");
            Map<String, Object> combinedFiles = new HashMap<>();
            for (Entry<String, Object> entry : files.entrySet()) {
                combinedFiles.put(entry.getKey(), entry.getValue());
            }
            for (Entry<String, Object> entry : heatFiles.entrySet()) {
                combinedFiles.put(entry.getKey(), entry.getValue());
            }
            stack.setFiles(combinedFiles);
        } else {
            // Handle if we only have one or neither:
            if (haveFiles) {
                logger.debug("Found files - adding to stack");
                stack.setFiles(files);
            }
            if (haveHeatFiles) {
                logger.debug("Found heatFiles - adding to stack");
                // the setFiles was modified to handle adding the entries
                stack.setFiles(heatFiles);
            }
        }

        // 1802 - attempt to add better formatted printout of request to openstack
        try {
            Map<String, Object> inputs = new HashMap<>();
            for (Entry<String, ?> entry : stackInputs.entrySet()) {
                if (entry.getValue() != null) {
                    inputs.put(entry.getKey(), entry.getValue());
                }
            }
            logger.debug("stack request: {}", stack.toString());
        } catch (Exception e) {
            // that's okay - this is a nice-to-have
            logger.debug("(had an issue printing nicely formatted request to debuglog) {}", e.getMessage());
        }

        return stack;
    }

}