3920cc28afe16ab6600d3c471c0a8e1406a03b1c
[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.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;
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 MibDao mibDao;
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     ComponentMibInfo componentMibInfo =
90         extractComponentMibInfo(componentEntry, vspId, version, errors);
91     enrichComponentMib(componentMibInfo, vspId, version, errors);
92
93     mdcDataDebugMessage.debugExitMessage(null, null);
94     return errors;
95   }
96
97   ComponentMibInfo extractComponentMibInfo(ComponentEntity componentEntity, String vspId,
98                                            Version version,
99                                            Map<String, List<ErrorMessage>> errors) {
100
101
102     mdcDataDebugMessage.debugEntryMessage(null, null);
103
104     String componentId = componentEntity.getId();
105     MibEntity entity = new MibEntity();
106
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,
113         errors);
114     updComponentMibInfoByType(componentName, ArtifactType.SNMP_TRAP, entity, componentMibInfo,
115         errors);
116
117     mdcDataDebugMessage.debugExitMessage(null, null);
118     return componentMibInfo;
119   }
120
121   void updComponentMibInfoByType(String componentName, ArtifactType type,
122                                  MibEntity mibEntity,
123                                  ComponentMibInfo componentMibInfo,
124                                  Map<String, List<ErrorMessage>> errors) {
125
126
127     mdcDataDebugMessage.debugEntryMessage(null, null);
128
129     String path;
130     mibEntity.setType(type);
131     Optional<MibEntity> artifact =
132         getMibDao().getByType(mibEntity);
133
134     if (!artifact.isPresent()) {
135       return;
136     }
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());
142     switch (type) {
143       case SNMP_POLL:
144         componentMibInfo.setSnmpPoll(mibInfo);
145         break;
146       case SNMP_TRAP:
147         componentMibInfo.setSnmpTrap(mibInfo);
148         break;
149       default:
150         break;
151     }
152
153     mdcDataDebugMessage.debugExitMessage(null, null);
154   }
155
156   void enrichComponentMib(ComponentMibInfo componentMibInfo, String vspId, Version version,
157                           Map<String, List<ErrorMessage>> errors) {
158
159
160     mdcDataDebugMessage.debugEntryMessage(null, null);
161
162     ServiceArtifact mibServiceArtifact = new ServiceArtifact();
163     mibServiceArtifact.setVspId(vspId);
164     mibServiceArtifact.setVersion(version);
165     enrichMibFiles(mibServiceArtifact, componentMibInfo, errors);
166
167     mdcDataDebugMessage.debugExitMessage(null, null);
168   }
169
170   void enrichMibFiles(ServiceArtifact mibServiceArtifact, ComponentMibInfo componentMibInfo,
171                       Map<String, List<ErrorMessage>> errors) {
172
173
174     mdcDataDebugMessage.debugEntryMessage(null, null);
175
176     if (componentMibInfo == null) {
177       return;
178     }
179     enrichMibByType(componentMibInfo.getSnmpTrap(), ArtifactType.SNMP_TRAP, mibServiceArtifact,
180         errors);
181     enrichMibByType(componentMibInfo.getSnmpPoll(), ArtifactType.SNMP_POLL, mibServiceArtifact,
182         errors);
183
184     mdcDataDebugMessage.debugExitMessage(null, null);
185   }
186
187   void enrichMibByType(MibInfo mibInfo, ArtifactType type, ServiceArtifact mibServiceArtifact,
188                        Map<String, List<ErrorMessage>> errors) {
189
190
191     mdcDataDebugMessage.debugEntryMessage(null, null);
192
193     if (mibInfo == null) {
194       return;
195     }
196     FileContentHandler mibs;
197     try {
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()));
203       return;
204     }
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);
210     }
211
212     mdcDataDebugMessage.debugExitMessage(null, null);
213   }
214
215   private EnrichedServiceModelDao getEnrichedServiceModelDao() {
216     if (enrichedServiceModelDao == null) {
217       enrichedServiceModelDao = EnrichedServiceModelDaoFactory.getInstance().createInterface();
218     }
219     return enrichedServiceModelDao;
220   }
221
222   private ComponentDao getComponentDao() {
223     if (componentDao == null) {
224       componentDao = ComponentDaoFactory.getInstance().createInterface();
225     }
226     return componentDao;
227   }
228
229   private MibDao getMibDao() {
230     if (mibDao == null) {
231       mibDao = MibDaoFactory.getInstance().createInterface();
232     }
233     return mibDao;
234   }
235
236 }