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.ElementInfo;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
25 import com.amdocs.zusammen.datatypes.SessionContext;
26 import com.amdocs.zusammen.datatypes.item.Action;
27 import com.amdocs.zusammen.datatypes.item.ElementContext;
28 import java.io.ByteArrayInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Arrays;
32 import java.util.Optional;
33 import org.apache.commons.io.IOUtils;
34 import org.onap.sdc.workflow.persistence.ArtifactRepository;
35 import org.onap.sdc.workflow.persistence.types.ArtifactEntity;
36 import org.onap.sdc.workflow.persistence.types.WorkflowElementType;
37 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
38 import org.openecomp.core.zusammen.api.ZusammenAdaptorFactory;
39 import org.springframework.stereotype.Repository;
42 public class ArtifactRepositoryImpl implements ArtifactRepository {
44 private static final String FILE_NAME_PROPERTY = "fileName";
45 private static final String EMPTY_DATA = "{}";
46 private ZusammenAdaptor zusammenAdaptor = ZusammenAdaptorFactory.getInstance().createInterface();
50 public void update(String workflowId, String versionId, ArtifactEntity artifactEntity) {
52 ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.UPDATE);
53 artifactElement.setData(artifactEntity.getArtifactData());
54 artifactElement.getInfo().addProperty(FILE_NAME_PROPERTY, artifactEntity.getFileName());
56 SessionContext context = createSessionContext();
57 ElementContext elementContext = new ElementContext(workflowId, versionId);
60 .saveElement(context, elementContext, artifactElement, "Update WorkflowVersion Artifact Element");
64 public Optional<ArtifactEntity> get(String workflowId, String versionId) {
65 SessionContext context = createSessionContext();
66 ElementContext elementContext = new ElementContext(workflowId, versionId);
68 Optional<Element> elementOptional =
69 zusammenAdaptor.getElementByName(context, elementContext, null, WorkflowElementType.ARTIFACT.name());
71 if (!elementOptional.isPresent() || hasEmptyData(elementOptional.get().getData())) {
72 return Optional.empty();
75 Element artifactElement = elementOptional.get();
77 ArtifactEntity artifact = new ArtifactEntity(artifactElement.getInfo().getProperty(FILE_NAME_PROPERTY),
78 artifactElement.getData());
80 return Optional.of(artifact);
84 public boolean isExist(String workflowId, String versionId) {
85 SessionContext context = createSessionContext();
86 ElementContext elementContext = new ElementContext(workflowId, versionId);
88 Optional<ElementInfo> optionalElementInfo = zusammenAdaptor.getElementInfoByName(context, elementContext, null,
89 WorkflowElementType.ARTIFACT.name());
90 return optionalElementInfo.isPresent() && optionalElementInfo.get().getInfo().getProperties()
91 .containsKey(FILE_NAME_PROPERTY);
95 public void createStructure(String workflowId, String versionId) {
96 SessionContext context = createSessionContext();
97 ElementContext elementContext = new ElementContext(workflowId, versionId);
99 ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.CREATE);
100 artifactElement.setData(new ByteArrayInputStream(EMPTY_DATA.getBytes()));
103 .saveElement(context, elementContext, artifactElement, "Create WorkflowVersion Artifact Element");
108 public void delete(String workflowId, String versionId) {
109 SessionContext context = createSessionContext();
110 ElementContext elementContext = new ElementContext(workflowId, versionId);
112 ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.UPDATE);
113 artifactElement.setData(new ByteArrayInputStream(EMPTY_DATA.getBytes()));
114 artifactElement.getInfo().getProperties().remove(FILE_NAME_PROPERTY);
117 .saveElement(context, elementContext, artifactElement, "Delete WorkflowVersion Artifact Data");
121 private boolean hasEmptyData(InputStream elementData) {
123 byte[] byteElementData;
125 byteElementData = IOUtils.toByteArray(elementData);
126 } catch (IOException ex) {
129 return Arrays.equals(EMPTY_DATA.getBytes(), byteElementData);