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/OrchestrationTemplateCandidateDaoZusammenImpl.java
blob: 22fe1dd98c7abef06aecd833edf64099d92ebc9a (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright © 2016-2018 European Support Limited
 *
 * 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.
 */

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.SessionContext;
import com.amdocs.zusammen.datatypes.item.Action;
import com.amdocs.zusammen.datatypes.item.ElementContext;
import com.amdocs.zusammen.utils.fileutils.FileUtils;
import org.openecomp.core.utilities.json.JsonUtil;
import org.openecomp.core.zusammen.api.ZusammenAdaptor;
import org.openecomp.sdc.datatypes.model.ElementType;
import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
import org.openecomp.sdc.versioning.dao.types.Version;

import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Optional;

import static org.openecomp.core.zusammen.api.ZusammenUtil.buildStructuralElement;
import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext;

public class OrchestrationTemplateCandidateDaoZusammenImpl
    implements OrchestrationTemplateCandidateDao {

  private static final Logger logger =
      LoggerFactory.getLogger(OrchestrationTemplateCandidateDaoZusammenImpl.class);

  private ZusammenAdaptor zusammenAdaptor;

  private static final String EMPTY_DATA = "{}";

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

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

  @Override
  public OrchestrationTemplateCandidateData get(String vspId, Version version) {
    logger.info("Getting orchestration template for VendorSoftwareProduct id -> " + vspId);

    SessionContext context = createSessionContext();
    ElementContext elementContext = new ElementContext(vspId, version.getId());

    Optional<Element> candidateElement =
        zusammenAdaptor.getElementByName(context, elementContext, null,
            ElementType.OrchestrationTemplateCandidate.name());
    if (candidateElement.isPresent()) {
      if (VspZusammenUtil.hasEmptyData(candidateElement.get().getData())) {
        return null;
      }
      OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData();
      candidateData.setFilesDataStructure(
          new String(FileUtils.toByteArray(candidateElement.get().getData())));

      Collection<Element> subElements = candidateElement.get().getSubElements();
      if (subElements.isEmpty()) {
        return candidateData;
      }

      for (Element element : subElements) {
        Optional<Element> subElement = zusammenAdaptor.getElement(context,
            elementContext, element.getElementId().toString());

        if (subElement.get().getInfo().getName()
            .equals(ElementType.OrchestrationTemplateCandidateContent
                .name())) {
          candidateData.setContentData(
              ByteBuffer.wrap(FileUtils.toByteArray(subElement.get().getData())));
          candidateData.setFileSuffix(subElement.get().getInfo()
              .getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
          candidateData.setFileName(subElement.get().getInfo()
              .getProperty(InfoPropertyName.FILE_NAME.getVal()));
        } else if (subElement.get().getInfo().getName()
            .equals(ElementType.OrchestrationTemplateCandidateValidationData.name())) {
          candidateData.setValidationData(new String(FileUtils.toByteArray(subElement
              .get().getData())));
        }
      }

      logger
          .info("Finished getting orchestration template for VendorSoftwareProduct id -> " + vspId);
      return candidateData;
    }
    logger.info(String
        .format("Orchestration template for VendorSoftwareProduct id %s does not exist", vspId));
    return null;
  }

  @Override
  public OrchestrationTemplateCandidateData getInfo(String vspId, Version version) {
    logger.info("Getting orchestration template info for VendorSoftwareProduct id -> " + vspId);

    SessionContext context = createSessionContext();
    ElementContext elementContext = new ElementContext(vspId, version.getId());

    OrchestrationTemplateCandidateData candidateData = new OrchestrationTemplateCandidateData();

    Optional<ElementInfo> candidateElement =
        zusammenAdaptor.getElementInfoByName(context, elementContext, null,
            ElementType.OrchestrationTemplateCandidate.name());

    if (candidateElement.isPresent()) {
      Collection<ElementInfo> subElements = candidateElement.get().getSubElements();
      if (subElements.isEmpty()) {
        return candidateData;
      }

      for (ElementInfo elementInfo : subElements) {
        Optional<Element> subElement = zusammenAdaptor.getElement(context,
            elementContext, elementInfo.getId().toString());

        if (subElement.get().getInfo().getName().equals(ElementType
            .OrchestrationTemplateCandidateContent.name())) {

          candidateData.setFileSuffix(subElement.get().getInfo()
              .getProperty(InfoPropertyName.FILE_SUFFIX.getVal()));
          candidateData.setFileName(subElement.get().getInfo()
              .getProperty(InfoPropertyName.FILE_NAME.getVal()));
        } else if (subElement.get().getInfo().getName().equals(ElementType
            .OrchestrationTemplateCandidateValidationData.name())) {
          candidateData.setValidationData(new String(FileUtils.toByteArray(subElement.get()
              .getData())));
        }
      }

      logger.info(
          "Finished getting orchestration template info for VendorSoftwareProduct id -> " + vspId);
      return candidateData;
    }
    logger.info(String
        .format("Orchestration template info for VendorSoftwareProduct id %s does not exist",
            vspId));
    return null;
  }

  @Override
  public void delete(String vspId, Version version) {
    ByteArrayInputStream emptyData = new ByteArrayInputStream(EMPTY_DATA.getBytes());

    ZusammenElement candidateContentElement =
        buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
    candidateContentElement.setData(emptyData);

    ZusammenElement validationData = buildStructuralElement(ElementType
        .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
    validationData.setData(emptyData);

    ZusammenElement candidateElement =
        buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
    candidateElement.setData(emptyData);
    candidateElement.addSubElement(candidateContentElement);
    candidateElement.addSubElement(validationData);

    SessionContext context = createSessionContext();
    ElementContext elementContext = new ElementContext(vspId, version.getId());
    zusammenAdaptor.saveElement(context, elementContext, candidateElement,
        "Delete Orchestration Template Candidate Elements's content");
  }

  @Override
  public void update(String vspId, Version version,
                     OrchestrationTemplateCandidateData candidateData) {
    logger.info("Uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);

    ZusammenElement candidateElement =
        buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
    candidateElement
        .setData(new ByteArrayInputStream(candidateData.getFilesDataStructure().getBytes()));

    ZusammenElement candidateContentElement =
        buildStructuralElement(ElementType.OrchestrationTemplateCandidateContent, Action.UPDATE);
    candidateContentElement
        .setData(new ByteArrayInputStream(candidateData.getContentData().array()));
    candidateContentElement.getInfo()
        .addProperty(InfoPropertyName.FILE_SUFFIX.getVal(), candidateData.getFileSuffix());
    candidateContentElement.getInfo()
        .addProperty(InfoPropertyName.FILE_NAME.getVal(), candidateData.getFileName());

    ZusammenElement validationData = buildStructuralElement(ElementType
        .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
    if (candidateData.getValidationData() != null) {
      validationData
          .setData(new ByteArrayInputStream(candidateData.getValidationData().getBytes()));
    }
    candidateElement.addSubElement(candidateContentElement);
    candidateElement.addSubElement(validationData);
    SessionContext context = createSessionContext();
    ElementContext elementContext = new ElementContext(vspId, version.getId());
    zusammenAdaptor.saveElement(context, elementContext, candidateElement,
        "Update Orchestration Template Candidate");
    logger
        .info("Finished uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
  }

  @Override
  public void updateValidationData(String vspId, Version version, ValidationStructureList
      validationData) {
    logger.info("Updating validation data of  orchestration template candidate for VSP id -> "
        + vspId);

    ZusammenElement validationDataElement = buildStructuralElement(ElementType
        .OrchestrationTemplateCandidateValidationData, Action.UPDATE);
    validationDataElement.setData(validationData == null ? new ByteArrayInputStream(EMPTY_DATA
        .getBytes()) : new ByteArrayInputStream(JsonUtil.object2Json(validationData).getBytes()));

    ZusammenElement candidateElement =
        buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.IGNORE);
    candidateElement.addSubElement(validationDataElement);

    SessionContext context = createSessionContext();
    ElementContext elementContext = new ElementContext(vspId, version.getId());
    zusammenAdaptor.saveElement(context, elementContext, candidateElement,
        "Update Orchestration Template Candidate validation data");
    logger.info("Finished updating validation data of  orchestration template candidate for VSP "
        + "id -> " + vspId);
  }

  @Override
  public void updateStructure(String vspId, Version version, FilesDataStructure fileDataStructure) {
    logger.info("Updating orchestration template for VSP id -> " + vspId);

    ZusammenElement candidateElement =
        buildStructuralElement(ElementType.OrchestrationTemplateCandidate, Action.UPDATE);
    candidateElement
        .setData(new ByteArrayInputStream(JsonUtil.object2Json(fileDataStructure).getBytes()));

    SessionContext context = createSessionContext();
    ElementContext elementContext = new ElementContext(vspId, version.getId());
    zusammenAdaptor.saveElement(context, elementContext, candidateElement,
        "Update Orchestration Template Candidate structure");
    logger
        .info("Finished uploading candidate data entity for VendorSoftwareProduct id -> " + vspId);
  }


  @Override
  public Optional<String> getStructure(String vspId, Version version) {
    logger.info("Getting orchestration template candidate structure for VendorSoftwareProduct id "
        + "-> " + vspId);

    SessionContext context = createSessionContext();
    ElementContext elementContext = new ElementContext(vspId, version.getId());

    Optional<Element> element = zusammenAdaptor.getElementByName(context, elementContext, null,
        ElementType.OrchestrationTemplateCandidate.name());

    if (element.isPresent() && !VspZusammenUtil.hasEmptyData(element.get().getData())) {
      return Optional.of(new String(FileUtils.toByteArray(element.get().getData())));
    }

    logger.info(
        "Finished getting orchestration template candidate structure for VendorSoftwareProduct "
            + "id -> " + vspId);

    return Optional.empty();
  }

  public enum InfoPropertyName {
    FILE_SUFFIX("fileSuffix"),
    FILE_NAME("fileName");

    private String val;

    InfoPropertyName(String val){
      this.val = val;
    }

    public String getVal() {
      return val;
    }
  }
}