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.ComponentMonitoringUploadInfo;
25 import org.openecomp.core.enrichment.types.MonitoringArtifactInfo;
26 import org.openecomp.core.enrichment.types.MonitoringUploadType;
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.api.Logger;
38 import org.openecomp.sdc.logging.api.LoggerFactory;
39 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
40 import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
41 import org.openecomp.sdc.tosca.services.DataModelUtil;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.MonitoringUploadDaoFactory;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
48 import org.openecomp.sdc.versioning.dao.types.Version;
51 import java.io.IOException;
52 import java.util.Collection;
53 import java.util.HashMap;
54 import java.util.List;
56 import java.util.Optional;
59 public class MonitoringMibEnricher implements ExternalArtifactEnricherInterface {
61 private EnrichedServiceModelDao enrichedServiceModelDao;
62 private ComponentDao componentDao;
63 private ComponentArtifactDao componentArtifactDao;
64 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
66 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
70 * @param enrichmentInfo the enrichmentInfo
73 public Map<String, List<ErrorMessage>> enrich(EnrichmentInfo enrichmentInfo) {
75 Map<String, List<ErrorMessage>> errors = new HashMap<>();
76 String vspId = enrichmentInfo.getKey();
77 Version version = enrichmentInfo.getVersion();
79 Collection<ComponentEntity> components =
80 getComponentDao().list(new ComponentEntity(vspId, version, null));
82 .forEach(componentEntry -> errors.putAll(enrichComponent(componentEntry, vspId, version)));
87 Map<String, List<ErrorMessage>> enrichComponent(ComponentEntity componentEntry, String vspId,
91 mdcDataDebugMessage.debugEntryMessage(null, null);
93 Map<String, List<ErrorMessage>> errors = new HashMap<>();
94 ComponentMonitoringUploadInfo componentMonitoringUploadInfo =
95 extractComponentMibInfo(componentEntry, vspId, version, errors);
96 enrichComponentMib(componentMonitoringUploadInfo, vspId, version, errors);
98 mdcDataDebugMessage.debugExitMessage(null, null);
102 private ComponentMonitoringUploadInfo extractComponentMibInfo(ComponentEntity componentEntity,
105 Map<String, List<ErrorMessage>> errors) {
108 mdcDataDebugMessage.debugEntryMessage(null, null);
110 String componentId = componentEntity.getId();
111 ComponentMonitoringUploadEntity entity = new ComponentMonitoringUploadEntity();
113 entity.setVspId(vspId);
114 entity.setVersion(version);
115 entity.setComponentId(componentId);
116 String componentName = componentEntity.getComponentCompositionData().getName();
117 ComponentMonitoringUploadInfo componentMonitoringUploadInfo =
118 new ComponentMonitoringUploadInfo();
119 for (MonitoringUploadType monitoringUploadType : MonitoringUploadType.values()) {
120 updComponentMibInfoByType(componentName, monitoringUploadType, entity,
121 componentMonitoringUploadInfo,
124 // updComponentMibInfoByType(componentName, MonitoringUploadType.SNMP_POLL, entity,
125 // componentMonitoringUploadInfo,
127 // updComponentMibInfoByType(componentName, MonitoringUploadType.SNMP_TRAP, entity,
128 // componentMonitoringUploadInfo,
131 mdcDataDebugMessage.debugExitMessage(null, null);
132 return componentMonitoringUploadInfo;
135 private void updComponentMibInfoByType(String componentName, MonitoringUploadType type,
136 ComponentMonitoringUploadEntity componentMonitoringUploadEntity,
137 ComponentMonitoringUploadInfo componentMonitoringUploadInfo,
138 Map<String, List<ErrorMessage>> errors) {
141 mdcDataDebugMessage.debugEntryMessage(null, null);
144 componentMonitoringUploadEntity.setType(type);
145 Optional<ComponentMonitoringUploadEntity> artifact =
146 getComponentArtifactDao().getByType(componentMonitoringUploadEntity);
148 if (!artifact.isPresent()) {
151 String unifiedComponentNodeType =
152 ToscaNodeType.ABSTRACT_NODE_TYPE_PREFIX + DataModelUtil.getNamespaceSuffix(componentName);
153 path = unifiedComponentNodeType + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName()
154 + File.separator + type.name();
155 MonitoringArtifactInfo monitoringArtifactInfo = new MonitoringArtifactInfo();
156 monitoringArtifactInfo.setName(path);
157 monitoringArtifactInfo.setContent(artifact.get().getArtifact().array());
158 switch (type) { //todo as part of ATTASDC-4503
160 componentMonitoringUploadInfo.setSnmpPoll(monitoringArtifactInfo);
163 componentMonitoringUploadInfo.setSnmpTrap(monitoringArtifactInfo);
166 componentMonitoringUploadInfo.setVesEvent(monitoringArtifactInfo);
172 mdcDataDebugMessage.debugExitMessage(null, null);
175 private void enrichComponentMib(ComponentMonitoringUploadInfo componentMonitoringUploadInfo,
178 Map<String, List<ErrorMessage>> errors) {
181 mdcDataDebugMessage.debugEntryMessage(null, null);
183 ServiceArtifact mibServiceArtifact = new ServiceArtifact();
184 mibServiceArtifact.setVspId(vspId);
185 mibServiceArtifact.setVersion(version);
186 enrichMibFiles(mibServiceArtifact, componentMonitoringUploadInfo, errors);
188 mdcDataDebugMessage.debugExitMessage(null, null);
191 private void enrichMibFiles(ServiceArtifact monitoringArtifact,
192 ComponentMonitoringUploadInfo componentMonitoringUploadInfo,
193 Map<String, List<ErrorMessage>> errors) {
196 mdcDataDebugMessage.debugEntryMessage(null, null);
198 if (componentMonitoringUploadInfo == null) {
201 //todo fix as part of ATTASDC-4503
202 enrichMibByType(componentMonitoringUploadInfo.getSnmpTrap(), MonitoringUploadType.SNMP_TRAP,
205 enrichMibByType(componentMonitoringUploadInfo.getSnmpPoll(), MonitoringUploadType.SNMP_POLL,
208 enrichMibByType(componentMonitoringUploadInfo.getVesEvent(), MonitoringUploadType.VES_EVENTS,
212 mdcDataDebugMessage.debugExitMessage(null, null);
215 private void enrichMibByType(MonitoringArtifactInfo monitoringArtifactInfo,
216 MonitoringUploadType type,
217 ServiceArtifact mibServiceArtifact,
218 Map<String, List<ErrorMessage>> errors) {
221 mdcDataDebugMessage.debugEntryMessage(null, null);
223 if (monitoringArtifactInfo == null) {
226 FileContentHandler mibs;
229 .getFileContentMapFromZip(FileUtils.toByteArray(monitoringArtifactInfo.getContent()));
230 } catch (IOException ioException) {
231 log.debug("",ioException);
232 ErrorMessage.ErrorMessageUtil
233 .addMessage(mibServiceArtifact.getName() + "." + type.name(), errors)
234 .add(new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
237 Set<String> fileList = mibs.getFileList();
238 for (String fileName : fileList) {
239 mibServiceArtifact.setContentData(FileUtils.toByteArray(mibs.getFileContent(fileName)));
240 mibServiceArtifact.setName(monitoringArtifactInfo.getName() + File.separator + fileName);
241 getEnrichedServiceModelDao().storeExternalArtifact(mibServiceArtifact);
244 mdcDataDebugMessage.debugExitMessage(null, null);
247 private EnrichedServiceModelDao getEnrichedServiceModelDao() {
248 if (enrichedServiceModelDao == null) {
249 enrichedServiceModelDao = EnrichedServiceModelDaoFactory.getInstance().createInterface();
251 return enrichedServiceModelDao;
254 private ComponentDao getComponentDao() {
255 if (componentDao == null) {
256 componentDao = ComponentDaoFactory.getInstance().createInterface();
261 private ComponentArtifactDao getComponentArtifactDao() {
262 if (componentArtifactDao == null) {
263 componentArtifactDao = MonitoringUploadDaoFactory.getInstance().createInterface();
265 return componentArtifactDao;