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