Add activity spec code
[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.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.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.impl.types.WorkflowElementType;
37 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Repository;
40
41 @Repository
42 public class ArtifactRepositoryImpl implements ArtifactRepository {
43
44     private static final String FILE_NAME_PROPERTY = "fileName";
45     private static final String EMPTY_DATA = "{}";
46
47     private final ZusammenAdaptor zusammenAdaptor;
48
49     @Autowired
50     public ArtifactRepositoryImpl(ZusammenAdaptor zusammenAdaptor) {
51         this.zusammenAdaptor = zusammenAdaptor;
52     }
53
54     @Override
55     public void update(String workflowId, String versionId, ArtifactEntity artifactEntity) {
56
57         ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.UPDATE);
58         artifactElement.setData(artifactEntity.getArtifactData());
59         artifactElement.getInfo().addProperty(FILE_NAME_PROPERTY, artifactEntity.getFileName());
60
61         SessionContext context = createSessionContext();
62         ElementContext elementContext = new ElementContext(workflowId, versionId);
63
64         zusammenAdaptor
65                 .saveElement(context, elementContext, artifactElement, "Update WorkflowVersion Artifact Element");
66     }
67
68     @Override
69     public Optional<ArtifactEntity> get(String workflowId, String versionId) {
70         SessionContext context = createSessionContext();
71         ElementContext elementContext = new ElementContext(workflowId, versionId);
72
73         Optional<Element> elementOptional =
74                 zusammenAdaptor.getElementByName(context, elementContext, null, WorkflowElementType.ARTIFACT.name());
75
76         if (!elementOptional.isPresent() || hasEmptyData(elementOptional.get().getData())) {
77             return Optional.empty();
78         }
79
80         Element artifactElement = elementOptional.get();
81
82         ArtifactEntity artifact = new ArtifactEntity(artifactElement.getInfo().getProperty(FILE_NAME_PROPERTY),
83                 artifactElement.getData());
84
85         return Optional.of(artifact);
86     }
87
88     @Override
89     public boolean isExist(String workflowId, String versionId) {
90         SessionContext context = createSessionContext();
91         ElementContext elementContext = new ElementContext(workflowId, versionId);
92
93         Optional<ElementInfo> optionalElementInfo = zusammenAdaptor.getElementInfoByName(context, elementContext, null,
94                 WorkflowElementType.ARTIFACT.name());
95         return optionalElementInfo.isPresent() && optionalElementInfo.get().getInfo().getProperties()
96                                                                      .containsKey(FILE_NAME_PROPERTY);
97     }
98
99     @Override
100     public void createStructure(String workflowId, String versionId) {
101         SessionContext context = createSessionContext();
102         ElementContext elementContext = new ElementContext(workflowId, versionId);
103
104         ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.CREATE);
105         artifactElement.setData(new ByteArrayInputStream(EMPTY_DATA.getBytes()));
106
107         zusammenAdaptor
108                 .saveElement(context, elementContext, artifactElement, "Create WorkflowVersion Artifact Element");
109
110     }
111
112     @Override
113     public void delete(String workflowId, String versionId) {
114         SessionContext context = createSessionContext();
115         ElementContext elementContext = new ElementContext(workflowId, versionId);
116
117         ZusammenElement artifactElement = buildStructuralElement(WorkflowElementType.ARTIFACT.name(), Action.UPDATE);
118         artifactElement.setData(new ByteArrayInputStream(EMPTY_DATA.getBytes()));
119         artifactElement.getInfo().getProperties().remove(FILE_NAME_PROPERTY);
120
121         zusammenAdaptor.saveElement(context, elementContext, artifactElement, "Delete WorkflowVersion Artifact Data");
122
123     }
124
125     private boolean hasEmptyData(InputStream elementData) {
126
127         byte[] byteElementData;
128         try {
129             byteElementData = IOUtils.toByteArray(elementData);
130         } catch (IOException ex) {
131             return false;
132         }
133         return Arrays.equals(EMPTY_DATA.getBytes(), byteElementData);
134     }
135 }