aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/migration/1702_to_1707_zusammen/src/main/java/org/openecomp/core/migration/store/ElementHandler.java
blob: 34e7d79001f97bacc9a193319dd4e2cf703d00de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.openecomp.core.migration.store;

import com.amdocs.zusammen.datatypes.Id;
import com.amdocs.zusammen.datatypes.Namespace;
import com.amdocs.zusammen.datatypes.SessionContext;
import com.amdocs.zusammen.datatypes.item.Info;
import com.amdocs.zusammen.datatypes.item.Relation;
import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext;
import com.amdocs.zusammen.sdk.collaboration.types.CollaborationElement;
import org.openecomp.core.migration.MigrationMain;
import org.openecomp.core.model.types.ServiceTemplate;
import org.openecomp.core.zusammen.plugin.ZusammenPluginUtil;
import org.openecomp.core.zusammen.plugin.dao.impl.CassandraElementRepository;
import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ElementHandler {

  private static final String GLOBAL_USER = "GLOBAL_USER";

  public static void save(SessionContext context,
                          CassandraElementRepository cassandraElementRepository,
                          String itemId, Version versionId,
                          CollaborationElement[] elements) {

    ElementEntityContext elementContext;
    for (CollaborationElement element : elements) {

      elementContext = new ElementEntityContext(GLOBAL_USER, new Id(itemId), getVersionId(itemId,
          versionId));
      ElementEntity elementEntity = ZusammenPluginUtil.getElementEntity(element);

      cassandraElementRepository.createNamespace(context, elementContext, elementEntity);

      cassandraElementRepository.create(context, elementContext, elementEntity);

      if (isActiveVersion(itemId, versionId)) {
        elementContext =
            new ElementEntityContext(GLOBAL_USER, new Id(itemId), new Id(versionId.toString()));
        cassandraElementRepository.create(context, elementContext, elementEntity);

      }
    }
  }

  public static CollaborationElement getElementEntity(String itemId,
                                                      String versionId,
                                                      String elementId,
                                                      List<String> elementPath,
                                                      Info info,
                                                      Collection<Relation> relations,
                                                      List<String> subElements,
                                                      byte[] data) {
    Namespace namespace = new Namespace();
    for (String pathElementId : elementPath) {
      namespace = new Namespace(namespace, new Id(pathElementId));
    }
    if (namespace.getValue() == null || namespace.getValue().equals("")) {
      namespace = Namespace.ROOT_NAMESPACE;
    }
    CollaborationElement elementEntity = new CollaborationElement(new Id(itemId), new Id(versionId),
        namespace, new Id(elementId));

    Id parentId = namespace.getParentElementId() != null ? namespace.getParentElementId() : Id.ZERO;
    elementEntity.setParentId(parentId);
    elementEntity.setInfo(info);
    elementEntity.setRelations(relations);
    if (subElements != null) {
      Set<Id> subElementSet = new HashSet<>();

      subElements.forEach(subElement -> subElementSet.add(new Id(subElement)));

    }
    if (data != null) {
      elementEntity.setData(new ByteArrayInputStream(data));
    }

    return elementEntity;
  }

  public static List<String> getElementPath(String... paths) {
    List<String> pathList = new ArrayList<>();
    if (paths != null) {
      Collections.addAll(pathList, paths);
    }
    return pathList;
  }

  public static Info getStructuralElementInfo(String elementName) {
    Info info = new Info();
    info.setName(elementName);
    return info;
  }


  private static Id getVersionId(String itemId, Version versionId) {
    VersionInfoEntity versionInfo =
        MigrationMain.versionInfoMap.get(itemId);
    if (versionInfo == null) {
      return new Id(versionId.toString());
    }
    Version lastVersion = versionInfo.getCandidate() != null ? versionInfo.getCandidate()
        .getVersion()
        : versionInfo.getActiveVersion();

    if (lastVersion.equals(versionId)) {
      return new Id(itemId);
    } else {
      return new Id(versionId.toString());
    }
  }

  private static boolean isActiveVersion(String itemId, Version versionId) {
    VersionInfoEntity versionInfo =
        MigrationMain.versionInfoMap.get(itemId);
    return versionInfo != null && versionInfo.getActiveVersion().equals(versionId);
  }


  public static Info getServiceModelElementInfo(String vspServiceModelEntityId,
                                                ServiceTemplate serviceTemplate) {
    Info info = ElementHandler.getStructuralElementInfo(vspServiceModelEntityId);
    info.addProperty("base", serviceTemplate.getBaseName());
    return info;

  }
}