d95045b87b37e0339ea0e80633f36fc5291a84fe
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonSerializationContext;
29 import com.google.gson.JsonSerializer;
30 import java.io.IOException;
31 import java.lang.reflect.Type;
32 import org.onap.sdc.gab.GABService;
33 import org.onap.sdc.gab.GABServiceImpl;
34 import org.onap.sdc.gab.model.GABQuery;
35 import org.onap.sdc.gab.model.GABResult;
36 import org.onap.sdc.gab.model.GABResults;
37 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
38 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
39 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
40 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
41 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
42 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
43 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
44 import org.springframework.beans.factory.annotation.Autowired;
45
46 @org.springframework.stereotype.Component
47 public class GenericArtifactBrowserBusinessLogic extends BaseBusinessLogic {
48
49     private GABService gabService;
50
51     @Autowired
52     public GenericArtifactBrowserBusinessLogic(IElementOperation elementDao,
53         IGroupOperation groupOperation,
54         IGroupInstanceOperation groupInstanceOperation,
55         IGroupTypeOperation groupTypeOperation,
56         InterfaceOperation interfaceOperation,
57         InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
58         ArtifactsOperations artifactToscaOperation) {
59         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
60             interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
61         gabService = new GABServiceImpl();
62     }
63
64     public String searchFor(GABQuery gabQuery) throws IOException {
65         GABResults gabResults = gabService.searchFor(gabQuery);
66         return createGsonForGABResult().toJson(gabResults);
67     }
68
69     private Gson createGsonForGABResult(){
70         return new GsonBuilder().setPrettyPrinting()
71             .registerTypeAdapter(GABResult.class, new GABResultSerializer())
72             .registerTypeAdapter(GABResults.class, new GABResultsSerializer())
73             .create();
74     }
75
76     private class GABResultsSerializer implements JsonSerializer<GABResults> {
77         @Override
78         public JsonElement serialize(GABResults gabResults, Type type,
79             JsonSerializationContext jsonSerializationContext) {
80             JsonObject result = new JsonObject();
81             JsonArray jsonArray = new JsonArray();
82             gabResults.getRows().stream().map(jsonSerializationContext::serialize).forEach(jsonArray::add);
83             result.add("data", jsonArray);
84             return result;
85         }
86     }
87
88     private class GABResultSerializer implements JsonSerializer<GABResult> {
89         @Override
90         public JsonElement serialize(GABResult gabResult, Type type, JsonSerializationContext jsonSerializationContext) {
91             JsonObject result = new JsonObject();
92             gabResult.getEntries().forEach(entry -> result.addProperty(entry.getPath(), String.valueOf(entry.getData())));
93             return result;
94         }
95     }
96
97 }