aboutsummaryrefslogtreecommitdiffstats
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/NetworkDaoZusammenImpl.java
blob: 8cd6bd1acd908f24c622ae33c505b2aeeb599809 (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
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.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.sdc.datatypes.model.ElementType;
import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen.convertor.ElementToNetworkConvertor;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.types.ElementPropertyName;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
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 NetworkDaoZusammenImpl implements NetworkDao {

  private ZusammenAdaptor zusammenAdaptor;

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

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

  @Override
  public Collection<NetworkEntity> list(NetworkEntity network) {
    SessionContext context = createSessionContext();
    ElementContext elementContext =
        new ElementContext(network.getVspId(), network.getVersion().getId());

    Optional<ElementInfo> vspModel = zusammenAdaptor
        .getElementInfoByName(context, elementContext, null, ElementType.VspModel.name());
    if (!vspModel.isPresent()) {
      return new ArrayList<>();
    }

    return zusammenAdaptor.listElementsByName(context, elementContext, vspModel.get().getId(),
        ElementType.Networks.name()).stream()
        .map(new ElementToNetworkConvertor()::convert)
        .map(entity -> {
          entity.setVspId(network.getVspId());
          entity.setVersion(network.getVersion());
          return entity;
        })
        .collect(Collectors.toList());
  }

  @Override
  public void create(NetworkEntity network) {
    ZusammenElement networkElement = buildNetworkElement(network, Action.CREATE);

    ZusammenElement networksElement = buildStructuralElement(ElementType.Networks, Action.IGNORE);
    networksElement.setSubElements(Collections.singletonList(networkElement));

    ZusammenElement vspModel = buildStructuralElement(ElementType.VspModel, Action.IGNORE);
    vspModel.addSubElement(networksElement);

    SessionContext context = createSessionContext();
    Element savedElement = zusammenAdaptor
        .saveElement(context, new ElementContext(network.getVspId(), network.getVersion().getId()),
            vspModel, "Create network");
    network.setId(savedElement.getSubElements().iterator().next()
        .getSubElements().iterator().next().getElementId().getValue());
  }

  @Override
  public void update(NetworkEntity network) {
    ZusammenElement networkElement = buildNetworkElement(network, Action.UPDATE);

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

  @Override
  public NetworkEntity get(NetworkEntity network) {
    SessionContext context = createSessionContext();

    Optional<Element> element =
        zusammenAdaptor.getElement(context,
            new ElementContext(network.getVspId(), network.getVersion().getId()), network.getId());

    if (element.isPresent()) {
      ElementToNetworkConvertor convertor = new ElementToNetworkConvertor();
      NetworkEntity entity = convertor.convert(element.get());
      entity.setVspId(network.getVspId());
      entity.setVersion(network.getVersion());
      return entity;
    } else {
      return null;
    }
  }

  @Override
  public void delete(NetworkEntity network) {
    ZusammenElement networkElement = buildElement(new Id(network.getId()), Action.DELETE);

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


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

    Collection<ElementInfo> vspModelSubs = zusammenAdaptor
        .listElementsByName(context, elementContext, null, ElementType.VspModel.name());

    Optional<ElementInfo> networksElement = vspModelSubs.stream()
        .filter(elementInfo -> elementInfo.getInfo() != null
            && ElementType.Networks.name().equals(elementInfo.getInfo().getName()))
        .findFirst();
    if (!networksElement.isPresent()) {
      return;
    }

    ZusammenElement networks = buildElement(networksElement.get().getId(), Action.IGNORE);
    networks.setSubElements(networksElement.get().getSubElements().stream()
        .map(network -> buildElement(network.getId(), Action.DELETE))
        .collect(Collectors.toList()));

    zusammenAdaptor.saveElement(context, elementContext, networks, "Delete all networks");
  }

  private ZusammenElement buildNetworkElement(NetworkEntity network, Action action) {
    ZusammenElement networkElement =
        buildElement(network.getId() == null ? null : new Id(network.getId()), action);
    Info info = new Info();
    info.addProperty(ElementPropertyName.elementType.name(), ElementType.Network);
    info.addProperty(ElementPropertyName.compositionData.name(), network.getCompositionData());
    networkElement.setInfo(info);
    networkElement.setData(new ByteArrayInputStream(network.getCompositionData().getBytes()));
    return networkElement;
  }
}