bf4077b9ea624a36822ecc033b35ec73a1db5525
[dcaegen2/services/prh.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2019 NOKIA 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dcaegen2.services.bootstrap;
22
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonPrimitive;
26
27 import java.util.Map;
28 import java.util.stream.Collectors;
29
30 class CbsJsonToPropertyMapConverter {
31
32     private static final String CBS_CONFIG_ROOT_PROPERTY = "config";
33
34     Map<String, Object> convertToMap(JsonObject jsonObject) {
35         verifyExpectedCbsJsonFormat(jsonObject);
36         JsonObject config = jsonObject.getAsJsonObject(CBS_CONFIG_ROOT_PROPERTY);
37         return config.entrySet().stream().collect(
38                 Collectors.toMap(Map.Entry::getKey, entry -> unpack(entry.getValue())));
39     }
40
41     private static void verifyExpectedCbsJsonFormat(JsonObject jsonObject) {
42         if (!jsonObject.has(CBS_CONFIG_ROOT_PROPERTY)) {
43             throw new IllegalArgumentException("Missing expected '" + CBS_CONFIG_ROOT_PROPERTY + "'" +
44                     " property in json from CBS.");
45         }
46     }
47
48     private Object unpack(JsonElement value) {
49         if (value.isJsonPrimitive()) {
50             JsonPrimitive primitiveValue = value.getAsJsonPrimitive();
51             if (primitiveValue.isString()) {
52                 return primitiveValue.getAsString();
53             }
54             if (primitiveValue.isBoolean()) {
55                 return primitiveValue.getAsBoolean();
56             }
57             if (primitiveValue.isNumber()) {
58                 return primitiveValue.getAsLong();
59             }
60         }
61         return value;
62     }
63
64 }