2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen;
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;
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;
46 import static org.openecomp.core.zusammen.api.ZusammenUtil.*;
48 public class NicDaoZusammenImpl implements NicDao {
50 private ZusammenAdaptor zusammenAdaptor;
52 public NicDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
53 this.zusammenAdaptor = zusammenAdaptor;
57 public void registerVersioning(String versionableEntityType) {
58 // registerVersioning is not implemented for NicDaoZusammenImpl
62 public Collection<NicEntity> list(NicEntity nic) {
63 SessionContext context = createSessionContext();
64 return listNics(context, new ElementContext(nic.getVspId(), nic.getVersion().getId()), nic);
67 private Collection<NicEntity> listNics(SessionContext context, ElementContext elementContext,
69 return zusammenAdaptor.listElementsByName(context, elementContext, new Id(nic.getComponentId()),
70 ElementType.Nics.name())
72 .map(new ElementToNicConvertor()::convert)
74 nicEntity.setComponentId(nic.getComponentId());
75 nicEntity.setVspId(nic.getVspId());
76 nicEntity.setVersion(nic.getVersion());
79 .collect(Collectors.toList());
84 public void create(NicEntity nic) {
85 ZusammenElement nicElement = nicToZusammen(nic, Action.CREATE);
87 ZusammenElement nicsElement = buildStructuralElement(ElementType.Nics, Action.IGNORE);
88 nicsElement.setSubElements(Collections.singletonList(nicElement));
90 ZusammenElement componentElement = buildElement(new Id(nic.getComponentId()), Action.IGNORE);
91 componentElement.setSubElements(Collections.singletonList(nicsElement));
93 SessionContext context = createSessionContext();
94 ElementContext elementContext = new ElementContext(nic.getVspId(), nic.getVersion().getId());
96 Element savedElement =
97 zusammenAdaptor.saveElement(context, elementContext, componentElement, "Create nic");
98 nic.setId(savedElement.getSubElements().iterator().next()
99 .getSubElements().iterator().next().getElementId().getValue());
103 public void update(NicEntity nic) {
104 ZusammenElement nicElement = nicToZusammen(nic, Action.UPDATE);
106 SessionContext context = createSessionContext();
108 .saveElement(context, new ElementContext(nic.getVspId(), nic.getVersion().getId()),
109 nicElement, String.format("Update nic with id %s", nic.getId()));
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()),
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());
133 public void delete(NicEntity nic) {
134 ZusammenElement nicElement = buildElement(new Id(nic.getId()), Action.DELETE);
136 SessionContext context = createSessionContext();
138 .saveElement(context, new ElementContext(nic.getVspId(), nic.getVersion().getId()),
139 nicElement, String.format("Delete nic with id %s", nic.getId()));
143 public NicEntity getQuestionnaireData(String vspId, Version version, String componentId,
145 SessionContext context = createSessionContext();
147 return getQuestionnaire(context, new ElementContext(vspId, version.getId()),
148 new NicEntity(vspId, version, componentId, nicId));
151 private NicEntity getQuestionnaire(SessionContext context, ElementContext elementContext,
153 Optional<Element> questionnaireElement = zusammenAdaptor
154 .getElementByName(context, elementContext, new Id(nic.getId()),
155 ElementType.NicQuestionnaire.name());
156 return questionnaireElement.map(new ElementToNicQuestionnaireConvertor()::convert)
158 entity.setVspId(nic.getVspId());
159 entity.setVersion(nic.getVersion());
160 entity.setComponentId(nic.getComponentId());
167 public void updateQuestionnaireData(String vspId, Version version, String componentId,
168 String nicId, String questionnaireData) {
169 ZusammenElement questionnaireElement =
170 nicQuestionnaireToZusammen(questionnaireData, Action.UPDATE);
172 ZusammenElement nicElement = buildElement(new Id(nicId), Action.IGNORE);
173 nicElement.setSubElements(Collections.singletonList(questionnaireElement));
175 SessionContext context = createSessionContext();
176 zusammenAdaptor.saveElement(context, new ElementContext(vspId, version.getId()), nicElement,
177 "Update nic questionnaire");
181 public Collection<NicEntity> listByVsp(String vspId, Version version) {
182 SessionContext context = createSessionContext();
184 Collection<ComponentEntity> components = ComponentDaoZusammenImpl
185 .listComponents(zusammenAdaptor, context, vspId, version);
187 ElementContext elementContext = new ElementContext(vspId, version.getId());
188 return components.stream()
190 listNics(context, elementContext,
191 new NicEntity(vspId, version, component.getId(), null)).stream()
193 nic.setQuestionnaireData(getQuestionnaire(context, elementContext, nic).getQuestionnaireData());
196 .collect(Collectors.toList()))
197 .flatMap(Collection::stream)
198 .collect(Collectors.toList());
202 public void deleteByComponentId(String vspId, Version version, String componentId) {
203 SessionContext context = createSessionContext();
204 ElementContext elementContext = new ElementContext(vspId, version.getId());
206 Optional<Element> optionalElement = zusammenAdaptor.getElementByName(context,
207 elementContext, new Id(componentId), ElementType.Nics.name());
209 if (optionalElement.isPresent()) {
210 Element nicsElement = optionalElement.get();
211 Collection<Element> nics = nicsElement.getSubElements();
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());
222 public void deleteByVspId(String vspId, Version version) {
223 // deleteByVspId not implemented for NicDaoZusammenImpl
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)));
235 private ZusammenElement nicQuestionnaireToZusammen(String questionnaireData,
237 ZusammenElement questionnaireElement =
238 buildStructuralElement(ElementType.NicQuestionnaire, action);
239 questionnaireElement.setData(new ByteArrayInputStream(questionnaireData.getBytes()));
240 return questionnaireElement;
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()));