0416cc94793e0431c7505fcaa3093cd2f2b1cf95
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 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.healing.healers;
18
19 import org.openecomp.sdc.healing.interfaces.Healer;
20 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDao;
21 import org.openecomp.sdc.vendorlicense.dao.FeatureGroupDaoFactory;
22 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
23 import org.openecomp.sdc.vendorlicense.dao.types.FeatureGroupEntity;
24 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
25 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacade;
26 import org.openecomp.sdc.vendorlicense.facade.VendorLicenseFacadeFactory;
27 import org.openecomp.sdc.versioning.dao.types.Version;
28
29 import java.util.Collection;
30 import java.util.Objects;
31 import java.util.Set;
32
33 public class ManufacturerReferenceNumberHealer implements Healer {
34
35   private static final String MANUFACTURER_REFERENCE_NUMBER = "MRN";
36   private final VendorLicenseFacade vendorLicenseFacade;
37   private final FeatureGroupDao featureGroupDao;
38
39   public ManufacturerReferenceNumberHealer() {
40     this(VendorLicenseFacadeFactory.getInstance().createInterface(), FeatureGroupDaoFactory
41         .getInstance().createInterface());
42   }
43
44   public ManufacturerReferenceNumberHealer(VendorLicenseFacade vendorLicenseFacade,
45                                            FeatureGroupDao featureGroupDao) {
46     this.vendorLicenseFacade = vendorLicenseFacade;
47     this.featureGroupDao = featureGroupDao;
48   }
49
50   @Override
51   public boolean isHealingNeeded(String itemId, Version version) {
52     return Objects.isNull(vendorLicenseFacade.listEntitlementPools
53         (itemId, version).iterator().next().getManufacturerReferenceNumber()) ? true :
54         false;
55   }
56
57   @Override
58   public void heal(String itemId, Version version) throws Exception {
59
60     healEntitlementPools(itemId, version);
61     healLicenseKeyGroups(itemId, version);
62     healFeatureGroups(itemId, version);
63   }
64
65   private void healEntitlementPools(String itemId, Version version) {
66     Collection<EntitlementPoolEntity> entitlementPoolEntities = vendorLicenseFacade
67         .listEntitlementPools(itemId, version);
68
69     for (EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities) {
70       Set<String> referencingFeatureGroup = entitlementPoolEntity.getReferencingFeatureGroups();
71
72       if (referencingFeatureGroup.size() == 1) {
73         entitlementPoolEntity.setManufacturerReferenceNumber(getMRN(itemId, version,
74             referencingFeatureGroup));
75       } else {
76         entitlementPoolEntity.setManufacturerReferenceNumber(MANUFACTURER_REFERENCE_NUMBER);
77       }
78       vendorLicenseFacade.updateEntitlementPool(entitlementPoolEntity);
79     }
80   }
81
82   private void healLicenseKeyGroups(String itemId, Version version) {
83     Collection<LicenseKeyGroupEntity> licenseKeyGroupEntities = vendorLicenseFacade
84         .listLicenseKeyGroups(itemId, version);
85
86     for (LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
87       Set<String> referencingFeatureGroup = licenseKeyGroupEntity.getReferencingFeatureGroups();
88       if (referencingFeatureGroup.size() == 1) {
89         licenseKeyGroupEntity.setManufacturerReferenceNumber(getMRN(itemId, version,
90             referencingFeatureGroup));
91       } else {
92         licenseKeyGroupEntity.setManufacturerReferenceNumber(MANUFACTURER_REFERENCE_NUMBER);
93       }
94       vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroupEntity);
95     }
96   }
97
98   private String getMRN(String itemId, Version
99       version, Set<String> referencingFeatureGroup) {
100     FeatureGroupEntity featureGroupEntity = vendorLicenseFacade.getFeatureGroup(new
101         FeatureGroupEntity(itemId, version, referencingFeatureGroup.iterator().next()));
102     return featureGroupEntity.getManufacturerReferenceNumber();
103   }
104
105   private void healFeatureGroups(String itemId, Version version) {
106
107     Collection<FeatureGroupEntity> featureGroupEntities = vendorLicenseFacade.listFeatureGroups
108         (itemId, version);
109
110     for (FeatureGroupEntity featureGroupEntity : featureGroupEntities) {
111       featureGroupEntity.setManufacturerReferenceNumber(null);
112       featureGroupDao.update(featureGroupEntity);
113     }
114   }
115 }