[SDC-29] Amdocs OnBoard 1707 initial commit.
[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.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   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 entitity exists it will be returned
68    * @param versionableEntities
69    * @return a list of entities that has been changed
70    */
71   public static List<VersionableEntity> filterChangedEntities(
72       Collection<? extends VersionableEntity> versionableEntities) {
73     MultiValuedMap<String, VersionableEntity> entitiesById = mapById(
74         versionableEntities);
75     MultiValuedMap<String, VersionableEntity> entitiesByVersionUuId =
76         new ArrayListValuedHashMap<>();
77     List<VersionableEntity> changedOnly = new ArrayList<>();
78
79     for (String epId : entitiesById.keySet()) {
80       Collection<VersionableEntity> versionableEntitiesForId = entitiesById.get(epId);
81       for (VersionableEntity ep : versionableEntitiesForId) {
82         entitiesByVersionUuId.put(ep.getVersionUuId(), ep);
83       }
84     }
85
86     //for every list of eps which have the same uuid, get the one with the earliest vlm version.
87     for (String versionUid : entitiesByVersionUuId.keySet()) {
88       List<VersionableEntity> versionableEntitiesForUuid =
89           (List<VersionableEntity>) entitiesByVersionUuId.get(versionUid);
90       versionableEntitiesForUuid.sort(new VersionableEntitySortByVlmMajorVersion());
91       changedOnly.add(versionableEntitiesForUuid.get(0));
92     }
93
94     return changedOnly;
95   }
96
97   public static Set<LicenseKeyGroupEntity> healLkgs(String user,
98                                                     Collection<? extends VersionableEntity> licenseKeyGroupEntities) {
99     Set<LicenseKeyGroupEntity> healed = new HashSet<>();
100     for (VersionableEntity licenseKeyGroupEntity : licenseKeyGroupEntities) {
101       healed.add((LicenseKeyGroupEntity) VendorLicenseArtifactsServiceImpl.healingService
102           .heal(licenseKeyGroupEntity, user));
103     }
104
105     return healed;
106   }
107
108   public static Set<EntitlementPoolEntity> healEPs(String user,
109                                                    Collection<? extends VersionableEntity> entitlementPoolEntities) {
110     Set<EntitlementPoolEntity> healed = new HashSet<>();
111     for (VersionableEntity entitlementPoolEntity : entitlementPoolEntities) {
112       healed.add((EntitlementPoolEntity) VendorLicenseArtifactsServiceImpl.healingService
113           .heal(entitlementPoolEntity, user));
114     }
115
116     return healed;
117   }
118
119   public static List<Version> getFinalVersionsForVlm(String vlmId) {
120     VersionInfo versionInfo =
121         VendorLicenseArtifactsServiceImpl.vendorLicenseFacade
122             .getVersionInfo(vlmId, VersionableEntityAction.Read, "");
123     return versionInfo.getFinalVersions();
124
125   }
126
127   public static String getVendorName(String vendorLicenseModelId, String user) {
128     return VendorLicenseArtifactsServiceImpl.vendorLicenseFacade
129         .getVendorLicenseModel(vendorLicenseModelId, null, user)
130         .getVendorLicenseModel().getVendorName();
131   }
132 }