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