cee77630cad09d23b04eea193517d911756d6386
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / entity / ChampObjectDeserializer.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ===================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.champ.entity;
23
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import org.onap.aai.champcore.model.ChampObject;
29
30 import com.fasterxml.jackson.core.JsonParser;
31 import com.fasterxml.jackson.core.JsonProcessingException;
32 import com.fasterxml.jackson.databind.DeserializationContext;
33 import com.fasterxml.jackson.databind.JsonNode;
34 import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
35
36 public class ChampObjectDeserializer extends StdDeserializer<ChampObject> {
37         
38         private static final long serialVersionUID = -3625275249560680339L;
39
40         public ChampObjectDeserializer() {
41                 this(null);
42         }
43         
44         protected ChampObjectDeserializer(Class<ChampObject> t) {
45                 super(t);
46         }
47         
48         public ChampObject deserialize(JsonParser jparser, DeserializationContext dctx)
49                         throws IOException, JsonProcessingException {
50                 
51                 JsonNode node = jparser.getCodec().readTree(jparser);
52                 JsonNode type = node.get("type");
53                 JsonNode key = node.get("key");
54                 Map<String, Object> props = new HashMap<>();
55                 JsonNode propNode = node.get("properties");
56                 propNode.fields().forEachRemaining((x)->props.put(x.getKey(), x.getValue().asText()));
57                 
58                 ChampObject.Builder builder = new ChampObject.Builder(type.asText()).properties(props);
59                 
60                 if(key != null){
61                         builder.key(key.asText());
62                 }
63                 
64                 return builder.build();
65         }
66
67 }