[sdc] docker file fix for cassandra
[sdc.git] / openecomp-be / lib / openecomp-sdc-enrichment-lib / openecomp-sdc-enrichment-impl / src / main / java / org / openecomp / sdc / enrichment / impl / tosca / CeilometerEnricher.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.tosca;
22
23 import org.apache.commons.collections4.MapUtils;
24 import org.openecomp.core.enrichment.types.CeilometerInfo;
25 import org.openecomp.sdc.datatypes.error.ErrorMessage;
26 import org.openecomp.sdc.enrichment.EnrichmentInfo;
27 import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
28 import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes;
29 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
30 import org.openecomp.sdc.tosca.datatypes.model.CapabilityDefinition;
31 import org.openecomp.sdc.tosca.datatypes.model.CapabilityType;
32 import org.openecomp.sdc.tosca.datatypes.model.NodeType;
33 import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
34 import org.openecomp.sdc.tosca.services.ToscaUtil;
35 import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
36 import org.openecomp.sdc.translator.services.heattotosca.globaltypes.CommonGlobalTypes;
37
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41
42 public class CeilometerEnricher {
43   /**
44    * Enrich map.
45    *
46    * @param toscaModel    the tosca model
47    * @param modelNodeType the model node type
48    * @param input         the input
49    * @return the map
50    */
51   public static Map<String, List<ErrorMessage>> enrich(ToscaServiceModel toscaModel,
52                                                        Map<String, List<NodeType>> modelNodeType,
53                                                        EnrichmentInfo input) {
54     Map<String, List<ErrorMessage>> errors = new HashMap<>();
55     input.getEntityInfo().entrySet().stream().forEach(
56         entry -> enrichNodeType(toscaModel, (ComponentInfo) entry.getValue(),
57             modelNodeType.get(entry.getKey())));
58
59     return errors;
60   }
61
62   private static void enrichNodeType(ToscaServiceModel toscaModel, ComponentInfo componentInfo,
63                                      List<NodeType> nodeTypes) {
64
65     for (CeilometerInfo ceilometerInfo : componentInfo.getCeilometerInfo()
66         .getCeilometerInfoList()) {
67       String capabilityId = ceilometerInfo.getName();
68
69       Map<String, Object> properties = getCeilometerProperties(ceilometerInfo);
70
71       //CapabilityType capabilityType = CommonGlobalTypes.createServiceTemplate()
72       // .getCapability_types().
73       // get(ToscaCapabilityType.METRIC_CEILOMETER.getDisplayName());
74       //CapabilityType metricCapabilityType = CommonGlobalTypes.createServiceTemplate().
75       // getCapability_types().get(ToscaCapabilityType.METRIC.getDisplayName());
76       ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
77       CapabilityType capabilityType = (CapabilityType) toscaAnalyzerService
78           .getFlatEntity(ToscaElementTypes.CAPABILITY_TYPE,
79               ToscaCapabilityType.METRIC_CEILOMETER.getDisplayName(),
80               CommonGlobalTypes.createServiceTemplate(), toscaModel);
81
82
83       nodeTypes.stream().forEach(nodeType ->
84           addCapability(nodeType, capabilityId, ToscaUtil
85               .convertTypeToDefinition(ToscaCapabilityType.METRIC_CEILOMETER.getDisplayName(),
86                   capabilityType, properties, ceilometerInfo.getDescription())));
87     }
88   }
89
90   private static Map<String, Object> getCeilometerProperties(CeilometerInfo ceilometerInfo) {
91     Map<String, Object> properties = new HashMap<>();
92     properties.put("name", ceilometerInfo.getName());
93     properties.put("type", ceilometerInfo.getType());
94     properties.put("unit", ceilometerInfo.getUnit());
95     if (ceilometerInfo.getCategory() != null) {
96       properties.put("category", ceilometerInfo.getCategory());
97     }
98     return properties;
99   }
100
101   private static void addCapability(NodeType nodeType, String capabilityId,
102                                     CapabilityDefinition capabilityDefinition) {
103     if (MapUtils.isEmpty(nodeType.getCapabilities())) {
104       nodeType.setCapabilities(new HashMap<>());
105     }
106     //clean unnecessary info
107     capabilityDefinition.setAttributes(null);
108     capabilityDefinition.setOccurrences(null);
109
110     nodeType.getCapabilities().put(capabilityId, capabilityDefinition);
111   }
112 }