Renaming openecomp to onap
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / synchronizer / SyncControllerBuilder.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (inventory 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.synchronizer;
27
28 import org.onap.aai.sparky.dal.aai.ActiveInventoryAdapter;
29 import org.onap.aai.sparky.dal.aai.config.ActiveInventoryConfig;
30 import org.onap.aai.sparky.dal.cache.InMemoryEntityCache;
31 import org.onap.aai.sparky.dal.cache.PersistentEntityCache;
32 import org.onap.aai.sparky.dal.elasticsearch.ElasticSearchAdapter;
33 import org.onap.aai.sparky.dal.elasticsearch.ElasticSearchDataProvider;
34 import org.onap.aai.sparky.dal.elasticsearch.config.ElasticSearchConfig;
35 import org.onap.aai.sparky.dal.rest.OperationResult;
36 import org.onap.aai.sparky.dal.rest.RestClientBuilder;
37 import org.onap.aai.sparky.dal.rest.RestfulDataAccessor;
38 import org.onap.aai.sparky.synchronizer.AutosuggestionSynchronizer;
39 import org.onap.aai.sparky.synchronizer.CrossEntityReferenceSynchronizer;
40 import org.onap.aai.sparky.synchronizer.ElasticSearchIndexCleaner;
41 import org.onap.aai.sparky.synchronizer.GeoSynchronizer;
42 import org.onap.aai.sparky.synchronizer.HistoricalEntitySummarizer;
43 import org.onap.aai.sparky.synchronizer.IndexCleaner;
44 import org.onap.aai.sparky.synchronizer.IndexIntegrityValidator;
45 import org.onap.aai.sparky.synchronizer.SearchableEntitySynchronizer;
46 import org.onap.aai.sparky.synchronizer.SyncController;
47 import org.onap.aai.sparky.synchronizer.SyncController.SyncActions;
48 import org.onap.aai.sparky.synchronizer.enumeration.SynchronizerState;
49 import org.slf4j.LoggerFactory;
50
51 import ch.qos.logback.classic.Level;
52
53 /**
54  * The Class SyncControllerBuilder.
55  */
56 public class SyncControllerBuilder {
57
58   /**
59    * Do master entity sync.
60    */
61   public void doMasterEntitySync() {
62
63   }
64
65   /**
66    * Test elastic search update api.
67    */
68   public void testElasticSearchUpdateApi() {
69     try {
70
71       RestClientBuilder clientBuilder = new RestClientBuilder();
72       clientBuilder.setUseHttps(false);
73
74       RestfulDataAccessor nonCachingRestProvider = new RestfulDataAccessor(clientBuilder);
75
76       ElasticSearchConfig esConfig = ElasticSearchConfig.getConfig();
77       ElasticSearchDataProvider elasticSearchDataProvider =
78           new ElasticSearchAdapter(nonCachingRestProvider, esConfig);
79
80       String payload =
81           "{ \"entityType\": \"complex\", \"pkey\": \"MORRISTOWN0075\", \"location\": { \"lat\": \"40.793414\", \"lon\": \"-74.480432\" }, \"selfLink\": \"https://aai-hostname:8443/aai/v8/cloud-infrastructure/complexes/complex/MORRISTOWN0075?nodes-only\" }\n";
82
83       String updateRequest = elasticSearchDataProvider.buildBulkImportOperationRequest(
84           "topographysearchindex-localhost", "default",
85           "1e2a6ba9e09d5e1bcb016b3a0b8d50273b42828e47957bd2a2f3ce1854744f5f", "6", payload);
86
87       OperationResult or =
88           elasticSearchDataProvider.doBulkOperation("http://localhost:9200/_bulk", updateRequest);
89
90       System.out.println(or.toString());
91
92       /*
93        * String BULK_IMPORT_INDEX_TEMPLATE =
94        * "{\"index\":{\"_index\":\"%s\",\"_type\":\"%s\",\"_id\":\"%s\", \"_version\":\"%s\"}}\n";
95        * 
96        * StringBuilder updateRequestPayload = new StringBuilder(128);
97        * updateRequestPayload.append(String.format(BULK_IMPORT_INDEX_TEMPLATE,
98        * "topographysearchindex-localhost", "default",
99        * "1e2a6ba9e09d5e1bcb016b3a0b8d50273b42828e47957bd2a2f3ce1854744f5f", "5"));
100        * 
101        * 
102        * updateRequestPayload.append(payload);
103        * 
104        * OperationResult or = nonCachingRestProvider.doRestfulOperation(HttpMethod.PUT,
105        * "http://localhost:9200/_bulk", updateRequestPayload.toString(),
106        * RestfulDataAccessor.APPLICATION_X_WWW_FORM_URL_ENCODED,
107        * RestfulDataAccessor.APPLICATION_JSON);
108        */
109
110
111
112     } catch (Exception exc) {
113       exc.printStackTrace();
114       System.out.println("Error:  failed to sync with message = " + exc.getMessage());
115     }
116   }
117
118   /**
119    * Do historical entity sync.
120    */
121   public void doHistoricalEntitySync() {
122     try {
123       SyncController syncController = new SyncController("historicalEntityTestController");
124
125       ActiveInventoryAdapter aaiAdapter = new ActiveInventoryAdapter(new RestClientBuilder());
126       aaiAdapter.setCacheEnabled(false);
127
128       RestClientBuilder clientBuilder = new RestClientBuilder();
129       clientBuilder.setUseHttps(false);
130
131       RestfulDataAccessor nonCachingRestProvider = new RestfulDataAccessor(clientBuilder);
132
133       ElasticSearchConfig esConfig = ElasticSearchConfig.getConfig();
134
135       ElasticSearchAdapter esAdapter = new ElasticSearchAdapter(nonCachingRestProvider,esConfig);
136
137
138       IndexIntegrityValidator entityCounterHistoryValidator =
139           new IndexIntegrityValidator(nonCachingRestProvider, esConfig.getEntityCountHistoryIndex(),
140               esConfig.getType(), esConfig.getIpAddress(), esConfig.getHttpPort(),
141               esConfig.buildElasticSearchEntityCountHistoryTableConfig());
142
143       syncController.registerIndexValidator(entityCounterHistoryValidator);
144
145
146       //////
147
148
149
150       HistoricalEntitySummarizer historicalSummarizer =
151           new HistoricalEntitySummarizer(esConfig.getEntityCountHistoryIndex());
152       historicalSummarizer.setAaiDataProvider(aaiAdapter);
153       historicalSummarizer.setEsDataProvider(esAdapter);
154       syncController.registerEntitySynchronizer(historicalSummarizer);
155
156       ////
157
158       /*
159        * IndexIntegrityValidator entitySearchIndexValidator = new IndexIntegrityValidator(new
160        * RestClientBuilder());
161        * 
162        * entitySearchIndexValidator.setIndexName("topographysearchindex-localhost");
163        * entitySearchIndexValidator.setIndexType("default");
164        * entitySearchIndexValidator.setIndexSettings("");
165        * entitySearchIndexValidator.setIndexSettings("");
166        * 
167        * syncController.registerIndexValidator(entitySearchIndexValidator);
168        */
169
170       ////
171
172       /*
173        * IndexCleaner index1Cleaner = new ElasticSearchIndexCleaner(nonCachingRestProvider,
174        * "topographysearchindex-localhost", "default", "127.0.0.1", "9200", 5, 5000);
175        */
176
177       // syncController.registerIndexCleaner(index1Cleaner);
178
179       ///
180
181       for (int x = 0; x < 10; x++) {
182
183         syncController.performAction(SyncActions.SYNCHRONIZE);
184
185         while (syncController.getState() == SynchronizerState.PERFORMING_SYNCHRONIZATION) {
186
187           System.out.println("sync controller state = " + syncController.getState());
188
189           Thread.sleep(1000);
190         }
191       }
192
193       syncController.shutdown();
194
195     } catch (Exception exc) {
196       exc.printStackTrace();
197       System.out.println("Error:  failed to sync with message = " + exc.getMessage());
198     }
199   }
200
201
202
203   /**
204    * Do geo entity sync.
205    */
206   public void doGeoEntitySync() {
207     try {
208
209       ActiveInventoryAdapter aaiAdapter = new ActiveInventoryAdapter(new RestClientBuilder());
210
211       aaiAdapter.setCacheEnabled(true);
212
213       InMemoryEntityCache aaiInMemoryCache = new InMemoryEntityCache();
214       aaiAdapter.setEntityCache(aaiInMemoryCache);
215
216       /*
217        * PersistentEntityCache aaiDiskCache = new PersistentEntityCache();
218        * aaiAdapter.setEntityCache(aaiDiskCache);
219        */
220
221       RestClientBuilder clientBuilder = new RestClientBuilder();
222       clientBuilder.setUseHttps(false);
223
224       RestfulDataAccessor nonCachingRestProvider = new RestfulDataAccessor(clientBuilder);
225       ElasticSearchConfig esConfig = ElasticSearchConfig.getConfig();
226
227       ElasticSearchAdapter esAdapter = new ElasticSearchAdapter(nonCachingRestProvider,esConfig);
228
229       IndexIntegrityValidator entitySearchIndexValidator =
230           new IndexIntegrityValidator(nonCachingRestProvider, esConfig.getIndexName(),
231               esConfig.getType(), esConfig.getIpAddress(), esConfig.getHttpPort(),
232               esConfig.buildElasticSearchTableConfig());
233
234       SyncController syncController = new SyncController("geoEntitySyncTestController");
235       syncController.registerIndexValidator(entitySearchIndexValidator);
236
237
238       //////
239
240       GeoSynchronizer geoSync = new GeoSynchronizer("topographysearchindex-localhost");
241       geoSync.setAaiDataProvider(aaiAdapter);
242       geoSync.setEsDataProvider(esAdapter);
243       syncController.registerEntitySynchronizer(geoSync);
244
245       ////
246
247       /*
248        * IndexIntegrityValidator entitySearchIndexValidator = new IndexIntegrityValidator(new
249        * RestClientBuilder());
250        * 
251        * entitySearchIndexValidator.setIndexName("topographysearchindex-localhost");
252        * entitySearchIndexValidator.setIndexType("default");
253        * entitySearchIndexValidator.setIndexSettings("");
254        * entitySearchIndexValidator.setIndexSettings("");
255        * 
256        * syncController.registerIndexValidator(entitySearchIndexValidator);
257        */
258
259       ////
260
261       /*
262        * IndexCleaner index1Cleaner = new ElasticSearchIndexCleaner(nonCachingRestProvider,
263        * "topographysearchindex-localhost", "default", "127.0.0.1", "9200", 5, 5000);
264        */
265
266       // syncController.registerIndexCleaner(index1Cleaner);
267
268       ///
269
270       syncController.performAction(SyncActions.SYNCHRONIZE);
271
272       while (syncController.getState() == SynchronizerState.PERFORMING_SYNCHRONIZATION) {
273         Thread.sleep(1000);
274       }
275
276       syncController.shutdown();
277
278     } catch (Exception exc) {
279       exc.printStackTrace();
280       System.out.println("Error:  failed to sync with message = " + exc.getMessage());
281     }
282   }
283
284   /**
285    * Do searchable entitysync.
286    */
287   public void doSearchableEntitysync() {
288     try {
289       
290
291       ActiveInventoryAdapter aaiAdapter = new ActiveInventoryAdapter(new RestClientBuilder());
292
293       aaiAdapter.setCacheEnabled(true);
294
295       /*
296        * InMemoryEntityCache aaiInMemoryCache = new InMemoryEntityCache();
297        * aaiAdapter.setEntityCache(aaiInMemoryCache);
298        */
299
300       PersistentEntityCache aaiDiskCache = new PersistentEntityCache();
301       aaiAdapter.setEntityCache(aaiDiskCache);
302
303       RestClientBuilder clientBuilder = new RestClientBuilder();
304       clientBuilder.setUseHttps(false);
305
306       RestfulDataAccessor nonCachingRestProvider = new RestfulDataAccessor(clientBuilder);
307       ElasticSearchConfig esConfig = ElasticSearchConfig.getConfig();
308
309       ElasticSearchAdapter esAdapter = new ElasticSearchAdapter(nonCachingRestProvider,esConfig);
310
311       //////
312       
313       SyncController syncController = new SyncController("searchtableEntityTestController");
314
315       SearchableEntitySynchronizer ses =
316           new SearchableEntitySynchronizer("entitysearchindex-localhost");
317       ses.setAaiDataProvider(aaiAdapter);
318       ses.setEsDataProvider(esAdapter);
319       syncController.registerEntitySynchronizer(ses);
320
321       ////
322
323       /*
324        * IndexIntegrityValidator entitySearchIndexValidator = new IndexIntegrityValidator(new
325        * RestClientBuilder());
326        * 
327        * entitySearchIndexValidator.setIndexName("esi-sync2-localhost");
328        * entitySearchIndexValidator.setIndexType("default");
329        * 
330        * syncController.registerIndexValidator(entitySearchIndexValidator);
331        */
332
333       ////
334
335       /*
336        * IndexCleaner index1Cleaner = new ElasticSearchIndexCleaner(nonCachingRestProvider,
337        * "entitysearchindex-localhost", "default", "127.0.0.1", "9200", 5, 5000);
338        * 
339        * syncController.registerIndexCleaner(index1Cleaner);
340        */
341
342       ///
343
344       syncController.performAction(SyncActions.SYNCHRONIZE);
345
346       while (syncController.getState() == SynchronizerState.PERFORMING_SYNCHRONIZATION) {
347         Thread.sleep(1000);
348       }
349
350       syncController.shutdown();
351
352     } catch (Exception exc) {
353       exc.printStackTrace();
354       System.out.println("Error:  failed to sync with message = " + exc.getMessage());
355     }
356   }
357
358   /**
359    * Do cross entity reference sync.
360    */
361   public void doCrossEntityReferenceSync() {
362     try {
363       
364
365       ActiveInventoryAdapter aaiAdapter = new ActiveInventoryAdapter(new RestClientBuilder());
366
367       aaiAdapter.setCacheEnabled(true);
368
369       /*
370        * InMemoryEntityCache aaiInMemoryCache = new InMemoryEntityCache();
371        * aaiAdapter.setEntityCache(aaiInMemoryCache);
372        */
373
374       PersistentEntityCache aaiDiskCache = new PersistentEntityCache();
375       aaiAdapter.setEntityCache(aaiDiskCache);
376
377       RestClientBuilder clientBuilder = new RestClientBuilder();
378       clientBuilder.setUseHttps(false);
379
380       RestfulDataAccessor nonCachingRestProvider = new RestfulDataAccessor(clientBuilder);
381       ElasticSearchConfig esConfig = ElasticSearchConfig.getConfig();
382
383       ElasticSearchAdapter esAdapter = new ElasticSearchAdapter(nonCachingRestProvider,esConfig);
384
385       SyncController syncController = new SyncController("crossEntityRefSyncController");
386
387       CrossEntityReferenceSynchronizer cers =
388           new CrossEntityReferenceSynchronizer("entitysearchindex-localhost", ActiveInventoryConfig.getConfig());
389       cers.setAaiDataProvider(aaiAdapter);
390       cers.setEsDataProvider(esAdapter);
391       syncController.registerEntitySynchronizer(cers);
392
393       SearchableEntitySynchronizer ses =
394           new SearchableEntitySynchronizer("entitysearchindex-localhost");
395       ses.setAaiDataProvider(aaiAdapter);
396       ses.setEsDataProvider(esAdapter);
397       syncController.registerEntitySynchronizer(ses);
398
399       ElasticSearchConfig config = ElasticSearchConfig.getConfig();
400
401       IndexIntegrityValidator entitySearchIndexValidator = new IndexIntegrityValidator(
402           nonCachingRestProvider, config.getIndexName(), config.getType(), config.getIpAddress(),
403           config.getHttpPort(), config.buildElasticSearchTableConfig());
404
405       syncController.registerIndexValidator(entitySearchIndexValidator);
406
407       ////
408
409       IndexCleaner index1Cleaner =
410           new ElasticSearchIndexCleaner(nonCachingRestProvider, config.getIndexName(),
411               config.getType(), config.getIpAddress(), config.getHttpPort(), 5, 5000);
412
413       syncController.registerIndexCleaner(index1Cleaner);
414
415       ///
416
417       syncController.performAction(SyncActions.SYNCHRONIZE);
418
419       while (syncController.getState() == SynchronizerState.PERFORMING_SYNCHRONIZATION) {
420         Thread.sleep(1000);
421       }
422
423       syncController.shutdown();
424
425     } catch (Exception exc) {
426       exc.printStackTrace();
427       System.out.println("Error:  Failed to sync with message = " + exc.getMessage());
428     }
429   }
430
431   /**
432    * Do suggestion entitysync.
433    */
434   public void doSuggestionEntitySync() {
435     try {
436       
437
438       ActiveInventoryAdapter aaiAdapter = new ActiveInventoryAdapter(new RestClientBuilder());
439
440       aaiAdapter.setCacheEnabled(true);
441
442       /*
443        * InMemoryEntityCache aaiInMemoryCache = new InMemoryEntityCache();
444        * aaiAdapter.setEntityCache(aaiInMemoryCache);
445        */
446
447       PersistentEntityCache aaiDiskCache = new PersistentEntityCache();
448       aaiAdapter.setEntityCache(aaiDiskCache);
449
450       RestClientBuilder clientBuilder = new RestClientBuilder();
451       clientBuilder.setUseHttps(false);
452
453       RestfulDataAccessor nonCachingRestProvider = new RestfulDataAccessor(clientBuilder);
454       ElasticSearchConfig esConfig = ElasticSearchConfig.getConfig();
455
456       ElasticSearchAdapter esAdapter = new ElasticSearchAdapter(nonCachingRestProvider,esConfig);
457       
458       SyncController syncController = new SyncController("suggestionEntityTestController");
459
460       AutosuggestionSynchronizer ses =
461           new AutosuggestionSynchronizer("suggestionentityindex-localhost");
462       ses.setAaiDataProvider(aaiAdapter);
463       ses.setEsDataProvider(esAdapter);
464       syncController.registerEntitySynchronizer(ses);
465
466       syncController.performAction(SyncActions.SYNCHRONIZE);
467
468       while (syncController.getState() == SynchronizerState.PERFORMING_SYNCHRONIZATION) {
469         Thread.sleep(1000);
470       }
471
472       syncController.shutdown();
473
474     } catch (Exception exc) {
475       exc.printStackTrace();
476       System.out.println("Error:  failed to sync with message = " + exc.getMessage());
477     }
478   }
479   
480   /*
481    * Do no op sync.
482    */
483   public void doNoOpSync() {
484     try {
485       SyncController syncController = new SyncController("noopSyncTestController");
486
487       /*
488        * ActiveInventoryAdapter aaiAdapter = new ActiveInventoryAdapter(new RestClientBuilder());
489        * 
490        * aaiAdapter.setCacheEnabled(true);
491        * 
492        * /*InMemoryEntityCache aaiInMemoryCache = new InMemoryEntityCache();
493        * aaiAdapter.setEntityCache(aaiInMemoryCache);
494        */
495
496       /*
497        * PersistentEntityCache aaiDiskCache = new PersistentEntityCache();
498        * aaiAdapter.setEntityCache(aaiDiskCache);
499        * 
500        * ElasticSearchConfig config = ElasticSearchConfig.getConfig(); OXMModelLoader loader =
501        * OXMModelLoader.getInstance(); SyncAdapter syncAdapter = new SyncAdapter(new
502        * RestClientBuilder(), config, loader);
503        * 
504        * //////
505        * 
506        * SearchableEntitySynchronizer ses = new SearchableEntitySynchronizer();
507        * ses.setAaiDataProvider(aaiAdapter); ses.setEsDataProvider(syncAdapter);
508        * syncController.registerEntitySynchronizer(ses);
509        * 
510        * ////
511        * 
512        * IndexIntegrityValidator entitySearchIndexValidator = new IndexIntegrityValidator(new
513        * RestClientBuilder());
514        * 
515        * entitySearchIndexValidator.setIndexName("esi-sync2-localhost");
516        * entitySearchIndexValidator.setIndexType("default");
517        * entitySearchIndexValidator.setIndexSettings("");
518        * entitySearchIndexValidator.setIndexSettings("");
519        * 
520        * syncController.registerIndexValidator(entitySearchIndexValidator);
521        * 
522        * ////
523        * 
524        * ElasticSearchEntityPurger p1 = new ElasticSearchEntityPurger(new RestClientBuilder());
525        * p1.setIndexName("esi-blal-blah");
526        * 
527        * ElasticSearchEntityPurger p2 = new ElasticSearchEntityPurger(new RestClientBuilder());
528        * p2.setIndexName("esi-topo-blah");
529        */
530       ///
531
532       syncController.performAction(SyncActions.SYNCHRONIZE);
533
534       while (syncController.getState() == SynchronizerState.PERFORMING_SYNCHRONIZATION) {
535         Thread.sleep(1000);
536       }
537
538       syncController.shutdown();
539
540     } catch (Exception exc) {
541       System.out.println("Error:  failed to sync with message = " + exc.getMessage());
542     }
543   }
544
545
546   /**
547    * The main method.
548    *
549    * @param args the arguments
550    */
551   public static void main(String[] args) {
552     boolean runSearchableEntitySync = false;
553     boolean runGeoEntitySync = true;
554
555     System.setProperty("AJSC_HOME", "e:\\dev");
556     // System.getProperties().setProperty("AJSC_HOME",
557     // "c:\\rpo\\tier-support-ui\\target\\swm\\package\\nix\\"
558     // + "dist_files\\opt\\app\\ajsc-tier-support-ui");
559     System.setProperty("AJSC_HOME", "d:\\AAI\\tier_support_ui\\tier-support-ui\\target\\swm\\package\\nix\\dist_files\\appl\\inventory-ui-service\\1.0-SNAPSHOT");
560
561     ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory
562         .getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
563     // root.detachAndStopAllAppenders();
564     // logger = new CaptureLoggerAppender();
565     root.setLevel(Level.INFO);
566     // root.addAppender(logger);
567
568
569     SyncControllerBuilder syncBuilder = new SyncControllerBuilder();
570
571     /*
572      * if (runSearchableEntitySync) syncBuilder.doSearchableEntitysync();
573      */
574
575     //syncBuilder.doSearchableEntitysync();
576     // syncBuilder.doCrossEntityReferenceSync();
577     // syncBuilder.doHistoricalEntitySync();
578     // syncBuilder.doGeoEntitySync();
579     syncBuilder.doSuggestionEntitySync();
580
581     // syncBuilder.testElasticSearchUpdateAPI();
582
583     /*
584      * if (runGeoEntitySync) { syncBuilder.doGeoEntitySync(); }
585      */
586
587
588
589   }
590 }