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