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