Unit Test coverage
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / main / java / org / onap / sdc / workflow / persistence / impl / ParameterRepositoryImpl.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.buildElement;
20 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
21 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
22
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
24 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
25 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
26 import com.amdocs.zusammen.datatypes.Id;
27 import com.amdocs.zusammen.datatypes.SessionContext;
28 import com.amdocs.zusammen.datatypes.item.Action;
29 import com.amdocs.zusammen.datatypes.item.ElementContext;
30 import com.amdocs.zusammen.datatypes.item.Info;
31 import java.util.Collection;
32 import java.util.Optional;
33 import java.util.stream.Collectors;
34 import org.onap.sdc.workflow.persistence.ParameterRepository;
35 import org.onap.sdc.workflow.persistence.impl.types.ParameterPropertyName;
36 import org.onap.sdc.workflow.persistence.impl.types.WorkflowElementType;
37 import org.onap.sdc.workflow.persistence.types.ParameterEntity;
38 import org.onap.sdc.workflow.persistence.types.ParameterRole;
39 import org.onap.sdc.workflow.persistence.types.ParameterType;
40 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
41 import org.openecomp.types.ElementPropertyName;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Repository;
44
45 @Repository
46 public class ParameterRepositoryImpl implements ParameterRepository {
47
48     private final ZusammenAdaptor zusammenAdaptor;
49
50     @Autowired
51     public ParameterRepositoryImpl(ZusammenAdaptor zusammenAdaptor) {
52         this.zusammenAdaptor = zusammenAdaptor;
53     }
54
55     @Override
56     public void createStructure(String id, String versionId) {
57
58         SessionContext context = createSessionContext();
59         ElementContext elementContext = new ElementContext(id, versionId);
60
61         ZusammenElement inputsElement = buildStructuralElement(WorkflowElementType.INPUTS.name(), Action.CREATE);
62         ZusammenElement outputsElement = buildStructuralElement(WorkflowElementType.OUTPUTS.name(), Action.CREATE);
63
64         zusammenAdaptor.saveElement(context, elementContext, inputsElement, "Create WorkflowVersion INPUTS Element");
65         zusammenAdaptor.saveElement(context, elementContext, outputsElement, "Create WorkflowVersion OUTPUTS Element");
66     }
67
68     @Override
69     public Collection<ParameterEntity> list(String id, String versionId, ParameterRole role) {
70
71         SessionContext context = createSessionContext();
72         ElementContext elementContext = new ElementContext(id, versionId);
73
74         return zusammenAdaptor.listElementsByName(context, elementContext, null, getParentElementType(role)).stream()
75                               .map(this::mapElementInfoToParameter).collect(Collectors.toList());
76
77     }
78
79     @Override
80     public void deleteAll(String id, String versionId, ParameterRole role) {
81
82         SessionContext context = createSessionContext();
83         ElementContext elementContext = new ElementContext(id, versionId);
84
85         Optional<ElementInfo> optionalParentElement =
86                 zusammenAdaptor.getElementInfoByName(context, elementContext, null, getParentElementType(role));
87
88         if (!optionalParentElement.isPresent()) {
89             throw new IllegalStateException(String.format("Missing data for workflow id %s version id %s",id,versionId));
90         }
91         ZusammenElement parentElement = buildElement(optionalParentElement.get().getId(), Action.IGNORE);
92         parentElement.setSubElements(optionalParentElement.get().getSubElements().stream()
93                                                           .map(parameter -> buildElement(parameter.getId(),
94                                                                   Action.DELETE)).collect(Collectors.toList()));
95
96         zusammenAdaptor.saveElement(context, elementContext, parentElement, "Delete all " + role);
97     }
98
99     @Override
100     public ParameterEntity get(String id, String versionId, String parameterId) {
101
102         SessionContext context = createSessionContext();
103         ElementContext elementContext = new ElementContext(id, versionId);
104
105         Optional<ElementInfo> element = zusammenAdaptor.getElementInfo(context, elementContext, new Id(parameterId));
106
107         return element.map(this::mapElementInfoToParameter).orElse(null);
108     }
109
110     @Override
111     public void delete(String id, String versionId, String parameterId) {
112         SessionContext context = createSessionContext();
113         ElementContext elementContext = new ElementContext(id, versionId);
114
115         ZusammenElement parameterElement = buildElement(new Id(parameterId), Action.DELETE);
116
117         zusammenAdaptor.saveElement(context, elementContext, parameterElement,
118                 String.format("Delete Parameter with id %s", parameterId));
119
120     }
121
122     @Override
123     public ParameterEntity create(String id, String versionId, ParameterRole role, ParameterEntity parameter) {
124
125         ZusammenElement parameterElement = parameterToZusammenElement(parameter, role, Action.CREATE);
126         ZusammenElement parentElement = buildStructuralElement(getParentElementType(role), Action.IGNORE);
127         parentElement.addSubElement(parameterElement);
128
129         SessionContext context = createSessionContext();
130         ElementContext elementContext = new ElementContext(id, versionId);
131
132         Element savedElement = zusammenAdaptor.saveElement(context, elementContext, parentElement,
133                 "Create WorkflowVersion Parameter Element");
134
135         parameter.setId(savedElement.getSubElements().iterator().next().getElementId().getValue());
136         return parameter;
137     }
138
139     @Override
140     public void update(String id, String versionId, ParameterRole role, ParameterEntity parameter) {
141
142         SessionContext context = createSessionContext();
143         ElementContext elementContext = new ElementContext(id, versionId);
144
145         ZusammenElement parameterElement = parameterToZusammenElement(parameter, role, Action.UPDATE);
146
147         zusammenAdaptor.saveElement(context, elementContext, parameterElement, "Update WorkflowVersion Parameter");
148     }
149
150     private ZusammenElement parameterToZusammenElement(ParameterEntity parameter, ParameterRole role, Action action) {
151
152         ZusammenElement parameterElement =
153                 buildElement(parameter.getId() == null ? null : new Id(parameter.getId()), action);
154         Info info = new Info();
155         info.setName(parameter.getName());
156         info.addProperty(ElementPropertyName.elementType.name(), WorkflowElementType.valueOf(role.name()));
157         info.addProperty(ParameterPropertyName.TYPE.name(), parameter.getType());
158         info.addProperty(ParameterPropertyName.MANDATORY.name(), parameter.isMandatory());
159         parameterElement.setInfo(info);
160
161         return parameterElement;
162     }
163
164     private ParameterEntity mapElementInfoToParameter(ElementInfo elementInfo) {
165         ParameterEntity parameterEntity = new ParameterEntity();
166         parameterEntity.setId(elementInfo.getId().getValue());
167         parameterEntity.setName(elementInfo.getInfo().getName());
168         parameterEntity
169                 .setType(ParameterType.valueOf(elementInfo.getInfo().getProperty(ParameterPropertyName.TYPE.name())));
170         parameterEntity.setMandatory(elementInfo.getInfo().getProperty(ParameterPropertyName.MANDATORY.name()));
171         return parameterEntity;
172     }
173
174     private static String getParentElementType(ParameterRole role) {
175         switch (role) {
176             case INPUT:
177                 return WorkflowElementType.INPUTS.name();
178             case OUTPUT:
179                 return WorkflowElementType.OUTPUTS.name();
180             default:
181                 throw new IllegalArgumentException("Wrong Element Type");
182         }
183     }
184
185 }