237e4bf55bc130213243457e87c82ece2e6580d5
[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 manufacturerReferenceNumber = "MRN";
36   private VendorLicenseFacade vendorLicenseFacade = VendorLicenseFacadeFactory.getInstance()
37       .createInterface();
38   private static final FeatureGroupDao featureGroupDao =
39       FeatureGroupDaoFactory.getInstance().createInterface();
40
41   @Override
42   public boolean isHealingNeeded(String itemId, Version version) {
43     return Objects.isNull(vendorLicenseFacade.listEntitlementPools
44         (itemId, version).iterator().next().getManufacturerReferenceNumber()) ? true :
45         false;
46   }
47
48   @Override
49   public void heal(String itemId, Version version) throws Exception {
50
51     healEntitlementPools(itemId, version);
52     healLicenseKeyGroups(itemId, version);
53     healFeatureGroups(itemId, version);
54   }
55
56   private void healEntitlementPools(String itemId, Version version) {
57     Collection<EntitlementPoolEntity> entitlementPoolEntities = vendorLicenseFacade
58         .listEntitlementPools(itemId, version);
59
60     for (EntitlementPoolEntity entitlementPoolEntity : entitlementPoolEntities) {
61       Set<String> referencingFeatureGroup = entitlementPoolEntity.getReferencingFeatureGroups();
62
63       if (referencingFeatureGroup.size() == 1) {
64         entitlementPoolEntity.setManufacturerReferenceNumber(getMRN(itemId, version,
65             referencingFeatureGroup));
66       } else {
67         entitlementPoolEntity.setManufacturerReferenceNumber(manufacturerReferenceNumber);
68       }
69       vendorLicenseFacade.updateEntitlementPool(entitlementPoolEntity);
70     }
71   }
72
73   private void healLicenseKeyGroups(String itemId, Version version) {
74     Collection<LicenseKeyGroupEntity> licenseKeyGroupEntities = vendorLicenseFacade
75         .listLicenseKeyGroups(itemId, version);
76
77     for (LicenseKeyGroupEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
78       Set<String> referencingFeatureGroup = licenseKeyGroupEntity.getReferencingFeatureGroups();
79       if (referencingFeatureGroup.size() == 1) {
80         licenseKeyGroupEntity.setManufacturerReferenceNumber(getMRN(itemId, version,
81             referencingFeatureGroup));
82       } else {
83         licenseKeyGroupEntity.setManufacturerReferenceNumber(manufacturerReferenceNumber);
84       }
85       vendorLicenseFacade.updateLicenseKeyGroup(licenseKeyGroupEntity);
86     }
87   }
88
89   private String getMRN(String itemId, Version
90       version, Set<String> referencingFeatureGroup) {
91     FeatureGroupEntity featureGroupEntity = vendorLicenseFacade.getFeatureGroup(new
92         FeatureGroupEntity(itemId, version, referencingFeatureGroup.iterator().next()));
93     return featureGroupEntity.getManufacturerReferenceNumber();
94   }
95
96   private void healFeatureGroups(String itemId, Version version) {
97
98     Collection<FeatureGroupEntity> featureGroupEntities = vendorLicenseFacade.listFeatureGroups
99         (itemId, version);
100
101     for (FeatureGroupEntity featureGroupEntity : featureGroupEntities) {
102       featureGroupEntity.setManufacturerReferenceNumber("");
103       featureGroupDao.update(featureGroupEntity);
104     }
105   }
106 }