Adding UI extensibility
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / util / ElasticEntitySummarizer.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
26 package org.onap.aai.sparky.util;
27
28 import java.util.Collection;
29 import java.util.Comparator;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.TreeMap;
33 import java.util.concurrent.atomic.AtomicInteger;
34
35 import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig;
36 import org.onap.aai.sparky.dal.exception.ElasticSearchOperationException;
37 import org.onap.aai.sparky.dal.rest.RestDataProvider;
38
39 /**
40  * The Class ElasticEntitySummarizer.
41  */
42 public class ElasticEntitySummarizer {
43
44   private RestDataProvider syncAdapter;
45   private ElasticSearchConfig elasticConfig;
46   private Map<String, AtomicInteger> entityCounters;
47
48   /**
49    * Instantiates a new elastic entity summarizer.
50    *
51    * @param loader the loader
52    * @throws Exception the exception
53    */
54   public ElasticEntitySummarizer() throws Exception {
55
56
57     elasticConfig = new ElasticSearchConfig();
58
59     elasticConfig.setIndexName("entitysearchindex-localhost");
60     elasticConfig.setIpAddress("127.0.0.1");
61     elasticConfig.setHttpPort("9200");
62     elasticConfig.setType("default");
63
64     // syncAdapter = new SyncAdapter(new RestClientBuilder(), elasticConfig, loader);
65
66     entityCounters = new HashMap<String, AtomicInteger>();
67
68   }
69
70   /**
71    * Peg counter.
72    *
73    * @param entityName the entity name
74    */
75   private synchronized void pegCounter(String entityName) {
76
77     if (entityName == null || entityName.length() == 0) {
78       return;
79     }
80
81     AtomicInteger counter = entityCounters.get(entityName);
82
83     if (counter == null) {
84       counter = new AtomicInteger(0);
85       entityCounters.put(entityName, counter);
86     }
87
88     counter.incrementAndGet();
89
90   }
91
92
93   /**
94    * Enumerate entities.
95    */
96   public void enumerateEntities() {
97
98     try {
99
100       Map<String, String> preSyncObjectIdsAndTypes = new HashMap<String, String>();
101
102       /*
103        * Map<String, String> preSyncObjectIdsAndTypes =
104        * syncAdapter.retrieveAllDocumentIdentifiers(elasticConfig.getIndexName(),
105        * elasticConfig.getType(), 5, 5000);
106        */
107
108       if (preSyncObjectIdsAndTypes != null) {
109
110         Collection<String> entityTypes = preSyncObjectIdsAndTypes.values();
111         for (String t : entityTypes) {
112           pegCounter(t);
113         }
114       }
115
116       TreeMap<String, AtomicInteger> elasticEntitySortedTreeMap =
117           new TreeMap<String, AtomicInteger>(new Comparator<String>() {
118
119             @Override
120             public int compare(String o1, String o2) {
121               return o1.toLowerCase().compareTo(o2.toLowerCase());
122             }
123           });
124
125       elasticEntitySortedTreeMap.putAll(entityCounters);
126
127       int totalEntities = 0;
128
129       System.out.println("\n");
130
131       for (String counterEntityKey : elasticEntitySortedTreeMap.keySet()) {
132
133         AtomicInteger counter = elasticEntitySortedTreeMap.get(counterEntityKey);
134         totalEntities += counter.get();
135         System.out.println(String.format("%-30s %-12d", counterEntityKey, counter.get()));
136       }
137
138       System.out.println(String.format("\n%-30s %-12d", "Total", totalEntities));
139
140     } catch (Exception exc) {
141       System.out.println("An error occurred while attempting to collect pre-sync elastic"
142           + " search document ids with an error cause = " + exc.getLocalizedMessage());
143     }
144
145
146   }
147
148
149   /**
150    * The main method.
151    *
152    * @param args the arguments
153    * @throws ElasticSearchOperationException the elastic search operation exception
154    */
155   public static void main(String[] args) throws ElasticSearchOperationException {
156
157
158     // ElasticEntitySummarizer summarizer = new ElasticEntitySummarizer();
159     // summarizer.enumerateEntities();
160
161
162
163   }
164
165
166
167 }