5f98e0b622da5d1b3d68bc77b263fd1e5897f91a
[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.config.oxm.OxmModelLoader;
36 import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig;
37 import org.onap.aai.sparky.dal.exception.ElasticSearchOperationException;
38 import org.onap.aai.sparky.dal.rest.RestDataProvider;
39 import org.onap.aai.sparky.synchronizer.config.TaskProcessorConfig;
40
41 /**
42  * The Class ElasticEntitySummarizer.
43  */
44 public class ElasticEntitySummarizer {
45
46   private RestDataProvider syncAdapter;
47
48   /**
49    * @return the syncAdapter
50    */
51   public RestDataProvider getSyncAdapter() {
52     return syncAdapter;
53   }
54
55   /**
56    * @param syncAdapter the syncAdapter to set
57    */
58   public void setSyncAdapter(RestDataProvider syncAdapter) {
59     this.syncAdapter = syncAdapter;
60   }
61
62   /**
63    * @return the elasticConfig
64    */
65   public ElasticSearchConfig getElasticConfig() {
66     return elasticConfig;
67   }
68
69   /**
70    * @param elasticConfig the elasticConfig to set
71    */
72   public void setElasticConfig(ElasticSearchConfig elasticConfig) {
73     this.elasticConfig = elasticConfig;
74   }
75
76   /**
77    * @return the entityCounters
78    */
79   public Map<String, AtomicInteger> getEntityCounters() {
80     return entityCounters;
81   }
82
83   /**
84    * @param entityCounters the entityCounters to set
85    */
86   public void setEntityCounters(Map<String, AtomicInteger> entityCounters) {
87     this.entityCounters = entityCounters;
88   }
89
90
91   private ElasticSearchConfig elasticConfig;
92   private Map<String, AtomicInteger> entityCounters;
93
94   /**
95    * Instantiates a new elastic entity summarizer.
96    *
97    * @param loader the loader
98    * @throws Exception the exception
99    */
100   public ElasticEntitySummarizer(OxmModelLoader loader) throws Exception {
101
102
103     elasticConfig = new ElasticSearchConfig();
104     TaskProcessorConfig tpc = new TaskProcessorConfig();
105     elasticConfig.setProcessorConfig(tpc);
106
107     elasticConfig.setIndexName("entitysearchindex-localhost");
108     elasticConfig.setIpAddress("127.0.0.1");
109     elasticConfig.setHttpPort("9200");
110     elasticConfig.setType("default");
111
112     // syncAdapter = new SyncAdapter(new RestClientBuilder(), elasticConfig, loader);
113
114     entityCounters = new HashMap<String, AtomicInteger>();
115
116   }
117
118   /**
119    * Peg counter.
120    *
121    * @param entityName the entity name
122    */
123   private synchronized void pegCounter(String entityName) {
124
125     if (entityName == null || entityName.length() == 0) {
126       return;
127     }
128
129     AtomicInteger counter = entityCounters.get(entityName);
130
131     if (counter == null) {
132       counter = new AtomicInteger(0);
133       entityCounters.put(entityName, counter);
134     }
135
136     counter.incrementAndGet();
137
138   }
139
140
141   /**
142    * Enumerate entities.
143    */
144   public void enumerateEntities() {
145
146     try {
147
148       Map<String, String> preSyncObjectIdsAndTypes = new HashMap<String, String>();
149
150       /*
151        * Map<String, String> preSyncObjectIdsAndTypes =
152        * syncAdapter.retrieveAllDocumentIdentifiers(elasticConfig.getIndexName(),
153        * elasticConfig.getType(), 5, 5000);
154        */
155
156       if (preSyncObjectIdsAndTypes != null) {
157
158         Collection<String> entityTypes = preSyncObjectIdsAndTypes.values();
159         for (String t : entityTypes) {
160           pegCounter(t);
161         }
162       }
163
164       TreeMap<String, AtomicInteger> elasticEntitySortedTreeMap =
165           new TreeMap<String, AtomicInteger>(new Comparator<String>() {
166
167             @Override
168             public int compare(String o1, String o2) {
169               return o1.toLowerCase().compareTo(o2.toLowerCase());
170             }
171           });
172
173       elasticEntitySortedTreeMap.putAll(entityCounters);
174
175       int totalEntities = 0;
176
177       System.out.println("\n");
178
179       for (String counterEntityKey : elasticEntitySortedTreeMap.keySet()) {
180
181         AtomicInteger counter = elasticEntitySortedTreeMap.get(counterEntityKey);
182         totalEntities += counter.get();
183         System.out.println(String.format("%-30s %-12d", counterEntityKey, counter.get()));
184       }
185
186       System.out.println(String.format("\n%-30s %-12d", "Total", totalEntities));
187
188     } catch (Exception exc) {
189       System.out.println("An error occurred while attempting to collect pre-sync elastic"
190           + " search document ids with an error cause = " + exc.getLocalizedMessage());
191     }
192
193
194   }
195 }