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