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