2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorlicense.licenseartifacts.impl.util;
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;
37 import java.util.ArrayList;
38 import java.util.Collection;
39 import java.util.HashSet;
40 import java.util.List;
42 import java.util.stream.Collectors;
46 * @since January 10, 2017
49 public class VendorLicenseArtifactsServiceUtils {
50 private static final HealingService healingService =
51 HealingServiceFactory.getInstance().createInterface();
54 * maps the entities by id
56 * @return a Map of id -> list of versionable entities with that id
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);
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
72 * @return a list of entities that has been changed
74 public static List<VersionableEntity> filterChangedEntities(
75 Collection<? extends VersionableEntity> versionableEntities) {
76 MultiValuedMap<String, VersionableEntity> entitiesById = mapById(
78 MultiValuedMap<String, VersionableEntity> entitiesByVersionUuId =
79 new ArrayListValuedHashMap<>();
80 List<VersionableEntity> changedOnly = new ArrayList<>();
82 for (String epId : entitiesById.keySet()) {
83 Collection<VersionableEntity> versionableEntitiesForId = entitiesById.get(epId);
84 for (VersionableEntity ep : versionableEntitiesForId) {
85 entitiesByVersionUuId.put(ep.getVersionUuId(), ep);
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));
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));
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));
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());
130 public static String getVendorName(String vendorLicenseModelId) {
131 return ItemManagerFactory.getInstance().createInterface().get(vendorLicenseModelId).getName();
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
144 public static Collection<? extends VersionableEntity> prepareForFiltering(Collection<? extends
145 VersionableEntity> versionableEntities, boolean isEP) {
146 MultiValuedMap<String, VersionableEntity> entitiesById = mapById(
147 versionableEntities);
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++) {
156 EntitlementPoolEntity current = (EntitlementPoolEntity) versionableEntitiesForId.get(i);
157 EntitlementPoolEntity previous = (EntitlementPoolEntity) versionableEntitiesForId
159 if (current.equals(previous) && current.getVersionUuId() == null) {
160 current.setVersionUuId(previous.getVersionUuId());
161 healingService.persistNoHealing(current);
163 versionableEntitiesForId.set(i, healingService.heal(versionableEntitiesForId.get(i)));
167 LicenseKeyGroupEntity current = (LicenseKeyGroupEntity) versionableEntitiesForId.get(i);
168 LicenseKeyGroupEntity previous = (LicenseKeyGroupEntity) versionableEntitiesForId
170 if (current.equals(previous) && current.getVersionUuId() == null) {
171 current.setVersionUuId(previous.getVersionUuId());
172 healingService.persistNoHealing(current);
174 versionableEntitiesForId.set(i, healingService.heal(versionableEntitiesForId.get(i)));
181 return versionableEntities;