summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-vendor-software-product-lib/openecomp-sdc-vendor-software-product-core/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/dao/impl/zusammen/NicDaoZusammenImpl.java
blob: 9e05151b5ef71a6908d2beafe9642ac45640ccbc (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
package org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen;

import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element;
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.sdc.datatypes.model.ElementType;
import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen.convertor.ElementToNicConvertor;
import org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen.convertor.ElementToNicQuestionnaireConvertor;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.types.ElementPropertyName;

import java.io.ByteArrayInputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.stream.Collectors;

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

public class NicDaoZusammenImpl implements NicDao {

  private ZusammenAdaptor zusammenAdaptor;

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

  @Override
  public void registerVersioning(String versionableEntityType) {
    // registerVersioning is not implemented for NicDaoZusammenImpl
  }

  @Override
  public Collection<NicEntity> list(NicEntity nic) {
    SessionContext context = createSessionContext();
    return listNics(context, new ElementContext(nic.getVspId(), nic.getVersion().getId()), nic);
  }

  private Collection<NicEntity> listNics(SessionContext context, ElementContext elementContext,
                                         NicEntity nic) {
    return zusammenAdaptor.listElementsByName(context, elementContext, new Id(nic.getComponentId()),
        ElementType.Nics.name())
        .stream()
        .map(new ElementToNicConvertor()::convert)
        .map(nicEntity -> {
          nicEntity.setComponentId(nic.getComponentId());
          nicEntity.setVspId(nic.getVspId());
          nicEntity.setVersion(nic.getVersion());
          return nicEntity;
        })
        .collect(Collectors.toList());
  }


  @Override
  public void create(NicEntity nic) {
    ZusammenElement nicElement = nicToZusammen(nic, Action.CREATE);

    ZusammenElement nicsElement = buildStructuralElement(ElementType.Nics, Action.IGNORE);
    nicsElement.setSubElements(Collections.singletonList(nicElement));

    ZusammenElement componentElement = buildElement(new Id(nic.getComponentId()), Action.IGNORE);
    componentElement.setSubElements(Collections.singletonList(nicsElement));

    SessionContext context = createSessionContext();
    ElementContext elementContext = new ElementContext(nic.getVspId(), nic.getVersion().getId());

    Element savedElement =
        zusammenAdaptor.saveElement(context, elementContext, componentElement, "Create nic");
    nic.setId(savedElement.getSubElements().iterator().next()
        .getSubElements().iterator().next().getElementId().getValue());
  }

  @Override
  public void update(NicEntity nic) {
    ZusammenElement nicElement = nicToZusammen(nic, Action.UPDATE);

    SessionContext context = createSessionContext();
    zusammenAdaptor
        .saveElement(context, new ElementContext(nic.getVspId(), nic.getVersion().getId()),
            nicElement, String.format("Update nic with id %s", nic.getId()));
  }

  @Override
  public NicEntity get(NicEntity nic) {
    SessionContext context = createSessionContext();
    ElementToNicConvertor convertor = new ElementToNicConvertor();
    Optional<Element> element = zusammenAdaptor
        .getElement(context, new ElementContext(nic.getVspId(), nic.getVersion().getId()),
            nic.getId());

    if (element.isPresent()) {
      NicEntity entity = convertor.convert(element.get());
      entity.setVspId(nic.getVspId());
      entity.setVersion(nic.getVersion());
      entity.setComponentId(nic.getComponentId());

      return entity;
    } else {
      return null;
    }
  }

  @Override
  public void delete(NicEntity nic) {
    ZusammenElement nicElement = buildElement(new Id(nic.getId()), Action.DELETE);

    SessionContext context = createSessionContext();
    zusammenAdaptor
        .saveElement(context, new ElementContext(nic.getVspId(), nic.getVersion().getId()),
            nicElement, String.format("Delete nic with id %s", nic.getId()));
  }

  @Override
  public NicEntity getQuestionnaireData(String vspId, Version version, String componentId,
                                        String nicId) {
    SessionContext context = createSessionContext();

    return getQuestionnaire(context, new ElementContext(vspId, version.getId()),
        new NicEntity(vspId, version, componentId, nicId));
  }

  private NicEntity getQuestionnaire(SessionContext context, ElementContext elementContext,
                                     NicEntity nic) {
    Optional<Element> questionnaireElement = zusammenAdaptor
        .getElementByName(context, elementContext, new Id(nic.getId()),
            ElementType.NicQuestionnaire.name());
    return questionnaireElement.map(new ElementToNicQuestionnaireConvertor()::convert)
        .map(entity -> {
          entity.setVspId(nic.getVspId());
          entity.setVersion(nic.getVersion());
          entity.setComponentId(nic.getComponentId());
          return entity;
        })
        .orElse(null);
  }

  @Override
  public void updateQuestionnaireData(String vspId, Version version, String componentId,
                                      String nicId, String questionnaireData) {
    ZusammenElement questionnaireElement =
        nicQuestionnaireToZusammen(questionnaireData, Action.UPDATE);

    ZusammenElement nicElement = buildElement(new Id(nicId), Action.IGNORE);
    nicElement.setSubElements(Collections.singletonList(questionnaireElement));

    SessionContext context = createSessionContext();
    zusammenAdaptor.saveElement(context, new ElementContext(vspId, version.getId()), nicElement,
        "Update nic questionnaire");
  }

  @Override
  public Collection<NicEntity> listByVsp(String vspId, Version version) {
    SessionContext context = createSessionContext();

    Collection<ComponentEntity> components = ComponentDaoZusammenImpl
        .listComponents(zusammenAdaptor, context, vspId, version);

    ElementContext elementContext = new ElementContext(vspId, version.getId());
    return components.stream()
        .map(component ->
            listNics(context, elementContext,
                new NicEntity(vspId, version, component.getId(), null)).stream()
                .map(nic -> {
                  nic.setQuestionnaireData(getQuestionnaire(context, elementContext, nic).getQuestionnaireData());
                  return nic;
                })
                .collect(Collectors.toList()))
        .flatMap(Collection::stream)
        .collect(Collectors.toList());
  }

  @Override
  public void deleteByComponentId(String vspId, Version version, String componentId) {
    SessionContext context = createSessionContext();
    ElementContext elementContext = new ElementContext(vspId, version.getId());

    Optional<Element> optionalElement = zusammenAdaptor.getElementByName(context,
        elementContext, new Id(componentId), ElementType.Nics.name());

    if (optionalElement.isPresent()) {
      Element nicsElement = optionalElement.get();
      Collection<Element> nics = nicsElement.getSubElements();

      nics.forEach(nic -> {
        ZusammenElement nicZusammenElement = buildElement(nic.getElementId(), Action.DELETE);
        zusammenAdaptor.saveElement(context, elementContext, nicZusammenElement,
            "Delete nic with id " + nic.getElementId());
      });
    }
  }

  @Override
  public void deleteByVspId(String vspId, Version version) {
    // deleteByVspId not implemented for NicDaoZusammenImpl
  }

  private ZusammenElement nicToZusammen(NicEntity nic, Action action) {
    ZusammenElement nicElement = buildNicElement(nic, action);
    if (action == Action.CREATE) {
      nicElement.setSubElements(Collections.singletonList(
          nicQuestionnaireToZusammen(nic.getQuestionnaireData(), Action.CREATE)));
    }
    return nicElement;
  }

  private ZusammenElement nicQuestionnaireToZusammen(String questionnaireData,
                                                     Action action) {
    ZusammenElement questionnaireElement =
        buildStructuralElement(ElementType.NicQuestionnaire, action);
    questionnaireElement.setData(new ByteArrayInputStream(questionnaireData.getBytes()));
    return questionnaireElement;
  }

  private ZusammenElement buildNicElement(NicEntity nic, Action action) {
    ZusammenElement nicElement =
        buildElement(nic.getId() == null ? null : new Id(nic.getId()), action);
    Info info = new Info();
    info.addProperty(ElementPropertyName.elementType.name(), ElementType.Nic);
    info.addProperty(ElementPropertyName.compositionData.name(), nic.getCompositionData());
    nicElement.setInfo(info);
    nicElement.setData(new ByteArrayInputStream(nic.getCompositionData().getBytes()));
    return nicElement;
  }
}