d30931c8c761c610555b7bc1f20f7851c73bc7ba
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;
18
19 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
20 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
21 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
22 import com.amdocs.zusammen.datatypes.Id;
23 import com.amdocs.zusammen.datatypes.SessionContext;
24 import com.amdocs.zusammen.datatypes.item.Action;
25 import com.amdocs.zusammen.datatypes.item.ElementContext;
26 import com.amdocs.zusammen.datatypes.item.Info;
27 import lombok.AllArgsConstructor;
28 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
29 import org.openecomp.sdc.datatypes.model.ElementType;
30 import org.openecomp.sdc.vendorlicense.dao.LicenseAgreementDao;
31 import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToLicenseAgreementConvertor;
32 import org.openecomp.sdc.vendorlicense.dao.types.LicenseAgreementEntity;
33 import org.openecomp.types.ElementPropertyName;
34
35 import java.util.Collection;
36 import java.util.Optional;
37 import java.util.Set;
38 import java.util.stream.Collectors;
39
40 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
41
42 @AllArgsConstructor
43 public class LicenseAgreementDaoZusammenImpl implements LicenseAgreementDao {
44
45   private ZusammenAdaptor zusammenAdaptor;
46
47   @Override
48   public void registerVersioning(String versionableEntityType) {
49     //no need
50   }
51
52   @Override
53   public void create(LicenseAgreementEntity licenseAgreement) {
54     ZusammenElement licenseAgreementElement =
55             buildLicenseAgreementElement(licenseAgreement, Action.CREATE);
56     ZusammenElement licenseAgreementsElement =
57             buildStructuralElement(ElementType.LicenseAgreements, Action.IGNORE);
58     licenseAgreementsElement.addSubElement(licenseAgreementElement);
59
60     SessionContext context = createSessionContext();
61     Element licenseAgreementsSavedElement = zusammenAdaptor.saveElement(context,
62             new ElementContext(licenseAgreement.getVendorLicenseModelId(),
63                     licenseAgreement.getVersion().getId()), licenseAgreementsElement,
64             "Create license agreement");
65     licenseAgreement
66             .setId(licenseAgreementsSavedElement.getSubElements().iterator().next().getElementId()
67                     .getValue());
68   }
69
70   @Override
71   public void update(LicenseAgreementEntity licenseAgreement) {
72     ZusammenElement licenseAgreementElement =
73             buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
74
75     SessionContext context = createSessionContext();
76     zusammenAdaptor.saveElement(context,
77             new ElementContext(licenseAgreement.getVendorLicenseModelId(),
78                     licenseAgreement.getVersion().getId()), licenseAgreementElement,
79             String.format("Update license agreement with id %s", licenseAgreement.getId()));
80   }
81
82   @Override
83   public LicenseAgreementEntity get(LicenseAgreementEntity licenseAgreement) {
84     SessionContext context = createSessionContext();
85     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
86             licenseAgreement.getVersion().getId());
87     ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
88     return zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()))
89             .map(elementInfo -> {
90               LicenseAgreementEntity entity = convertor.convert(elementInfo);
91               entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
92               entity.setVersion(licenseAgreement.getVersion());
93               return entity;
94             })
95             .orElse(null);
96   }
97
98   @Override
99   public void delete(LicenseAgreementEntity licenseAgreement) {
100     ZusammenElement zusammenElement = buildElement(new Id(licenseAgreement.getId()), Action.DELETE);
101
102     SessionContext context = createSessionContext();
103     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
104             licenseAgreement.getVersion().getId());
105     zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
106             "delete license agreement. id:" + licenseAgreement.getId() + ".");
107   }
108
109
110   @Override
111   public Collection<LicenseAgreementEntity> list(LicenseAgreementEntity licenseAgreement) {
112     SessionContext context = createSessionContext();
113     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
114             licenseAgreement.getVersion().getId());
115     ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
116     return zusammenAdaptor
117             .listElementsByName(context, elementContext, null,
118                     ElementType.LicenseAgreements.name())
119             .stream().map(elementInfo -> {
120               LicenseAgreementEntity entity = convertor.convert(elementInfo);
121               entity.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
122               entity.setVersion(licenseAgreement.getVersion());
123               return entity;
124             })
125             .collect(Collectors.toList());
126   }
127
128   @Override
129   public long count(LicenseAgreementEntity licenseAgreement) {
130     SessionContext context = createSessionContext();
131     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
132             licenseAgreement.getVersion().getId());
133
134     return zusammenAdaptor.listElementsByName(context, elementContext, null,
135             ElementType.LicenseAgreements.name())
136             .size();
137   }
138
139   @Override
140   public void deleteAll(LicenseAgreementEntity entity) {
141     //not supported
142   }
143
144   @Override
145   public void removeFeatureGroup(LicenseAgreementEntity licenseAgreement, String featureGroupId) {
146     SessionContext context = createSessionContext();
147     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
148             licenseAgreement.getVersion().getId());
149
150     Optional<ElementInfo> elementInfo = zusammenAdaptor.getElementInfo(context,
151             elementContext, new Id(licenseAgreement.getId()));
152     if (elementInfo.isPresent()) {
153       ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
154       zusammenElement.setAction(Action.UPDATE);
155       zusammenElement.setRelations(elementInfo.get().getRelations().stream()
156               .filter(relation -> !featureGroupId.equals(relation.getEdge2().getElementId().getValue()))
157               .collect(Collectors.toList()));
158       zusammenAdaptor.saveElement(context, elementContext, zusammenElement, "remove feature group");
159     }
160   }
161
162   @Override
163   public void updateColumnsAndDeltaFeatureGroupIds(LicenseAgreementEntity licenseAgreement,
164                                                    Set<String> addedFeatureGroupIds,
165                                                    Set<String> removedFeatureGroupIds) {
166     ZusammenElement licenseAgreementElement =
167             buildLicenseAgreementElement(licenseAgreement, Action.UPDATE);
168
169     SessionContext context = createSessionContext();
170     ElementContext elementContext = new ElementContext(licenseAgreement.getVendorLicenseModelId(),
171             licenseAgreement.getVersion().getId());
172     ElementToLicenseAgreementConvertor convertor = new ElementToLicenseAgreementConvertor();
173     Optional<ElementInfo> elementInfo =
174             zusammenAdaptor.getElementInfo(context, elementContext, new Id(licenseAgreement.getId()));
175     if (elementInfo.isPresent()) {
176       LicenseAgreementEntity currentLicenseAgreement =
177               convertor.convert(elementInfo.get());
178       currentLicenseAgreement.setVendorLicenseModelId(licenseAgreement.getVendorLicenseModelId());
179       currentLicenseAgreement.setVersion(licenseAgreement.getVersion());
180       if (!(removedFeatureGroupIds == null)) {
181         currentLicenseAgreement.getFeatureGroupIds().removeAll(removedFeatureGroupIds);
182       }
183
184       if (!(addedFeatureGroupIds == null)) {
185         currentLicenseAgreement.getFeatureGroupIds().addAll(addedFeatureGroupIds);
186       }
187       licenseAgreementElement.setRelations(currentLicenseAgreement.getFeatureGroupIds().stream()
188               .map(relation -> VlmZusammenUtil
189                       .createRelation(RelationType.LicenseAgreementToFeatureGroup, relation))
190               .collect(Collectors.toList()));
191       zusammenAdaptor.saveElement(context, elementContext, licenseAgreementElement,
192               "update license agreement");
193     }
194   }
195
196   private ZusammenElement buildLicenseAgreementElement(LicenseAgreementEntity licenseAgreement,
197                                                        Action action) {
198     ZusammenElement licenseAgreementElement =
199             buildElement(licenseAgreement.getId() == null ? null : new Id(licenseAgreement.getId()),
200                     action);
201     Info info = new Info();
202     info.setName(licenseAgreement.getName());
203     info.setDescription(licenseAgreement.getDescription());
204     info.addProperty(ElementPropertyName.elementType.name(), ElementType.LicenseAgreement);
205     info.addProperty("licenseTerm", licenseAgreement.getLicenseTerm());
206     info.addProperty("requirementsAndConstrains", licenseAgreement.getRequirementsAndConstrains());
207     licenseAgreementElement.setInfo(info);
208
209     if (licenseAgreement.getFeatureGroupIds() != null &&
210             !licenseAgreement.getFeatureGroupIds().isEmpty()) {
211       licenseAgreementElement.setRelations(licenseAgreement.getFeatureGroupIds().stream()
212               .map(rel -> VlmZusammenUtil
213                       .createRelation(RelationType.LicenseAgreementToFeatureGroup, rel))
214               .collect(Collectors.toList()));
215     }
216     return licenseAgreementElement;
217   }
218 }