0dabcca9847f4bfeded4613b94e402d0ec414e9e
[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.setInputs(activitySpecData.getInputs());
116       entity.setOutputs(activitySpecData.getOutputs());
117       entity.setType(activitySpecData.getType());
118       entity.setContent(activitySpecData.getContent());
119     }
120   }
121
122   private void enrichElementInfoFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
123     element.getInfo().addProperty(InfoPropertyName.DESCRIPTION.getValue(), entity.getDescription());
124     element.getInfo().addProperty(InfoPropertyName.NAME.getValue(), entity.getName());
125     element.getInfo().addProperty(InfoPropertyName.CATEGORY.getValue(),
126         entity.getCategoryList());
127   }
128
129
130   private void enrichElementDataFromEntity(ZusammenElement element, ActivitySpecEntity entity) {
131     ActivitySpecData activitySpecData = new ActivitySpecData();
132     activitySpecData.setInputs(entity.getInputs());
133     activitySpecData.setOutputs(entity.getOutputs());
134     activitySpecData.setType(entity.getType());
135     activitySpecData.setContent(entity.getContent());
136     element.setData(new ByteArrayInputStream(JsonUtil.object2Json(activitySpecData).getBytes()));
137   }
138
139   public enum InfoPropertyName {
140     DESCRIPTION("description"),
141     NAME("name"),
142     CATEGORY(ActivitySpecConstant.CATEGORY_ATTRIBUTE_NAME);
143
144     private final String value;
145
146     InfoPropertyName(String value) {
147       this.value = value;
148     }
149
150     public String getValue() {
151       return value;
152     }
153   }
154
155 }