Adding interfaces in documentation
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / sync / entity / AggregationSuggestionEntity.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.sync.entity;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.json.JSONArray;
31 import org.json.JSONObject;
32 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
33 import org.onap.aai.sparky.search.filters.config.UiFilterListItemConfig;
34 import org.onap.aai.sparky.search.filters.config.UiViewListItemConfig;
35 import org.onap.aai.sparky.util.NodeUtils;
36
37 import com.fasterxml.jackson.annotation.JsonIgnore;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39
40 public class AggregationSuggestionEntity extends IndexableEntity implements IndexDocument {
41
42   private static final String FILTER_ID = "filterId";
43   private static final String FILTER_LIST = "filterList";
44   
45   private List<String> inputs = new ArrayList<>();
46   private final String outputString = "VNFs";
47   protected ObjectMapper mapper = new ObjectMapper();
48   List<String> filterIds = new ArrayList<>();
49   
50   @JsonIgnore
51   private FiltersConfig filtersConfig;
52   
53   public AggregationSuggestionEntity(FiltersConfig filtersConfig) {
54     super();
55     this.filtersConfig = filtersConfig;
56     inputs.add("VNFs");
57     inputs.add("generic-vnfs");
58   }
59
60   @Override
61   public void deriveFields() {
62     this.id = NodeUtils.generateUniqueShaDigest(this.outputString);
63   }
64
65   @Override
66   public String getAsJson() {
67     JSONArray inputArray = new JSONArray();
68     for (String input: inputs) {
69       input = input.replace(",","" );
70       input = input.replace("[","" );
71       input = input.replace("]","" );
72       inputArray.put(input);
73     }
74
75     JSONObject entitySuggest = new JSONObject();
76     entitySuggest.put("input", inputArray);
77     entitySuggest.put("output", this.outputString);
78     entitySuggest.put("weight", 100);
79
80     JSONArray payloadFilters = new JSONArray();
81
82     for (String filterId : filterIds) {
83       JSONObject filterPayload = new JSONObject();
84       filterPayload.put(FILTER_ID, filterId);
85       payloadFilters.put(filterPayload);
86     }
87
88     JSONObject payloadNode = new JSONObject();
89     payloadNode.put(FILTER_LIST, payloadFilters);
90     entitySuggest.put("payload", payloadNode);
91     
92     JSONObject rootNode = new JSONObject();
93     rootNode.put("entity_suggest", entitySuggest);
94
95     return rootNode.toString();
96   }
97
98   public void initializeFilters() {
99     for (UiViewListItemConfig view : filtersConfig.getViewsConfig().getViews()) {
100       if (view.getViewName().equals("vnfSearch")) {
101         for (UiFilterListItemConfig currentViewFilter : view.getFilters()) {
102           filterIds.add(currentViewFilter.getFilterId());
103         }
104       }
105     }
106   }
107
108   public void setFilterIds(List<String> filterIds) {
109     this.filterIds = filterIds;
110   }
111 }