Add bulk API to gizmo
[aai/gizmo.git] / src / main / java / org / openecomp / crud / service / BulkPayload.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
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 is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.openecomp.crud.service;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31
32 import org.openecomp.crud.exception.CrudException;
33
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38
39 import javax.ws.rs.core.Response.Status;
40
41 public class BulkPayload {
42         public enum OperationType {
43                 CREATE, UPDATE, DELETE
44         }
45
46           
47           
48         private List<JsonElement> objects = new ArrayList<JsonElement>();
49         private List<JsonElement> relationships = new ArrayList<JsonElement>();
50
51         private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
52
53         public String toJson() {
54                 return gson.toJson(this);
55         }
56
57         public static BulkPayload fromJson(String payload) throws CrudException {
58                 try {
59                         if (payload == null || payload.isEmpty()) {
60                                 throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST);
61                         }
62                         return gson.fromJson(payload, BulkPayload.class);
63                 } catch (Exception ex) {
64                         throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST);
65                 }
66         }
67
68         public List<JsonElement> getObjects() {
69                 return objects;
70         }
71
72         public void setObjects(List<JsonElement> objects) {
73                 this.objects = objects;
74         }
75
76         public List<JsonElement> getRelationships() {
77                 return relationships;
78         }
79
80         public void setRelationships(List<JsonElement> relationships) {
81                 this.relationships = relationships;
82         }
83
84         @Override
85         public String toString() {
86                 return "BulkPayload [objects=" + objects + ", relationships=" + relationships + "]";
87         }
88         
89         public static void main(String[] args)  throws Exception {
90                 BulkPayload p = new BulkPayload();
91                 JsonObject root = new JsonObject();
92                 JsonArray vertices = new JsonArray();
93                 JsonObject v1 = new JsonObject();
94                 JsonObject v2 = new JsonObject();
95                 JsonObject prop = new JsonObject();
96                 
97                 prop.addProperty("p1","value1");
98                 prop.addProperty("p2","value2");
99                 v1.add("v1", prop);
100                 v2.add("v2",prop);
101                 
102                 vertices.add(v1);
103                 vertices.add(v2);
104                 
105                 
106                 root.add("objects", vertices);
107                 
108                 String s = "{\"objects\":[{\"v1\":{\"p1\":\"value1\",\"p2\":\"value2\"}},{\"v2\":{\"p1\":\"value1\",\"p2\":\"value2\"}}]}";
109                 
110                 p = BulkPayload.fromJson(s);
111                 
112                 List<JsonElement> po = p.getObjects();
113                 List<String> ids = new ArrayList<String>();
114                 for (JsonElement e : po){
115                         Set<Map.Entry<String, JsonElement>> entries = e.getAsJsonObject().entrySet();
116
117                         for (Map.Entry<String, JsonElement> entry : entries) {
118                                 ids.add(entry.getKey());
119                         }
120                 }
121                 
122                 
123                 System.out.println("root: " + root.toString());
124                 System.out.println("payload ids: " + ids.toString());
125                 
126         }
127
128 }