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