2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Copyright (C) 2021 Samsung Electronics.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.dcaegen2.services.pmmapper.utils;
24 import com.google.gson.Gson;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
27 import com.google.gson.TypeAdapter;
28 import com.google.gson.stream.JsonReader;
29 import com.google.gson.stream.JsonWriter;
30 import java.io.IOException;
31 import org.onap.dcaegen2.services.pmmapper.model.MeasFilterConfig;
32 import org.onap.logging.ref.slf4j.ONAPLogAdapter;
33 import org.slf4j.LoggerFactory;
35 public class MeasFilterConfigAdapter extends TypeAdapter<MeasFilterConfig> {
36 private static final ONAPLogAdapter logger = new ONAPLogAdapter(
37 LoggerFactory.getLogger(MeasFilterConfigAdapter.class));
40 public void write(JsonWriter jsonWriter, MeasFilterConfig o) throws IOException {
41 throw new UnsupportedOperationException();
45 public MeasFilterConfig read(JsonReader jsonReader) throws IOException {
46 JsonElement rootElement = JsonParser.parseReader(jsonReader);
47 if (rootElement.isJsonObject()) {
48 logger.unwrap().debug("Reading filter as an object.");
49 return new Gson().fromJson(rootElement, MeasFilterConfig.class);
50 } else if (rootElement.isJsonPrimitive()) {
51 logger.unwrap().debug("Reading filter as an object in a JSON string.");
52 return new Gson().fromJson(rootElement.getAsString(), MeasFilterConfig.class);
54 logger.unwrap().error("Filter does not appear to be formatted correctly.");
55 throw new UnsupportedOperationException("Expected an Object or Object as JSON String.");