Update the dependencies to use project version
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / sync / entity / AggregationSuggestionEntity.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.sparky.sync.entity;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
31 import org.onap.aai.sparky.search.filters.config.UiFilterListItemConfig;
32 import org.onap.aai.sparky.search.filters.config.UiViewListItemConfig;
33 import org.onap.aai.sparky.util.NodeUtils;
34
35 import com.fasterxml.jackson.databind.ObjectMapper;
36
37 public class AggregationSuggestionEntity extends IndexableEntity implements IndexDocument {
38
39   private static final String FILTER_ID = "filterId";
40   private static final String FILTER_LIST = "filterList";
41
42   private List<String> inputs = new ArrayList<>();
43   private final String outputString = "VNFs";
44   protected ObjectMapper mapper = new ObjectMapper();
45   List<String> filterIds = new ArrayList<>();
46
47   public AggregationSuggestionEntity() {
48     super();
49     inputs.add("VNFs");
50     inputs.add("generic-vnfs");
51   }
52
53   @Override
54   public void deriveFields() {
55     this.id = NodeUtils.generateUniqueShaDigest(this.outputString);
56   }
57
58   @Override
59   public String getAsJson() {
60     JSONArray inputArray = new JSONArray();
61     for (String input : inputs) {
62       input = input.replace(",", "");
63       input = input.replace("[", "");
64       input = input.replace("]", "");
65       inputArray.put(input);
66     }
67
68     JSONObject entitySuggest = new JSONObject();
69     entitySuggest.put("input", inputArray);
70     entitySuggest.put("output", this.outputString);
71     entitySuggest.put("weight", 100);
72
73     JSONArray payloadFilters = new JSONArray();
74
75     for (String filterId : filterIds) {
76       JSONObject filterPayload = new JSONObject();
77       filterPayload.put(FILTER_ID, filterId);
78       payloadFilters.put(filterPayload);
79     }
80
81     JSONObject payloadNode = new JSONObject();
82     payloadNode.put(FILTER_LIST, payloadFilters);
83     entitySuggest.put("payload", payloadNode);
84
85     JSONObject rootNode = new JSONObject();
86     rootNode.put("entity_suggest", entitySuggest);
87
88     return rootNode.toString();
89   }
90
91   public void initializeFilters() {
92     for (UiViewListItemConfig view : FiltersConfig.getInstance().getViewsConfig().getViews()) {
93       if (view.getViewName().equals("vnfSearch")) {
94         for (UiFilterListItemConfig currentViewFilter : view.getFilters()) {
95           filterIds.add(currentViewFilter.getFilterId());
96         }
97       }
98     }
99   }
100
101   public void setFilterIds(List<String> filterIds) {
102     this.filterIds = filterIds;
103   }
104 }