[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-core / src / main / java / org / openecomp / sdc / vendorlicense / healing / impl / SimpleHealingServiceImpl.java
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.common.utils.CommonUtil;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
26 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
27 import org.openecomp.sdc.logging.types.LoggerConstants;
28 import org.openecomp.sdc.logging.types.LoggerErrorCode;
29 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
30 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
31 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
32 import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDaoFactory;
33 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
34 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDaoFactory;
35 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
36 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
37 import org.openecomp.sdc.vendorlicense.healing.HealingService;
38 import org.openecomp.sdc.versioning.dao.types.VersionableEntity;
39
40 import java.util.UUID;
41
42 public class SimpleHealingServiceImpl implements HealingService {
43   private static final EntitlementPoolDao entitlementPoolDao =
44       EntitlementPoolDaoFactory.getInstance().createInterface();
45   private static final LicenseKeyGroupDao licenseKeyGroupDao =
46       LicenseKeyGroupDaoFactory.getInstance().createInterface();
47   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
48
49   @Override
50   public VersionableEntity heal(VersionableEntity toHeal, String user) {
51     return handleMissingVersionId(toHeal, user);
52   }
53
54   private VersionableEntity handleMissingVersionId(VersionableEntity toHeal, String user) {
55
56
57     mdcDataDebugMessage.debugEntryMessage(null, null);
58
59     if (toHeal != null && toHeal.getVersionUuId() != null) {
60       return toHeal;
61     }
62
63     if (toHeal instanceof EntitlementPoolEntity) {
64       toHeal.setVersionUuId(UUID.randomUUID().toString());
65       entitlementPoolDao.update((EntitlementPoolEntity) toHeal);
66     } else if (toHeal instanceof LicenseKeyGroupEntity) {
67       toHeal.setVersionUuId(UUID.randomUUID().toString());
68       licenseKeyGroupDao.update((LicenseKeyGroupEntity) toHeal);
69     } else {
70       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
71           LoggerTragetServiceName.SELF_HEALING, ErrorLevel.ERROR.name(),
72           LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.UNSUPPORTED_OPERATION);
73       throw new UnsupportedOperationException(
74           "Unsupported operation for 1610 release/1607->1610 migration.");
75       //todo maybe errorbuilder?
76     }
77
78     mdcDataDebugMessage.debugExitMessage(null, null);
79     return toHeal;
80   }
81 }