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