2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.impl;
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;
44 import java.io.IOException;
45 import java.lang.reflect.Type;
47 @org.springframework.stereotype.Component
48 public class GenericArtifactBrowserBusinessLogic extends BaseBusinessLogic {
50 private GABService gabService;
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();
65 public String searchFor(GABQuery gabQuery) throws IOException {
66 GABResults gabResults = gabService.searchFor(gabQuery);
67 return createGsonForGABResult().toJson(gabResults);
70 private Gson createGsonForGABResult(){
71 return new GsonBuilder().setPrettyPrinting()
72 .registerTypeAdapter(GABResult.class, new GABResultSerializer())
73 .registerTypeAdapter(GABResults.class, new GABResultsSerializer())
77 private class GABResultsSerializer implements JsonSerializer<GABResults> {
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);
89 private class GABResultSerializer implements JsonSerializer<GABResult> {
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())));