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