fix incorrect dependency
[sdc.git] / openecomp-be / tools / migration / 1702_to_1707_zusammen / src / main / java / org / openecomp / core / migration / store / ElementHandler.java
1 package org.openecomp.core.migration.store;
2
3 import com.amdocs.zusammen.datatypes.Id;
4 import com.amdocs.zusammen.datatypes.Namespace;
5 import com.amdocs.zusammen.datatypes.SessionContext;
6 import com.amdocs.zusammen.datatypes.item.Info;
7 import com.amdocs.zusammen.datatypes.item.Relation;
8 import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext;
9 import com.amdocs.zusammen.sdk.collaboration.types.CollaborationElement;
10 import org.openecomp.core.migration.MigrationMain;
11 import org.openecomp.core.model.types.ServiceTemplate;
12 import org.openecomp.core.zusammen.plugin.ZusammenPluginUtil;
13 import org.openecomp.core.zusammen.plugin.dao.impl.CassandraElementRepository;
14 import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
15 import org.openecomp.sdc.versioning.dao.types.Version;
16 import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;
17
18 import java.io.ByteArrayInputStream;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25
26 public class ElementHandler {
27
28   private static final String GLOBAL_USER = "GLOBAL_USER";
29
30   public static void save(SessionContext context,
31                           CassandraElementRepository cassandraElementRepository,
32                           String itemId, Version versionId,
33                           CollaborationElement[] elements) {
34
35     ElementEntityContext elementContext;
36     for (CollaborationElement element : elements) {
37
38       elementContext = new ElementEntityContext(GLOBAL_USER, new Id(itemId), getVersionId(itemId,
39           versionId));
40       ElementEntity elementEntity = ZusammenPluginUtil.getElementEntity(element);
41
42       cassandraElementRepository.createNamespace(context, elementContext, elementEntity);
43
44       cassandraElementRepository.create(context, elementContext, elementEntity);
45
46       if (isActiveVersion(itemId, versionId)) {
47         elementContext =
48             new ElementEntityContext(GLOBAL_USER, new Id(itemId), new Id(versionId.toString()));
49         cassandraElementRepository.create(context, elementContext, elementEntity);
50
51       }
52     }
53   }
54
55   public static CollaborationElement getElementEntity(String itemId,
56                                                       String versionId,
57                                                       String elementId,
58                                                       List<String> elementPath,
59                                                       Info info,
60                                                       Collection<Relation> relations,
61                                                       List<String> subElements,
62                                                       byte[] data) {
63     Namespace namespace = new Namespace();
64     for (String pathElementId : elementPath) {
65       namespace = new Namespace(namespace, new Id(pathElementId));
66     }
67     if (namespace.getValue() == null || namespace.getValue().equals("")) {
68       namespace = Namespace.ROOT_NAMESPACE;
69     }
70     CollaborationElement elementEntity = new CollaborationElement(new Id(itemId), new Id(versionId),
71         namespace, new Id(elementId));
72
73     Id parentId = namespace.getParentElementId() != null ? namespace.getParentElementId() : Id.ZERO;
74     elementEntity.setParentId(parentId);
75     elementEntity.setInfo(info);
76     elementEntity.setRelations(relations);
77     if (subElements != null) {
78       Set<Id> subElementSet = new HashSet<>();
79
80       subElements.forEach(subElement -> subElementSet.add(new Id(subElement)));
81
82     }
83     if (data != null) {
84       elementEntity.setData(new ByteArrayInputStream(data));
85     }
86
87     return elementEntity;
88   }
89
90   public static List<String> getElementPath(String... paths) {
91     List<String> pathList = new ArrayList<>();
92     if (paths != null) {
93       Collections.addAll(pathList, paths);
94     }
95     return pathList;
96   }
97
98   public static Info getStructuralElementInfo(String elementName) {
99     Info info = new Info();
100     info.setName(elementName);
101     return info;
102   }
103
104
105   private static Id getVersionId(String itemId, Version versionId) {
106     VersionInfoEntity versionInfo =
107         MigrationMain.getVersionInfoMap().get(itemId);
108     if (versionInfo == null) {
109       return new Id(versionId.toString());
110     }
111     Version lastVersion = versionInfo.getCandidate() != null ? versionInfo.getCandidate()
112         .getVersion()
113         : versionInfo.getActiveVersion();
114
115     if (lastVersion.equals(versionId)) {
116       return new Id(itemId);
117     } else {
118       return new Id(versionId.toString());
119     }
120   }
121
122   private static boolean isActiveVersion(String itemId, Version versionId) {
123     VersionInfoEntity versionInfo =
124         MigrationMain.getVersionInfoMap().get(itemId);
125     return versionInfo != null && versionInfo.getActiveVersion().equals(versionId);
126   }
127
128
129   public static Info getServiceModelElementInfo(String vspServiceModelEntityId,
130                                                 ServiceTemplate serviceTemplate) {
131     Info info = ElementHandler.getStructuralElementInfo(vspServiceModelEntityId);
132     info.addProperty("base", serviceTemplate.getBaseName());
133     return info;
134
135   }
136 }