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