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