2 * Copyright © 2016-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.buildStructuralElement;
20 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
22 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
23 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
24 import com.amdocs.zusammen.datatypes.SessionContext;
25 import com.amdocs.zusammen.datatypes.item.Action;
26 import com.amdocs.zusammen.datatypes.item.ElementContext;
27 import com.amdocs.zusammen.datatypes.item.Info;
28 import java.io.ByteArrayInputStream;
29 import java.io.InputStream;
30 import java.util.Objects;
31 import java.util.Optional;
32 import org.onap.sdc.workflow.persistence.ActivitySpecRepository;
33 import org.onap.sdc.workflow.persistence.impl.types.ActivitySpecData;
34 import org.onap.sdc.workflow.persistence.impl.types.ActivitySpecElementType;
35 import org.onap.sdc.workflow.persistence.types.ActivitySpecEntity;
36 import org.onap.sdc.workflow.services.ActivitySpecConstant;
37 import org.openecomp.core.utilities.json.JsonUtil;
38 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Repository;
43 public class ActivitySpecRepositoryImpl implements ActivitySpecRepository {
45 private final ZusammenAdaptor zusammenAdaptor;
48 public ActivitySpecRepositoryImpl(ZusammenAdaptor zusammenAdaptor) {
49 this.zusammenAdaptor = zusammenAdaptor;
53 public void create(ActivitySpecEntity activitySpec) {
54 SessionContext context = createSessionContext();
55 ZusammenElement generalElement = mapActivityDetailsToZusammenElement(activitySpec, Action.CREATE);
57 ElementContext elementContext = new ElementContext(activitySpec.getId(), activitySpec.getVersion().getId());
59 .saveElement(context, elementContext, generalElement, "Create Activity Spec General Info Element");
63 public ActivitySpecEntity get(ActivitySpecEntity entity) {
64 SessionContext context = createSessionContext();
66 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId());
67 Optional<Element> element =
68 zusammenAdaptor.getElementByName(context, elementContext, null, ActivitySpecElementType.ACTIVITYSPEC.name());
69 return element.map(this::mapZusammenElementToActivityDetails).orElse(null);
73 public void update(ActivitySpecEntity entity) {
74 SessionContext context = createSessionContext();
75 ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.UPDATE);
77 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId());
79 .saveElement(context, elementContext, generalElement, "Update Activity Spec General Info Element");
82 private ZusammenElement mapActivityDetailsToZusammenElement(ActivitySpecEntity entity, Action action) {
83 ZusammenElement generalElement = buildStructuralElement(ActivitySpecElementType.ACTIVITYSPEC.name(), action);
85 enrichElementInfoFromEntity(generalElement, entity);
86 enrichElementDataFromEntity(generalElement, entity);
87 return generalElement;
90 private void enrichElementInfoFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
91 element.getInfo().addProperty(InfoPropertyName.DESCRIPTION.getValue(), entity.getDescription());
92 element.getInfo().addProperty(InfoPropertyName.NAME.getValue(), entity.getName());
93 element.getInfo().addProperty(InfoPropertyName.CATEGORY.getValue(), entity.getCategoryList());
96 private void enrichElementDataFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
97 ActivitySpecData activitySpecData = new ActivitySpecData();
98 activitySpecData.setInputs(entity.getInputs());
99 activitySpecData.setOutputs(entity.getOutputs());
100 activitySpecData.setType(entity.getType());
101 activitySpecData.setContent(entity.getContent());
102 element.setData(new ByteArrayInputStream(JsonUtil.object2Json(activitySpecData).getBytes()));
105 private ActivitySpecEntity mapZusammenElementToActivityDetails(Element element) {
106 ActivitySpecEntity entity = new ActivitySpecEntity();
107 entity.setId(element.getElementId().getValue());
108 enrichEntityFromElementData(entity, element.getData());
109 enrichEntityFromElementInfo(entity, element.getInfo());
113 private void enrichEntityFromElementData(ActivitySpecEntity entity, InputStream data) {
114 ActivitySpecData activitySpecData = JsonUtil.json2Object(data, ActivitySpecData.class);
115 if (Objects.nonNull(activitySpecData)) {
116 entity.setInputs(activitySpecData.getInputs());
117 entity.setOutputs(activitySpecData.getOutputs());
118 entity.setType(activitySpecData.getType());
119 entity.setContent(activitySpecData.getContent());
123 private void enrichEntityFromElementInfo(ActivitySpecEntity entity, Info info) {
124 entity.setName(info.getProperty(InfoPropertyName.NAME.getValue()));
125 entity.setDescription(info.getProperty(InfoPropertyName.DESCRIPTION.getValue()));
126 entity.setCategoryList(info.getProperty(InfoPropertyName.CATEGORY.getValue()));
129 public enum InfoPropertyName {
130 DESCRIPTION("description"), NAME("name"), CATEGORY(ActivitySpecConstant.CATEGORY_ATTRIBUTE_NAME);
132 private final String value;
134 InfoPropertyName(String value) {
138 public String getValue() {