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