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.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;
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.impl.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.impl.types.WorkflowElementType;
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;
46 public class ParameterRepositoryImpl implements ParameterRepository {
48 private final ZusammenAdaptor zusammenAdaptor;
51 public ParameterRepositoryImpl(ZusammenAdaptor zusammenAdaptor) {
52 this.zusammenAdaptor = zusammenAdaptor;
56 public void createStructure(String id, String versionId) {
58 SessionContext context = createSessionContext();
59 ElementContext elementContext = new ElementContext(id, versionId);
61 ZusammenElement inputsElement = buildStructuralElement(WorkflowElementType.INPUTS.name(), Action.CREATE);
62 ZusammenElement outputsElement = buildStructuralElement(WorkflowElementType.OUTPUTS.name(), Action.CREATE);
64 zusammenAdaptor.saveElement(context, elementContext, inputsElement, "Create WorkflowVersion INPUTS Element");
65 zusammenAdaptor.saveElement(context, elementContext, outputsElement, "Create WorkflowVersion OUTPUTS Element");
69 public Collection<ParameterEntity> list(String id, String versionId, ParameterRole role) {
71 SessionContext context = createSessionContext();
72 ElementContext elementContext = new ElementContext(id, versionId);
74 return zusammenAdaptor.listElementsByName(context, elementContext, null, getParentElementType(role)).stream()
75 .map(this::mapElementInfoToParameter).collect(Collectors.toList());
80 public void deleteAll(String id, String versionId, ParameterRole role) {
82 SessionContext context = createSessionContext();
83 ElementContext elementContext = new ElementContext(id, versionId);
85 Optional<ElementInfo> optionalParentElement =
86 zusammenAdaptor.getElementInfoByName(context, elementContext, null, getParentElementType(role));
88 if (!optionalParentElement.isPresent()) {
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()));
96 zusammenAdaptor.saveElement(context, elementContext, parentElement, "Delete all " + role);
100 public ParameterEntity get(String id, String versionId, String parameterId) {
102 SessionContext context = createSessionContext();
103 ElementContext elementContext = new ElementContext(id, versionId);
105 Optional<ElementInfo> element = zusammenAdaptor.getElementInfo(context, elementContext, new Id(parameterId));
107 return element.map(this::mapElementInfoToParameter).orElse(null);
111 public void delete(String id, String versionId, String parameterId) {
112 SessionContext context = createSessionContext();
113 ElementContext elementContext = new ElementContext(id, versionId);
115 ZusammenElement parameterElement = buildElement(new Id(parameterId), Action.DELETE);
117 zusammenAdaptor.saveElement(context, elementContext, parameterElement,
118 String.format("Delete Parameter with id %s", parameterId));
123 public ParameterEntity create(String id, String versionId, ParameterRole role, ParameterEntity parameter) {
125 ZusammenElement parameterElement = parameterToZusammenElement(parameter, role, Action.CREATE);
126 ZusammenElement parentElement = buildStructuralElement(getParentElementType(role), Action.IGNORE);
127 parentElement.addSubElement(parameterElement);
129 SessionContext context = createSessionContext();
130 ElementContext elementContext = new ElementContext(id, versionId);
132 Element savedElement = zusammenAdaptor.saveElement(context, elementContext, parentElement,
133 "Create WorkflowVersion Parameter Element");
135 parameter.setId(savedElement.getSubElements().iterator().next().getElementId().getValue());
140 public void update(String id, String versionId, ParameterRole role, ParameterEntity parameter) {
142 SessionContext context = createSessionContext();
143 ElementContext elementContext = new ElementContext(id, versionId);
145 ZusammenElement parameterElement = parameterToZusammenElement(parameter, role, Action.UPDATE);
147 zusammenAdaptor.saveElement(context, elementContext, parameterElement, "Update WorkflowVersion Parameter");
150 private ZusammenElement parameterToZusammenElement(ParameterEntity parameter, ParameterRole role, Action action) {
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);
161 return parameterElement;
164 private ParameterEntity mapElementInfoToParameter(ElementInfo elementInfo) {
165 ParameterEntity parameterEntity = new ParameterEntity();
166 parameterEntity.setId(elementInfo.getId().getValue());
167 parameterEntity.setName(elementInfo.getInfo().getName());
169 .setType(ParameterType.valueOf(elementInfo.getInfo().getProperty(ParameterPropertyName.TYPE.name())));
170 parameterEntity.setMandatory(elementInfo.getInfo().getProperty(ParameterPropertyName.MANDATORY.name()));
171 return parameterEntity;
174 private static String getParentElementType(ParameterRole role) {
177 return WorkflowElementType.INPUTS.name();
179 return WorkflowElementType.OUTPUTS.name();
181 throw new IllegalArgumentException("Wrong Element Type");