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.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;
46 public class ParameterRepositoryImpl implements ParameterRepository {
48 private ZusammenAdaptor zusammenAdaptor = ZusammenAdaptorFactory.getInstance().createInterface();
51 public void createStructure(String id, String versionId) {
53 SessionContext context = createSessionContext();
54 ElementContext elementContext = new ElementContext(id, versionId);
56 ZusammenElement inputsElement = buildStructuralElement(WorkflowElementType.INPUTS.name(), Action.CREATE);
57 ZusammenElement outputsElement = buildStructuralElement(WorkflowElementType.OUTPUTS.name(), Action.CREATE);
59 zusammenAdaptor.saveElement(context, elementContext, inputsElement, "Create WorkflowVersion INPUTS Element");
60 zusammenAdaptor.saveElement(context, elementContext, outputsElement, "Create WorkflowVersion OUTPUTS Element");
64 public Collection<ParameterEntity> list(String id, String versionId, ParameterRole role) {
66 SessionContext context = createSessionContext();
67 ElementContext elementContext = new ElementContext(id, versionId);
69 return zusammenAdaptor.listElementsByName(context, elementContext, null, getParentElementType(role)).stream()
70 .map(this::mapElementInfoToParameter).collect(Collectors.toList());
75 public void deleteAll(String id, String versionId, ParameterRole role) {
77 SessionContext context = createSessionContext();
78 ElementContext elementContext = new ElementContext(id, versionId);
80 Optional<ElementInfo> optionalParentElement =
81 zusammenAdaptor.getElementInfoByName(context, elementContext, null, getParentElementType(role));
83 if (!optionalParentElement.isPresent()) {
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()));
91 zusammenAdaptor.saveElement(context, elementContext, parentElement, "Delete all " + role);
95 public ParameterEntity get(String id, String versionId, String parameterId) {
97 SessionContext context = createSessionContext();
98 ElementContext elementContext = new ElementContext(id, versionId);
100 Optional<ElementInfo> element = zusammenAdaptor.getElementInfo(context, elementContext, new Id(parameterId));
102 return element.map(this::mapElementInfoToParameter).orElse(null);
106 public void delete(String id, String versionId, String parameterId) {
107 SessionContext context = createSessionContext();
108 ElementContext elementContext = new ElementContext(id, versionId);
110 ZusammenElement parameterElement = buildElement(new Id(parameterId), Action.DELETE);
112 zusammenAdaptor.saveElement(context, elementContext, parameterElement,
113 String.format("Delete Parameter with id %s", parameterId));
118 public ParameterEntity create(String id, String versionId, ParameterRole role, ParameterEntity parameter) {
120 ZusammenElement parameterElement = parameterToZusammenElement(parameter, role, Action.CREATE);
121 ZusammenElement parentElement = buildStructuralElement(getParentElementType(role), Action.IGNORE);
122 parentElement.addSubElement(parameterElement);
124 SessionContext context = createSessionContext();
125 ElementContext elementContext = new ElementContext(id, versionId);
127 Element savedElement = zusammenAdaptor.saveElement(context, elementContext, parentElement,
128 "Create WorkflowVersion Parameter Element");
130 parameter.setId(savedElement.getSubElements().iterator().next().getElementId().getValue());
135 public void update(String id, String versionId, ParameterRole role, ParameterEntity parameter) {
137 SessionContext context = createSessionContext();
138 ElementContext elementContext = new ElementContext(id, versionId);
140 ZusammenElement parameterElement = parameterToZusammenElement(parameter, role, Action.UPDATE);
142 zusammenAdaptor.saveElement(context, elementContext, parameterElement, "Update WorkflowVersion Parameter");
145 private ZusammenElement parameterToZusammenElement(ParameterEntity parameter, ParameterRole role, Action action) {
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);
156 return parameterElement;
159 private ParameterEntity mapElementInfoToParameter(ElementInfo elementInfo) {
160 ParameterEntity parameterEntity = new ParameterEntity();
161 parameterEntity.setId(elementInfo.getId().getValue());
162 parameterEntity.setName(elementInfo.getInfo().getName());
164 .setType(ParameterType.valueOf(elementInfo.getInfo().getProperty(ParameterPropertyName.TYPE.name())));
165 parameterEntity.setMandatory(elementInfo.getInfo().getProperty(ParameterPropertyName.mandatory.name()));
166 return parameterEntity;
169 private static String getParentElementType(ParameterRole role) {
172 return WorkflowElementType.INPUTS.name();
174 return WorkflowElementType.OUTPUTS.name();
176 throw new IllegalArgumentException("Wrong Element Type");