2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.enrichment.impl.external.artifact;
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;
38 import java.io.IOException;
39 import java.util.HashMap;
40 import java.util.List;
44 public class ExternalArtifactEnricher extends Enricher {
46 private static EnrichedServiceModelDao enrichedServiceModelDao =
47 EnrichedServiceModelDaoFactory.getInstance().createInterface();
51 public Map<String, List<ErrorMessage>> enrich() {
53 Map<String, List<ErrorMessage>> errors = new HashMap<>();
54 input.getEntityInfo().entrySet().stream().forEach(
55 entry -> enrichComponentMib(entry.getKey(), (ComponentInfo) entry.getValue(), errors));
62 private void enrichComponentMib(String componentName, ComponentInfo componentInfo,
63 Map<String, List<ErrorMessage>> errors) {
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);
73 private void enrichMibFiles(ServiceArtifact mibServiceArtifact, ComponentInfo componentInfo,
74 Map<String, List<ErrorMessage>> errors) {
75 if (componentInfo.getMibInfo() == null) {
78 enrichMibByType(componentInfo.getMibInfo().getSnmpTrap(), ComponentArtifactType.SNMP_TRAP,
79 mibServiceArtifact, errors);
80 enrichMibByType(componentInfo.getMibInfo().getSnmpPoll(), ComponentArtifactType.SNMP_POLL,
81 mibServiceArtifact, errors);
84 private void enrichMibByType(MibInfo mibInfo, ComponentArtifactType type,
85 ServiceArtifact mibServiceArtifact,
86 Map<String, List<ErrorMessage>> errors) {
87 if (mibInfo == null) {
90 FileContentHandler mibs;
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()));
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);