aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-vendor-license-lib/openecomp-sdc-vendor-license-core/src/main/java/org/openecomp/sdc/vendorlicense/dao/impl/zusammen/EntitlementPoolZusammenDaoImpl.java
blob: b3adddb3d27cf448e909b14328af45b03ebc5582 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package org.openecomp.sdc.vendorlicense.dao.impl.zusammen;

import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo;
import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement;
import com.amdocs.zusammen.datatypes.Id;
import com.amdocs.zusammen.datatypes.SessionContext;
import com.amdocs.zusammen.datatypes.item.Action;
import com.amdocs.zusammen.datatypes.item.ElementContext;
import com.amdocs.zusammen.datatypes.item.Info;
import org.openecomp.core.zusammen.api.ZusammenAdaptor;
import org.openecomp.core.zusammen.api.ZusammenUtil;
import org.openecomp.sdc.datatypes.model.ElementType;
import org.openecomp.sdc.vendorlicense.dao.EntitlementPoolDao;
import org.openecomp.sdc.vendorlicense.dao.impl.zusammen.convertor.ElementToEntitlementPoolConvertor;
import org.openecomp.sdc.vendorlicense.dao.types.EntitlementPoolEntity;
import org.openecomp.types.ElementPropertyName;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.openecomp.core.zusammen.api.ZusammenUtil.buildElement;

public class EntitlementPoolZusammenDaoImpl implements EntitlementPoolDao {

  private ZusammenAdaptor zusammenAdaptor;

