d9d495a01a2f65c5a78e471fe116fd3e84e07661
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 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.openecomp.activityspec.be.dao.impl;
18
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
26 import org.openecomp.activityspec.be.dao.ActivitySpecDao;
27 import org.openecomp.activityspec.be.dao.types.ActivitySpecEntity;
28 import org.openecomp.activityspec.utils.ActivitySpecConstant;
29 import org.openecomp.activityspec.be.datatypes.ActivitySpecData;
30 import org.openecomp.activityspec.be.datatypes.ElementType;
31 import org.openecomp.core.utilities.json.JsonUtil;
32 import org.openecomp.core.zusammen.api.ZusammenAdaptor;
33
34 import java.io.ByteArrayInputStream;
35 import java.io.InputStream;
36 import java.util.Objects;
37 import java.util.Optional;
38
39 import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
40 import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;
41
42 public class ActivitySpecDaoZusammenImpl implements ActivitySpecDao {
43
44   private final ZusammenAdaptor zusammenAdaptor;
45
46   public ActivitySpecDaoZusammenImpl(ZusammenAdaptor zusammenAdaptor) {
47     this.zusammenAdaptor = zusammenAdaptor;
48   }
49
50   @Override
51   public void create(ActivitySpecEntity entity) {
52     SessionContext context = createSessionContext();
53     ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.CREATE);
54
55     ElementContext elementContext =
56         new ElementContext(entity.getId(), entity.getVersion().getId());
57     zusammenAdaptor.saveElement(context, elementContext, generalElement,
58         "Create Activity Spec General Info Element");
59   }
60
61   @Override
62   public ActivitySpecEntity get(ActivitySpecEntity entity) {
63     SessionContext context = createSessionContext();
64
65     ElementContext elementContext = new ElementContext(entity.getId(), entity.getVersion().getId());
66     Optional<Element> element = zusammenAdaptor.getElementByName(context, elementContext,
67         null, ElementType.ACTIVITYSPEC.name());
68     if (element.isPresent()) {
69       return mapZusammenElementToActivityDetails(element.get());
70     } else {
71       return null;
72     }
73   }
74
75   @Override
76   public void update(ActivitySpecEntity entity) {
77     SessionContext context = createSessionContext();
78     ZusammenElement generalElement = mapActivityDetailsToZusammenElement(entity, Action.UPDATE);
79
80     ElementContext elementContext =
81         new ElementContext(entity.getId(), entity.getVersion().getId());
82     zusammenAdaptor.saveElement(context, elementContext, generalElement,
83         "Update Activity Spec General Info Element");
84   }
85
86
87   private ActivitySpecEntity mapZusammenElementToActivityDetails(Element element) {
88     ActivitySpecEntity entity = new ActivitySpecEntity();
89     entity.setId(element.getElementId().getValue());
90     enrichEntityFromElementData(entity, element.getData());
91     enrichEntityFromElementInfo(entity, element.getInfo());
92     return entity;
93   }
94
95   private ZusammenElement mapActivityDetailsToZusammenElement(ActivitySpecEntity entity,
96                                                               Action action) {
97     ZusammenElement generalElement =
98         buildStructuralElement(ElementType.ACTIVITYSPEC.name(), action);
99
100     enrichElementInfoFromEntity(generalElement, entity);
101     enrichElementDataFromEntity(generalElement, entity);
102     return generalElement;
103   }
104
105
106   private void enrichEntityFromElementInfo(ActivitySpecEntity entity, Info info) {
107     entity.setName(info.getProperty(InfoPropertyName.NAME.getValue()));
108     entity.setDescription(info.getProperty(InfoPropertyName.DESCRIPTION.getValue()));
109     entity.setCategoryList(info.getProperty(InfoPropertyName.CATEGORY.getValue()));
110   }
111
112   private void enrichEntityFromElementData(ActivitySpecEntity entity, InputStream data) {
113     ActivitySpecData activitySpecData = JsonUtil.json2Object(data, ActivitySpecData.class);
114     if (Objects.nonNull(activitySpecData)) {
115       entity.setInputParameters(activitySpecData.getInputParameters());
116       entity.setOutputParameters(activitySpecData.getOutputParameters());
117     }
118   }
119
120   private void enrichElementInfoFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
121     element.getInfo().addProperty(InfoPropertyName.DESCRIPTION.getValue(), entity.getDescription());
122     element.getInfo().addProperty(InfoPropertyName.NAME.getValue(), entity.getName());
123     element.getInfo().addProperty(InfoPropertyName.CATEGORY.getValue(),
124         entity.getCategoryList());
125   }
126
127
128   private void enrichElementDataFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
129     ActivitySpecData activitySpecData = new ActivitySpecData();
130     activitySpecData.setInputParameters(entity.getInputParameters());
131     activitySpecData.setOutputParameters(entity.getOutputParameters());
132     element.setData(new ByteArrayInputStream(JsonUtil.object2Json(activitySpecData).getBytes()));
133   }
134
135   public enum InfoPropertyName {
136     DESCRIPTION("description"),
137     NAME("name"),
138     CATEGORY(ActivitySpecConstant.CATEGORY_ATTRIBUTE_NAME);
139
140     private final String value;
141
142     InfoPropertyName(String value) {
143       this.value = value;
144     }
145
146     public String getValue() {
147       return value;
148     }
149   }
150
151 }