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.activityspec.be.dao.impl;
19 import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
20 import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
21 import com.amdocs.zusammen.datatypes.SessionContext;
22 import com.amdocs.zusammen.datatypes.item.Action;
23 import com.amdocs.zusammen.datatypes.item.ElementContext;
24 import com.amdocs.zusammen.datatypes.item.Info;
25 import org.onap.sdc.activityspec.be.dao.ActivitySpecDao;
26 import org.onap.sdc.activityspec.be.dao.types.ActivitySpecEntity;
27 import org.onap.sdc.activityspec.be.datatypes.ActivitySpecData;
28 import org.onap.sdc.activityspec.be.datatypes.ElementType;
29 import org.onap.sdc.activityspec.utils.ActivitySpecConstant;
30 import org.openecomp.core.utilities.json.JsonUtil;
31 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
33 import java.io.ByteArrayInputStream;
34 import java.io.InputStream;
35 import java.util.Objects;
36 import java.util.Optional;
38 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
39 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
41 public class ActivitySpecDaoZusammenImpl implements ActivitySpecDao {
43 private final ZusammenAdaptor zusammenAdaptor;
45 public ActivitySpecDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
46 this.zusammenAdaptor = zusammenAdaptor;
50 public void create(ActivitySpecEntity entity) {
51 SessionContext context = createSessionContext();
52 ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.CREATE);
54 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId());
56 .saveElement(context, elementContext, generalElement, "Create Activity Spec General Info Element");
60 public ActivitySpecEntity get(ActivitySpecEntity entity) {
61 SessionContext context = createSessionContext();
63 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId());
64 Optional<Element> element =
65 zusammenAdaptor.getElementByName(context, elementContext, null, ElementType.ACTIVITYSPEC.name());
66 if (element.isPresent()) {
67 return mapZusammenElementToActivityDetails(element.get());
74 public void update(ActivitySpecEntity entity) {
75 SessionContext context = createSessionContext();
76 ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.UPDATE);
78 ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId());
80 .saveElement(context, elementContext, generalElement, "Update Activity Spec General Info Element");
84 private ActivitySpecEntity mapZusammenElementToActivityDetails(Element element) {
85 ActivitySpecEntity entity = new ActivitySpecEntity();
86 entity.setId(element.getElementId().getValue());
87 enrichEntityFromElementData(entity, element.getData());
88 enrichEntityFromElementInfo(entity, element.getInfo());
92 private ZusammenElement mapActivityDetailsToZusammenElement(ActivitySpecEntity entity, Action action) {
93 ZusammenElement generalElement = buildStructuralElement(ElementType.ACTIVITYSPEC.name(), action);
95 enrichElementInfoFromEntity(generalElement, entity);
96 enrichElementDataFromEntity(generalElement, entity);
97 return generalElement;
101 private void enrichEntityFromElementInfo(ActivitySpecEntity entity, Info info) {
102 entity.setName(info.getProperty(InfoPropertyName.NAME.getValue()));
103 entity.setDescription(info.getProperty(InfoPropertyName.DESCRIPTION.getValue()));
104 entity.setCategoryList(info.getProperty(InfoPropertyName.CATEGORY.getValue()));
107 private void enrichEntityFromElementData(ActivitySpecEntity entity, InputStream data) {
108 ActivitySpecData activitySpecData = JsonUtil.json2Object(data, ActivitySpecData.class);
109 if (Objects.nonNull(activitySpecData)) {
110 entity.setInputs(activitySpecData.getInputs());
111 entity.setOutputs(activitySpecData.getOutputs());
112 entity.setType(activitySpecData.getType());
113 entity.setContent(activitySpecData.getContent());
117 private void enrichElementInfoFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
118 element.getInfo().addProperty(InfoPropertyName.DESCRIPTION.getValue(), entity.getDescription());
119 element.getInfo().addProperty(InfoPropertyName.NAME.getValue(), entity.getName());
120 element.getInfo().addProperty(InfoPropertyName.CATEGORY.getValue(), entity.getCategoryList());
124 private void enrichElementDataFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
125 ActivitySpecData activitySpecData = new ActivitySpecData();
126 activitySpecData.setInputs(entity.getInputs());
127 activitySpecData.setOutputs(entity.getOutputs());
128 activitySpecData.setType(entity.getType());
129 activitySpecData.setContent(entity.getContent());
130 element.setData(new ByteArrayInputStream(JsonUtil.object2Json(activitySpecData).getBytes()));
133 public enum InfoPropertyName {
134 DESCRIPTION("description"), NAME("name"), CATEGORY(ActivitySpecConstant.CATEGORY_ATTRIBUTE_NAME);
136 private final String value;
138 InfoPropertyName(String value) {
142 public String getValue() {