summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-healing-lib/openecomp-sdc-healing-impl/src/main/java/org/openecomp/sdc/healing/healers/ManufacturerReferenceNumberHealer.java
blob: e371463ef3b41e69adb440b148f019ba630ba265 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openecomp.sdc.healing.healers;

import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import org.openecomp.sdc.healing.interfaces.Healer;
import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
import org.openecomp.sdc.versioning.dao.types.Version;

public class ManufacturerReferenceNumberHealer implements Healer {

    private static final String MANUFACTURER_REFERENCE_NUMBER = "MRN";
    private final VendorLicenseFacade vendorLicenseFacade;
    private final FeatureGroupDao featureGroupDao;

    public ManufacturerReferenceNumberHealer() {
        this(VendorLicenseFacadeFactory.getInstance().createInterface(), FeatureGroupDaoFactory.getInstance().createInterface());
    }

    public ManufacturerReferenceNumberHealer(VendorLicenseFacade vendorLicenseFacade, FeatureGroupDao featureGroupDao) {
        this.vendorLicenseFacade = vendorLicenseFacade;
        this.featureGroupDao = featureGroupDao;
    }

    @Override
    public boolean isHealingNeeded(String itemId, Version version) {
        return Objects.isNull(vendorLicenseFacade.listEntitlementPools(itemId, version).iterator().next().getManufacturerReferenceNumber()) ? true
            : false;
    }

    @Override
    public void heal(String itemId, Version version) throws Exception {
        healEntitlementPools(itemId, version);
        healLicenseKeyGroups(itemId, version);
        healFeatureGroups(itemId, version);
    }

    private void healEntitlementPools(String itemId, Version version) {
        Collection<EntitlementPoolEntity> entitlementPoolEntities = vendorLicenseFacade.listEntitlementPools(itemId, version);
        for (EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities) {
            Set<String> referencingFeatureGroup = entitlementPoolEntity.getReferencingFeatureGroups();
            if (referencingFeatureGroup.size() == 1) {
                entitlementPoolEntity.setManufacturerReferenceNumber(getMRN(itemId, version, referencingFeatureGroup));
            } else {
                entitlementPoolEntity.setManufacturerReferenceNumber(MANUFACTURER_REFERENCE_NUMBER);
            }
            vendorLicenseFacade.updateEntitlementPool(entitlementPoolEntity);
        }
    }

    private void healLicenseKeyGroups(String itemId, Version version) {
        Collection<LicenseKeyGroupEntity> licenseKeyGroupEntities = vendorLicenseFacade.listLicenseKeyGroups(itemId, version);
        for (LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
            Set<String> referencingFeatureGroup = licenseKeyGroupEntity.getReferencingFeatureGroups();
            if (referencingFeatureGroup.size() == 1) {
                licenseKeyGroupEntity.setManufacturerReferenceNumber(getMRN(itemId, version, referencingFeatureGroup));
            } else {
                licenseKeyGroupEntity.setManufacturerReferenceNumber(MANUFACTURER_REFERENCE_NUMBER);
            }
            vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroupEntity);
        }
    }

    private String getMRN(String itemId, Version version, Set<String> referencingFeatureGroup) {
        FeatureGroupEntity featureGroupEntity = vendorLicenseFacade
            .getFeatureGroup(new FeatureGroupEntity(itemId, version, referencingFeatureGroup.iterator().next()));
        return featureGroupEntity.getManufacturerReferenceNumber();
    }

    private void healFeatureGroups(String itemId, Version version) {
        Collection<FeatureGroupEntity> featureGroupEntities = vendorLicenseFacade.listFeatureGroups(itemId, version);
        for (FeatureGroupEntity featureGroupEntity : featureGroupEntities) {
            featureGroupEntity.setManufacturerReferenceNumber(null);
            featureGroupDao.update(featureGroupEntity);
        }
    }
}