Fix Sonar code smells
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / gizmo / GizmoVertexOperation.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.JsonElement;
24 import com.google.gson.JsonObject;
25 import com.google.gson.JsonParser;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29
30 public class GizmoVertexOperation {
31     private String operation;
32     private String internalId;
33     private GizmoVertex vertex;
34
35     public GizmoVertexOperation(String op, String id, GizmoVertex vertex) {
36         this.operation = op;
37         this.internalId = id;
38         this.vertex = vertex;
39     }
40
41     public JsonElement toJsonElement() {
42         JsonObject opObj = new JsonObject();
43         JsonParser parser = new JsonParser();
44         JsonObject vertexObj = parser.parse(vertex.toJson()).getAsJsonObject();
45
46         opObj.addProperty(GizmoBulkPayload.OP_KEY, getOperation());
47         opObj.add(internalId, vertexObj);
48
49         return opObj;
50     }
51
52     public static GizmoVertexOperation fromJsonElement(JsonElement element) {
53         List<Map.Entry<String, JsonElement>> entries = new ArrayList<>(element.getAsJsonObject().entrySet());
54
55         String op = null;
56         String id = null;
57         GizmoVertex vertex = null;
58
59         for (Map.Entry<String, JsonElement> entry : entries) {
60             if (entry.getKey().equalsIgnoreCase(GizmoBulkPayload.OP_KEY)) {
61                 op = entry.getValue().getAsString();
62             } else {
63                 id = entry.getKey();
64                 vertex = GizmoVertex.fromJson(entry.getValue().getAsJsonObject().toString());
65             }
66         }
67
68         if (op == null) {
69             // Use default
70             op = GizmoBulkPayload.EXISTS_OP;
71         }
72
73         return new GizmoVertexOperation(op, id, vertex);
74     }
75
76     public String getOperation() {
77         return operation;
78     }
79
80     public void setOperation(String op) {
81         operation = op;
82     }
83
84     public String getInternalId() {
85         return internalId;
86     }
87
88     public GizmoVertex getVertex() {
89         return vertex;
90     }
91 }