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