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