cb620d855a773180b4125542fab899456d0b936a
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / entity / SpikeAggregationEntity.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.HashMap;
27 import java.util.Iterator;
28 import java.util.Map;
29 import java.util.Map.Entry;
30
31 import org.onap.aai.datarouter.util.NodeUtils;
32
33 import com.fasterxml.jackson.databind.JsonNode;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35 import com.fasterxml.jackson.databind.node.ObjectNode;
36
37 /**
38  * The Class SpikeAggregationEntity. Mimics functionality of SPIKEUI's AggregationEntity
39  */
40 public class SpikeAggregationEntity implements DocumentStoreDataEntity, Serializable {
41   private String id;
42   private String link;
43   private String lastmodTimestamp;
44
45   public String getLink() {
46     return link;
47   }
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
59   public void setId(String id) {
60     this.id = id;
61   }
62
63
64   public String getLastmodTimestamp() {
65     return lastmodTimestamp;
66   }
67
68   public void setLastmodTimestamp(String lastmodTimestamp) {
69     this.lastmodTimestamp = lastmodTimestamp;
70   }
71
72
73   Map<String, String> attributes = new HashMap<>();
74   ObjectMapper mapper = new ObjectMapper();
75
76   /**
77    * Instantiates a new aggregation entity.
78    */
79   public SpikeAggregationEntity() {}
80
81   public void deriveFields(JsonNode uebPayload) {
82
83     this.setId(NodeUtils.generateUniqueShaDigest(link));
84     this.setLastmodTimestamp(Long.toString(System.currentTimeMillis()));
85     JsonNode entityNode = uebPayload.get("vertex").get("properties");
86     Iterator<Entry<String, JsonNode>> nodes = entityNode.fields();
87     while (nodes.hasNext()) {
88       Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodes.next();
89       attributes.put(entry.getKey(), entry.getValue().asText());
90     }
91   }
92
93  
94
95   @Override
96   public String getAsJson() {
97     ObjectNode rootNode = mapper.createObjectNode();
98     rootNode.put("link", this.getLink());
99     rootNode.put("lastmodTimestamp", lastmodTimestamp);
100     for (Map.Entry<String, String> entry : this.attributes.entrySet()) {
101       String key = entry.getKey();
102       String value = entry.getValue();
103       rootNode.put(key, value);
104     }
105     return rootNode.toString();
106   }
107
108   @Override
109   public String toString() {
110     return "AggregationEntity [id=" + id + ", link=" + link + ", attributes=" + attributes
111         + ", mapper=" + mapper + "]";
112   }
113 }