2 * Copyright © 2016-2017 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.openecomp.core.zusammen.plugin;
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;
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;
44 import static org.openecomp.core.zusammen.plugin.ZusammenPluginConstants.ROOT_ELEMENTS_PARENT_ID;
46 public class ZusammenPluginUtil {
48 private ZusammenPluginUtil() {
49 // prevent instantiation
52 public static String getSpaceName(SessionContext context, Space space) {
55 return ZusammenPluginConstants.PUBLIC_SPACE;
57 return getPrivateSpaceName(context);
59 throw new IllegalArgumentException(String.format("Space %s is not supported.", space));
63 public static String getPrivateSpaceName(SessionContext context) {
64 return context.getUser().getUserName();
67 public static ElementContext getPrivateElementContext(ElementContext elementContext) {
68 return new ElementContext(elementContext.getItemId(),elementContext.getVersionId(),Id.ZERO);
72 public static VersionEntity convertToVersionEntity(Id versionId, Id baseVersionId,
74 Date modificationTime) {
75 VersionEntity version = new VersionEntity(versionId);
76 version.setBaseId(baseVersionId);
77 version.setCreationTime(creationTime);
78 version.setModificationTime(modificationTime);
82 public static ItemVersion convertToItemVersion(VersionEntity versionEntity,
83 ItemVersionData itemVersionData) {
84 ItemVersion itemVersion = new ItemVersion();
85 itemVersion.setId(versionEntity.getId());
87 itemVersion.setBaseId(versionEntity.getBaseId());
88 itemVersion.setCreationTime(versionEntity.getCreationTime());
89 itemVersion.setModificationTime(versionEntity.getModificationTime());
90 itemVersion.setData(itemVersionData);
94 public static ElementEntity convertToElementEntity(CollaborationElement element) {
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());
103 elementEntity.setInfo(element.getInfo());
105 elementEntity.setRelations(element.getRelations());
107 if (element.getData() != null) {
108 elementEntity.setData(ByteBuffer.wrap(FileUtils.toByteArray(element.getData())));
110 if (element.getSearchableData() != null) {
111 elementEntity.setSearchableData(
112 ByteBuffer.wrap(FileUtils.toByteArray(element.getSearchableData())));
114 if (element.getVisualization() != null) {
115 elementEntity.setVisualization(
116 ByteBuffer.wrap(FileUtils.toByteArray(element.getVisualization())));
118 elementEntity.setElementHash(new Id(calculateElementHash(elementEntity)));
120 return elementEntity;
123 public static ElementDescriptor convertToElementDescriptor(
124 ElementContext elementContext, ElementEntity elementEntity) {
125 if (elementEntity == null) {
128 ElementDescriptor element = new ElementDescriptor(elementContext.getItemId(),
129 elementContext.getVersionId(), elementEntity.getNamespace(), elementEntity.getId());
131 mapElementEntityToDescriptor(elementEntity, element);
135 public static CollaborationElement convertToCollaborationElement(
136 ElementContext elementContext, ElementEntity elementEntity) {
137 CollaborationElement element = new CollaborationElement(elementContext.getItemId(),
138 elementContext.getVersionId(), elementEntity.getNamespace(), elementEntity.getId());
140 mapElementEntityToDescriptor(elementEntity, element);
142 if (elementEntity.getData() != null) {
143 element.setData(new ByteArrayInputStream(elementEntity.getData().array()));
145 if (elementEntity.getSearchableData() != null) {
146 element.setSearchableData(
147 new ByteArrayInputStream(elementEntity.getSearchableData().array()));
149 if (elementEntity.getVisualization() != null) {
150 element.setVisualization(new ByteArrayInputStream(elementEntity.getVisualization().array()));
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;
163 public static ItemVersionChange convertToVersionChange(ElementContext elementContext,
164 ElementEntity versionDataElement,
166 ItemVersionChange versionChange = new ItemVersionChange();
168 ItemVersion itemVersion = new ItemVersion();
169 itemVersion.setId(elementContext.getVersionId());
171 itemVersion.setData(convertToVersionData(versionDataElement));
173 versionChange.setItemVersion(itemVersion);
174 versionChange.setAction(action);
175 return versionChange;
178 public static ItemVersionData convertToVersionData(ElementEntity versionDataElement) {
179 ItemVersionData versionData = new ItemVersionData();
180 versionData.setInfo(versionDataElement.getInfo());
181 versionData.setRelations(versionDataElement.getRelations());
185 private static void mapElementEntityToDescriptor(ElementEntity elementEntity,
186 ElementDescriptor elementDescriptor) {
187 Id parentId = ROOT_ELEMENTS_PARENT_ID.equals(elementEntity.getParentId())
189 : elementEntity.getParentId();
191 elementDescriptor.setParentId(parentId);
192 elementDescriptor.setInfo(elementEntity.getInfo());
193 elementDescriptor.setRelations(elementEntity.getRelations());
194 elementDescriptor.setSubElements(elementEntity.getSubElementIds());
197 public static String calculateElementHash(ElementEntity elementEntity) {
199 StringBuilder elementHash = new StringBuilder();
200 if (elementEntity.getData() != null) {
201 elementHash.append(calculateSHA1(elementEntity.getData().array()));
203 elementHash.append("0");
205 elementHash.append("_");
206 if (elementEntity.getVisualization() != null) {
207 elementHash.append(calculateSHA1(elementEntity.getVisualization().array()));
209 elementHash.append("0");
211 elementHash.append("_");
213 if (elementEntity.getSearchableData() != null) {
214 elementHash.append(calculateSHA1(elementEntity.getSearchableData().array()));
216 elementHash.append("0");
218 elementHash.append("_");
220 if (elementEntity.getInfo() != null) {
221 elementHash.append(calculateSHA1(JsonUtil.object2Json(elementEntity.getInfo()).getBytes()));
223 elementHash.append("0");
225 elementHash.append("_");
227 if (elementEntity.getRelations() != null) {
229 .append(calculateSHA1(JsonUtil.object2Json(elementEntity.getRelations()).getBytes()));
231 elementHash.append("0");
234 return elementHash.toString();
237 private static String calculateSHA1(byte[] content2Convert) {
239 return Base64.getEncoder().encodeToString(MessageDigest.getInstance("SHA-1").digest(content2Convert));
240 } catch (NoSuchAlgorithmException e) {
241 throw new SdcRuntimeException(e);
246 public static StateElement getStateElement(ElementContext elementContext, ElementEntity
248 Id parentId = ROOT_ELEMENTS_PARENT_ID.equals(elementEntity.getParentId())
250 : elementEntity.getParentId();
251 StateElement element = new StateElement(elementContext.getItemId(),
252 elementContext.getVersionId(), elementEntity.getNamespace(), elementEntity.getId());
254 element.setParentId(parentId);
255 element.setInfo(elementEntity.getInfo());
256 element.setRelations(elementEntity.getRelations());
257 element.setSubElements(elementEntity.getSubElementIds());