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