367d8b244fec903cfd7811146a450ab41711ea0b
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-lcm / etsi-sol003-lcm-adapter / src / main / java / org / onap / so / adapters / etsisol003adapter / lcm / NvfmAdapterUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.etsisol003adapter.lcm;
22
23 import static org.slf4j.LoggerFactory.getLogger;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import org.slf4j.Logger;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonObject;
29
30 public final class NvfmAdapterUtils {
31     private static Logger logger = getLogger(NvfmAdapterUtils.class);
32
33     private NvfmAdapterUtils() {
34         throw new IllegalStateException("Utility class");
35     }
36
37     public static JsonObject child(final JsonObject parent, final String name) {
38         return childElement(parent, name).getAsJsonObject();
39     }
40
41     public static JsonElement childElement(final JsonObject parent, final String name) {
42         final JsonElement child = parent.get(name);
43         if (child == null) {
44             throw abortOperation("Missing child " + name);
45         }
46         return child;
47     }
48
49     public static Collection<JsonObject> children(final JsonObject parent) {
50         final ArrayList<JsonObject> childElements = new ArrayList<>();
51         for (final String childKey : parent.keySet()) {
52             if (parent.get(childKey).isJsonObject()) {
53                 childElements.add(parent.get(childKey).getAsJsonObject());
54             }
55         }
56         return childElements;
57     }
58
59     public static RuntimeException abortOperation(final String msg, final Exception e) {
60         logger.error(msg, e);
61         return new RuntimeException(msg, e);
62     }
63
64     public static RuntimeException abortOperation(final String msg) {
65         logger.error(msg);
66         return new RuntimeException(msg);
67     }
68 }