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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dcaegen2.services.bootstrap;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonPrimitive;
28 import java.util.stream.Collectors;
30 class CbsJsonToPropertyMapConverter {
32 private static final String CBS_CONFIG_ROOT_PROPERTY = "config";
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())));
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.");
48 private Object unpack(JsonElement value) {
49 if (value.isJsonPrimitive()) {
50 JsonPrimitive primitiveValue = value.getAsJsonPrimitive();
51 if (primitiveValue.isString()) {
52 return primitiveValue.getAsString();
54 if (primitiveValue.isBoolean()) {
55 return primitiveValue.getAsBoolean();
57 if (primitiveValue.isNumber()) {
58 return primitiveValue.getAsLong();