re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-license-lib / openecomp-sdc-vendor-license-core / src / main / java / org / openecomp / sdc / vendorlicense / dao / impl / zusammen / LicenseKeyGroupZusammenDaoImpl.java
1 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
2
3 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
4 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
5 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
6 import com.amdocs.zusammen.datatypes.Id;
7 import com.amdocs.zusammen.datatypes.SessionContext;
8 import com.amdocs.zusammen.datatypes.item.Action;
9 import com.amdocs.zusammen.datatypes.item.ElementContext;
10 import com.amdocs.zusammen.datatypes.item.Info;
11 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
12 import org.openecomp.sdc.datatypes.model.ElementType;
13 import org.openecomp.sdc.vendorlicense.dao.LicenseKeyGroupDao;
14 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToLicenseKeyGroupConvertor;
15 import org.openecomp.sdc.vendorlicense.dao.types.LicenseKeyGroupEntity;
16 import org.openecomp.types.ElementPropertyName;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Optional;
21 import java.util.stream.Collectors;
22
23 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
24
25
26 public class LicenseKeyGroupZusammenDaoImpl implements LicenseKeyGroupDao {
27   private ZusammenAdaptor zusammenAdaptor;
28
29   public LicenseKeyGroupZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
30     this.zusammenAdaptor = zusammenAdaptor;
31   }
32
33   @Override
34   public void registerVersioning(String versionableEntityType) {
35     //no need
36   }
37
38   @Override
39   public void create(LicenseKeyGroupEntity licenseKeyGroup) {
40     ZusammenElement licenseKeyGroupElement =
41         buildLicenseKeyGroupElement(licenseKeyGroup, Action.CREATE);
42
43     ZusammenElement limitsElement = buildStructuralElement(ElementType.Limits, Action.CREATE);
44     licenseKeyGroupElement.addSubElement(limitsElement);
45
46     ZusammenElement lkgsElement =
47         buildStructuralElement(ElementType.LicenseKeyGroups, Action.IGNORE);
48     lkgsElement.addSubElement(licenseKeyGroupElement);
49
50     SessionContext context = createSessionContext();
51     Element lkgsSavedElement = zusammenAdaptor.saveElement(context,
52         new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
53             licenseKeyGroup.getVersion().getId()), lkgsElement, "Create license Key Group");
54
55     licenseKeyGroup
56         .setId(lkgsSavedElement.getSubElements().iterator().next().getElementId().getValue());
57   }
58
59   @Override
60   public void update(LicenseKeyGroupEntity licenseKeyGroup) {
61     ZusammenElement licenseKeyGroupElement =
62         buildLicenseKeyGroupElement(licenseKeyGroup, Action.UPDATE);
63
64     SessionContext context = createSessionContext();
65     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
66         licenseKeyGroup.getVersion().getId());
67
68     Optional<ElementInfo> lkgFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
69         new Id(licenseKeyGroup.getId()));
70
71     if (lkgFromDb.isPresent()) {
72
73       if (licenseKeyGroupElement.getRelations() == null) {
74         licenseKeyGroupElement.setRelations(new ArrayList<>());
75       }
76
77       if (lkgFromDb.get().getRelations() != null && !lkgFromDb.get().getRelations().isEmpty()) {
78         licenseKeyGroupElement.getRelations().addAll(lkgFromDb.get().getRelations());
79       }
80     }
81     zusammenAdaptor.saveElement(context, elementContext, licenseKeyGroupElement,
82         String.format("Update license key group with id %s", licenseKeyGroup.getId()));
83   }
84
85   @Override
86   public LicenseKeyGroupEntity get(LicenseKeyGroupEntity licenseKeyGroup) {
87     SessionContext context = createSessionContext();
88     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
89         licenseKeyGroup.getVersion().getId());
90     ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
91     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()))
92         .map(elementInfo -> {
93           LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
94           entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
95           entity.setVersion(licenseKeyGroup.getVersion());
96           return entity;
97         })
98         .orElse(null);
99   }
100
101   @Override
102   public void delete(LicenseKeyGroupEntity licenseKeyGroup) {
103     ZusammenElement zusammenElement = buildElement(new Id(licenseKeyGroup.getId()), Action.DELETE);
104
105     SessionContext context = createSessionContext();
106     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
107         licenseKeyGroup.getVersion().getId());
108
109     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
110         "delete license key group. id:" + licenseKeyGroup.getId() + ".");
111   }
112
113   @Override
114   public Collection<LicenseKeyGroupEntity> list(LicenseKeyGroupEntity licenseKeyGroup) {
115     SessionContext context = createSessionContext();
116     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
117         licenseKeyGroup.getVersion().getId());
118     ElementToLicenseKeyGroupConvertor convertor = new ElementToLicenseKeyGroupConvertor();
119     return zusammenAdaptor
120         .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
121         .stream().map(elementInfo -> {
122           LicenseKeyGroupEntity entity = convertor.convert(elementInfo);
123           entity.setVendorLicenseModelId(licenseKeyGroup.getVendorLicenseModelId());
124           entity.setVersion(licenseKeyGroup.getVersion());
125           return entity;
126         })
127         .collect(Collectors.toList());
128   }
129
130   @Override
131   public long count(LicenseKeyGroupEntity licenseKeyGroup) {
132     SessionContext context = createSessionContext();
133     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
134         licenseKeyGroup.getVersion().getId());
135
136     return zusammenAdaptor
137         .listElementsByName(context, elementContext, null, ElementType.LicenseKeyGroups.name())
138         .size();
139   }
140
141   @Override
142   public void deleteAll(LicenseKeyGroupEntity licenseKeyGroup) {
143     //not supported
144   }
145
146   @Override
147   public void removeReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
148                                             String featureGroupId) {
149     SessionContext context = createSessionContext();
150     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
151         licenseKeyGroup.getVersion().getId());
152
153     Optional<ElementInfo> elementInfo =
154         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
155
156     if (elementInfo.isPresent()) {
157       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
158       zusammenElement.setAction(Action.UPDATE);
159       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
160           .filter(relation -> !featureGroupId
161               .equals(relation.getEdge2().getElementId().getValue()))
162           .collect(Collectors.toList()));
163
164       zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
165           "remove referencing feature group");
166     }
167   }
168
169   @Override
170   public void addReferencingFeatureGroup(LicenseKeyGroupEntity licenseKeyGroup,
171                                          String featureGroupId) {
172     SessionContext context = createSessionContext();
173     ElementContext elementContext = new ElementContext(licenseKeyGroup.getVendorLicenseModelId(),
174         licenseKeyGroup.getVersion().getId());
175
176     Optional<ElementInfo> elementInfo =
177         zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseKeyGroup.getId()));
178
179     if (elementInfo.isPresent()) {
180       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
181       zusammenElement.setAction(Action.UPDATE);
182       if (zusammenElement.getRelations() == null) {
183         zusammenElement.setRelations(new ArrayList<>());
184       }
185       zusammenElement.getRelations().add(VlmZusammenUtil
186           .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup,
187               featureGroupId));
188       zusammenAdaptor
189           .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
190     }
191   }
192
193   private ZusammenElement buildLicenseKeyGroupElement(LicenseKeyGroupEntity licenseKeyGroup,
194                                                       Action action) {
195     ZusammenElement lkgElement =
196         buildElement(licenseKeyGroup.getId() == null ? null : new Id(licenseKeyGroup.getId()),
197             action);
198     Info info = new Info();
199     info.setName(licenseKeyGroup.getName());
200     info.setDescription(licenseKeyGroup.getDescription());
201     info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseKeyGroup);
202     info.addProperty("version_uuid", licenseKeyGroup.getVersionUuId());
203     info.addProperty("LicenseKeyType", licenseKeyGroup.getType());
204     info.addProperty("operational_scope", licenseKeyGroup.getOperationalScope());
205     info.addProperty("startDate", licenseKeyGroup.getStartDate());
206     info.addProperty("expiryDate", licenseKeyGroup.getExpiryDate());
207     info.addProperty("thresholdValue", licenseKeyGroup.getThresholdValue());
208     info.addProperty("thresholdUnits", licenseKeyGroup.getThresholdUnits());
209     info.addProperty("increments", licenseKeyGroup.getIncrements());
210     info.addProperty("manufacturerReferenceNumber", licenseKeyGroup.getManufacturerReferenceNumber());
211     lkgElement.setInfo(info);
212
213     if (licenseKeyGroup.getReferencingFeatureGroups() != null
214         && !licenseKeyGroup.getReferencingFeatureGroups().isEmpty()) {
215       lkgElement.setRelations(licenseKeyGroup.getReferencingFeatureGroups().stream()
216           .map(rel -> VlmZusammenUtil
217               .createRelation(RelationType.LicenseKeyGroupToReferencingFeatureGroup, rel))
218           .collect(Collectors.toList()));
219     }
220     return lkgElement;
221   }
222
223
224 }