[SDC-29] rebase continue work to align source
[sdc.git] / openecomp-be / tools / migration / 1702_to_1707_zusammen / src / main / java / org / openecomp / core / migration / MigrationMain.java
1 package org.openecomp.core.migration;
2
3 import com.amdocs.zusammen.datatypes.SessionContext;
4 import com.amdocs.zusammen.datatypes.UserInfo;
5 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.impl.ItemCassandraDao;
6 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.impl.VersionCassandraDao;
7 import org.openecomp.core.migration.convertors.ComponentConvertor;
8 import org.openecomp.core.migration.convertors.EntitlementPoolConvertor;
9 import org.openecomp.core.migration.convertors.FeatureGroupConvertor;
10 import org.openecomp.core.migration.convertors.LKGConvertor;
11 import org.openecomp.core.migration.convertors.LicenseAgreementConvertor;
12 import org.openecomp.core.migration.convertors.MibConvertor;
13 import org.openecomp.core.migration.convertors.NetworkConvertor;
14 import org.openecomp.core.migration.convertors.NicConvertor;
15 import org.openecomp.core.migration.convertors.OrchestrationTemplateCandidateConvertor;
16 import org.openecomp.core.migration.convertors.ProcessConvertor;
17 import org.openecomp.core.migration.convertors.VlmConvertor;
18 import org.openecomp.core.migration.convertors.VspInformationConvertor;
19 import org.openecomp.core.migration.convertors.VspServiceArtifactConvertor;
20 import org.openecomp.core.migration.convertors.VspServiceTemplateConvertor;
21 import org.openecomp.core.migration.loaders.ComponentCassandraLoader;
22 import org.openecomp.core.migration.loaders.EntitlementPoolCassandraLoader;
23 import org.openecomp.core.migration.loaders.FeatureGroupCassandraLoader;
24 import org.openecomp.core.migration.loaders.LKGCassandraLoader;
25 import org.openecomp.core.migration.loaders.LicenseAgreementCassandraLoader;
26 import org.openecomp.core.migration.loaders.MibCassandraLoader;
27 import org.openecomp.core.migration.loaders.NetworkCassandraLoader;
28 import org.openecomp.core.migration.loaders.NicCassandraLoader;
29 import org.openecomp.core.migration.loaders.OrchestrationTemplateCandidateCassandraLoader;
30 import org.openecomp.core.migration.loaders.ProcessCassandraLoader;
31 import org.openecomp.core.migration.loaders.ServiceArtifactCassandraLoader;
32 import org.openecomp.core.migration.loaders.ServiceTemplateCassandraLoader;
33 import org.openecomp.core.migration.loaders.VendorLicenseModelCassandraLoader;
34 import org.openecomp.core.migration.loaders.VendorSoftwareProductInfoLoader;
35 import org.openecomp.core.migration.loaders.VersionInfoCassandraLoader;
36 import org.openecomp.core.migration.loaders.VspInformation;
37 import org.openecomp.core.migration.store.ElementHandler;
38 import org.openecomp.core.migration.store.ItemHandler;
39 import org.openecomp.core.migration.util.marker.MigrationMarker;
40 import org.openecomp.core.model.types.ServiceArtifact;
41 import org.openecomp.core.model.types.ServiceTemplate;
42 import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer;
43 import org.openecomp.core.zusammen.plugin.dao.impl.CassandraElementRepository;
44 import org.openecomp.sdc.logging.api.Logger;
45 import org.openecomp.sdc.logging.api.LoggerFactory;
46 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
47 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
48 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
49 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
50 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
51 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
52 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.MibEntity;
53 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
54 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
55 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
56 import org.openecomp.sdc.versioning.dao.types.Version;
57 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
58
59 import java.time.Duration;
60 import java.time.Instant;
61 import java.util.Collection;
62 import java.util.Date;
63 import java.util.HashMap;
64 import java.util.Map;
65
66 import static org.openecomp.core.migration.util.Utils.printMessage;
67
68 public class MigrationMain {
69   private static final String GLOBAL_USER = "GLOBAL_USER";
70   private static Logger logger = LoggerFactory.getLogger(MigrationMain.class);
71   private static int status = 0;
72
73   public static Map<String, VersionInfoEntity> versionInfoMap = new HashMap<>();
74
75   public static void main(String[] args) {
76     CassandraElementRepository cassandraElementRepository = new CassandraElementRepository();
77     CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem();
78     printMessage(logger, "Checking whether a migration has already been run.");
79     if (MigrationMarker.isMigrated()) {
80       printMessage(logger, "The DB has already been migrated, this script will now exit.");
81       System.exit(status);
82     }
83     ItemCassandraDao itemCassandraDao = new ItemCassandraDao();
84     VersionCassandraDao versionCassandraDao = new VersionCassandraDao();
85     SessionContext context = new SessionContext();
86     context.setUser(new UserInfo(GLOBAL_USER));
87     context.setTenant("dox");
88     printMessage(logger, "Starting migration.\n");
89     Instant startTime = Instant.now();
90
91     migrateToZusammen(cassandraElementRepository, itemCassandraDao, versionCassandraDao, context);
92
93     Instant stopTime = Instant.now();
94     Duration duration = Duration.between(startTime, stopTime);
95     long minutesPart = duration.toMinutes();
96     long secondsPart = duration.minusMinutes(minutesPart).getSeconds();
97
98     if (status == 0) {
99       MigrationMarker.markMigrated();
100     }
101     printMessage(logger,
102         "Migration finished . Total run time was : " + minutesPart + ":" + secondsPart
103             + " minutes");
104     System.exit(status);
105   }
106
107
108   private static void migrateToZusammen(CassandraElementRepository cassandraElementRepository,
109                                         ItemCassandraDao itemCassandraDao,
110                                         VersionCassandraDao versionCassandraDao,
111                                         SessionContext context) {
112     loadVersionInfo();
113
114
115     try {
116       convertVsp(context, itemCassandraDao, versionCassandraDao, cassandraElementRepository);
117       printMessage(logger, "Converted VSPs\n");
118     } catch (Exception e) {
119       printMessage(logger, "Could not perform migration for VSPs ,the error is :");
120       e.printStackTrace();
121       status = -1;
122     }
123
124     try {
125       convertOrchestrationTemplateCandidate(context, cassandraElementRepository);
126       printMessage(logger, "Converted OrchestrationTemplateCandidates\n");
127     } catch (Exception e) {
128       printMessage(logger,
129           "Could not perform migration for OrchestrationTemplateCandidates ,the error is :");
130       e.printStackTrace();
131       status = -1;
132     }
133
134
135     try {
136       convertComponent(context, cassandraElementRepository);
137       printMessage(logger, "Converted Components\n");
138     } catch (Exception e) {
139       printMessage(logger,
140           "Could not perform migration for Components ,the error is :");
141       e.printStackTrace();
142       status = -1;
143     }
144     try {
145       convertNic(context, cassandraElementRepository);
146       printMessage(logger, "Converted Nics\n");
147     } catch (Exception e) {
148       printMessage(logger, "Could not perform migration for Nics ,the error is :");
149       e.printStackTrace();
150       status = -1;
151     }
152     try {
153       convertNetwork(context, cassandraElementRepository);
154       printMessage(logger, "Converted Networks\n");
155     } catch (Exception e) {
156       printMessage(logger,
157           "Could not perform migration for Networks ,the error is :");
158       e.printStackTrace();
159       status = -1;
160     }
161     try {
162       convertMibs(context, cassandraElementRepository);
163       printMessage(logger, "Converted MIBs\n");
164     } catch (Exception e) {
165       printMessage(logger, "Could not perform migration for MIBs,the error is :");
166       e.printStackTrace();
167       status = -1;
168     }
169     try {
170       convertServiceArtifact(context, cassandraElementRepository);
171       printMessage(logger, "Converted Service Artifacts\n");
172     } catch (Exception e) {
173       printMessage(logger,
174           "Could not perform migration for Service Artifacts,the error is :");
175       e.printStackTrace();
176       status = -1;
177     }
178     try {
179       convertServiceTemplate(context, cassandraElementRepository);
180       printMessage(logger, "Converted Service Templates\n");
181     } catch (Exception e) {
182       printMessage(logger,
183           "Could not perform migration for Service Templates,the error is :");
184       e.printStackTrace();
185       status = -1;
186     }
187     try {
188       convertProcesses(context, cassandraElementRepository);
189       printMessage(logger, "Converted Processes\n");
190     } catch (Exception e) {
191       printMessage(logger,
192           "Could not perform migration for Processes,the error is :");
193       e.printStackTrace();
194       status = -1;
195     }
196
197
198     try {
199       convertVlm(context, itemCassandraDao, versionCassandraDao, cassandraElementRepository);
200       printMessage(logger, "Converted VLMs\n");
201     } catch (Exception e) {
202       printMessage(logger, "Could not perform migration for VLMs,the error is :");
203       e.printStackTrace();
204       status = -1;
205     }
206     try {
207       convertLKG(context, cassandraElementRepository);
208       printMessage(logger, "Converted LKGs\n");
209     } catch (Exception e) {
210       printMessage(logger, "Could not perform migration for LKGs,the error is :");
211       e.printStackTrace();
212       status = -1;
213     }
214     try {
215       convertFeatureGroup(context, cassandraElementRepository);
216       printMessage(logger, "Converted Feature Groups\n");
217     } catch (Exception e) {
218       printMessage(logger, "Could not perform migration for Feature Groups,the error is :");
219       e.printStackTrace();
220       status = -1;
221     }
222     try {
223       convertEP(context, cassandraElementRepository);
224       printMessage(logger, "Converted EPs\n");
225     } catch (Exception e) {
226       printMessage(logger, "Could not perform migration for EPs,the error is :");
227       e.printStackTrace();
228       status = -1;
229     }
230     try {
231       convertLicenseAgreement(context, cassandraElementRepository);
232       printMessage(logger, "Converted License Agreements\n");
233     } catch (Exception e) {
234       printMessage(logger, "Could not perform migration for License Agreements,the error is :");
235       e.printStackTrace();
236       status = -1;
237     }
238   }
239
240   private static void convertOrchestrationTemplateCandidate(SessionContext context,
241                                                             CassandraElementRepository cassandraElementRepository) {
242     OrchestrationTemplateCandidateCassandraLoader orchestrationTemplateCandidateCassandraLoader =
243         new OrchestrationTemplateCandidateCassandraLoader();
244
245     orchestrationTemplateCandidateCassandraLoader.list().stream()
246         .filter(entity -> needMigration(entity.getId(), entity.getVersion()) &&
247             entity.getContentData()!=null && entity.getFilesDataStructure()!=null)
248         .forEach(entity -> ElementHandler
249             .save(context, cassandraElementRepository, entity.getId(), entity.getVersion(),
250                 OrchestrationTemplateCandidateConvertor
251                     .convertOrchestrationTemplateCandidateToElement(entity)));
252   }
253
254   private static void loadVersionInfo() {
255
256     VersionInfoCassandraLoader versionInfoCassandraLoader = new VersionInfoCassandraLoader();
257     Collection<VersionInfoEntity> versions =
258         versionInfoCassandraLoader.list();
259
260     versions.forEach(versionInfoEntity -> versionInfoMap.put(versionInfoEntity.getEntityId
261         (), versionInfoEntity));
262
263
264   }
265
266   private static void convertMibs(SessionContext context,
267                                   CassandraElementRepository cassandraElementRepository) {
268     MibCassandraLoader cassandraLoader = new MibCassandraLoader();
269     Collection<MibEntity> mibs = cassandraLoader.list();
270     mibs.stream().filter(mibEntity -> needMigration(mibEntity.getVspId(), mibEntity.getVersion()))
271         .forEach
272             (mibEntity -> {
273               ElementHandler.save(context, cassandraElementRepository,
274                   mibEntity
275                       .getVspId(), mibEntity.getVersion(),
276                   MibConvertor.convertMibToElement
277                       (mibEntity));
278             });
279   }
280
281   private static void convertProcesses(SessionContext context,
282                                        CassandraElementRepository cassandraElementRepository) {
283     ProcessCassandraLoader cassandraLoader = new ProcessCassandraLoader();
284     Collection<ProcessEntity> processes = cassandraLoader.list();
285     processes.stream()
286         .filter(processEntity -> needMigration(processEntity.getVspId(), processEntity.getVersion
287             ())).forEach(processEntity -> {
288       ElementHandler.save(
289           context,
290           cassandraElementRepository,
291           processEntity
292               .getId(), processEntity.getVersion(),
293           ProcessConvertor.convertProcessToElement(processEntity));
294     });
295   }
296
297   private static void convertVsp(SessionContext context, ItemCassandraDao itemCassandraDao,
298                                  VersionCassandraDao versionCassandraDao,
299                                  CassandraElementRepository cassandraElementRepository) {
300     VendorSoftwareProductInfoLoader vendorSoftwareProductInfoLoader = new
301         VendorSoftwareProductInfoLoader();
302     Collection<VspInformation> vsps =
303         vendorSoftwareProductInfoLoader.list();
304     vsps.stream().filter(vspInformation -> needMigration(vspInformation.getId(),
305         vspInformation.getVersion())).forEach
306         (vspInformation
307             ->
308             ItemHandler.save(context,
309                 itemCassandraDao,
310                 versionCassandraDao,
311                 vspInformation.getId(), vspInformation
312                     .getVersion(),
313                 VspInformationConvertor
314                     .getVspInfo
315                         (vspInformation),
316                 VspInformationConvertor.getItemVersionData(vspInformation),
317                 vspInformation.getWritetimeMicroSeconds()));
318
319     vsps.stream().filter(vspInformation -> needMigration(vspInformation.getId(),
320         vspInformation.getVersion()))
321         .forEach(vspInformation -> ElementHandler.save(context, cassandraElementRepository,
322             vspInformation.getId(), vspInformation.getVersion(),
323             VspInformationConvertor.convertVspToElement
324                 (vspInformation)));
325   }
326
327   private static void convertVlm(SessionContext context, ItemCassandraDao itemCassandraDao,
328                                  VersionCassandraDao versionCassandraDao,
329                                  CassandraElementRepository cassandraElementRepository) {
330     VendorLicenseModelCassandraLoader
331         vendorLicenseModelCassandraDao = new VendorLicenseModelCassandraLoader();
332     Collection<VendorLicenseModelEntity> vlms =
333         vendorLicenseModelCassandraDao.list();
334     vlms.stream().filter(vlm -> needMigration(vlm.getId(), vlm.getVersion())).forEach(vlmEntity ->
335         ItemHandler.save
336             (context, itemCassandraDao,
337                 versionCassandraDao,
338                 vlmEntity.getId(), vlmEntity.getVersion(),
339                 VlmConvertor.getVlmInfo
340                     (vlmEntity),
341                 VlmConvertor.getItemVersionData(vlmEntity), new Date().getTime()));
342     vlms.stream().filter(vlm -> needMigration(vlm.getId(), vlm.getVersion()))
343         .forEach(vlmEntity -> ElementHandler.save(context, cassandraElementRepository,
344             vlmEntity.getId(), vlmEntity.getVersion(),
345             VlmConvertor.convertVlmToElement
346                 (vlmEntity)));
347
348   }
349
350   private static void convertNic(SessionContext context,
351                                  CassandraElementRepository cassandraElementRepository) {
352     NicCassandraLoader nicCassandraLoader = new NicCassandraLoader();
353     Collection<NicEntity> nics = nicCassandraLoader.list();
354     nics.stream().filter(entity -> needMigration(entity.getVspId(), entity.getVersion
355         ())).forEach(nicEntity -> ElementHandler.save(context, cassandraElementRepository,
356         nicEntity.getVspId(), nicEntity.getVersion(), NicConvertor.convertNicToElement
357             (nicEntity)));
358
359   }
360
361   private static void convertNetwork(SessionContext context,
362                                      CassandraElementRepository cassandraElementRepository) {
363     NetworkCassandraLoader networkCassandraLoader = new NetworkCassandraLoader();
364     Collection<NetworkEntity> networks = networkCassandraLoader.list();
365     networks.stream().filter(entity -> needMigration(entity.getVspId(), entity.getVersion
366         ())).forEach(networkEntity -> ElementHandler.save(context, cassandraElementRepository,
367         networkEntity.getVspId(), networkEntity.getVersion(), NetworkConvertor
368             .convertNetworkToElement(networkEntity)));
369
370   }
371
372   private static void convertComponent(SessionContext context,
373                                        CassandraElementRepository cassandraElementRepository) {
374     ComponentCassandraLoader componentCassandraLoader = new ComponentCassandraLoader();
375     Collection<ComponentEntity> components = componentCassandraLoader.list();
376     components.stream().filter(entity -> needMigration(entity.getVspId(), entity.getVersion
377         ())).forEach(componentEntity -> ElementHandler.save(context, cassandraElementRepository,
378         componentEntity
379             .getVspId(), componentEntity.getVersion(), ComponentConvertor
380             .convertComponentToElement(componentEntity)));
381
382   }
383
384   private static void convertServiceArtifact(SessionContext context,
385                                              CassandraElementRepository cassandraElementRepository) {
386     ServiceArtifactCassandraLoader serviceArtifactCassandraLoader =
387         new ServiceArtifactCassandraLoader();
388     Collection<ServiceArtifact> serviceArtifacts = serviceArtifactCassandraLoader.list();
389     serviceArtifacts.stream().filter(entity -> needMigration(entity.getVspId(), entity.getVersion
390         ())).forEach(serviceArtifact -> ElementHandler.save(context,
391         cassandraElementRepository,
392         serviceArtifact
393             .getVspId(), serviceArtifact.getVersion(),
394         VspServiceArtifactConvertor
395             .convertServiceArtifactToElement(serviceArtifact)));
396
397   }
398
399   private static void convertServiceTemplate(SessionContext context,
400                                              CassandraElementRepository cassandraElementRepository) {
401     ServiceTemplateCassandraLoader serviceTemplateCassandraLoader =
402         new ServiceTemplateCassandraLoader();
403     Collection<ServiceTemplate> serviceTemplates = serviceTemplateCassandraLoader.list();
404     serviceTemplates.stream().filter(entity -> needMigration(entity.getVspId(), entity.getVersion
405         ())).forEach(serviceTemplate -> ElementHandler.save(context,
406         cassandraElementRepository,
407         serviceTemplate
408             .getVspId(), serviceTemplate.getVersion(),
409         VspServiceTemplateConvertor
410             .convertServiceTemplateToElement(serviceTemplate)));
411
412   }
413
414   private static void convertLKG(SessionContext context,
415                                  CassandraElementRepository cassandraElementRepository) {
416     LKGCassandraLoader LKGCassandraLoader = new LKGCassandraLoader();
417     Collection<LicenseKeyGroupEntity> lkgs = LKGCassandraLoader.list();
418     lkgs.stream().filter(entity -> needMigration(entity.getVendorLicenseModelId(), entity.getVersion
419         ()))
420         .forEach(licenseKeyGroupEntity -> ElementHandler.save(context, cassandraElementRepository,
421             licenseKeyGroupEntity
422                 .getVendorLicenseModelId(), licenseKeyGroupEntity.getVersion(),
423             LKGConvertor.convertLKGToElement
424                 (licenseKeyGroupEntity)));
425   }
426
427   private static void convertEP(SessionContext context,
428                                 CassandraElementRepository cassandraElementRepository) {
429     EntitlementPoolCassandraLoader entitlementPoolCassandraLoader =
430         new EntitlementPoolCassandraLoader();
431     Collection<EntitlementPoolEntity> entitlementPools = entitlementPoolCassandraLoader.list();
432     entitlementPools.stream()
433         .filter(entity -> needMigration(entity.getVendorLicenseModelId(), entity.getVersion
434             ()))
435         .forEach(entitlementPoolEntity -> ElementHandler.save(context, cassandraElementRepository,
436             entitlementPoolEntity
437                 .getVendorLicenseModelId(), entitlementPoolEntity.getVersion(),
438             EntitlementPoolConvertor.convertEntitlementPoolToElement(entitlementPoolEntity)));
439   }
440
441   private static void convertFeatureGroup(SessionContext context,
442                                           CassandraElementRepository cassandraElementRepository) {
443     FeatureGroupCassandraLoader featureGroupCassandraLoader = new FeatureGroupCassandraLoader();
444     Collection<FeatureGroupEntity> featureGroupEntities = featureGroupCassandraLoader.list();
445     featureGroupEntities.stream()
446         .filter(entity -> needMigration(entity.getVendorLicenseModelId(), entity.getVersion
447             ()))
448         .forEach(featureGroupEntity -> ElementHandler.save(context, cassandraElementRepository,
449             featureGroupEntity
450                 .getVendorLicenseModelId(), featureGroupEntity.getVersion(),
451             FeatureGroupConvertor.convertFeatureGroupToElement(featureGroupEntity)));
452   }
453
454   private static void convertLicenseAgreement(SessionContext context,
455                                               CassandraElementRepository cassandraElementRepository) {
456     LicenseAgreementCassandraLoader licenseAgreementCassandraLoader =
457         new LicenseAgreementCassandraLoader();
458     Collection<LicenseAgreementEntity> licenseAgreementEntities =
459         licenseAgreementCassandraLoader.list();
460     licenseAgreementEntities.stream()
461         .filter(entity -> needMigration(entity.getVendorLicenseModelId(), entity.getVersion
462             ()))
463         .forEach(licenseAgreementEntity -> ElementHandler.save(context, cassandraElementRepository,
464             licenseAgreementEntity
465                 .getVendorLicenseModelId(), licenseAgreementEntity.getVersion(),
466             LicenseAgreementConvertor.convertLicenseAgreementToElement(licenseAgreementEntity)));
467   }
468
469
470   private static boolean needMigration(String itemId, Version versionId) {
471
472     VersionInfoEntity versionInfo =
473         MigrationMain.versionInfoMap.get(itemId);
474     if (versionInfo == null) {
475       printMessage(logger, "ItemId: " + itemId + " is not in version_info table.");
476       return false;
477     }
478     return (versionInfo.getCandidate() != null && versionId.equals(versionInfo.getCandidate()
479         .getVersion()))
480         || (versionInfo
481         .getViewableVersions() != null && versionInfo
482         .getViewableVersions().contains(versionId));
483   }
484
485
486 }