Add collaboration feature
[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  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util;
22
23 import org.apache.commons.collections4.MultiValuedMap;
24 import org.apache.commons.collections4.multimap.ArrayListValuedHashMap;
25 import org.openecomp.sdc.vendorlicense.HealingServiceFactory;
26 import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
27 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
28 import org.openecomp.sdc.vendorlicense.healing.HealingService;
29 import org.openecomp.sdc.vendorlicense.licenseartifacts.impl.VendorLicenseArtifactsServiceImpl;
30 import org.openecomp.sdc.versioning.ItemManagerFactory;
31 import org.openecomp.sdc.versioning.VersioningManager;
32 import org.openecomp.sdc.versioning.VersioningManagerFactory;
33 import org.openecomp.sdc.versioning.dao.types.Version;
34 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
35 import org.openecomp.sdc.versioning.dao.types.VersionableEntity;
36
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.HashSet;
40 import java.util.List;
41 import java.util.Set;
42 import java.util.stream.Collectors;
43
44 /**
45  * @author katyr
46  * @since January 10, 2017
47  */
48
49 public class VendorLicenseArtifactsServiceUtils {
50   private static final HealingService healingService =
51       HealingServiceFactory.getInstance().createInterface();
52
53   /**
54    * maps the entities by id
55    *
56    * @return a Map of id -> list of versionable entities with that id
57    */
58   private static MultiValuedMap<String, VersionableEntity> mapById(
59       Collection<? extends VersionableEntity> versionableEntities) {
60     MultiValuedMap<String, VersionableEntity> mappedById = new ArrayListValuedHashMap<>();
61     for (VersionableEntity ve : versionableEntities) {
62       mappedById.put(ve.getId(), ve);
63     }
64     return mappedById;
65   }
66
67   /**
68    * For all entities with same id, only entities that differ from one another will be returned.
69    * If no change has occured, the entity with the earlier VLM version will be returned.
70    * If only one version of said entities exists it will be returned
71    *
72    * @return a list of entities that has been changed
73    */
74   public static List<VersionableEntity> filterChangedEntities(
75       Collection<? extends VersionableEntity> versionableEntities) {
76     MultiValuedMap<String, VersionableEntity> entitiesById = mapById(
77         versionableEntities);
78     MultiValuedMap<String, VersionableEntity> entitiesByVersionUuId =
79         new ArrayListValuedHashMap<>();
80     List<VersionableEntity> changedOnly = new ArrayList<>();
81
82     for (String epId : entitiesById.keySet()) {
83       Collection<VersionableEntity> versionableEntitiesForId = entitiesById.get(epId);
84       for (VersionableEntity ep : versionableEntitiesForId) {
85         entitiesByVersionUuId.put(ep.getVersionUuId(), ep);
86       }
87     }
88
89     //for every list of eps which have the same uuid, get the one with the earliest vlm version.
90     for (String versionUid : entitiesByVersionUuId.keySet()) {
91       List<VersionableEntity> versionableEntitiesForUuid =
92           (List<VersionableEntity>) entitiesByVersionUuId.get(versionUid);
93       versionableEntitiesForUuid.sort(new VersionableEntitySortByVlmMajorVersion());
94       changedOnly.add(versionableEntitiesForUuid.get(0));
95     }
96
97     return changedOnly;
98   }
99
100   public static Set<LicenseKeyGroupEntity> healLkgs(
101       Collection<? extends VersionableEntity> licenseKeyGroupEntities) {
102     Set<LicenseKeyGroupEntity> healed = new HashSet<>();
103     for (VersionableEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
104       healed.add((LicenseKeyGroupEntity) VendorLicenseArtifactsServiceImpl.healingService
105           .heal(licenseKeyGroupEntity));
106     }
107
108     return healed;
109   }
110
111   public static Set<EntitlementPoolEntity> healEPs(
112       Collection<? extends VersionableEntity> entitlementPoolEntities) {
113     Set<EntitlementPoolEntity> healed = new HashSet<>();
114     for (VersionableEntity entitlementPoolEntity : entitlementPoolEntities) {
115       healed.add((EntitlementPoolEntity) VendorLicenseArtifactsServiceImpl.healingService
116           .heal(entitlementPoolEntity));
117     }
118
119     return healed;
120   }
121
122   public static List<Version> getFinalVersionsForVlm(String vlmId) {
123     VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface();
124     return versioningManager.list(vlmId).stream()
125         .filter(version -> VersionStatus.Certified == version.getStatus())
126         .map(certifiedVersion -> versioningManager.get(vlmId, certifiedVersion)) //sync to private
127         .collect(Collectors.toList());
128   }
129
130   public static String getVendorName(String vendorLicenseModelId) {
131     return ItemManagerFactory.getInstance().createInterface().get(vendorLicenseModelId).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 }