73bf54c7519e1bbc715d29ff45f4bc69da832510
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / query / ChameleonResponseBuiler.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.datarouter.query;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.camel.Exchange;
30
31 import com.google.gson.JsonElement;
32 import com.google.gson.JsonObject;
33 import com.google.gson.JsonParser;
34
35 public class ChameleonResponseBuiler  {
36
37   public static void buildEntity(Exchange exchange, String ID){
38     String response = exchange.getIn().getBody().toString();
39     JsonParser parser = new JsonParser();
40     JsonObject root = parser.parse(response).getAsJsonObject();
41     JsonObject champResponse = new JsonObject();
42     if (!root.has("type")) {
43       exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
44       return ;
45     }
46     champResponse.addProperty("key", ID);
47     champResponse.addProperty("type", root.get("type").getAsString());
48     if (root.has("source")) {
49       champResponse.add("source", root.get("source"));
50     }
51     if (root.has("target")) {
52       champResponse.add("target", root.get("target"));
53     }
54
55     JsonObject props = new JsonObject();
56     List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(
57         root.getAsJsonObject().entrySet());
58     for (Map.Entry<String, JsonElement> e : entries) {
59       if (!e.getKey().equals("type") && !e.getKey().equals("source") && !e.getKey().equals("target")) {
60         if (e.getKey().equals("source")) {
61
62         }
63         props.addProperty(e.getKey(), e.getValue().getAsString());
64       }
65
66     }
67     
68     champResponse.add("properties", props);
69
70     exchange.getIn().setBody(champResponse.toString());
71     
72   }
73   
74  
75   public static void buildObjectRelationship(Exchange exchange, String ID){
76     //TODO: implement when chameleon supports this query     
77   }
78   public static void buildCollection(Exchange exchange){
79     //TODO: implement when chameleon supports this query   
80   }
81   
82  
83 }