summaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/store/VspGeneralLoader.java
blob: 1d9d2d97742a1440a39ded58fcf62f7657596e8c (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package org.openecomp.core.tools.store;

import com.amdocs.zusammen.datatypes.Id;
import com.amdocs.zusammen.datatypes.SessionContext;
import com.amdocs.zusammen.datatypes.item.Info;
import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext;
import org.openecomp.core.zusammen.plugin.dao.impl.CassandraElementRepository;
import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Objects;

public class VspGeneralLoader {

  public static final String NAME = "name";
  public static final String GENERAL = "General";

  private static CassandraElementRepository cassandraElementRepository =
          new CassandraElementRepository();

  public static Map<String, ElementEntity> load(SessionContext context,
                                                Map<String, List<String>> vspItemVersionsMap,
                                                Map<String, List<String>> vspItemChangeRefssMap) {
    Map<String, ElementEntity> elementEntityMap = new HashMap<>();
    System.setProperty("cassandra.dox.keystore", "zusammen_dox");

    Id entityId;
    Id itemId;
    Id changeRefId;
    for (Map.Entry<String, List<String>> entry : vspItemVersionsMap.entrySet()) {

      for (String version : entry.getValue()) {


        itemId = new Id(entry.getKey());
        changeRefId = new Id(version);
        entityId = getEntityIdByInfoNameValue(context, itemId, changeRefId, null, null, NAME,
                GENERAL);
        if (entityId != null) {
          Optional<ElementEntity> result =
                  cassandraElementRepository.get(context, new ElementEntityContext(
                                  context.getUser().getUserName(),
                                  itemId,
                                  changeRefId),
                          new ElementEntity(entityId));
          if (result.isPresent()) {
            elementEntityMap.put(buildKey(context, entry, version), result.get());
          }
        }
      }
    }


    for (Map.Entry<String, List<String>> entry : vspItemChangeRefssMap.entrySet()) {

      for (String changeRef : entry.getValue()) {


        itemId = new Id(entry.getKey());

        entityId = getEntityIdByInfoNameValue(context, itemId, null, changeRef,null, NAME,
                GENERAL);
        if (entityId != null) {
          ElementEntityContext elementContext = new ElementEntityContext(
                  context.getUser().getUserName(),
                  itemId,
                  null);
          elementContext.setChangeRef(changeRef);
          Optional<ElementEntity> result =
                  cassandraElementRepository.get(context, elementContext,
                          new ElementEntity(entityId));
          if (result.isPresent()) {
            elementEntityMap.put(buildKey(context, entry, changeRef), result.get());
          }
        }
      }
    }


    return elementEntityMap;
  }

  public static String buildKey(SessionContext context, Map.Entry<String, List<String>> entry, String version) {
    return String.format("%s_%s_%s", context.getUser().getUserName(), entry.getKey(), version);
  }

  private static Id getEntityIdByInfoNameValue(SessionContext context,
                                               Id itemId,
                                               Id versionId,
                                               String changeRef,
                                               Id elementId,
                                               String name,
                                               String value) {


    ElementEntityContext elementContext = new ElementEntityContext(
            context.getUser().getUserName(),
            itemId,
            versionId);
    if (changeRef != null) {
      elementContext.setChangeRef(changeRef);
    }
    Optional<ElementEntity> result =
            cassandraElementRepository.get(context, elementContext,
                    new ElementEntity(Id.ZERO));
    if (result.isPresent()) {
      ElementEntity elementEntity = result.get();
      return elementEntity.getSubElementIds().stream().filter(subelementId -> {
        ElementEntityContext subElementContext = new ElementEntityContext(
                context.getUser().getUserName(),
                itemId,
                versionId);
        if(changeRef!= null){
          subElementContext.setChangeRef(changeRef);
        }
        Optional<ElementEntity> subElementEntityOptional =
                cassandraElementRepository.get(context, subElementContext,
                        new ElementEntity(subelementId));
        if (subElementEntityOptional.isPresent()) {
          Info info = subElementEntityOptional.get().getInfo();
          if (isValid(name, info)) {
            return false;
          }
          if (NAME.equals(name)) {
            if (value.equals(info.getName())) {
              return true;
            }
          }
          if (value.equals(info.getProperty(name))) {
            return true;
          }
        }
        return false;

      }).findFirst().orElse(null);
    }
    return null;


  }

  private static boolean isValid(String name, Info info) {
    return Objects.isNull(info)|| Objects.isNull(info.getProperty(name));
  }


}