76635b31324229b0a1ddf41da46042f841b4a51a
[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.dao.types.VersionableEntity;
27
28 import java.util.UUID;
29
30 public class SimpleHealingServiceImpl implements HealingService {
31   private static final EntitlementPoolDao entitlementPoolDao =
32       EntitlementPoolDaoFactory.getInstance().createInterface();
33   private static final LicenseKeyGroupDao licenseKeyGroupDao =
34       LicenseKeyGroupDaoFactory.getInstance().createInterface();
35   @Override
36   public VersionableEntity heal(VersionableEntity toHeal) {
37     return handleMissingVersionId(toHeal);
38   }
39
40   @Override
41   public void persistNoHealing(VersionableEntity alreadyHealed) {
42     if (alreadyHealed instanceof EntitlementPoolEntity) {
43       entitlementPoolDao.update((EntitlementPoolEntity) alreadyHealed);
44     } else if (alreadyHealed instanceof LicenseKeyGroupEntity) {
45       licenseKeyGroupDao.update((LicenseKeyGroupEntity) alreadyHealed);
46     }
47   }
48
49   private VersionableEntity handleMissingVersionId(VersionableEntity toHeal) {
50     if (toHeal != null && toHeal.getVersionUuId() != null) {
51       return toHeal;
52     }
53
54     if (toHeal instanceof EntitlementPoolEntity) {
55       toHeal.setVersionUuId(UUID.randomUUID().toString());
56       entitlementPoolDao.update((EntitlementPoolEntity) toHeal);
57     } else if (toHeal instanceof LicenseKeyGroupEntity) {
58       toHeal.setVersionUuId(UUID.randomUUID().toString());
59       licenseKeyGroupDao.update((LicenseKeyGroupEntity) toHeal);
60     } else {
61       throw new UnsupportedOperationException(
62           "Unsupported operation for 1610 release/1607->1610 migration.");
63     }
64     return toHeal;
65   }
66 }