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 .forEach(entity -> ElementHandler
248 .save(context, cassandraElementRepository, entity.getId(), entity.getVersion(),
249 OrchestrationTemplateCandidateConvertor
250 .convertOrchestrationTemplateCandidateToElement(entity)));
253 private static void loadVersionInfo() {
255 VersionInfoCassandraLoader versionInfoCassandraLoader = new VersionInfoCassandraLoader();
256 Collection<VersionInfoEntity> versions =
257 versionInfoCassandraLoader.list();
259 versions.forEach(versionInfoEntity -> versionInfoMap.put(versionInfoEntity.getEntityId
260 (), versionInfoEntity));
265 private static void convertMibs(SessionContext context,
266 CassandraElementRepository cassandraElementRepository) {
267 MibCassandraLoader cassandraLoader = new MibCassandraLoader();
268 Collection<MibEntity> mibs = cassandraLoader.list();
269 mibs.stream().filter(mibEntity -> needMigration(mibEntity.getVspId(), mibEntity.getVersion()))
272 ElementHandler.save(context, cassandraElementRepository,
274 .getVspId(), mibEntity.getVersion(),
275 MibConvertor.convertMibToElement
280 private static void convertProcesses(SessionContext context,
281 CassandraElementRepository cassandraElementRepository) {
282 ProcessCassandraLoader cassandraLoader = new ProcessCassandraLoader();
283 Collection<ProcessEntity> processes = cassandraLoader.list();
285 .filter(processEntity -> needMigration(processEntity.getVspId(), processEntity.getVersion
286 ())).forEach(processEntity -> {
289 cassandraElementRepository,
291 .getId(), processEntity.getVersion(),
292 ProcessConvertor.convertProcessToElement(processEntity));
296 private static void convertVsp(SessionContext context, ItemCassandraDao itemCassandraDao,
297 VersionCassandraDao versionCassandraDao,
298 CassandraElementRepository cassandraElementRepository) {
299 VendorSoftwareProductInfoLoader vendorSoftwareProductInfoLoader = new
300 VendorSoftwareProductInfoLoader();
301 Collection<VspInformation> vsps =
302 vendorSoftwareProductInfoLoader.list();
303 vsps.stream().filter(vspInformation -> needMigration(vspInformation.getId(),
304 vspInformation.getVersion())).forEach
307 ItemHandler.save(context,
310 vspInformation.getId(), vspInformation
312 VspInformationConvertor
315 VspInformationConvertor.getItemVersionData(vspInformation),
316 vspInformation.getWritetimeMicroSeconds()));
318 vsps.stream().filter(vspInformation -> needMigration(vspInformation.getId(),
319 vspInformation.getVersion()))
320 .forEach(vspInformation -> ElementHandler.save(context, cassandraElementRepository,
321 vspInformation.getId(), vspInformation.getVersion(),
322 VspInformationConvertor.convertVspToElement
326 private static void convertVlm(SessionContext context, ItemCassandraDao itemCassandraDao,
327 VersionCassandraDao versionCassandraDao,
328 CassandraElementRepository cassandraElementRepository) {
329 VendorLicenseModelCassandraLoader
330 vendorLicenseModelCassandraDao = new VendorLicenseModelCassandraLoader();
331 Collection<VendorLicenseModelEntity> vlms =
332 vendorLicenseModelCassandraDao.list();
333 vlms.stream().filter(vlm -> needMigration(vlm.getId(), vlm.getVersion())).forEach(vlmEntity ->
335 (context, itemCassandraDao,
337 vlmEntity.getId(), vlmEntity.getVersion(),
338 VlmConvertor.getVlmInfo
340 VlmConvertor.getItemVersionData(vlmEntity), new Date().getTime()));
341 vlms.stream().filter(vlm -> needMigration(vlm.getId(), vlm.getVersion()))
342 .forEach(vlmEntity -> ElementHandler.save(context, cassandraElementRepository,
343 vlmEntity.getId(), vlmEntity.getVersion(),
344 VlmConvertor.convertVlmToElement
349 private static void convertNic(SessionContext context,
350 CassandraElementRepository cassandraElementRepository) {
351 NicCassandraLoader nicCassandraLoader = new NicCassandraLoader();
352 Collection<NicEntity> nics = nicCassandraLoader.list();
353 nics.stream().filter(entity -> needMigration(entity.getVspId(), entity.getVersion
354 ())).forEach(nicEntity -> ElementHandler.save(context, cassandraElementRepository,
355 nicEntity.getVspId(), nicEntity.getVersion(), NicConvertor.convertNicToElement
360 private static void convertNetwork(SessionContext context,
361 CassandraElementRepository cassandraElementRepository) {
362 NetworkCassandraLoader networkCassandraLoader = new NetworkCassandraLoader();
363 Collection<NetworkEntity> networks = networkCassandraLoader.list();
364 networks.stream().filter(entity -> needMigration(entity.getVspId(), entity.getVersion
365 ())).forEach(networkEntity -> ElementHandler.save(context, cassandraElementRepository,
366 networkEntity.getVspId(), networkEntity.getVersion(), NetworkConvertor
367 .convertNetworkToElement(networkEntity)));
371 private static void convertComponent(SessionContext context,
372 CassandraElementRepository cassandraElementRepository) {
373 ComponentCassandraLoader componentCassandraLoader = new ComponentCassandraLoader();
374 Collection<ComponentEntity> components = componentCassandraLoader.list();
375 components.stream().filter(entity -> needMigration(entity.getVspId(), entity.getVersion
376 ())).forEach(componentEntity -> ElementHandler.save(context, cassandraElementRepository,
378 .getVspId(), componentEntity.getVersion(), ComponentConvertor
379 .convertComponentToElement(componentEntity)));
383 private static void convertServiceArtifact(SessionContext context,
384 CassandraElementRepository cassandraElementRepository) {
385 ServiceArtifactCassandraLoader serviceArtifactCassandraLoader =
386 new ServiceArtifactCassandraLoader();
387 Collection<ServiceArtifact> serviceArtifacts = serviceArtifactCassandraLoader.list();
388 serviceArtifacts.stream().filter(entity -> needMigration(entity.getVspId(), entity.getVersion
389 ())).forEach(serviceArtifact -> ElementHandler.save(context,
390 cassandraElementRepository,
392 .getVspId(), serviceArtifact.getVersion(),
393 VspServiceArtifactConvertor
394 .convertServiceArtifactToElement(serviceArtifact)));
398 private static void convertServiceTemplate(SessionContext context,
399 CassandraElementRepository cassandraElementRepository) {
400 ServiceTemplateCassandraLoader serviceTemplateCassandraLoader =
401 new ServiceTemplateCassandraLoader();
402 Collection<ServiceTemplate> serviceTemplates = serviceTemplateCassandraLoader.list();
403 serviceTemplates.stream().filter(entity -> needMigration(entity.getVspId(), entity.getVersion
404 ())).forEach(serviceTemplate -> ElementHandler.save(context,
405 cassandraElementRepository,
407 .getVspId(), serviceTemplate.getVersion(),
408 VspServiceTemplateConvertor
409 .convertServiceTemplateToElement(serviceTemplate)));
413 private static void convertLKG(SessionContext context,
414 CassandraElementRepository cassandraElementRepository) {
415 LKGCassandraLoader LKGCassandraLoader = new LKGCassandraLoader();
416 Collection<LicenseKeyGroupEntity> lkgs = LKGCassandraLoader.list();
417 lkgs.stream().filter(entity -> needMigration(entity.getVendorLicenseModelId(), entity.getVersion
419 .forEach(licenseKeyGroupEntity -> ElementHandler.save(context, cassandraElementRepository,
420 licenseKeyGroupEntity
421 .getVendorLicenseModelId(), licenseKeyGroupEntity.getVersion(),
422 LKGConvertor.convertLKGToElement
423 (licenseKeyGroupEntity)));
426 private static void convertEP(SessionContext context,
427 CassandraElementRepository cassandraElementRepository) {
428 EntitlementPoolCassandraLoader entitlementPoolCassandraLoader =
429 new EntitlementPoolCassandraLoader();
430 Collection<EntitlementPoolEntity> entitlementPools = entitlementPoolCassandraLoader.list();
431 entitlementPools.stream()
432 .filter(entity -> needMigration(entity.getVendorLicenseModelId(), entity.getVersion
434 .forEach(entitlementPoolEntity -> ElementHandler.save(context, cassandraElementRepository,
435 entitlementPoolEntity
436 .getVendorLicenseModelId(), entitlementPoolEntity.getVersion(),
437 EntitlementPoolConvertor.convertEntitlementPoolToElement(entitlementPoolEntity)));
440 private static void convertFeatureGroup(SessionContext context,
441 CassandraElementRepository cassandraElementRepository) {
442 FeatureGroupCassandraLoader featureGroupCassandraLoader = new FeatureGroupCassandraLoader();
443 Collection<FeatureGroupEntity> featureGroupEntities = featureGroupCassandraLoader.list();
444 featureGroupEntities.stream()
445 .filter(entity -> needMigration(entity.getVendorLicenseModelId(), entity.getVersion
447 .forEach(featureGroupEntity -> ElementHandler.save(context, cassandraElementRepository,
449 .getVendorLicenseModelId(), featureGroupEntity.getVersion(),
450 FeatureGroupConvertor.convertFeatureGroupToElement(featureGroupEntity)));
453 private static void convertLicenseAgreement(SessionContext context,
454 CassandraElementRepository cassandraElementRepository) {
455 LicenseAgreementCassandraLoader licenseAgreementCassandraLoader =
456 new LicenseAgreementCassandraLoader();
457 Collection<LicenseAgreementEntity> licenseAgreementEntities =
458 licenseAgreementCassandraLoader.list();
459 licenseAgreementEntities.stream()
460 .filter(entity -> needMigration(entity.getVendorLicenseModelId(), entity.getVersion
462 .forEach(licenseAgreementEntity -> ElementHandler.save(context, cassandraElementRepository,
463 licenseAgreementEntity
464 .getVendorLicenseModelId(), licenseAgreementEntity.getVersion(),
465 LicenseAgreementConvertor.convertLicenseAgreementToElement(licenseAgreementEntity)));
469 private static boolean needMigration(String itemId, Version versionId) {
471 VersionInfoEntity versionInfo =
472 MigrationMain.versionInfoMap.get(itemId);
473 if (versionInfo == null) {
474 printMessage(logger, "ItemId: " + itemId + " is not in version_info table.");
477 return (versionInfo.getCandidate() != null && versionId.equals(versionInfo.getCandidate()
480 .getViewableVersions() != null && versionInfo
481 .getViewableVersions().contains(versionId));