Fix Spike Event Processing with Common Format
[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-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.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Map.Entry;
28
29 import org.onap.aai.datarouter.util.NodeUtils;
30
31 import com.fasterxml.jackson.annotation.JsonIgnore;
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 SpikeAggregationEntity. Mimics functionality of SPIKEUI's AggregationEntity
38  */
39 public class SpikeAggregationEntity implements DocumentStoreDataEntity, Serializable {
40   
41   private Map<String, String> attributes = new HashMap<>();
42   
43   @JsonIgnore
44   private ObjectMapper mapper = new ObjectMapper();
45   
46   
47   private String id;
48   private String link;
49   private String lastmodTimestamp;
50
51   public String getLink() {
52     return link;
53   }
54
55   public void setLink(String link) {
56     this.link = link;
57   }
58
59   @Override
60   public String getId() {
61     // make sure that deliveFields() is called before getting the id
62     return id;
63   }
64
65   public void setId(String id) {
66     this.id = id;
67   }
68
69
70   public String getLastmodTimestamp() {
71     return lastmodTimestamp;
72   }
73
74   public void setLastmodTimestamp(String lastmodTimestamp) {
75     this.lastmodTimestamp = lastmodTimestamp;
76   }
77
78
79
80   /**
81    * Instantiates a new aggregation entity.
82    */
83   public SpikeAggregationEntity() {}
84
85   public void deriveFields(JsonNode entityProperties) {
86
87     this.setId(NodeUtils.generateUniqueShaDigest(link));
88     this.setLastmodTimestamp(Long.toString(System.currentTimeMillis()));
89     Iterator<Entry<String, JsonNode>> nodes = entityProperties.fields();
90     while (nodes.hasNext()) {
91       Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodes.next();
92       attributes.put(entry.getKey(), entry.getValue().asText());
93     }
94   }
95
96   @Override
97   public String getAsJson() {
98     ObjectNode rootNode = mapper.createObjectNode();
99     rootNode.put("link", this.getLink());
100     rootNode.put("lastmodTimestamp", lastmodTimestamp);
101     for (Map.Entry<String, String> entry : this.attributes.entrySet()) {
102       String key = entry.getKey();
103       String value = entry.getValue();
104       rootNode.put(key, value);
105     }
106     return rootNode.toString();
107   }
108
109   @Override
110   public String toString() {
111     return "SpikeAggregationEntity ["
112         + (attributes != null ? "attributes=" + attributes + ", " : "")
113         + (id != null ? "id=" + id + ", " : "") + (link != null ? "link=" + link + ", " : "")
114         + (lastmodTimestamp != null ? "lastmodTimestamp=" + lastmodTimestamp : "") + "]";
115   }
116   
117   
118
119 }