cc2f8acb9740260806673c493dc2d20ce3995211
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorlicense.healing.impl;
18
19 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
20 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
21 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
22 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
23 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
24 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
25 import org.openecomp.sdc.vendorlicense.healing.HealingService;
26 import org.openecomp.sdc.versioning.VersioningManager;
27 import org.openecomp.sdc.versioning.VersioningManagerFactory;
28 import org.openecomp.sdc.versioning.dao.types.VersionableEntity;
29
30 import java.util.UUID;
31
32 public class SimpleHealingServiceImpl implements HealingService {
33   private static final EntitlementPoolDao entitlementPoolDao =
34       EntitlementPoolDaoFactory.getInstance().createInterface();
35   private static final LicenseKeyGroupDao licenseKeyGroupDao =
36       LicenseKeyGroupDaoFactory.getInstance().createInterface();
37   private static final VersioningManager VERSIONING_MANAGER =
38       VersioningManagerFactory.getInstance().createInterface();
39
40   @Override
41   public VersionableEntity heal(VersionableEntity toHeal) {
42     return handleMissingVersionId(toHeal);
43   }
44
45   @Override
46   public void persistNoHealing(VersionableEntity alreadyHealed) {
47     if (alreadyHealed instanceof EntitlementPoolEntity) {
48       entitlementPoolDao.update((EntitlementPoolEntity) alreadyHealed);
49     } else if (alreadyHealed instanceof LicenseKeyGroupEntity) {
50       licenseKeyGroupDao.update((LicenseKeyGroupEntity) alreadyHealed);
51     }
52   }
53
54   private VersionableEntity handleMissingVersionId(VersionableEntity toHeal) {
55     if (toHeal != null && toHeal.getVersionUuId() != null) {
56       return toHeal;
57     }
58
59     if (toHeal instanceof EntitlementPoolEntity) {
60       toHeal.setVersionUuId(UUID.randomUUID().toString());
61       entitlementPoolDao.update((EntitlementPoolEntity) toHeal);
62     } else if (toHeal instanceof LicenseKeyGroupEntity) {
63       toHeal.setVersionUuId(UUID.randomUUID().toString());
64       licenseKeyGroupDao.update((LicenseKeyGroupEntity) toHeal);
65     } else {
66       throw new UnsupportedOperationException(
67           "Unsupported operation for 1610 release/1607->1610 migration.");
68     }
69
70     VERSIONING_MANAGER.publish(toHeal.getFirstClassCitizenId(), toHeal.getVersion(), "Add missing version_uuid on ep/lkg");
71
72     return toHeal;
73   }
74 }