56ef6fdc8b51ff5856292122614d98482e85878b
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / entity / AggregationEntity.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  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.datarouter.entity;
24
25 import java.io.Serializable;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 import org.openecomp.datarouter.util.NodeUtils;
33
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.fasterxml.jackson.databind.node.ObjectNode;
37
38 /**
39  * The Class AggregationEntity. Mimics functionality of AAIUI's AggregationEntity
40  */
41 public class AggregationEntity implements DocumentStoreDataEntity, Serializable {
42   private String id;
43   private String link;
44   private String lastmodTimestamp;
45   
46   public String getLink() {
47     return link;
48   }
49   public void setLink(String link) {
50     this.link = link;
51   }
52
53   @Override
54   public String getId() {
55     // make sure that deliveFields() is called before getting the id
56     return id;
57   }
58   public void setId(String id) {
59     this.id = id;
60   }
61
62
63   public String getLastmodTimestamp() {
64     return lastmodTimestamp;
65   }
66   public void setLastmodTimestamp(String lastmodTimestamp) {
67     this.lastmodTimestamp = lastmodTimestamp;
68   }
69
70
71   Map<String, String> attributes = new HashMap<>();
72   ObjectMapper mapper = new ObjectMapper();
73   
74   /**
75    * Instantiates a new aggregation entity.
76    */
77   public AggregationEntity() {  }
78
79   public void deriveFields(JsonNode uebPayload) {
80     
81     this.setId(NodeUtils.generateUniqueShaDigest(link));
82     
83     this.setLastmodTimestamp(Long.toString(System.currentTimeMillis()));
84     
85     JsonNode entityNode = uebPayload.get("entity");
86     
87     Iterator<Entry<String, JsonNode>> nodes = entityNode.fields();
88
89     while (nodes.hasNext()) {
90       Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodes.next();
91       if (!"relationship-list".equalsIgnoreCase(entry.getKey())){
92         attributes.put(entry.getKey(), entry.getValue().asText());
93       }
94     }
95   }
96   
97   public void copyAttributeKeyValuePair(Map<String, Object> map){
98     for(Map.Entry<String, Object> entry: map.entrySet()){
99       String key = entry.getKey();
100       Object value = entry.getValue();
101       if (!"relationship-list".equalsIgnoreCase(key)){   // ignore relationship data which is not required in aggregation
102         this.attributes.put(key, value.toString());    // not sure if entity attribute can contain an object as value
103       }
104     }
105   }
106  
107   public void addAttributeKeyValuePair(String key, String value){
108     this.attributes.put(key, value);
109   }
110
111   public String getAsJson() {
112     ObjectNode rootNode = mapper.createObjectNode();
113     rootNode.put("link", this.getLink());
114     rootNode.put("lastmodTimestamp", lastmodTimestamp);
115     for (Map.Entry<String, String> entry: this.attributes.entrySet()){
116       String key = entry.getKey();
117       String value = entry.getValue();
118       rootNode.put(key, value);
119     }
120     return rootNode.toString();
121   }
122   
123   @Override
124   public String toString() {
125     return "AggregationEntity [id=" + id + ", link=" + link + ", attributes=" + attributes
126         + ", mapper=" + mapper + "]";
127   }
128 }