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