3c528db14a6ad51562866062d7e2937eda1b5d8e
[sdc/sdc-workflow-designer.git] /
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.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
20 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
21
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;
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     private ZusammenAdaptor zusammenAdaptor = ZusammenAdaptorFactory.getInstance().createInterface();
46
47
48     @Override
49     public void update(String id, String versionId, ArtifactEntity artifactEntity) {
50
51         ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.UPDATE);
52         artifactElement.setData(artifactEntity.getArtifactData());
53         artifactElement.getInfo().addProperty(FILE_NAME_PROPERTY, artifactEntity.getFileName());
54
55         SessionContext context = createSessionContext();
56         ElementContext elementContext = new ElementContext(id, versionId);
57
58         zusammenAdaptor
59                 .saveElement(context, elementContext, artifactElement, "Update WorkflowVersion Artifact Element");
60     }
61
62     @Override
63     public Optional<ArtifactEntity> get(String id, String versionId) {
64         SessionContext context = createSessionContext();
65         ElementContext elementContext = new ElementContext(id, versionId);
66
67         Optional<Element> elementOptional =
68                 zusammenAdaptor.getElementByName(context, elementContext, null, WorkflowElementType.ARTIFACT.name());
69
70         if (!elementOptional.isPresent() || hasEmptyData(elementOptional.get().getData())) {
71             return Optional.empty();
72         }
73
74         Element artifactElement = elementOptional.get();
75
76         ArtifactEntity artifact = new ArtifactEntity(artifactElement.getInfo().getProperty(FILE_NAME_PROPERTY),
77                 artifactElement.getData());
78
79         return Optional.of(artifact);
80     }
81
82     @Override
83     public void createStructure(String id, String versionId) {
84         SessionContext context = createSessionContext();
85         ElementContext elementContext = new ElementContext(id, versionId);
86
87         ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.CREATE);
88         artifactElement.setData(new ByteArrayInputStream(EMPTY_DATA.getBytes()));
89
90         zusammenAdaptor
91                 .saveElement(context, elementContext, artifactElement, "Create WorkflowVersion Artifact Element");
92
93     }
94
95     @Override
96     public void delete(String id, String versionId) {
97         SessionContext context = createSessionContext();
98         ElementContext elementContext = new ElementContext(id, versionId);
99
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);
103
104         zusammenAdaptor
105                 .saveElement(context, elementContext, artifactElement, "Delete WorkflowVersion Artifact Data");
106
107     }
108
109     private boolean hasEmptyData(InputStream elementData) {
110
111         byte[] byteElementData;
112         try {
113             byteElementData = IOUtils.toByteArray(elementData);
114         } catch (IOException ex) {
115             return false;
116         }
117         return Arrays.equals(EMPTY_DATA.getBytes(), byteElementData);
118     }
119 }