Use versioning, zusammen and session libs from sdc-common-be
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / main / java / org / onap / sdc / workflow / persistence / impl / ArtifactRepositoryImpl.java
1 /*
2  * Copyright © 2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.sdc.workflow.persistence.impl;
18
19 import static org.onap.sdc.common.zusammen.services.ZusammenElementUtil.buildStructuralElement;
20
21 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
22 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
24 import com.amdocs.zusammen.datatypes.item.Action;
25 import com.amdocs.zusammen.datatypes.item.ElementContext;
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Arrays;
30 import java.util.Optional;
31 import org.apache.commons.io.IOUtils;
32 import org.onap.sdc.common.versioning.persistence.zusammen.ZusammenSessionContextCreator;
33 import org.onap.sdc.common.zusammen.services.ZusammenAdaptor;
34 import org.onap.sdc.workflow.persistence.ArtifactRepository;
35 import org.onap.sdc.workflow.persistence.impl.types.WorkflowElementType;
36 import org.onap.sdc.workflow.persistence.types.ArtifactEntity;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Repository;
39
40 @Repository
41 public class ArtifactRepositoryImpl implements ArtifactRepository {
42
43     private static final String FILE_NAME_PROPERTY = "fileName";
44     private static final String EMPTY_DATA = "{}";
45
46     private final ZusammenAdaptor zusammenAdaptor;
47     private final ZusammenSessionContextCreator contextCreator;
48
49     @Autowired
50     public ArtifactRepositoryImpl(ZusammenAdaptor zusammenAdaptor, ZusammenSessionContextCreator contextCreator) {
51         this.zusammenAdaptor = zusammenAdaptor;
52         this.contextCreator = contextCreator;
53     }
54
55     @Override
56     public void update(String workflowId, String versionId, ArtifactEntity artifactEntity) {
57
58         ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.UPDATE);
59         artifactElement.setData(artifactEntity.getArtifactData());
60         artifactElement.getInfo().addProperty(FILE_NAME_PROPERTY, artifactEntity.getFileName());
61
62
63         ElementContext elementContext = new ElementContext(workflowId, versionId);
64
65         zusammenAdaptor
66                 .saveElement(contextCreator.create(), elementContext, artifactElement, "Update WorkflowVersion Artifact Element");
67     }
68
69     @Override
70     public Optional<ArtifactEntity> get(String workflowId, String versionId) {
71
72         ElementContext elementContext = new ElementContext(workflowId, versionId);
73
74         Optional<Element> elementOptional =
75                 zusammenAdaptor.getElementByName(contextCreator.create(), elementContext, null, WorkflowElementType.ARTIFACT.name());
76
77         if (!elementOptional.isPresent() || hasEmptyData(elementOptional.get().getData())) {
78             return Optional.empty();
79         }
80
81         Element artifactElement = elementOptional.get();
82
83         ArtifactEntity artifact = new ArtifactEntity(artifactElement.getInfo().getProperty(FILE_NAME_PROPERTY),
84                 artifactElement.getData());
85
86         return Optional.of(artifact);
87     }
88
89     @Override
90     public boolean isExist(String workflowId, String versionId) {
91
92         ElementContext elementContext = new ElementContext(workflowId, versionId);
93
94         Optional<ElementInfo> optionalElementInfo = zusammenAdaptor.getElementInfoByName(contextCreator.create(), elementContext, null,
95                 WorkflowElementType.ARTIFACT.name());
96         return optionalElementInfo.isPresent() && optionalElementInfo.get().getInfo().getProperties()
97                                                                      .containsKey(FILE_NAME_PROPERTY);
98     }
99
100     @Override
101     public void createStructure(String workflowId, String versionId) {
102
103         ElementContext elementContext = new ElementContext(workflowId, versionId);
104
105         ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.CREATE);
106         artifactElement.setData(new ByteArrayInputStream(EMPTY_DATA.getBytes()));
107
108         zusammenAdaptor
109                 .saveElement(contextCreator.create(), elementContext, artifactElement, "Create WorkflowVersion Artifact Element");
110
111     }
112
113     @Override
114     public void delete(String workflowId, String versionId) {
115
116         ElementContext elementContext = new ElementContext(workflowId, versionId);
117
118         ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.UPDATE);
119         artifactElement.setData(new ByteArrayInputStream(EMPTY_DATA.getBytes()));
120         artifactElement.getInfo().getProperties().remove(FILE_NAME_PROPERTY);
121
122         zusammenAdaptor.saveElement(contextCreator.create(), elementContext, artifactElement, "Delete WorkflowVersion Artifact Data");
123
124     }
125
126     private boolean hasEmptyData(InputStream elementData) {
127
128         byte[] byteElementData;
129         try {
130             byteElementData = IOUtils.toByteArray(elementData);
131         } catch (IOException ex) {
132             return false;
133         }
134         return Arrays.equals(EMPTY_DATA.getBytes(), byteElementData);
135     }
136 }