push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / util / CompilationUtil.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.vendorsoftwareproduct.util;
22
23 import org.openecomp.core.enrichment.types.ComponentArtifactType;
24 import org.openecomp.core.enrichment.types.ComponentCeilometerInfo;
25 import org.openecomp.core.enrichment.types.ComponentMibInfo;
26 import org.openecomp.core.enrichment.types.MibInfo;
27 import org.openecomp.core.utilities.applicationconfig.ApplicationConfig;
28 import org.openecomp.core.utilities.applicationconfig.ApplicationConfigFactory;
29 import org.openecomp.core.utilities.json.JsonUtil;
30 import org.openecomp.sdc.datatypes.error.ErrorMessage;
31 import org.openecomp.sdc.enrichment.impl.tosca.ComponentInfo;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDaoFactory;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentArtifactEntity;
35 import org.openecomp.sdc.versioning.dao.types.Version;
36
37 import java.io.File;
38 import java.util.List;
39 import java.util.Map;
40
41 /**
42  * The type Compilation util.
43  */
44 public class CompilationUtil {
45
46   private static final ApplicationConfig applicationConfig =
47       ApplicationConfigFactory.getInstance().createInterface();
48   private static final ComponentArtifactDao componentArtifactDao =
49       ComponentArtifactDaoFactory.getInstance().createInterface();
50
51   /**
52    * Add monitoring info.
53    *
54    * @param componentInfo the component info
55    * @param compileErrors the compile errors
56    */
57   public static void addMonitoringInfo(ComponentInfo componentInfo,
58                                        Map<String, List<ErrorMessage>> compileErrors) {
59
60     String ceilometerJson =
61         applicationConfig.getConfigurationData("vsp.monitoring", "component.ceilometer").getValue();
62     ComponentCeilometerInfo ceilometerInfo =
63         JsonUtil.json2Object(ceilometerJson, ComponentCeilometerInfo.class);
64     componentInfo.setCeilometerInfo(ceilometerInfo);
65   }
66
67   /**
68    * Add mib info.
69    *
70    * @param vspId           the vsp id
71    * @param version         the version
72    * @param componentEntity the component entity
73    * @param componentInfo   the component info
74    * @param compileErrors   the compile errors
75    */
76   public static void addMibInfo(String vspId, Version version, org.openecomp.sdc
77       .vendorsoftwareproduct.dao.type.ComponentEntity componentEntity,
78       ComponentInfo componentInfo,
79       Map<String, List<ErrorMessage>> compileErrors) {
80
81     String componentId = componentEntity.getId();
82
83     ComponentArtifactEntity entity = new ComponentArtifactEntity();
84     entity.setVspId(vspId);
85     entity.setVersion(version);
86     entity.setComponentId(componentId);
87
88     ComponentMibInfo componentMibInfo = new ComponentMibInfo();
89
90     extractAndInsertMibContentToComponentInfo(componentId, ComponentArtifactType.SNMP_POLL, entity,
91         componentMibInfo, compileErrors);
92     extractAndInsertMibContentToComponentInfo(componentId, ComponentArtifactType.SNMP_TRAP, entity,
93         componentMibInfo, compileErrors);
94     componentInfo.setMibInfo(componentMibInfo);
95   }
96
97   private static void extractAndInsertMibContentToComponentInfo(String componentId,
98                                          ComponentArtifactType type,
99                                          ComponentArtifactEntity componentArtifactEntity,
100                                          ComponentMibInfo componentMibInfo,
101                                          Map<String, List<ErrorMessage>> compileErrors) {
102     String path;
103     componentArtifactEntity.setType(type);
104     ComponentArtifactEntity artifact =
105         componentArtifactDao.getArtifactByType(componentArtifactEntity);
106
107     if (artifact == null) {
108       return;
109     }
110     path = componentId + File.separator + type.name();
111     MibInfo mibInfo = new MibInfo();
112     mibInfo.setName(path);
113     mibInfo.setContent(artifact.getArtifact().array());
114     switch (type) {
115       case SNMP_POLL:
116         componentMibInfo.setSnmpPoll(mibInfo);
117         break;
118       case SNMP_TRAP:
119         componentMibInfo.setSnmpTrap(mibInfo);
120         break;
121       default:
122     }
123
124
125   }
126 }