3bd715e47b24a8027237ae732461c005e92510b7
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / util / CbamUtils.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util;
18
19 import com.google.gson.JsonElement;
20 import com.google.gson.JsonObject;
21 import org.slf4j.Logger;
22
23 /**
24  * Collection of utility functions
25  */
26 public class CbamUtils {
27
28     /**
29      * Separator for multiple keys concatenated into a single string
30      */
31     public static final String SEPARATOR = "_";
32
33     private CbamUtils() {
34         //use static way
35     }
36
37     /**
38      * @param parent the parent JSON object
39      * @param name   the name of the child
40      * @return the child JSON object of parent with given name
41      */
42     public static JsonObject child(JsonObject parent, String name) {
43         return childElement(parent, name).getAsJsonObject();
44     }
45
46     /**
47      * @param parent the parent JSON object
48      * @param name   the name of the child
49      * @return the child JSON object of parent with given name
50      */
51     public static JsonElement childElement(JsonObject parent, String name) {
52         JsonElement child = parent.get(name);
53         if (child == null) {
54             throw new OperationMustBeAborted("Missing child " + name);
55         }
56         return child;
57     }
58
59     /**
60      * Logs and throws a runtime exception
61      *
62      * @param logger the logger
63      * @param msg    the error message
64      * @param e      the exception to be wrapped
65      * @return never reached (runtime exception is thrown)
66      */
67     public static RuntimeException fatalFailure(Logger logger, String msg, Exception e) {
68         logger.error(msg, e);
69         throw new OperationMustBeAborted(e, msg);
70     }
71
72     /**
73      * Logs and throws a runtime exception
74      *
75      * @param logger the logger
76      * @param msg    the error message
77      * @return never reached (runtime exception is thrown)
78      */
79     public static RuntimeException fatalFailure(Logger logger, String msg) {
80         logger.error(msg);
81         throw new OperationMustBeAborted(msg);
82     }
83
84     private static class OperationMustBeAborted extends RuntimeException {
85         OperationMustBeAborted(String msg) {
86             super(msg);
87         }
88
89         OperationMustBeAborted(Exception e, String msg) {
90             super(msg, e);
91         }
92     }
93 }