Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / sync / entity / AggregationEntity.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
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.onap.aai.sparky.sync.entity;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.onap.aai.sparky.util.NodeUtils;
31
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import com.fasterxml.jackson.databind.node.ObjectNode;
34
35 /**
36  * The Class AggregationEntity.
37  */
38 public class AggregationEntity extends IndexableEntity implements IndexDocument {
39   private Map<String, String> attributes = new HashMap<String, String>();
40   protected ObjectMapper mapper = new ObjectMapper();
41   
42   /**
43    * Instantiates a new aggregation entity.
44    */
45   public AggregationEntity() {
46     super();
47   }
48
49   /* (non-Javadoc)
50    * @see org.openecomp.sparky.synchronizer.entity.IndexDocument#deriveFields()
51    */
52   @Override
53   public void deriveFields() {
54
55     /*
56      * We'll try and create a unique identity key that we can use for differencing the previously
57      * imported record sets as we won't have granular control of what is created/removed and when.
58      * The best we can hope for is identification of resources by generated Id until the
59      * Identity-Service UUID is tagged against all resources, then we can use that instead.
60      */
61     this.id =
62         NodeUtils.generateUniqueShaDigest(link);
63   }
64
65   public void copyAttributeKeyValuePair(Map<String, Object> map){
66     for(String key: map.keySet()){
67       if (!key.equalsIgnoreCase("relationship-list")){   // ignore relationship data which is not required in aggregation
68         this.attributes.put(key, map.get(key).toString());    // not sure if entity attribute can contain an object as value
69       }
70     }
71   }
72   
73   public void addAttributeKeyValuePair(String key, String value){
74     this.attributes.put(key, value);
75   }
76
77   @Override
78   public String getAsJson() {
79     ObjectNode rootNode = mapper.createObjectNode();
80     rootNode.put("link", this.getLink());
81     rootNode.put("lastmodTimestamp", this.getEntityTimeStamp());
82     for (String key: this.attributes.keySet()){
83       rootNode.put(key, this.attributes.get(key));
84     }
85     return rootNode.toString();
86   }
87
88   /* (non-Javadoc)
89    * @see java.lang.Object#toString()
90    */
91   @Override
92   public String toString() {
93     return "IndexDocument [" + (entityType != null ? "entityType=" + entityType + ", " : "")
94         + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
95             : "")
96         + (mapper != null ? "mapper=" + mapper + ", " : "") + (id != null ? "id=" + id + ", " : "")
97         + (lastmodTimestamp != null ? "lastmodTimestamp=" + lastmodTimestamp + ", " : "") + "]";
98   }
99 }