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.ArtifactCategory;
24 import org.openecomp.core.enrichment.types.ArtifactType;
25 import org.openecomp.core.enrichment.types.ComponentMibInfo;
26 import org.openecomp.core.enrichment.types.MibInfo;
27 import org.openecomp.core.model.dao.EnrichedServiceModelDao;
28 import org.openecomp.core.model.dao.EnrichedServiceModelDaoFactory;
29 import org.openecomp.core.model.types.ServiceArtifact;
30 import org.openecomp.core.utilities.file.FileContentHandler;
31 import org.openecomp.core.utilities.file.FileUtils;
32 import org.openecomp.sdc.common.errors.Messages;
33 import org.openecomp.sdc.datatypes.error.ErrorLevel;
34 import org.openecomp.sdc.datatypes.error.ErrorMessage;
35 import org.openecomp.sdc.enrichment.EnrichmentInfo;
36 import org.openecomp.sdc.enrichment.inter.ExternalArtifactEnricherInterface;
37 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.MibDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.MibDaoFactory;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.MibEntity;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
44 import org.openecomp.sdc.versioning.dao.types.Version;
47 import java.io.IOException;
48 import java.util.Collection;
49 import java.util.HashMap;
50 import java.util.List;
52 import java.util.Optional;
55 public class MonitoringMibEnricher implements ExternalArtifactEnricherInterface {
57 private EnrichedServiceModelDao enrichedServiceModelDao;
58 private ComponentDao componentDao;
59 private MibDao mibDao;
60 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
65 * @param enrichmentInfo the enrichmentInfo
68 public Map<String, List<ErrorMessage>> enrich(EnrichmentInfo enrichmentInfo) {
70 Map<String, List<ErrorMessage>> errors = new HashMap<>();
71 String vspId = enrichmentInfo.getKey();
72 Version version = enrichmentInfo.getVersion();
74 Collection<ComponentEntity> components =
75 getComponentDao().list(new ComponentEntity(vspId, version, null));
77 .forEach(componentEntry -> errors.putAll(enrichComponent(componentEntry, vspId, version)));
82 Map<String, List<ErrorMessage>> enrichComponent(ComponentEntity componentEntry, String vspId,
86 mdcDataDebugMessage.debugEntryMessage(null, null);
88 Map<String, List<ErrorMessage>> errors = new HashMap<>();
89 ComponentMibInfo componentMibInfo =
90 extractComponentMibInfo(componentEntry, vspId, version, errors);
91 enrichComponentMib(componentMibInfo, vspId, version, errors);
93 mdcDataDebugMessage.debugExitMessage(null, null);
97 ComponentMibInfo extractComponentMibInfo(ComponentEntity componentEntity, String vspId,
99 Map<String, List<ErrorMessage>> errors) {
102 mdcDataDebugMessage.debugEntryMessage(null, null);
104 String componentId = componentEntity.getId();
105 MibEntity entity = new MibEntity();
107 entity.setVspId(vspId);
108 entity.setVersion(version);
109 entity.setComponentId(componentId);
110 String componentName = componentEntity.getComponentCompositionData().getName();
111 ComponentMibInfo componentMibInfo = new ComponentMibInfo();
112 updComponentMibInfoByType(componentName, ArtifactType.SNMP_POLL, entity, componentMibInfo,
114 updComponentMibInfoByType(componentName, ArtifactType.SNMP_TRAP, entity, componentMibInfo,
117 mdcDataDebugMessage.debugExitMessage(null, null);
118 return componentMibInfo;
121 void updComponentMibInfoByType(String componentName, ArtifactType type,
123 ComponentMibInfo componentMibInfo,
124 Map<String, List<ErrorMessage>> errors) {
127 mdcDataDebugMessage.debugEntryMessage(null, null);
130 mibEntity.setType(type);
131 Optional<MibEntity> artifact =
132 getMibDao().getByType(mibEntity);
134 if (!artifact.isPresent()) {
137 path = componentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName()
138 + File.separator + type.name();
139 MibInfo mibInfo = new MibInfo();
140 mibInfo.setName(path);
141 mibInfo.setContent(artifact.get().getArtifact().array());
144 componentMibInfo.setSnmpPoll(mibInfo);
147 componentMibInfo.setSnmpTrap(mibInfo);
153 mdcDataDebugMessage.debugExitMessage(null, null);
156 void enrichComponentMib(ComponentMibInfo componentMibInfo, String vspId, Version version,
157 Map<String, List<ErrorMessage>> errors) {
160 mdcDataDebugMessage.debugEntryMessage(null, null);
162 ServiceArtifact mibServiceArtifact = new ServiceArtifact();
163 mibServiceArtifact.setVspId(vspId);
164 mibServiceArtifact.setVersion(version);
165 enrichMibFiles(mibServiceArtifact, componentMibInfo, errors);
167 mdcDataDebugMessage.debugExitMessage(null, null);
170 void enrichMibFiles(ServiceArtifact mibServiceArtifact, ComponentMibInfo componentMibInfo,
171 Map<String, List<ErrorMessage>> errors) {
174 mdcDataDebugMessage.debugEntryMessage(null, null);
176 if (componentMibInfo == null) {
179 enrichMibByType(componentMibInfo.getSnmpTrap(), ArtifactType.SNMP_TRAP, mibServiceArtifact,
181 enrichMibByType(componentMibInfo.getSnmpPoll(), ArtifactType.SNMP_POLL, mibServiceArtifact,
184 mdcDataDebugMessage.debugExitMessage(null, null);
187 void enrichMibByType(MibInfo mibInfo, ArtifactType type, ServiceArtifact mibServiceArtifact,
188 Map<String, List<ErrorMessage>> errors) {
191 mdcDataDebugMessage.debugEntryMessage(null, null);
193 if (mibInfo == null) {
196 FileContentHandler mibs;
198 mibs = FileUtils.getFileContentMapFromZip(FileUtils.toByteArray(mibInfo.getContent()));
199 } catch (IOException ioException) {
200 ErrorMessage.ErrorMessageUtil
201 .addMessage(mibServiceArtifact.getName() + "." + type.name(), errors)
202 .add(new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
205 Set<String> fileList = mibs.getFileList();
206 for (String fileName : fileList) {
207 mibServiceArtifact.setContentData(FileUtils.toByteArray(mibs.getFileContent(fileName)));
208 mibServiceArtifact.setName(mibInfo.getName() + File.separator + fileName);
209 getEnrichedServiceModelDao().storeExternalArtifact(mibServiceArtifact);
212 mdcDataDebugMessage.debugExitMessage(null, null);
215 private EnrichedServiceModelDao getEnrichedServiceModelDao() {
216 if (enrichedServiceModelDao == null) {
217 enrichedServiceModelDao = EnrichedServiceModelDaoFactory.getInstance().createInterface();
219 return enrichedServiceModelDao;
222 private ComponentDao getComponentDao() {
223 if (componentDao == null) {
224 componentDao = ComponentDaoFactory.getInstance().createInterface();
229 private MibDao getMibDao() {
230 if (mibDao == null) {
231 mibDao = MibDaoFactory.getInstance().createInterface();