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