1 package org.openecomp.core.migration;
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;
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;
66 import static org.openecomp.core.migration.util.Utils.printMessage;
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;
73 public static Map<String, VersionInfoEntity> versionInfoMap = new HashMap<>();
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.");
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();
91 migrateToZusammen(cassandraElementRepository, itemCassandraDao, versionCassandraDao, context);
93 Instant stopTime = Instant.now();
94 Duration duration = Duration.between(startTime, stopTime);
95 long minutesPart = duration.toMinutes();
96 long secondsPart = duration.minusMinutes(minutesPart).getSeconds();
99 MigrationMarker.markMigrated();
102 "Migration finished . Total run time was : " + minutesPart + ":" + secondsPart
108 private static void migrateToZusammen(CassandraElementRepository cassandraElementRepository,
109 ItemCassandraDao itemCassandraDao,
110 VersionCassandraDao versionCassandraDao,
111 SessionContext context) {
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 :");
125 convertOrchestrationTemplateCandidate(context, cassandraElementRepository);
126 printMessage(logger, "Converted OrchestrationTemplateCandidates\n");
127 } catch (Exception e) {
129 "Could not perform migration for OrchestrationTemplateCandidates ,the error is :");
136 convertComponent(context, cassandraElementRepository);
137 printMessage(logger, "Converted Components\n");
138 } catch (Exception e) {
140 "Could not perform migration for Components ,the error is :");
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 :");
153 convertNetwork(context, cassandraElementRepository);
154 printMessage(logger, "Converted Networks\n");
155 } catch (Exception e) {
157 "Could not perform migration for Networks ,the error is :");
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 :");
170 convertServiceArtifact(context, cassandraElementRepository);
171 printMessage(logger, "Converted Service Artifacts\n");
172 } catch (Exception e) {
174 "Could not perform migration for Service Artifacts,the error is :");
179 convertServiceTemplate(context, cassandraElementRepository);
180 printMessage(logger, "Converted Service Templates\n");
181 } catch (Exception e) {
183 "Could not perform migration for Service Templates,the error is :");
188 convertProcesses(context, cassandraElementRepository);
189 printMessage(logger, "Converted Processes\n");
190 } catch (Exception e) {
192 "Could not perform migration for Processes,the error is :");
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 :");
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 :");
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 :");
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 :");
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 :");
240 private static void convertOrchestrationTemplateCandidate(SessionContext context,
241 CassandraElementRepository cassandraElementRepository) {
242 OrchestrationTemplateCandidateCassandraLoader orchestrationTemplateCandidateCassandraLoader =
243 new OrchestrationTemplateCandidateCassandraLoader();
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)));
254 private static void loadVersionInfo() {
256 VersionInfoCassandraLoader versionInfoCassandraLoader = new VersionInfoCassandraLoader();
257 Collection<VersionInfoEntity> versions =
258 versionInfoCassandraLoader.list();
260 versions.forEach(versionInfoEntity -> versionInfoMap.put(versionInfoEntity.getEntityId
261 (), versionInfoEntity));
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()))
273 ElementHandler.save(context, cassandraElementRepository,
275 .getVspId(), mibEntity.getVersion(),
276 MibConvertor.convertMibToElement
281 private static void convertProcesses(SessionContext context,
282 CassandraElementRepository cassandraElementRepository) {
283 ProcessCassandraLoader cassandraLoader = new ProcessCassandraLoader();
284 Collection<ProcessEntity> processes = cassandraLoader.list();
286 .filter(processEntity -> needMigration(processEntity.getVspId(), processEntity.getVersion
287 ())).forEach(processEntity -> {
290 cassandraElementRepository,
292 .getId(), processEntity.getVersion(),
293 ProcessConvertor.convertProcessToElement(processEntity));
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
308 ItemHandler.save(context,
311 vspInformation.getId(), vspInformation
313 VspInformationConvertor
316 VspInformationConvertor.getItemVersionData(vspInformation),
317 vspInformation.getWritetimeMicroSeconds()));
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
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 ->
336 (context, itemCassandraDao,
338 vlmEntity.getId(), vlmEntity.getVersion(),
339 VlmConvertor.getVlmInfo
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
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
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)));
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,
379 .getVspId(), componentEntity.getVersion(), ComponentConvertor
380 .convertComponentToElement(componentEntity)));
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,
393 .getVspId(), serviceArtifact.getVersion(),
394 VspServiceArtifactConvertor
395 .convertServiceArtifactToElement(serviceArtifact)));
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,
408 .getVspId(), serviceTemplate.getVersion(),
409 VspServiceTemplateConvertor
410 .convertServiceTemplateToElement(serviceTemplate)));
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
420 .forEach(licenseKeyGroupEntity -> ElementHandler.save(context, cassandraElementRepository,
421 licenseKeyGroupEntity
422 .getVendorLicenseModelId(), licenseKeyGroupEntity.getVersion(),
423 LKGConvertor.convertLKGToElement
424 (licenseKeyGroupEntity)));
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
435 .forEach(entitlementPoolEntity -> ElementHandler.save(context, cassandraElementRepository,
436 entitlementPoolEntity
437 .getVendorLicenseModelId(), entitlementPoolEntity.getVersion(),
438 EntitlementPoolConvertor.convertEntitlementPoolToElement(entitlementPoolEntity)));
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
448 .forEach(featureGroupEntity -> ElementHandler.save(context, cassandraElementRepository,
450 .getVendorLicenseModelId(), featureGroupEntity.getVersion(),
451 FeatureGroupConvertor.convertFeatureGroupToElement(featureGroupEntity)));
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
463 .forEach(licenseAgreementEntity -> ElementHandler.save(context, cassandraElementRepository,
464 licenseAgreementEntity
465 .getVendorLicenseModelId(), licenseAgreementEntity.getVersion(),
466 LicenseAgreementConvertor.convertLicenseAgreementToElement(licenseAgreementEntity)));
470 private static boolean needMigration(String itemId, Version versionId) {
472 VersionInfoEntity versionInfo =
473 MigrationMain.versionInfoMap.get(itemId);
474 if (versionInfo == null) {
475 printMessage(logger, "ItemId: " + itemId + " is not in version_info table.");
478 return (versionInfo.getCandidate() != null && versionId.equals(versionInfo.getCandidate()
481 .getViewableVersions() != null && versionInfo
482 .getViewableVersions().contains(versionId));