Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / GenericArtifactBrowserBusinessLogic.java
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 org.onap.sdc.gab.GABService;
31 import org.onap.sdc.gab.GABServiceImpl;
32 import org.onap.sdc.gab.model.GABQuery;
33 import org.onap.sdc.gab.model.GABResult;
34 import org.onap.sdc.gab.model.GABResults;
35 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
37 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
38 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
39 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
40 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
41 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
42 import org.springframework.beans.factory.annotation.Autowired;
43
44 import java.io.IOException;
45 import java.lang.reflect.Type;
46
47 @org.springframework.stereotype.Component
48 public class GenericArtifactBrowserBusinessLogic extends BaseBusinessLogic {
49
50     private GABService gabService;
51
52     @Autowired
53     public GenericArtifactBrowserBusinessLogic(IElementOperation elementDao,
54         IGroupOperation groupOperation,
55         IGroupInstanceOperation groupInstanceOperation,
56         IGroupTypeOperation groupTypeOperation,
57         InterfaceOperation interfaceOperation,
58         InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
59         ArtifactsOperations artifactToscaOperation) {
60         super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
61             interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
62         gabService = new GABServiceImpl();
63     }
64
65     public String searchFor(GABQuery gabQuery) throws IOException {
66         GABResults gabResults = gabService.searchFor(gabQuery);
67         return createGsonForGABResult().toJson(gabResults);
68     }
69
70     private Gson createGsonForGABResult(){
71         return new GsonBuilder().setPrettyPrinting()
72             .registerTypeAdapter(GABResult.class, new GABResultSerializer())
73             .registerTypeAdapter(GABResults.class, new GABResultsSerializer())
74             .create();
75     }
76
77     private class GABResultsSerializer implements JsonSerializer<GABResults> {
78         @Override
79         public JsonElement serialize(GABResults gabResults, Type type,
80             JsonSerializationContext jsonSerializationContext) {
81             JsonObject result = new JsonObject();
82             JsonArray jsonArray = new JsonArray();
83             gabResults.getRows().stream().map(jsonSerializationContext::serialize).forEach(jsonArray::add);
84             result.add("data", jsonArray);
85             return result;
86         }
87     }
88
89     private class GABResultSerializer implements JsonSerializer<GABResult> {
90         @Override
91         public JsonElement serialize(GABResult gabResult, Type type, JsonSerializationContext jsonSerializationContext) {
92             JsonObject result = new JsonObject();
93             gabResult.getEntries().forEach(entry -> result.addProperty(entry.getPath(), String.valueOf(entry.getData())));
94             return result;
95         }
96     }
97
98 }