Initial ONAP Synapse commit
[aai/data-router.git] / src / main / java / org / openecomp / datarouter / entity / AggregationEntity.java
1 /**
2  * ============LICENSE_START=======================================================
3  * DataRouter
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.datarouter.entity;
26
27 import java.io.Serializable;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32 import java.util.Map.Entry;
33
34 import org.openecomp.datarouter.util.NodeUtils;
35
36 import com.fasterxml.jackson.databind.JsonNode;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import com.fasterxml.jackson.databind.node.ObjectNode;
39
40 /**
41  * The Class AggregationEntity. Mimics functionality of AAIUI's AggregationEntity
42  */
43 public class AggregationEntity implements DocumentStoreDataEntity, Serializable {
44   private String id;
45   private String link;
46   private String lastmodTimestamp;
47   
48   public String getLink() {
49     return link;
50   }
51   public void setLink(String link) {
52     this.link = link;
53   }
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<String, String>();
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 (!entry.getKey().equalsIgnoreCase("relationship-list")){
92         attributes.put(entry.getKey(), entry.getValue().asText());
93       }
94     }
95   }
96   
97   public void copyAttributeKeyValuePair(Map<String, Object> map){
98     for(String key: map.keySet()){
99       if (!key.equalsIgnoreCase("relationship-list")){   // ignore relationship data which is not required in aggregation
100         this.attributes.put(key, map.get(key).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
109   public String getAsJson() {
110     ObjectNode rootNode = mapper.createObjectNode();
111     rootNode.put("link", this.getLink());
112     rootNode.put("lastmodTimestamp", lastmodTimestamp);
113     for (String key: this.attributes.keySet()){
114       rootNode.put(key, this.attributes.get(key));
115     }
116     return rootNode.toString();
117   }
118   
119   @Override
120   public String toString() {
121     return "AggregationEntity [id=" + id + ", link=" + link + ", attributes=" + attributes
122         + ", mapper=" + mapper + "]";
123   }
124 }