Configurable option to load models via Gizmo
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / gizmo / GizmoBulkPayload.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 European Software Marketing Ltd.
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.modelloader.gizmo;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonElement;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 public class GizmoBulkPayload {
31
32     public static final String ADD_OP = "add";
33     public static final String UPDATE_OP = "modify";
34     public static final String DELETE_OP = "delete";
35     public static final String PATCH_OP = "patch";
36     public static final String EXISTS_OP = "exists";
37     public static final String ALL_OPS = "all";
38     public static final String OP_KEY = "operation";
39
40
41     private List<JsonElement> objects = new ArrayList<JsonElement>();
42     private List<JsonElement> relationships = new ArrayList<JsonElement>();
43
44     private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
45
46     public String toJson() {
47         return gson.toJson(this);
48     }
49
50     public static GizmoBulkPayload fromJson(String payload) {
51         return gson.fromJson(payload, GizmoBulkPayload.class);
52     }
53
54     public List<JsonElement> getObjects() {
55         return objects;
56     }
57
58     public void setObjects(List<JsonElement> objects) {
59         this.objects = objects;
60     }
61
62     public List<JsonElement> getRelationships() {
63         return relationships;
64     }
65
66     public void setRelationships(List<JsonElement> relationships) {
67         this.relationships = relationships;
68     }
69
70     public List<GizmoVertexOperation> getVertexOperations() {
71         return getVertexOperations(ALL_OPS);
72     }
73     
74     public List<GizmoVertexOperation> getVertexOperations(String opType) {
75         List<GizmoVertexOperation> ops = new ArrayList<GizmoVertexOperation>();
76         for (JsonElement v : getObjects()) {
77             GizmoVertexOperation op = GizmoVertexOperation.fromJsonElement(v);
78                         
79             if ( (opType.equalsIgnoreCase(ALL_OPS)) || (op.getOperation().equalsIgnoreCase(opType)) ) {
80                 ops.add(op);
81             }
82         }
83         
84         return ops;
85     }
86     
87     public void addVertexOperation(GizmoVertexOperation newOp) {
88         objects.add(newOp.toJsonElement());
89     }
90     
91     public List<GizmoEdgeOperation> getEdgeOperations() {
92         return getEdgeOperations(ALL_OPS);
93     }
94     
95     public List<GizmoEdgeOperation> getEdgeOperations(String opType) {
96         List<GizmoEdgeOperation> ops = new ArrayList<GizmoEdgeOperation>();
97
98         for (JsonElement v : getRelationships()) {
99             GizmoEdgeOperation op = GizmoEdgeOperation.fromJsonElement(v);
100                         
101             if ( (opType.equalsIgnoreCase(ALL_OPS)) || (op.getOperation().equalsIgnoreCase(opType)) ) {
102                 ops.add(op);
103             }
104         }
105         
106         return ops;
107     }
108     
109     public void addEdgeOperation(GizmoEdgeOperation newOp) {
110         relationships.add(newOp.toJsonElement());
111     }
112 }