Performance Improvements for Gizmo bulk API
[aai/champ.git] / champ-service / src / main / java / org / onap / champ / entity / ChampBulkOp.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.champ.entity;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.stream.Collectors;
26
27 import javax.ws.rs.core.Response.Status;
28
29 import org.onap.aai.champcore.model.ChampObject;
30 import org.onap.aai.champcore.model.ChampRelationship;
31 import org.onap.champ.exception.ChampServiceException;
32 import org.onap.champ.util.ChampProperties;
33 import org.onap.champ.util.ChampServiceConstants;
34
35 import com.google.gson.Gson;
36 import com.google.gson.GsonBuilder;
37
38 public class ChampBulkOp {
39   private static final Gson gson = new GsonBuilder().create();
40
41   private static final String CREATED_TS_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_CREATED_TS_NAME);
42   private static final String LAST_MOD_TS_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_LAST_MOD_TS_NAME);
43
44
45   private String operation;
46   private String id;
47   private String type;
48   private String label;
49   private String source;
50   private String target;
51   private Map<String, Object> properties;
52
53
54   public String toJson() {
55     return gson.toJson(this);
56   }
57
58   public static ChampBulkOp fromJson(String jsonString) {
59     return gson.fromJson(jsonString, ChampBulkOp.class);
60   }
61
62   public ChampObject toChampObject() throws ChampServiceException {
63     if (type == null) {
64       throw new ChampServiceException("Error constructing object from: " + toJson(), Status.INTERNAL_SERVER_ERROR);
65     }
66
67     ChampObject.Builder builder = new ChampObject.Builder(type);
68
69     if (id != null) {
70       builder = builder.key(id);
71     }
72     if (properties != null) {
73
74       //remove the create/updated timestamps as it cause mismatch issue while updating from graph
75       Map<String, Object> champProperties = properties.entrySet().stream()
76               .filter(x -> !(x.getKey().equals(CREATED_TS_NAME) || x.getKey().equals(LAST_MOD_TS_NAME)))
77               .collect(Collectors.toMap(x -> x.getKey(), x -> x.getValue()));
78
79       builder = builder.properties(champProperties);
80     }
81
82     return builder.build();
83   }
84
85   public ChampRelationship toChampRelationship() throws ChampServiceException {
86     if ( (type == null) || (source == null) || (target == null) ) {
87       throw new ChampServiceException("Error constructing relationship from: " + toJson(), Status.INTERNAL_SERVER_ERROR);
88     }
89
90     ChampObject srcObj = new ChampObject.Builder("").key(source).build();
91     ChampObject targetObj = new ChampObject.Builder("").key(target).build();
92     ChampRelationship.Builder builder = new ChampRelationship.Builder(srcObj, targetObj, type);
93
94     if (id != null) {
95       builder = builder.key(id);
96     }
97     if (properties != null) {
98       builder = builder.properties(properties);
99     }
100
101     return builder.build();
102   }
103
104   public String getId() {
105     return id;
106   }
107
108   public void setId(String id) {
109     this.id = id;
110   }
111
112   public String getType() {
113     return type;
114   }
115
116   public void setType(String type) {
117     this.type = type;
118   }
119
120   public Map<String, Object> getProperties() {
121     return properties;
122   }
123
124   public Object getProperty(String key) {
125     return properties.get(key);
126   }
127
128   public void setProperties(Map<String, Object> properties) {
129     this.properties = properties;
130   }
131
132   public void setProperty(String key, String value) {
133     if (properties == null) {
134       properties = new HashMap<String,Object>();
135     }
136
137     properties.put(key, value);
138   }
139
140   public String getOperation() {
141     return operation;
142   }
143
144   public void setOperation(String operation) {
145     this.operation = operation;
146   }
147
148   public String getLabel() {
149     return label;
150   }
151
152   public void setLabel(String label) {
153     this.label = label;
154   }
155
156   public String getSource() {
157     return source;
158   }
159
160   public void setSource(String source) {
161     this.source = source;
162   }
163
164   public String getTarget() {
165     return target;
166   }
167
168   public void setTarget(String target) {
169     this.target = target;
170   }
171 }