  public EntitlementPoolZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) {
    this.zusammenAdaptor = zusammenAdaptor;
  }

  @Override
  public void registerVersioning(String versionableEntityType) {
    //no need
  }

  @Override
  public void create(EntitlementPoolEntity entitlementPool) {
    ZusammenElement entitlementPoolElement =
        buildEntitlementPoolElement(entitlementPool, Action.CREATE);

    ZusammenElement entitlementPoolsElement =
        ZusammenUtil.buildStructuralElement(ElementType.EntitlementPools, Action.IGNORE);

    ZusammenElement limitsElement =
        ZusammenUtil.buildStructuralElement(ElementType.Limits, Action.CREATE);

    entitlementPoolElement.addSubElement(limitsElement);
    entitlementPoolsElement.addSubElement(entitlementPoolElement);

    SessionContext context = ZusammenUtil.createSessionContext();
    Element epsSavedElement = zusammenAdaptor.saveElement(context,
        new ElementContext(entitlementPool.getVendorLicenseModelId(),
            entitlementPool.getVersion().getId()),
        entitlementPoolsElement, "Create entitlement pool");

    entitlementPool
        .setId(epsSavedElement.getSubElements().iterator().next().getElementId().getValue());
  }

  @Override
  public void update(EntitlementPoolEntity entitlementPool) {
    ZusammenElement entitlmentpoolElement =
        buildEntitlementPoolElement(entitlementPool, Action.UPDATE);

    SessionContext context = ZusammenUtil.createSessionContext();
    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
        entitlementPool.getVersion().getId());

    Optional<ElementInfo> epFromDb = zusammenAdaptor.getElementInfo(context, elementContext,
        new Id(entitlementPool.getId()));

    if (epFromDb.isPresent()) {
      if (entitlmentpoolElement.getRelations() == null) {
        entitlmentpoolElement.setRelations(new ArrayList<>());
      }
      if (epFromDb.get().getRelations() != null && !epFromDb.get().getRelations().isEmpty()) {
        entitlmentpoolElement.getRelations().addAll(epFromDb.get().getRelations());
      }
    }

    zusammenAdaptor.saveElement(context, elementContext, entitlmentpoolElement,
        String.format("Update entitlement pool with id %s", entitlementPool.getId()));
  }

  @Override
  public EntitlementPoolEntity get(EntitlementPoolEntity entitlementPool) {
    SessionContext context = ZusammenUtil.createSessionContext();
    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
        entitlementPool.getVersion().getId());
    ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
    return zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()))
        .map(elementInfo -> {
          EntitlementPoolEntity entity = convertor.convert(elementInfo);
          entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
          entity.setVersion(entitlementPool.getVersion());
          return entity;
        })
        .orElse(null);
  }

  @Override
  public void delete(EntitlementPoolEntity entitlementPool) {
    ZusammenElement zusammenElement = buildElement(new Id(entitlementPool.getId()), Action.DELETE);

    SessionContext context = ZusammenUtil.createSessionContext();
    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
        entitlementPool.getVersion().getId());
    zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
        "delete entitlement pool. id:" + entitlementPool.getId() + ".");
  }

  @Override
  public Collection<EntitlementPoolEntity> list(EntitlementPoolEntity entitlementPool) {
    SessionContext context = ZusammenUtil.createSessionContext();
    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
        entitlementPool.getVersion().getId());
    ElementToEntitlementPoolConvertor convertor = new ElementToEntitlementPoolConvertor();
    return zusammenAdaptor
        .listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name())
        .stream().map(elementInfo -> {
          EntitlementPoolEntity entity = convertor.convert(elementInfo);
          entity.setVendorLicenseModelId(entitlementPool.getVendorLicenseModelId());
          entity.setVersion(entitlementPool.getVersion());
          return entity;
        }).collect(Collectors.toList());
  }

  @Override
  public long count(EntitlementPoolEntity entitlementPool) {
    SessionContext context = ZusammenUtil.createSessionContext();
    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
        entitlementPool.getVersion().getId());

    return zusammenAdaptor
        .listElementsByName(context, elementContext, null, ElementType.EntitlementPools.name())
        .size();
  }

  @Override
  public void removeReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
                                            String referencingFeatureGroupId) {
    SessionContext context = ZusammenUtil.createSessionContext();
    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
        entitlementPool.getVersion().getId());

    Optional<ElementInfo> elementInfo =
        zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));

    if (elementInfo.isPresent()) {
      ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
      zusammenElement.setAction(Action.UPDATE);
      zusammenElement.setRelations(elementInfo.get().getRelations().stream()
          .filter(relation -> !referencingFeatureGroupId
              .equals(relation.getEdge2().getElementId().getValue()))
          .collect(Collectors.toList()));

      zusammenAdaptor.saveElement(context, elementContext, zusammenElement,
          "remove referencing feature group");
    }
  }

  @Override
  public void addReferencingFeatureGroup(EntitlementPoolEntity entitlementPool,
                                         String referencingFeatureGroupId) {
    SessionContext context = ZusammenUtil.createSessionContext();
    ElementContext elementContext = new ElementContext(entitlementPool.getVendorLicenseModelId(),
        entitlementPool.getVersion().getId());

    Optional<ElementInfo> elementInfo =
        zusammenAdaptor.getElementInfo(context, elementContext, new Id(entitlementPool.getId()));

    if (elementInfo.isPresent()) {
      ZusammenElement zusammenElement = VlmZusammenUtil.getZusammenElement(elementInfo.get());
      zusammenElement.setAction(Action.UPDATE);
      if (zusammenElement.getRelations() == null) {
        zusammenElement.setRelations(new ArrayList<>());
      }
      zusammenElement.getRelations().add(VlmZusammenUtil
          .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup,
              referencingFeatureGroupId));
      zusammenAdaptor
          .saveElement(context, elementContext, zusammenElement, "add referencing feature group");
    }
  }

  @Override
  public void deleteAll(EntitlementPoolEntity entitlementPool) {
    //not supported
  }

  @Override
  public String getManufacturerReferenceNumber(EntitlementPoolEntity entitlementPoolEntity) {
    SessionContext context = ZusammenUtil.createSessionContext();
    ElementContext elementContext =
        new ElementContext(entitlementPoolEntity.getVendorLicenseModelId(),
            entitlementPoolEntity.getVersion().getId());

    Optional<ElementInfo> elementInfo1 = zusammenAdaptor
        .getElementInfo(context, elementContext, new Id(entitlementPoolEntity.getId()));
    Map<String, Object> properties = elementInfo1.get().getInfo().getProperties();
    String manufacturerReferenceNumber = null;
    if (properties != null && properties.containsKey("manufacturerReferenceNumber")) {
      manufacturerReferenceNumber = (String) properties.get("manufacturerReferenceNumber");
    }
    return manufacturerReferenceNumber;
  }

  private ZusammenElement buildEntitlementPoolElement(EntitlementPoolEntity entitlementPool,
                                                      Action action) {
    ZusammenElement entitlementPoolElement =
        buildElement(entitlementPool.getId() == null ? null : new Id(entitlementPool.getId()),
            action);
    Info info = new Info();
    info.setName(entitlementPool.getName());
    info.setDescription(entitlementPool.getDescription());
    info.addProperty(ElementPropertyName.elementType.name(), ElementType.EntitlementPool);
    info.addProperty("version_uuid", entitlementPool.getVersionUuId());
    info.addProperty("thresholdValue", entitlementPool.getThresholdValue());
    info.addProperty("threshold_unit", entitlementPool.getThresholdUnit());
    info.addProperty("increments", entitlementPool.getIncrements());
    info.addProperty("operational_scope", entitlementPool.getOperationalScope());
    info.addProperty("startDate", entitlementPool.getStartDate());
    info.addProperty("expiryDate", entitlementPool.getExpiryDate());
    info.addProperty("manufacturerReferenceNumber", entitlementPool.getManufacturerReferenceNumber());
    entitlementPoolElement.setInfo(info);

    if (entitlementPool.getReferencingFeatureGroups() != null
        && !entitlementPool.getReferencingFeatureGroups().isEmpty()) {
      entitlementPoolElement.setRelations(entitlementPool.getReferencingFeatureGroups().stream()
          .map(rel -> VlmZusammenUtil
              .createRelation(RelationType.EntitlmentPoolToReferencingFeatureGroup, rel))
          .collect(Collectors.toList()));
    }
    return entitlementPoolElement;
  }


}