2 * Copyright © 2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.sdc.workflow.persistence.impl;
19 import static org.onap.sdc.common.zusammen.services.ZusammenElementUtil.ELEMENT_TYPE_PROPERTY;
20 import static org.onap.sdc.common.zusammen.services.ZusammenElementUtil.buildElement;
21 import static org.onap.sdc.common.zusammen.services.ZusammenElementUtil.buildStructuralElement;
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.item.Action;
28 import com.amdocs.zusammen.datatypes.item.ElementContext;
29 import com.amdocs.zusammen.datatypes.item.Info;
30 import java.util.Collection;
31 import java.util.Optional;
32 import java.util.stream.Collectors;
33 import org.onap.sdc.common.versioning.persistence.zusammen.ZusammenSessionContextCreator;
34 import org.onap.sdc.common.zusammen.services.ZusammenAdaptor;
35 import org.onap.sdc.workflow.persistence.ParameterRepository;
36 import org.onap.sdc.workflow.persistence.impl.types.ParameterPropertyName;
37 import org.onap.sdc.workflow.persistence.impl.types.WorkflowElementType;
38 import org.onap.sdc.workflow.persistence.types.ParameterEntity;
39 import org.onap.sdc.workflow.persistence.types.ParameterRole;
40 import org.onap.sdc.workflow.persistence.types.ParameterType;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Repository;
45 public class ParameterRepositoryImpl implements ParameterRepository {
47 private final ZusammenAdaptor zusammenAdaptor;
48 private final ZusammenSessionContextCreator contextCreator;
51 public ParameterRepositoryImpl(ZusammenAdaptor zusammenAdaptor, ZusammenSessionContextCreator contextCreator) {
52 this.zusammenAdaptor = zusammenAdaptor;
53 this.contextCreator = contextCreator;
57 public void createStructure(String id, String versionId) {
58 ElementContext elementContext = new ElementContext(id, versionId);
60 ZusammenElement inputsElement = buildStructuralElement(WorkflowElementType.INPUTS.name(), Action.CREATE);
61 ZusammenElement outputsElement = buildStructuralElement(WorkflowElementType.OUTPUTS.name(), Action.CREATE);
63 zusammenAdaptor.saveElement(contextCreator.create(), elementContext, inputsElement, "Create WorkflowVersion INPUTS Element");
64 zusammenAdaptor.saveElement(contextCreator.create(), elementContext, outputsElement, "Create WorkflowVersion OUTPUTS Element");
68 public Collection<ParameterEntity> list(String id, String versionId, ParameterRole role) {
69 ElementContext elementContext = new ElementContext(id, versionId);
71 return zusammenAdaptor.listElementsByName(contextCreator.create(), elementContext, null, getParentElementType(role)).stream()
72 .map(this::mapElementInfoToParameter).collect(Collectors.toList());
76 public void deleteAll(String id, String versionId, ParameterRole role) {
77 ElementContext elementContext = new ElementContext(id, versionId);
79 Optional<ElementInfo> optionalParentElement =
80 zusammenAdaptor.getElementInfoByName(contextCreator.create(), elementContext, null, getParentElementType(role));
82 if (!optionalParentElement.isPresent()) {
83 throw new IllegalStateException(
84 String.format("Missing data for workflow id %s version id %s", id, versionId));
86 ZusammenElement parentElement = buildElement(optionalParentElement.get().getId(), Action.IGNORE);
87 parentElement.setSubElements(optionalParentElement.get().getSubElements().stream()
88 .map(parameter -> buildElement(parameter.getId(), Action.DELETE))
89 .collect(Collectors.toList()));
91 zusammenAdaptor.saveElement(contextCreator.create(), elementContext, parentElement, "Delete all " + role);
95 public ParameterEntity get(String id, String versionId, String parameterId) {
96 ElementContext elementContext = new ElementContext(id, versionId);
98 Optional<ElementInfo> element = zusammenAdaptor.getElementInfo(contextCreator.create(), elementContext, new Id(parameterId));
100 return element.map(this::mapElementInfoToParameter).orElse(null);
104 public void delete(String id, String versionId, String parameterId) {
105 ElementContext elementContext = new ElementContext(id, versionId);
107 ZusammenElement parameterElement = buildElement(new Id(parameterId), Action.DELETE);
109 zusammenAdaptor.saveElement(contextCreator.create(), elementContext, parameterElement,
110 String.format("Delete Parameter with id %s", parameterId));
114 public ParameterEntity create(String id, String versionId, ParameterRole role, ParameterEntity parameter) {
115 ZusammenElement parameterElement = parameterToZusammenElement(parameter, role, Action.CREATE);
116 ZusammenElement parentElement = buildStructuralElement(getParentElementType(role), Action.IGNORE);
117 parentElement.addSubElement(parameterElement);
119 ElementContext elementContext = new ElementContext(id, versionId);
121 Element savedElement = zusammenAdaptor.saveElement(contextCreator.create(), elementContext, parentElement,
122 "Create WorkflowVersion Parameter Element");
124 parameter.setId(savedElement.getSubElements().iterator().next().getElementId().getValue());
129 public void update(String id, String versionId, ParameterRole role, ParameterEntity parameter) {
130 ElementContext elementContext = new ElementContext(id, versionId);
132 ZusammenElement parameterElement = parameterToZusammenElement(parameter, role, Action.UPDATE);
134 zusammenAdaptor.saveElement(contextCreator.create(), elementContext, parameterElement, "Update WorkflowVersion Parameter");
137 private ZusammenElement parameterToZusammenElement(ParameterEntity parameter, ParameterRole role, Action action) {
138 ZusammenElement parameterElement =
139 buildElement(parameter.getId() == null ? null : new Id(parameter.getId()), action);
140 Info info = new Info();
141 info.setName(parameter.getName());
142 info.addProperty(ELEMENT_TYPE_PROPERTY, WorkflowElementType.valueOf(role.name()));
143 info.addProperty(ParameterPropertyName.TYPE.name(), parameter.getType());
144 info.addProperty(ParameterPropertyName.MANDATORY.name(), parameter.isMandatory());
145 parameterElement.setInfo(info);
147 return parameterElement;
150 private ParameterEntity mapElementInfoToParameter(ElementInfo elementInfo) {
151 ParameterEntity parameterEntity = new ParameterEntity();
152 parameterEntity.setId(elementInfo.getId().getValue());
153 parameterEntity.setName(elementInfo.getInfo().getName());
155 .setType(ParameterType.valueOf(elementInfo.getInfo().getProperty(ParameterPropertyName.TYPE.name())));
156 parameterEntity.setMandatory(elementInfo.getInfo().getProperty(ParameterPropertyName.MANDATORY.name()));
157 return parameterEntity;
160 private static String getParentElementType(ParameterRole role) {
163 return WorkflowElementType.INPUTS.name();
165 return WorkflowElementType.OUTPUTS.name();
167 throw new IllegalArgumentException("Wrong Element Type");