3836796420feea6620ae0b0e6c699535c623aef7
[sdc.git] /
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.logging.context.impl.MdcDataDebugMessage;
34 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
35 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDaoFactory;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
39 import org.openecomp.sdc.vendorsoftwareproduct.factory.InformationArtifactGeneratorFactory;
40 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactGenerator;
41 import org.openecomp.sdc.versioning.dao.types.Version;
42
43 import java.io.File;
44 import java.io.IOException;
45 import java.nio.ByteBuffer;
46 import java.util.ArrayList;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50 import java.util.Objects;
51
52 /**
53  * Created by Talio on 11/24/2016
54  */
55 public class VspInformationArtifactEnricher implements ExternalArtifactEnricherInterface {
56   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
57   private static InformationArtifactGenerator informationArtifactGenerator =
58       InformationArtifactGeneratorFactory.getInstance().createInterface();
59   private EnrichedServiceModelDao enrichedServiceModelDao =
60       EnrichedServiceModelDaoFactory.getInstance().createInterface();
61   private VendorSoftwareProductInfoDao vspInfoDao = VendorSoftwareProductInfoDaoFactory
62       .getInstance().createInterface();
63
64   public VspInformationArtifactEnricher() {
65   }
66
67   public Map<String, List<ErrorMessage>> enrich(EnrichmentInfo enrichmentInfo,
68                                                 ToscaServiceModel serviceModel)
69       throws IOException {
70
71     String vspId = enrichmentInfo.getKey();
72     Version version = enrichmentInfo.getVersion();
73     return enrichInformationArtifact(vspId, version);
74   }
75
76   private Map<String, List<ErrorMessage>> enrichInformationArtifact(String vspId, Version version)
77       throws IOException {
78
79
80     mdcDataDebugMessage.debugEntryMessage(null);
81
82     Map<String, List<ErrorMessage>> errors = new HashMap<>();
83     ByteBuffer infoArtifactByteBuffer = ByteBuffer.wrap(informationArtifactGenerator.generate(
84         vspId, version).getBytes());
85
86     if (Objects.isNull(infoArtifactByteBuffer)) {
87       List<ErrorMessage> errorList = new ArrayList<>();
88       errorList.add(new ErrorMessage(ErrorLevel.ERROR, String.format(
89           "Cannot enrich information artifact for vendor software product with id %s and version %s",
90           vspId, version.toString())));
91
92       mdcDataDebugMessage.debugExitMessage(null);
93       return errors;
94     }
95
96     enrichInformationArtifact(vspId, version, infoArtifactByteBuffer);
97
98     mdcDataDebugMessage.debugExitMessage(null);
99     return errors;
100   }
101
102   private void enrichInformationArtifact(String vspId, Version version,
103                                          ByteBuffer infoArtifactByteBuffer) {
104     ServiceArtifact infoArtifactServiceArtifact = new ServiceArtifact();
105
106     String vspName = vspInfoDao.get(new VspDetails(vspId, version)).getName();
107
108     infoArtifactServiceArtifact.setVspId(vspId);
109     infoArtifactServiceArtifact.setVersion(version);
110     infoArtifactServiceArtifact
111         .setName(ArtifactCategory.INFORMATIONAL.getDisplayName() + File.separator
112             + InformationArtifactFolderNames.Guide + File.separator + String.format(
113             VendorSoftwareProductConstants
114                 .INFORMATION_ARTIFACT_NAME,
115             vspName));
116     infoArtifactServiceArtifact.setContentData(infoArtifactByteBuffer.array());
117
118     enrichedServiceModelDao.storeExternalArtifact(infoArtifactServiceArtifact);
119
120   }
121
122 }