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