2 * Copyright © 2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.sdc.workflow.persistence.impl;
19 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
20 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
22 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
24 import com.amdocs.zusammen.datatypes.SessionContext;
25 import com.amdocs.zusammen.datatypes.item.Action;
26 import com.amdocs.zusammen.datatypes.item.ElementContext;
27 import java.io.ByteArrayInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.Arrays;
31 import java.util.Optional;
32 import org.apache.commons.io.IOUtils;
33 import org.onap.sdc.workflow.persistence.ArtifactRepository;
34 import org.onap.sdc.workflow.persistence.types.ArtifactEntity;
35 import org.onap.sdc.workflow.persistence.types.WorkflowElementType;
36 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
37 import org.openecomp.core.zusammen.api.ZusammenAdaptorFactory;
38 import org.springframework.stereotype.Repository;
41 public class ArtifactRepositoryImpl implements ArtifactRepository {
43 private static final String FILE_NAME_PROPERTY = "fileName";
44 private static final String EMPTY_DATA = "{}";
45 private ZusammenAdaptor zusammenAdaptor = ZusammenAdaptorFactory.getInstance().createInterface();
49 public void update(String id, String versionId, ArtifactEntity artifactEntity) {
51 ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.UPDATE);
52 artifactElement.setData(artifactEntity.getArtifactData());
53 artifactElement.getInfo().addProperty(FILE_NAME_PROPERTY, artifactEntity.getFileName());
55 SessionContext context = createSessionContext();
56 ElementContext elementContext = new ElementContext(id, versionId);
59 .saveElement(context, elementContext, artifactElement, "Update WorkflowVersion Artifact Element");
63 public Optional<ArtifactEntity> get(String id, String versionId) {
64 SessionContext context = createSessionContext();
65 ElementContext elementContext = new ElementContext(id, versionId);
67 Optional<Element> elementOptional =
68 zusammenAdaptor.getElementByName(context, elementContext, null, WorkflowElementType.ARTIFACT.name());
70 if (!elementOptional.isPresent() || hasEmptyData(elementOptional.get().getData())) {
71 return Optional.empty();
74 Element artifactElement = elementOptional.get();
76 ArtifactEntity artifact = new ArtifactEntity(artifactElement.getInfo().getProperty(FILE_NAME_PROPERTY),
77 artifactElement.getData());
79 return Optional.of(artifact);
83 public void createStructure(String id, String versionId) {
84 SessionContext context = createSessionContext();
85 ElementContext elementContext = new ElementContext(id, versionId);
87 ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.CREATE);
88 artifactElement.setData(new ByteArrayInputStream(EMPTY_DATA.getBytes()));
91 .saveElement(context, elementContext, artifactElement, "Create WorkflowVersion Artifact Element");
96 public void delete(String id, String versionId) {
97 SessionContext context = createSessionContext();
98 ElementContext elementContext = new ElementContext(id, versionId);
100 ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.UPDATE);
101 artifactElement.setData(new ByteArrayInputStream(EMPTY_DATA.getBytes()));
102 artifactElement.getInfo().getProperties().remove(FILE_NAME_PROPERTY);
105 .saveElement(context, elementContext, artifactElement, "Delete WorkflowVersion Artifact Data");
109 private boolean hasEmptyData(InputStream elementData) {
111 byte[] byteElementData;
113 byteElementData = IOUtils.toByteArray(elementData);
114 } catch (IOException ex) {
117 return Arrays.equals(EMPTY_DATA.getBytes(), byteElementData);