Add activity spec code
[sdc/sdc-workflow-designer.git] / workflow-designer-be / src / main / java / org / onap / sdc / workflow / persistence / impl / ActivitySpecRepositoryImpl.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.sdc.workflow.persistence.impl;
18
19 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
20 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
21
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;
41
42 @Repository
43 public class ActivitySpecRepositoryImpl implements ActivitySpecRepository {
44
45     private final ZusammenAdaptor zusammenAdaptor;
46
47     @Autowired
48     public ActivitySpecRepositoryImpl(ZusammenAdaptor zusammenAdaptor) {
49         this.zusammenAdaptor = zusammenAdaptor;
50     }
51
52     @Override
53     public void create(ActivitySpecEntity activitySpec) {
54         SessionContext context = createSessionContext();
55         ZusammenElement generalElement = mapActivityDetailsToZusammenElement(activitySpec, Action.CREATE);
56
57         ElementContext elementContext = new ElementContext(activitySpec.getId(), activitySpec.getVersion().getId());
58         zusammenAdaptor
59                 .saveElement(context, elementContext, generalElement, "Create Activity Spec General Info Element");
60     }
61
62     @Override
63     public ActivitySpecEntity get(ActivitySpecEntity entity) {
64         SessionContext context = createSessionContext();
65
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);
70     }
71
72     @Override
73     public void update(ActivitySpecEntity entity) {
74         SessionContext context = createSessionContext();
75         ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.UPDATE);
76
77         ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId());
78         zusammenAdaptor
79                 .saveElement(context, elementContext, generalElement, "Update Activity Spec General Info Element");
80     }
81
82     private ZusammenElement mapActivityDetailsToZusammenElement(ActivitySpecEntity entity, Action action) {
83         ZusammenElement generalElement = buildStructuralElement(ActivitySpecElementType.ACTIVITYSPEC.name(), action);
84
85         enrichElementInfoFromEntity(generalElement, entity);
86         enrichElementDataFromEntity(generalElement, entity);
87         return generalElement;
88     }
89
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());
94     }
95
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()));
103     }
104
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());
110         return entity;
111     }
112
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());
120         }
121     }
122
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()));
127     }
128
129     public enum InfoPropertyName {
130         DESCRIPTION("description"), NAME("name"), CATEGORY(ActivitySpecConstant.CATEGORY_ATTRIBUTE_NAME);
131
132         private final String value;
133
134         InfoPropertyName(String value) {
135             this.value = value;
136         }
137
138         public String getValue() {
139             return value;
140         }
141     }
142
143 }