re base code
[sdc.git] / openecomp-be / lib / openecomp-sdc-enrichment-lib / openecomp-sdc-enrichment-impl / src / main / java / org / openecomp / sdc / enrichment / impl / external / artifact / VspInformationArtifactEnricher.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.enrichment.impl.external.artifact;
22
23
24 import org.openecomp.core.enrichment.types.ArtifactCategory;
25 import org.openecomp.core.enrichment.types.InformationArtifactFolderNames;
26 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
27 import org.openecomp.core.model.dao.EnrichedServiceModelDaoFactory;
28 import org.openecomp.core.model.types.ServiceArtifact;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.datatypes.error.ErrorMessage;
31 import org.openecomp.sdc.enrichment.EnrichmentInfo;
32 import org.openecomp.sdc.enrichment.inter.ExternalArtifactEnricherInterface;
33 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
34 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDaoFactory;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
38 import org.openecomp.sdc.vendorsoftwareproduct.factory.InformationArtifactGeneratorFactory;
39 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
40 import org.openecomp.sdc.versioning.dao.types.Version;
41
42 import java.io.File;
43 import java.io.IOException;
44 import java.nio.ByteBuffer;
45 import java.util.*;
46
47 /**
48  * Created by Talio on 11/24/2016
49  */
50 public class VspInformationArtifactEnricher implements ExternalArtifactEnricherInterface {
51   private static InformationArtifactGenerator informationArtifactGenerator =
52       InformationArtifactGeneratorFactory.getInstance().createInterface();
53   private EnrichedServiceModelDao enrichedServiceModelDao =
54       EnrichedServiceModelDaoFactory.getInstance().createInterface();
55   private VendorSoftwareProductInfoDao vspInfoDao = VendorSoftwareProductInfoDaoFactory
56       .getInstance().createInterface();
57
58   public VspInformationArtifactEnricher() {
59   }
60
61   public Map<String, List<ErrorMessage>> enrich(EnrichmentInfo enrichmentInfo,
62                                                 ToscaServiceModel serviceModel)
63       throws IOException {
64
65     String vspId = enrichmentInfo.getKey();
66     Version version = enrichmentInfo.getVersion();
67     return enrichInformationArtifact(vspId, version);
68   }
69
70   private Map<String, List<ErrorMessage>> enrichInformationArtifact(String vspId, Version version)
71       throws IOException {
72     Map<String, List<ErrorMessage>> errors = new HashMap<>();
73     ByteBuffer infoArtifactByteBuffer = ByteBuffer.wrap(informationArtifactGenerator.generate(
74         vspId, version).getBytes());
75
76     if (Objects.isNull(infoArtifactByteBuffer)) {
77       List<ErrorMessage> errorList = new ArrayList<>();
78       errorList.add(new ErrorMessage(ErrorLevel.ERROR, String.format(
79           "Cannot enrich information artifact for vendor software product with id %s and version %s",
80           vspId, version.toString())));
81       return errors;
82     }
83
84     enrichInformationArtifact(vspId, version, infoArtifactByteBuffer);
85     return errors;
86   }
87
88   private void enrichInformationArtifact(String vspId, Version version,
89                                          ByteBuffer infoArtifactByteBuffer) {
90     ServiceArtifact infoArtifactServiceArtifact = new ServiceArtifact();
91
92     String vspName = vspInfoDao.get(new VspDetails(vspId, version)).getName();
93
94     infoArtifactServiceArtifact.setVspId(vspId);
95     infoArtifactServiceArtifact.setVersion(version);
96     infoArtifactServiceArtifact
97         .setName(ArtifactCategory.INFORMATIONAL.getDisplayName() + File.separator
98             + InformationArtifactFolderNames.Guide + File.separator + String.format(
99             VendorSoftwareProductConstants
100                 .INFORMATION_ARTIFACT_NAME,
101             vspName));
102     infoArtifactServiceArtifact.setContentData(infoArtifactByteBuffer.array());
103
104     enrichedServiceModelDao.storeExternalArtifact(infoArtifactServiceArtifact);
105
106   }
107
108 }