re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-core / src / main / java / org / openecomp / sdc / vendorlicense / licenseartifacts / impl / util / VendorLicenseArtifactsServiceUtils.java
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.vendorlicense.licenseartifacts.impl.util;
18
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.stream.Collectors;
26
27 import org.apache.commons.collections4.MultiValuedMap;
28 import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
29 import org.openecomp.sdc.vendorlicense.HealingServiceFactory;
30 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
31 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
32 import org.openecomp.sdc.vendorlicense.healing.HealingService;
33 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.VendorLicenseArtifactsServiceImpl;
34 import org.openecomp.sdc.versioning.AsdcItemManagerFactory;
35 import org.openecomp.sdc.versioning.VersioningManager;
36 import org.openecomp.sdc.versioning.VersioningManagerFactory;
37 import org.openecomp.sdc.versioning.dao.types.Version;
38 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
39 import org.openecomp.sdc.versioning.dao.types.VersionableEntity;
40
41
42
43 /**
44  * @author katyr
45  * @since January 10, 2017
46  */
47
48 public class VendorLicenseArtifactsServiceUtils {
49   private static final HealingService healingService =
50       HealingServiceFactory.getInstance().createInterface();
51
52   /**
53    * maps the entities by id
54    *
55    * @return a Map of id -> list of versionable entities with that id
56    */
57   private static MultiValuedMap<String, VersionableEntity> mapById(
58       Collection<? extends VersionableEntity> versionableEntities) {
59     MultiValuedMap<String, VersionableEntity> mappedById = new ArrayListValuedHashMap<>();
60     for (VersionableEntity ve : versionableEntities) {
61       mappedById.put(ve.getId(), ve);
62     }
63     return mappedById;
64   }
65
66   /**
67    * For all entities with same id, only entities that differ from one another will be returned.
68    * If no change has occured, the entity with the earlier VLM version will be returned.
69    * If only one version of said entities exists it will be returned
70    *
71    * @return a list of entities that has been changed
72    */
73   public static List<VersionableEntity> filterChangedEntities(
74       Collection<? extends VersionableEntity> versionableEntities) {
75     MultiValuedMap<String, VersionableEntity> entitiesById = mapById(
76         versionableEntities);
77     MultiValuedMap<String, VersionableEntity> entitiesByVersionUuId =
78         new ArrayListValuedHashMap<>();
79     List<VersionableEntity> changedOnly = new ArrayList<>();
80
81     for (String epId : entitiesById.keySet()) {
82       Collection<VersionableEntity> versionableEntitiesForId = entitiesById.get(epId);
83       for (VersionableEntity ep : versionableEntitiesForId) {
84         entitiesByVersionUuId.put(ep.getVersionUuId(), ep);
85       }
86     }
87
88     //for every list of eps which have the same uuid, get the one with the earliest vlm version.
89     for (String versionUid : entitiesByVersionUuId.keySet()) {
90       List<VersionableEntity> versionableEntitiesForUuid =
91           (List<VersionableEntity>) entitiesByVersionUuId.get(versionUid);
92       versionableEntitiesForUuid.sort(new VersionableEntitySortByVlmMajorVersion());
93       changedOnly.add(versionableEntitiesForUuid.get(0));
94     }
95
96     return changedOnly;
97   }
98
99   public static Set<LicenseKeyGroupEntity> healLkgs(
100       Collection<? extends VersionableEntity> licenseKeyGroupEntities) {
101     Set<LicenseKeyGroupEntity> healed = new HashSet<>();
102     for (VersionableEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
103       healed.add((LicenseKeyGroupEntity) VendorLicenseArtifactsServiceImpl.healingService
104           .heal(licenseKeyGroupEntity));
105     }
106
107     return healed;
108   }
109
110   public static Set<EntitlementPoolEntity> healEPs(
111       Collection<? extends VersionableEntity> entitlementPoolEntities) {
112     Set<EntitlementPoolEntity> healed = new HashSet<>();
113     for (VersionableEntity entitlementPoolEntity : entitlementPoolEntities) {
114       healed.add((EntitlementPoolEntity) VendorLicenseArtifactsServiceImpl.healingService
115           .heal(entitlementPoolEntity));
116     }
117
118     return healed;
119   }
120
121   public static List<Version> getFinalVersionsForVlm(String vlmId) {
122     VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface();
123     return versioningManager.list(vlmId).stream()
124         .filter(version -> VersionStatus.Certified == version.getStatus())
125         .map(certifiedVersion -> versioningManager.get(vlmId, certifiedVersion)) //sync to private
126         .collect(Collectors.toList());
127   }
128
129   public static String getVendorName(String vendorLicenseModelId) {
130     return AsdcItemManagerFactory.getInstance().createInterface().get(vendorLicenseModelId)
131         .getName();
132   }
133
134
135   /**
136    * Written to handle the consequences of ATTASDC-4780 where version_uuid was not saved or
137    * retrieved correctly by DAO for EPs and LKGs. Performs a healing of sorts according to the
138    * following : 1. all versions of a specific entity (EP or LKG that have the same invariant_uuid)
139    * are ordered by their VLM version 2. first element is sent to healing (which will set a
140    * versionUUID for it IF it doesnt exist) 3. each subsequent element is compared to previous . If
141    * same, UUID is copied from the previous element , if they differ - the current element is sent
142    * to healing as before. For VLMs created post-bugfix this code should not update any element
143    */
144   public static Collection<? extends VersionableEntity> prepareForFiltering(Collection<? extends
145       VersionableEntity> versionableEntities, boolean isEP) {
146     MultiValuedMap<String, VersionableEntity> entitiesById = mapById(
147         versionableEntities);
148
149     for (String epId : entitiesById.keySet()) {
150       List<VersionableEntity> versionableEntitiesForId = new ArrayList<>();
151       versionableEntitiesForId.addAll(entitiesById.get(epId));
152       versionableEntitiesForId.sort(new VersionableEntitySortByVlmMajorVersion());
153       healingService.heal(versionableEntitiesForId.get(0));
154       for (int i = 1; i < versionableEntitiesForId.size(); i++) {
155         if (isEP) {
156           EntitlementPoolEntity current = (EntitlementPoolEntity) versionableEntitiesForId.get(i);
157           EntitlementPoolEntity previous = (EntitlementPoolEntity) versionableEntitiesForId
158               .get(i - 1);
159           if (current.equals(previous) && current.getVersionUuId() == null) {
160             current.setVersionUuId(previous.getVersionUuId());
161             healingService.persistNoHealing(current);
162           } else {
163             versionableEntitiesForId.set(i, healingService.heal(versionableEntitiesForId.get(i)));
164           }
165
166         } else {
167           LicenseKeyGroupEntity current = (LicenseKeyGroupEntity) versionableEntitiesForId.get(i);
168           LicenseKeyGroupEntity previous = (LicenseKeyGroupEntity) versionableEntitiesForId
169               .get(i - 1);
170           if (current.equals(previous) && current.getVersionUuId() == null) {
171             current.setVersionUuId(previous.getVersionUuId());
172             healingService.persistNoHealing(current);
173           } else {
174             versionableEntitiesForId.set(i, healingService.heal(versionableEntitiesForId.get(i)));
175           }
176
177
178         }
179       }
180     }
181     return versionableEntities;
182   }
183
184
185 }