ebb5b296d1f153cd089d2f95587dc80e65021a18
[ccsdk/apps.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 package org.onap.ccsdk.apps.ms.vlantagapi.extinf.pm;\r
17 \r
18 import java.io.IOException;\r
19 import java.util.HashMap;\r
20 import java.util.Iterator;\r
21 import java.util.Map;\r
22 import java.util.Map.Entry;\r
23 \r
24 import org.onap.ccsdk.apps.ms.vlantagapi.core.extinf.pm.model.PolicyData;\r
25 import org.slf4j.Logger;\r
26 import org.slf4j.LoggerFactory;\r
27 \r
28 import com.fasterxml.jackson.core.JsonParser;\r
29 import com.fasterxml.jackson.databind.DeserializationContext;\r
30 import com.fasterxml.jackson.databind.JsonNode;\r
31 import com.fasterxml.jackson.databind.ObjectMapper;\r
32 import com.fasterxml.jackson.databind.deser.std.StdDeserializer;\r
33 import com.fasterxml.jackson.databind.node.ObjectNode;\r
34 \r
35 /**\r
36  * PolicyDataDeserializer.java Purpose:\r
37  * This is a custom deserializer for "policy-data" element of the response from the Policy management REST call\r
38  * to get Policy details based on a policy name.\r
39  * \r
40  * The response will have different "child tree"s ( policy data)  for different policy names.\r
41  * Each type of child is structurally, syntactically, semantically unique.\r
42  * However, there is no explicit way to identify the right child type in the "policy-data" element itself\r
43  * Hence , the hack is  : assuming there is a unique property in each type of child of Policy-data,\r
44  * look for that property to determine the correct target child type to deserialize to.\r
45  * These unique properties are stored in polymorphicsRegistry and looked up against to find a match\r
46  * If a match is found, use that child type is used.\r
47  */\r
48 public class PolicyDataDeserializer extends StdDeserializer<PolicyData> {\r
49 \r
50         /**\r
51          * \r
52          */\r
53         private static final long serialVersionUID = 1L;\r
54 \r
55         private static Logger log = LoggerFactory.getLogger(PolicyDataDeserializer.class);\r
56 \r
57         /*\r
58          * Keep a register of all extensions of PolicyData type by a unique attribute that distringuishes\r
59          * them from one another\r
60          */\r
61         private Map<String, Class<? extends PolicyData>> polymorphicsRegistry =\r
62               new HashMap<>();\r
63 \r
64         public PolicyDataDeserializer() {\r
65             super(PolicyData.class);\r
66         }\r
67 \r
68         /*\r
69          * Method to add to the registry of PolicyData extensions by their distinguishing attribure\r
70          */\r
71         public void registerExtendersByUniqueness(String uniqueAttribute, Class<? extends PolicyData> policyDataClass) {\r
72             polymorphicsRegistry.put(uniqueAttribute, policyDataClass);\r
73         }\r
74 \r
75         @Override\r
76         public PolicyData deserialize(JsonParser jp, DeserializationContext ctxt) \r
77               throws IOException {\r
78                 \r
79                 PolicyData retVal = null;\r
80             Class<? extends PolicyData> policyDataClass = null;\r
81             \r
82             ObjectMapper mapper = (ObjectMapper) jp.getCodec();\r
83             ObjectNode root = mapper.readTree(jp);\r
84 \r
85             //Go through all the elements in this node. If a match is found in the registry of uniqueness,\r
86             // it means we identified what extension of PolicyData we are dealing with. If no match found, return null\r
87             Iterator<Entry<String, JsonNode>> elementsIterator = root.fields();\r
88             while (elementsIterator.hasNext())\r
89             {\r
90               Entry<String, JsonNode> element=elementsIterator.next();\r
91               String name = element.getKey();\r
92               \r
93               if (polymorphicsRegistry.containsKey(name))\r
94               {\r
95                 policyDataClass = polymorphicsRegistry.get(name);\r
96                 break;\r
97               }\r
98             }\r
99             \r
100             //If we know what child type of PolicyData we are working on , use that class to deserialize this node\r
101             // from its root. If not, throw exception\r
102             if (policyDataClass != null) {\r
103                 retVal = mapper.treeToValue(root,  policyDataClass);\r
104             }\r
105             else {\r
106                 log.info("No matching Child of PolicyData is present in this node tree");\r
107             }\r
108             return retVal;\r
109         }\r
110 }\r
111 \r