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/ComponentArtifactDaoZusammenImpl.java
blob: e80e7d5d668dda5d34b2e23c762d455541f1e8ec (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

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 org.openecomp.core.enrichment.types.MonitoringUploadType;
import org.openecomp.core.utilities.file.FileUtils;
import org.openecomp.core.zusammen.api.ZusammenAdaptor;
import org.openecomp.sdc.datatypes.model.ElementType;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.impl.zusammen.convertor.ElementToComponentMonitoringUploadConvertor;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;

import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
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.*;

/**
 * @author Avrahamg.
 * @since March 21, 2017
 */
public class ComponentArtifactDaoZusammenImpl implements ComponentArtifactDao {

  private static final String ARTIFACT_NAME = "artifactName";

  private ZusammenAdaptor zusammenAdaptor;

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

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

  @Override
  public Optional<ComponentMonitoringUploadEntity> getByType(
      ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {
    SessionContext context = createSessionContext();
    ElementContext elementContext =
        new ElementContext(componentMonitoringUploadEntity.getVspId(),
            componentMonitoringUploadEntity.getVersion().getId());

    Optional<Element> mibsElement =
        zusammenAdaptor.getElementByName(context, elementContext,
            new Id(componentMonitoringUploadEntity.getComponentId()), ElementType.Mibs.toString());
    if (mibsElement.isPresent()) {
      Optional<Element> monitoringElement = zusammenAdaptor
          .getElementByName(context, elementContext, mibsElement.get().getElementId(),
              getMonitoringStructuralElement(componentMonitoringUploadEntity.getType()).toString());
      if (monitoringElement.isPresent()) {
        ComponentMonitoringUploadEntity entity =
            new ElementToComponentMonitoringUploadConvertor().convert(monitoringElement.get());
        entity.setVspId(componentMonitoringUploadEntity.getVspId());
        entity.setVersion(componentMonitoringUploadEntity.getVersion());
        entity.setComponentId(componentMonitoringUploadEntity.getComponentId());
        return Optional.of(entity);
      }
    }

    return Optional.empty();
  }

  @Override
  public void create(ComponentMonitoringUploadEntity entity) {
    ZusammenElement mibElement = buildMibElement(entity);

    ZusammenElement mibsElement = buildStructuralElement(ElementType.Mibs, null);

    ZusammenElement componentElement = buildElement(new Id(entity.getComponentId()), Action.IGNORE);

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

    Element savedElement = zusammenAdaptor.saveElement(context, elementContext,
        VspZusammenUtil.aggregateElements(componentElement, mibsElement, mibElement),
        "Create monitoring upload");
    entity.setId(savedElement.getSubElements().iterator().next()
        .getSubElements().iterator().next().getElementId().getValue());
  }

  @Override
  public void delete(ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {

    ZusammenElement mibElement = buildMibElementStructure(componentMonitoringUploadEntity);
    mibElement.setElementId(new Id(componentMonitoringUploadEntity.getId()));
    SessionContext context = createSessionContext();
    ElementContext elementContext =
        new ElementContext(componentMonitoringUploadEntity.getVspId(),
            componentMonitoringUploadEntity.getVersion().getId());

    zusammenAdaptor.saveElement(context, elementContext, mibElement,
        String.format("Delete mib with id %s", componentMonitoringUploadEntity.getId()));
  }



  @Override
  public Collection<ComponentMonitoringUploadEntity> list(
      ComponentMonitoringUploadEntity mibEntity) {
    SessionContext context = createSessionContext();
    ElementContext elementContext =
        new ElementContext(mibEntity.getVspId(), mibEntity.getVersion().getId());
    return zusammenAdaptor
        .listElementsByName(context, elementContext, new Id(mibEntity.getComponentId()),
            ElementType.Mibs.toString())
        .stream()
        .map(new ElementToComponentMonitoringUploadConvertor()::convert)
        .map(mib -> {
          mib.setVspId(mibEntity.getVspId());
          mib.setVersion(mibEntity.getVersion());
          mib.setComponentId(mibEntity.getComponentId());
          return mib;
        })
        .collect(Collectors.toList());
  }

  @Override
  public void deleteAll(ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {
    SessionContext context = createSessionContext();
    ElementContext elementContext =
        new ElementContext(componentMonitoringUploadEntity.getVspId(),
            componentMonitoringUploadEntity.getVersion().getId());

    Optional<Element> optionalElement = zusammenAdaptor.getElementByName(context,
        elementContext, new Id(componentMonitoringUploadEntity.getComponentId()), ElementType.Mibs
            .name());

    if (optionalElement.isPresent()) {
      Element mibsElement = optionalElement.get();
      Collection<Element> mibs = mibsElement.getSubElements();

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

  @Override
  public Collection<ComponentMonitoringUploadEntity> listArtifacts(
      ComponentMonitoringUploadEntity monitoringUploadEntity) {
    SessionContext context = createSessionContext();
    ElementContext elementContext = new ElementContext(monitoringUploadEntity.getVspId(),
        monitoringUploadEntity.getVersion().getId());

    final Optional<Element> elementByName =
        zusammenAdaptor.getElementByName(context, elementContext,
            new Id(monitoringUploadEntity.getComponentId()), ElementType.Mibs.name());

    if (!elementByName.isPresent()) {
      return Collections.emptyList();
    } else {
      final Id elementId = elementByName.get().getElementId();
      return zusammenAdaptor.listElementData(context, elementContext, elementId).stream()
          .map(element -> buildMibEntity(element, monitoringUploadEntity))
          .collect(Collectors.toList());
    }
  }

  private ComponentMonitoringUploadEntity buildMibEntity(Element element,
                                                         ComponentMonitoringUploadEntity monitoringUploadEntity) {
    final String componentId = monitoringUploadEntity.getComponentId();
    ComponentMonitoringUploadEntity
        createdMib = new ComponentMonitoringUploadEntity(monitoringUploadEntity.getVspId(),
        monitoringUploadEntity.getVersion(),
        componentId,
        null);
    createdMib.setArtifactName((String) element.getInfo().getProperties().get(ARTIFACT_NAME));
    createdMib.setArtifact(ByteBuffer.wrap(FileUtils.toByteArray(element.getData())));
    createdMib.setType(MonitoringUploadType.valueOf(element.getInfo().getName()));
    return createdMib;
  }

  private ZusammenElement buildMibElement(
      ComponentMonitoringUploadEntity monitoringUploadEntity) {

    ZusammenElement monitoringElement = buildMibElementStructure(monitoringUploadEntity);
    monitoringElement.getInfo().getProperties()
        .put(ARTIFACT_NAME, monitoringUploadEntity.getArtifactName());
    monitoringElement
        .setData(new ByteArrayInputStream(monitoringUploadEntity.getArtifact().array()));
    return monitoringElement;
  }

  private ZusammenElement buildMibElementStructure(
      ComponentMonitoringUploadEntity componentMonitoringUploadEntity) {
    return buildStructuralElement(getMonitoringStructuralElement(componentMonitoringUploadEntity
            .getType()), Action.UPDATE);
  }

  private ElementType getMonitoringStructuralElement(MonitoringUploadType type){
    switch (type) {
      case SNMP_POLL:
        return ElementType.SNMP_POLL;
      case SNMP_TRAP:
        return ElementType.SNMP_TRAP;
      case VES_EVENTS:
        return ElementType.VES_EVENTS;
      default:
        throw new IllegalArgumentException();
    }
  }
}