2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
18 package org.onap.ccsdk.apps.controllerblueprints.resource.dict.data;
\r
20 import com.fasterxml.jackson.core.JsonParser;
\r
21 import com.fasterxml.jackson.core.JsonProcessingException;
\r
22 import com.fasterxml.jackson.databind.*;
\r
23 import com.fasterxml.jackson.databind.node.ObjectNode;
\r
24 import com.google.common.base.Preconditions;
\r
25 import org.apache.commons.lang3.StringUtils;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
\r
27 import org.slf4j.Logger;
\r
28 import org.slf4j.LoggerFactory;
\r
30 import java.io.IOException;
\r
31 import java.util.HashMap;
\r
32 import java.util.Map;
\r
34 public class SourceDeserializer extends JsonDeserializer<Map<String, ResourceSource>> {
\r
36 private static final Logger log = LoggerFactory.getLogger(SourceDeserializer.class);
\r
38 private Class<?> keyAs;
\r
40 private Class<?> contentAs;
\r
42 private static Map<String, Class<? extends ResourceSource>> registry = new HashMap<String, Class<? extends ResourceSource>>();
\r
44 public static void registerSource(String uniqueAttribute, Class<? extends ResourceSource> sourceClass) {
\r
45 registry.put(uniqueAttribute, sourceClass);
\r
49 public Map<String, ResourceSource> deserialize(JsonParser p, DeserializationContext ctxt)
\r
50 throws IOException, JsonProcessingException {
\r
52 ObjectMapper mapper = (ObjectMapper) p.getCodec();
\r
53 ObjectNode root = (ObjectNode) mapper.readTree(p);
\r
54 Map<String, ResourceSource> sources = new HashMap();
\r
55 root.fields().forEachRemaining((node) -> {
\r
56 String key = node.getKey();
\r
57 JsonNode valueNode = node.getValue();
\r
58 Preconditions.checkArgument(StringUtils.isNotBlank(key), "missing source key");
\r
59 Preconditions.checkArgument(registry.containsKey(key), key +" source not registered");
\r
60 if (StringUtils.isNotBlank(key) && registry.containsKey(key)) {
\r
61 Class<? extends ResourceSource> sourceClass = registry.get(key);
\r
62 ResourceSource resourceSource = JacksonUtils.readValue(valueNode.toString(), sourceClass);
\r
63 sources.put(key, resourceSource);
\r