Unit Tests
[sdc.git] / openecomp-be / lib / openecomp-core-lib / openecomp-zusammen-lib / openecomp-zusammen-plugin / src / main / java / org / openecomp / core / zusammen / plugin / ZusammenPluginUtil.java
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.core.zusammen.plugin;
18
19 import com.amdocs.zusammen.datatypes.Id;
20 import com.amdocs.zusammen.datatypes.SessionContext;
21 import com.amdocs.zusammen.datatypes.Space;
22 import com.amdocs.zusammen.datatypes.item.Action;
23 import com.amdocs.zusammen.datatypes.item.ElementContext;
24 import com.amdocs.zusammen.datatypes.item.ItemVersion;
25 import com.amdocs.zusammen.datatypes.item.ItemVersionChange;
26 import com.amdocs.zusammen.datatypes.item.ItemVersionData;
27 import com.amdocs.zusammen.sdk.collaboration.types.CollaborationElement;
28 import com.amdocs.zusammen.sdk.collaboration.types.CollaborationElementChange;
29 import com.amdocs.zusammen.sdk.state.types.StateElement;
30 import com.amdocs.zusammen.sdk.types.ElementDescriptor;
31 import com.amdocs.zusammen.utils.fileutils.FileUtils;
32 import com.amdocs.zusammen.utils.fileutils.json.JsonUtil;
33 import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
34 import org.openecomp.core.zusammen.plugin.dao.types.VersionEntity;
35 import org.openecomp.sdc.common.errors.SdcRuntimeException;
36
37 import java.io.ByteArrayInputStream;
38 import java.nio.ByteBuffer;
39 import java.security.MessageDigest;
40 import java.security.NoSuchAlgorithmException;
41 import java.util.Base64;
42 import java.util.Date;
43
44 import static org.openecomp.core.zusammen.plugin.ZusammenPluginConstants.ROOT_ELEMENTS_PARENT_ID;
45
46 public class ZusammenPluginUtil {
47
48   private ZusammenPluginUtil() {
49     // prevent instantiation
50   }
51
52   public static String getSpaceName(SessionContext context, Space space) {
53     switch (space) {
54       case PUBLIC:
55         return ZusammenPluginConstants.PUBLIC_SPACE;
56       case PRIVATE:
57         return getPrivateSpaceName(context);
58       default:
59         throw new IllegalArgumentException(String.format("Space %s is not supported.", space));
60     }
61   }
62
63   public static String getPrivateSpaceName(SessionContext context) {
64     return context.getUser().getUserName();
65   }
66
67   public static ElementContext getPrivateElementContext(ElementContext elementContext) {
68     return new ElementContext(elementContext.getItemId(),elementContext.getVersionId(),Id.ZERO);
69   }
70
71
72   public static VersionEntity convertToVersionEntity(Id versionId, Id baseVersionId,
73                                                      Date creationTime,
74                                                      Date modificationTime) {
75     VersionEntity version = new VersionEntity(versionId);
76     version.setBaseId(baseVersionId);
77     version.setCreationTime(creationTime);
78     version.setModificationTime(modificationTime);
79     return version;
80   }
81
82   public static ItemVersion convertToItemVersion(VersionEntity versionEntity,
83                                                  ItemVersionData itemVersionData) {
84     ItemVersion itemVersion = new ItemVersion();
85     itemVersion.setId(versionEntity.getId());
86
87     itemVersion.setBaseId(versionEntity.getBaseId());
88     itemVersion.setCreationTime(versionEntity.getCreationTime());
89     itemVersion.setModificationTime(versionEntity.getModificationTime());
90     itemVersion.setData(itemVersionData);
91     return itemVersion;
92   }
93
94   public static ElementEntity convertToElementEntity(CollaborationElement element) {
95
96     ElementEntity elementEntity = new ElementEntity(element.getId());
97     elementEntity.setNamespace(element.getNamespace());
98     elementEntity.setParentId(element.getParentId() == null
99         ? ROOT_ELEMENTS_PARENT_ID
100         : element.getParentId());
101
102
103     elementEntity.setInfo(element.getInfo());
104
105     elementEntity.setRelations(element.getRelations());
106
107     if (element.getData() != null) {
108       elementEntity.setData(ByteBuffer.wrap(FileUtils.toByteArray(element.getData())));
109     }
110     if (element.getSearchableData() != null) {
111       elementEntity.setSearchableData(
112           ByteBuffer.wrap(FileUtils.toByteArray(element.getSearchableData())));
113     }
114     if (element.getVisualization() != null) {
115       elementEntity.setVisualization(
116           ByteBuffer.wrap(FileUtils.toByteArray(element.getVisualization())));
117     }
118     elementEntity.setElementHash(new Id(calculateElementHash(elementEntity)));
119
120     return elementEntity;
121   }
122
123   public static ElementDescriptor convertToElementDescriptor(
124       ElementContext elementContext, ElementEntity elementEntity) {
125     if (elementEntity == null) {
126       return null;
127     }
128     ElementDescriptor element = new ElementDescriptor(elementContext.getItemId(),
129         elementContext.getVersionId(), elementEntity.getNamespace(), elementEntity.getId());
130
131     mapElementEntityToDescriptor(elementEntity, element);
132     return element;
133   }
134
135   public static CollaborationElement convertToCollaborationElement(
136       ElementContext elementContext, ElementEntity elementEntity) {
137     CollaborationElement element = new CollaborationElement(elementContext.getItemId(),
138         elementContext.getVersionId(), elementEntity.getNamespace(), elementEntity.getId());
139
140     mapElementEntityToDescriptor(elementEntity, element);
141
142     if (elementEntity.getData() != null) {
143       element.setData(new ByteArrayInputStream(elementEntity.getData().array()));
144     }
145     if (elementEntity.getSearchableData() != null) {
146       element.setSearchableData(
147           new ByteArrayInputStream(elementEntity.getSearchableData().array()));
148     }
149     if (elementEntity.getVisualization() != null) {
150       element.setVisualization(new ByteArrayInputStream(elementEntity.getVisualization().array()));
151     }
152     return element;
153   }
154
155   public static CollaborationElementChange convertToElementChange(
156       ElementContext changedElementContext, ElementEntity changedElement, Action action) {
157     CollaborationElementChange elementChange = new CollaborationElementChange();
158     elementChange.setElement(convertToCollaborationElement(changedElementContext, changedElement));
159     elementChange.setAction(action);
160     return elementChange;
161   }
162
163   public static ItemVersionChange convertToVersionChange(ElementContext elementContext,
164                                                          ElementEntity versionDataElement,
165                                                          Action action) {
166     ItemVersionChange versionChange = new ItemVersionChange();
167
168     ItemVersion itemVersion = new ItemVersion();
169     itemVersion.setId(elementContext.getVersionId());
170
171     itemVersion.setData(convertToVersionData(versionDataElement));
172
173     versionChange.setItemVersion(itemVersion);
174     versionChange.setAction(action);
175     return versionChange;
176   }
177
178   public static ItemVersionData convertToVersionData(ElementEntity versionDataElement) {
179     ItemVersionData versionData = new ItemVersionData();
180     versionData.setInfo(versionDataElement.getInfo());
181     versionData.setRelations(versionDataElement.getRelations());
182     return versionData;
183   }
184
185   private static void mapElementEntityToDescriptor(ElementEntity elementEntity,
186                                                    ElementDescriptor elementDescriptor) {
187     Id parentId = ROOT_ELEMENTS_PARENT_ID.equals(elementEntity.getParentId())
188         ? null
189         : elementEntity.getParentId();
190
191     elementDescriptor.setParentId(parentId);
192     elementDescriptor.setInfo(elementEntity.getInfo());
193     elementDescriptor.setRelations(elementEntity.getRelations());
194     elementDescriptor.setSubElements(elementEntity.getSubElementIds());
195   }
196
197   public static String calculateElementHash(ElementEntity elementEntity) {
198
199     StringBuilder elementHash = new StringBuilder();
200     if (elementEntity.getData() != null) {
201       elementHash.append(calculateSHA1(elementEntity.getData().array()));
202     } else {
203       elementHash.append("0");
204     }
205     elementHash.append("_");
206     if (elementEntity.getVisualization() != null) {
207       elementHash.append(calculateSHA1(elementEntity.getVisualization().array()));
208     } else {
209       elementHash.append("0");
210     }
211     elementHash.append("_");
212
213     if (elementEntity.getSearchableData() != null) {
214       elementHash.append(calculateSHA1(elementEntity.getSearchableData().array()));
215     } else {
216       elementHash.append("0");
217     }
218     elementHash.append("_");
219
220     if (elementEntity.getInfo() != null) {
221       elementHash.append(calculateSHA1(JsonUtil.object2Json(elementEntity.getInfo()).getBytes()));
222     } else {
223       elementHash.append("0");
224     }
225     elementHash.append("_");
226
227     if (elementEntity.getRelations() != null) {
228       elementHash
229           .append(calculateSHA1(JsonUtil.object2Json(elementEntity.getRelations()).getBytes()));
230     } else {
231       elementHash.append("0");
232     }
233
234     return elementHash.toString();
235   }
236
237   private static String calculateSHA1(byte[] content2Convert) {
238     try {
239       return Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-1").digest(content2Convert));
240     } catch (NoSuchAlgorithmException e) {
241       throw new SdcRuntimeException(e);
242     }
243   }
244
245
246   public static StateElement getStateElement(ElementContext elementContext, ElementEntity
247       elementEntity) {
248     Id parentId = ROOT_ELEMENTS_PARENT_ID.equals(elementEntity.getParentId())
249         ? null
250         : elementEntity.getParentId();
251     StateElement element = new StateElement(elementContext.getItemId(),
252         elementContext.getVersionId(), elementEntity.getNamespace(), elementEntity.getId());
253
254     element.setParentId(parentId);
255     element.setInfo(elementEntity.getInfo());
256     element.setRelations(elementEntity.getRelations());
257     element.setSubElements(elementEntity.getSubElementIds());
258     return element;
259   }
260
261
262 }