push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-enrichment-lib / openecomp-sdc-enrichment-impl / src / main / java / org / openecomp / sdc / enrichment / impl / external / artifact / ExternalArtifactEnricher.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 import org.openecomp.core.enrichment.types.ComponentArtifactType;
24 import org.openecomp.core.enrichment.types.MibInfo;
25 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
26 import org.openecomp.core.model.dao.EnrichedServiceModelDaoFactory;
27 import org.openecomp.core.model.types.ServiceArtifact;
28 import org.openecomp.core.utilities.file.FileContentHandler;
29 import org.openecomp.core.utilities.file.FileUtils;
30 import org.openecomp.core.validation.errors.Messages;
31 import org.openecomp.sdc.datatypes.error.ErrorLevel;
32 import org.openecomp.sdc.datatypes.error.ErrorMessage;
33 import org.openecomp.sdc.enrichment.impl.tosca.ComponentInfo;
34 import org.openecomp.sdc.enrichment.inter.Enricher;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36
37 import java.io.File;
38 import java.io.IOException;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Set;
43
44 public class ExternalArtifactEnricher extends Enricher {
45
46   private static EnrichedServiceModelDao enrichedServiceModelDao =
47       EnrichedServiceModelDaoFactory.getInstance().createInterface();
48
49
50   @Override
51   public Map<String, List<ErrorMessage>> enrich() {
52
53     Map<String, List<ErrorMessage>> errors = new HashMap<>();
54     input.getEntityInfo().entrySet().stream().forEach(
55         entry -> enrichComponentMib(entry.getKey(), (ComponentInfo) entry.getValue(), errors));
56
57
58     return errors;
59   }
60
61
62   private void enrichComponentMib(String componentName, ComponentInfo componentInfo,
63                                   Map<String, List<ErrorMessage>> errors) {
64
65     String vspId = input.getKey();
66     Version version = input.getVersion();
67     ServiceArtifact mibServiceArtifact = new ServiceArtifact();
68     mibServiceArtifact.setVspId(vspId);
69     mibServiceArtifact.setVersion(version);
70     enrichMibFiles(mibServiceArtifact, componentInfo, errors);
71   }
72
73   private void enrichMibFiles(ServiceArtifact mibServiceArtifact, ComponentInfo componentInfo,
74                               Map<String, List<ErrorMessage>> errors) {
75     if (componentInfo.getMibInfo() == null) {
76       return;
77     }
78     enrichMibByType(componentInfo.getMibInfo().getSnmpTrap(), ComponentArtifactType.SNMP_TRAP,
79         mibServiceArtifact, errors);
80     enrichMibByType(componentInfo.getMibInfo().getSnmpPoll(), ComponentArtifactType.SNMP_POLL,
81         mibServiceArtifact, errors);
82   }
83
84   private void enrichMibByType(MibInfo mibInfo, ComponentArtifactType type,
85                                ServiceArtifact mibServiceArtifact,
86                                Map<String, List<ErrorMessage>> errors) {
87     if (mibInfo == null) {
88       return;
89     }
90     FileContentHandler mibs;
91     try {
92       mibs = FileUtils.getFileContentMapFromZip(FileUtils.toByteArray(mibInfo.getContent()));
93     } catch (IOException ioException) {
94       ErrorMessage.ErrorMessageUtil
95           .addMessage(mibServiceArtifact.getName() + "." + type.name(), errors)
96           .add(new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
97       return;
98     }
99     Set<String> fileList = mibs.getFileList();
100     for (String fileName : fileList) {
101       mibServiceArtifact.setContentData(FileUtils.toByteArray(mibs.getFileContent(fileName)));
102       mibServiceArtifact.setName(mibInfo.getName() + File.separator + fileName);
103       enrichedServiceModelDao.storeExternalArtifact(mibServiceArtifact);
104     }
105   }
106 }