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