Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-core / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / dao / impl / zusammen / NicDaoZusammenImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.vendorsoftwareproduct.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.ZusammenElement;
25 import com.amdocs.zusammen.datatypes.Id;
26 import com.amdocs.zusammen.datatypes.SessionContext;
27 import com.amdocs.zusammen.datatypes.item.Action;
28 import com.amdocs.zusammen.datatypes.item.ElementContext;
29 import com.amdocs.zusammen.datatypes.item.Info;
30 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
31 import org.openecomp.sdc.datatypes.model.ElementType;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen.convertor.ElementToNicConvertor;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen.convertor.ElementToNicQuestionnaireConvertor;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
37 import org.openecomp.sdc.versioning.dao.types.Version;
38 import org.openecomp.types.ElementPropertyName;
39
40 import java.io.ByteArrayInputStream;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.Optional;
44 import java.util.stream.Collectors;
45
46 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
47
48 public class NicDaoZusammenImpl implements NicDao {
49
50   private ZusammenAdaptor zusammenAdaptor;
51
52   public NicDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
53     this.zusammenAdaptor = zusammenAdaptor;
54   }
55
56   @Override
57   public void registerVersioning(String versionableEntityType) {
58     // registerVersioning is not implemented for NicDaoZusammenImpl
59   }
60
61   @Override
62   public Collection<NicEntity> list(NicEntity nic) {
63     SessionContext context = createSessionContext();
64     return listNics(context, new ElementContext(nic.getVspId(), nic.getVersion().getId()), nic);
65   }
66
67   private Collection<NicEntity> listNics(SessionContext context, ElementContext elementContext,
68                                          NicEntity nic) {
69     return zusammenAdaptor.listElementsByName(context, elementContext, new Id(nic.getComponentId()),
70         ElementType.Nics.name())
71         .stream()
72         .map(new ElementToNicConvertor()::convert)
73         .map(nicEntity -> {
74           nicEntity.setComponentId(nic.getComponentId());
75           nicEntity.setVspId(nic.getVspId());
76           nicEntity.setVersion(nic.getVersion());
77           return nicEntity;
78         })
79         .collect(Collectors.toList());
80   }
81
82
83   @Override
84   public void create(NicEntity nic) {
85     ZusammenElement nicElement = nicToZusammen(nic, Action.CREATE);
86
87     ZusammenElement nicsElement = buildStructuralElement(ElementType.Nics, Action.IGNORE);
88     nicsElement.setSubElements(Collections.singletonList(nicElement));
89
90     ZusammenElement componentElement = buildElement(new Id(nic.getComponentId()), Action.IGNORE);
91     componentElement.setSubElements(Collections.singletonList(nicsElement));
92
93     SessionContext context = createSessionContext();
94     ElementContext elementContext = new ElementContext(nic.getVspId(), nic.getVersion().getId());
95
96     Element savedElement =
97         zusammenAdaptor.saveElement(context, elementContext, componentElement, "Create nic");
98     nic.setId(savedElement.getSubElements().iterator().next()
99         .getSubElements().iterator().next().getElementId().getValue());
100   }
101
102   @Override
103   public void update(NicEntity nic) {
104     ZusammenElement nicElement = nicToZusammen(nic, Action.UPDATE);
105
106     SessionContext context = createSessionContext();
107     zusammenAdaptor
108         .saveElement(context, new ElementContext(nic.getVspId(), nic.getVersion().getId()),
109             nicElement, String.format("Update nic with id %s", nic.getId()));
110   }
111
112   @Override
113   public NicEntity get(NicEntity nic) {
114     SessionContext context = createSessionContext();
115     ElementToNicConvertor convertor = new ElementToNicConvertor();
116     Optional<Element> element = zusammenAdaptor
117         .getElement(context, new ElementContext(nic.getVspId(), nic.getVersion().getId()),
118             nic.getId());
119
120     if (element.isPresent()) {
121       NicEntity entity = convertor.convert(element.get());
122       entity.setVspId(nic.getVspId());
123       entity.setVersion(nic.getVersion());
124       entity.setComponentId(nic.getComponentId());
125
126       return entity;
127     } else {
128       return null;
129     }
130   }
131
132   @Override
133   public void delete(NicEntity nic) {
134     ZusammenElement nicElement = buildElement(new Id(nic.getId()), Action.DELETE);
135
136     SessionContext context = createSessionContext();
137     zusammenAdaptor
138         .saveElement(context, new ElementContext(nic.getVspId(), nic.getVersion().getId()),
139             nicElement, String.format("Delete nic with id %s", nic.getId()));
140   }
141
142   @Override
143   public NicEntity getQuestionnaireData(String vspId, Version version, String componentId,
144                                         String nicId) {
145     SessionContext context = createSessionContext();
146
147     return getQuestionnaire(context, new ElementContext(vspId, version.getId()),
148         new NicEntity(vspId, version, componentId, nicId));
149   }
150
151   private NicEntity getQuestionnaire(SessionContext context, ElementContext elementContext,
152                                      NicEntity nic) {
153     Optional<Element> questionnaireElement = zusammenAdaptor
154         .getElementByName(context, elementContext, new Id(nic.getId()),
155             ElementType.NicQuestionnaire.name());
156     return questionnaireElement.map(new ElementToNicQuestionnaireConvertor()::convert)
157         .map(entity -> {
158           entity.setVspId(nic.getVspId());
159           entity.setVersion(nic.getVersion());
160           entity.setComponentId(nic.getComponentId());
161           return entity;
162         })
163         .orElse(null);
164   }
165
166   @Override
167   public void updateQuestionnaireData(String vspId, Version version, String componentId,
168                                       String nicId, String questionnaireData) {
169     ZusammenElement questionnaireElement =
170         nicQuestionnaireToZusammen(questionnaireData, Action.UPDATE);
171
172     ZusammenElement nicElement = buildElement(new Id(nicId), Action.IGNORE);
173     nicElement.setSubElements(Collections.singletonList(questionnaireElement));
174
175     SessionContext context = createSessionContext();
176     zusammenAdaptor.saveElement(context, new ElementContext(vspId, version.getId()), nicElement,
177         "Update nic questionnaire");
178   }
179
180   @Override
181   public Collection<NicEntity> listByVsp(String vspId, Version version) {
182     SessionContext context = createSessionContext();
183
184     Collection<ComponentEntity> components = ComponentDaoZusammenImpl
185         .listComponents(zusammenAdaptor, context, vspId, version);
186
187     ElementContext elementContext = new ElementContext(vspId, version.getId());
188     return components.stream()
189         .map(component ->
190             listNics(context, elementContext,
191                 new NicEntity(vspId, version, component.getId(), null)).stream()
192                 .map(nic -> {
193                   nic.setQuestionnaireData(getQuestionnaire(context, elementContext, nic).getQuestionnaireData());
194                   return nic;
195                 })
196                 .collect(Collectors.toList()))
197         .flatMap(Collection::stream)
198         .collect(Collectors.toList());
199   }
200
201   @Override
202   public void deleteByComponentId(String vspId, Version version, String componentId) {
203     SessionContext context = createSessionContext();
204     ElementContext elementContext = new ElementContext(vspId, version.getId());
205
206     Optional<Element> optionalElement = zusammenAdaptor.getElementByName(context,
207         elementContext, new Id(componentId), ElementType.Nics.name());
208
209     if (optionalElement.isPresent()) {
210       Element nicsElement = optionalElement.get();
211       Collection<Element> nics = nicsElement.getSubElements();
212
213       nics.forEach(nic -> {
214         ZusammenElement nicZusammenElement = buildElement(nic.getElementId(), Action.DELETE);
215         zusammenAdaptor.saveElement(context, elementContext, nicZusammenElement,
216             "Delete nic with id " + nic.getElementId());
217       });
218     }
219   }
220
221   @Override
222   public void deleteByVspId(String vspId, Version version) {
223     // deleteByVspId not implemented for NicDaoZusammenImpl
224   }
225
226   private ZusammenElement nicToZusammen(NicEntity nic, Action action) {
227     ZusammenElement nicElement = buildNicElement(nic, action);
228     if (action == Action.CREATE) {
229       nicElement.setSubElements(Collections.singletonList(
230           nicQuestionnaireToZusammen(nic.getQuestionnaireData(), Action.CREATE)));
231     }
232     return nicElement;
233   }
234
235   private ZusammenElement nicQuestionnaireToZusammen(String questionnaireData,
236                                                      Action action) {
237     ZusammenElement questionnaireElement =
238         buildStructuralElement(ElementType.NicQuestionnaire, action);
239     questionnaireElement.setData(new ByteArrayInputStream(questionnaireData.getBytes()));
240     return questionnaireElement;
241   }
242
243   private ZusammenElement buildNicElement(NicEntity nic, Action action) {
244     ZusammenElement nicElement =
245         buildElement(nic.getId() == null ? null : new Id(nic.getId()), action);
246     Info info = new Info();
247     info.addProperty(ElementPropertyName.elementType.name(), ElementType.Nic);
248     info.addProperty(ElementPropertyName.compositionData.name(), nic.getCompositionData());
249     nicElement.setInfo(info);
250     nicElement.setData(new ByteArrayInputStream(nic.getCompositionData().getBytes()));
251     return nicElement;
252   }
253 }