[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / lib / openecomp-sdc-enrichment-lib / openecomp-sdc-enrichment-impl / src / main / java / org / openecomp / sdc / enrichment / impl / external / artifact / MonitoringMibEnricher.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.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.context.impl.MdcDataDebugMessage;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.MonitoringUploadDaoFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
44 import org.openecomp.sdc.versioning.dao.types.Version;
45
46 import java.io.File;
47 import java.io.IOException;
48 import java.util.Collection;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.Optional;
53 import java.util.Set;
54
55 public class MonitoringMibEnricher implements ExternalArtifactEnricherInterface {
56
57   private EnrichedServiceModelDao enrichedServiceModelDao;
58   private ComponentDao componentDao;
59   private ComponentArtifactDao componentArtifactDao;
60   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
61
62   /**
63    * Enrich map.
64    *
65    * @param enrichmentInfo the enrichmentInfo
66    * @return the map
67    */
68   public Map<String, List<ErrorMessage>> enrich(EnrichmentInfo enrichmentInfo) {
69
70     Map<String, List<ErrorMessage>> errors = new HashMap<>();
71     String vspId = enrichmentInfo.getKey();
72     Version version = enrichmentInfo.getVersion();
73
74     Collection<ComponentEntity> components =
75         getComponentDao().list(new ComponentEntity(vspId, version, null));
76     components
77         .forEach(componentEntry -> errors.putAll(enrichComponent(componentEntry, vspId, version)));
78
79     return errors;
80   }
81
82   Map<String, List<ErrorMessage>> enrichComponent(ComponentEntity componentEntry, String vspId,
83                                                   Version version) {
84
85
86     mdcDataDebugMessage.debugEntryMessage(null, null);
87
88     Map<String, List<ErrorMessage>> errors = new HashMap<>();
89     ComponentMonitoringUploadInfo componentMonitoringUploadInfo =
90         extractComponentMibInfo(componentEntry, vspId, version, errors);
91     enrichComponentMib(componentMonitoringUploadInfo, vspId, version, errors);
92
93     mdcDataDebugMessage.debugExitMessage(null, null);
94     return errors;
95   }
96
97   private ComponentMonitoringUploadInfo extractComponentMibInfo(ComponentEntity componentEntity,
98                                                                 String vspId,
99                                                                 Version version,
100                                                                 Map<String, List<ErrorMessage>> errors) {
101
102
103     mdcDataDebugMessage.debugEntryMessage(null, null);
104
105     String componentId = componentEntity.getId();
106     ComponentMonitoringUploadEntity entity = new ComponentMonitoringUploadEntity();
107
108     entity.setVspId(vspId);
109     entity.setVersion(version);
110     entity.setComponentId(componentId);
111     String componentName = componentEntity.getComponentCompositionData().getName();
112     ComponentMonitoringUploadInfo componentMonitoringUploadInfo =
113         new ComponentMonitoringUploadInfo();
114     for (MonitoringUploadType monitoringUploadType : MonitoringUploadType.values()) {
115       updComponentMibInfoByType(componentName, monitoringUploadType, entity,
116           componentMonitoringUploadInfo,
117           errors);
118     }
119 //    updComponentMibInfoByType(componentName, MonitoringUploadType.SNMP_POLL, entity,
120 //        componentMonitoringUploadInfo,
121 //        errors);
122 //    updComponentMibInfoByType(componentName, MonitoringUploadType.SNMP_TRAP, entity,
123 //        componentMonitoringUploadInfo,
124 //        errors);
125
126     mdcDataDebugMessage.debugExitMessage(null, null);
127     return componentMonitoringUploadInfo;
128   }
129
130   private void updComponentMibInfoByType(String componentName, MonitoringUploadType type,
131                                          ComponentMonitoringUploadEntity componentMonitoringUploadEntity,
132                                          ComponentMonitoringUploadInfo componentMonitoringUploadInfo,
133                                          Map<String, List<ErrorMessage>> errors) {
134
135
136     mdcDataDebugMessage.debugEntryMessage(null, null);
137
138     String path;
139     componentMonitoringUploadEntity.setType(type);
140     Optional<ComponentMonitoringUploadEntity> artifact =
141         getComponentArtifactDao().getByType(componentMonitoringUploadEntity);
142
143     if (!artifact.isPresent()) {
144       return;
145     }
146     path = componentName + File.separator + ArtifactCategory.DEPLOYMENT.getDisplayName()
147         + File.separator + type.name();
148     MonitoringArtifactInfo monitoringArtifactInfo = new MonitoringArtifactInfo();
149     monitoringArtifactInfo.setName(path);
150     monitoringArtifactInfo.setContent(artifact.get().getArtifact().array());
151     switch (type) { //todo as part of ATTASDC-4503
152       case SNMP_POLL:
153         componentMonitoringUploadInfo.setSnmpPoll(monitoringArtifactInfo);
154         break;
155       case SNMP_TRAP:
156         componentMonitoringUploadInfo.setSnmpTrap(monitoringArtifactInfo);
157         break;
158       case VES_EVENTS:
159         componentMonitoringUploadInfo.setVesEvent(monitoringArtifactInfo);
160         break;
161       default:
162         break;
163     }
164
165     mdcDataDebugMessage.debugExitMessage(null, null);
166   }
167
168   private void enrichComponentMib(ComponentMonitoringUploadInfo componentMonitoringUploadInfo,
169                                   String vspId,
170                                   Version version,
171                                   Map<String, List<ErrorMessage>> errors) {
172
173
174     mdcDataDebugMessage.debugEntryMessage(null, null);
175
176     ServiceArtifact mibServiceArtifact = new ServiceArtifact();
177     mibServiceArtifact.setVspId(vspId);
178     mibServiceArtifact.setVersion(version);
179     enrichMibFiles(mibServiceArtifact, componentMonitoringUploadInfo, errors);
180
181     mdcDataDebugMessage.debugExitMessage(null, null);
182   }
183
184   private void enrichMibFiles(ServiceArtifact monitoringArtifact,
185                               ComponentMonitoringUploadInfo componentMonitoringUploadInfo,
186                               Map<String, List<ErrorMessage>> errors) {
187
188
189     mdcDataDebugMessage.debugEntryMessage(null, null);
190
191     if (componentMonitoringUploadInfo == null) {
192       return;
193     }
194     //todo fix as part of ATTASDC-4503
195     enrichMibByType(componentMonitoringUploadInfo.getSnmpTrap(), MonitoringUploadType.SNMP_TRAP,
196         monitoringArtifact,
197         errors);
198     enrichMibByType(componentMonitoringUploadInfo.getSnmpPoll(), MonitoringUploadType.SNMP_POLL,
199         monitoringArtifact,
200         errors);
201     enrichMibByType(componentMonitoringUploadInfo.getVesEvent(), MonitoringUploadType.VES_EVENTS,
202         monitoringArtifact,
203         errors);
204
205     mdcDataDebugMessage.debugExitMessage(null, null);
206   }
207
208   private void enrichMibByType(MonitoringArtifactInfo monitoringArtifactInfo,
209                                MonitoringUploadType type,
210                                ServiceArtifact mibServiceArtifact,
211                                Map<String, List<ErrorMessage>> errors) {
212
213
214     mdcDataDebugMessage.debugEntryMessage(null, null);
215
216     if (monitoringArtifactInfo == null) {
217       return;
218     }
219     FileContentHandler mibs;
220     try {
221       mibs = FileUtils
222           .getFileContentMapFromZip(FileUtils.toByteArray(monitoringArtifactInfo.getContent()));
223     } catch (IOException ioException) {
224       ErrorMessage.ErrorMessageUtil
225           .addMessage(mibServiceArtifact.getName() + "." + type.name(), errors)
226           .add(new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
227       return;
228     }
229     Set<String> fileList = mibs.getFileList();
230     for (String fileName : fileList) {
231       mibServiceArtifact.setContentData(FileUtils.toByteArray(mibs.getFileContent(fileName)));
232       mibServiceArtifact.setName(monitoringArtifactInfo.getName() + File.separator + fileName);
233       getEnrichedServiceModelDao().storeExternalArtifact(mibServiceArtifact);
234     }
235
236     mdcDataDebugMessage.debugExitMessage(null, null);
237   }
238
239   private EnrichedServiceModelDao getEnrichedServiceModelDao() {
240     if (enrichedServiceModelDao == null) {
241       enrichedServiceModelDao = EnrichedServiceModelDaoFactory.getInstance().createInterface();
242     }
243     return enrichedServiceModelDao;
244   }
245
246   private ComponentDao getComponentDao() {
247     if (componentDao == null) {
248       componentDao = ComponentDaoFactory.getInstance().createInterface();
249     }
250     return componentDao;
251   }
252
253   private ComponentArtifactDao getComponentArtifactDao() {
254     if (componentArtifactDao == null) {
255       componentArtifactDao = MonitoringUploadDaoFactory.getInstance().createInterface();
256     }
257     return componentArtifactDao;
258   }
259
260 }