ca4d40517a908d2cb0bf99fb6494b729f14770c2
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / PolicyExportParserImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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.be.tosca;
22
23
24 import fj.data.Either;
25 import org.openecomp.sdc.be.components.impl.exceptions.SdcResourceNotFoundException;
26 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
27 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
28 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.ComponentInstance;
31 import org.openecomp.sdc.be.model.DataTypeDefinition;
32 import org.openecomp.sdc.be.model.GroupDefinition;
33 import org.openecomp.sdc.be.model.PolicyDefinition;
34 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
35 import org.openecomp.sdc.be.tosca.model.IToscaMetadata;
36 import org.openecomp.sdc.be.tosca.model.ToscaMetadata;
37 import org.openecomp.sdc.be.tosca.model.ToscaPolicyTemplate;
38 import org.openecomp.sdc.common.log.wrappers.Logger;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Service;
41
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.function.Supplier;
47 import java.util.stream.Collectors;
48
49 import static org.apache.commons.collections.CollectionUtils.isEmpty;
50 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
51 import static org.apache.commons.collections.MapUtils.isNotEmpty;
52 import static org.apache.commons.lang.StringUtils.isNotEmpty;
53
54 @Service
55 public class PolicyExportParserImpl implements PolicyExportParser {
56
57         private static final Logger log = Logger.getLogger(PolicyExportParserImpl.class);
58    
59         private ApplicationDataTypeCache dataTypeCache;
60         private Map<String, DataTypeDefinition> dataTypes;
61         private PropertyConvertor propertyConvertor;
62         
63         @Autowired
64         public PolicyExportParserImpl(ApplicationDataTypeCache dataTypeCache, PropertyConvertor propertyConvertor) {
65                 this.dataTypeCache = dataTypeCache;
66                 this.propertyConvertor = propertyConvertor;
67                 this.dataTypes = getDataTypes();
68         }
69         
70         private Map<String, DataTypeDefinition> getDataTypes()  {
71                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
72                 if (dataTypesEither.isRight()) {
73                         log.error("Failed to retrieve all data types {}", dataTypesEither.right().value()); 
74                         throw new SdcResourceNotFoundException(); 
75                 }
76                 
77                 return dataTypesEither.left().value();
78         }
79         
80         @Override
81         public Map<String, ToscaPolicyTemplate> getPolicies(Component component) {
82                 Map<String, ToscaPolicyTemplate> toscaPolicies = null;
83                 Map<String, PolicyDefinition> policies = component.getPolicies();               
84                 if (isNotEmpty(policies)) {
85
86                          toscaPolicies = policies.values().stream().collect(
87                                          Collectors.toMap(
88                                                          PolicyDefinition::getName,
89                                                          policy->getToscaPolicyTemplate(policy,component)));
90                         log.debug("policies converted");
91                 }       
92                 return toscaPolicies;   
93         }
94         
95         private ToscaPolicyTemplate getToscaPolicyTemplate(PolicyDefinition policyDefinition,Component component) {
96                 
97                  String type = policyDefinition.getPolicyTypeName();
98              IToscaMetadata metadata = getToscaPolicyTemplateMetadata(policyDefinition);
99              Map<String, Object> properties = getToscaPolicyTemplateProperties(policyDefinition);
100              List<String> targets = getToscaPolicyTemplateTargets(
101                          policyDefinition,component.getComponentInstances(),component.getGroups());
102                                 
103                 return new ToscaPolicyTemplate(type, metadata, properties, targets);
104         }
105         
106         private List<String> getToscaPolicyTemplateTargets(PolicyDefinition policyDefinition,
107                         List<ComponentInstance> componentInstances, List<GroupDefinition> groups) {
108
109                 Map<PolicyTargetType, List<String>> targets = policyDefinition.getTargets();
110                 List<String> targetNames = null;
111
112                 if (targets == null || targets.isEmpty()) {
113                         return null;
114                 }
115
116                 List<String> componentInstancesTargets = targets.get(PolicyTargetType.COMPONENT_INSTANCES);
117                 List<String> groupTargets = targets.get(PolicyTargetType.GROUPS);
118                 
119                 if (isNotEmpty(componentInstancesTargets) && isNotEmpty(componentInstances)) {  
120                         // get target names by Id from component instances
121                         Map<String, String> targetNamesByIdFromComponentInstances = 
122                                         getTargetNamesByIdFromComponentInstances(componentInstances);
123                         targetNames = targetNamesLazyInstantiation(targetNames);
124                         addTargetNames(componentInstancesTargets, targetNames, targetNamesByIdFromComponentInstances);
125                         
126                 }
127                 
128                 if (isNotEmpty(groupTargets) && isNotEmpty(groups)) {
129                         // get target names by id from group definitions
130                         Map<String, String> targetNamesByIdFromGroupDefinitions = getTargetNamesByIdFromGroupDefinitions(groups);
131                         targetNames = targetNamesLazyInstantiation(targetNames);
132                         addTargetNames(groupTargets, targetNames, targetNamesByIdFromGroupDefinitions);
133                         
134                 }
135
136                 return targetNames;
137         }
138
139         private List<String> targetNamesLazyInstantiation(List<String> targetNames) {
140                 if (targetNames == null) {
141                         targetNames = new ArrayList<>();
142                 }
143                 return targetNames;
144         }
145
146         private void addTargetNames(List<String> targets, List<String> targetNames,
147                         Map<String, String> targetNamesById) {
148                 
149                 if (!targetNamesById.isEmpty()) {
150                         
151                         for (String id : targets) {
152                                 String name = targetNamesById.get(id);
153                                 if (name != null) {
154                                         targetNames.add(name);
155                                 }
156                         }
157                 }
158         }
159
160         private Map<String, String> getTargetNamesByIdFromGroupDefinitions(List<GroupDefinition> groups) {      
161                 return groups.stream().collect(
162                                 Collectors.toMap(GroupDefinition::getUniqueId, GroupDefinition::getName));              
163         }
164
165         private Map<String, String> getTargetNamesByIdFromComponentInstances(List<ComponentInstance> componentInstances) {
166                 return componentInstances.stream().collect(
167                                 Collectors.toMap(ComponentInstance::getUniqueId,ComponentInstance::getName));
168         }
169
170         private Map<String, Object> getToscaPolicyTemplateProperties(PolicyDefinition policyDefinition) {
171                 
172                 List<PropertyDataDefinition> tempProperties = policyDefinition.getProperties();
173                 
174                 if (isEmpty(tempProperties)) {
175                         return null;
176                 }
177                                 
178                 Map<String, Object> props = new HashMap<>();
179
180                 tempProperties.forEach(input -> 
181                         propertyConvertor.convertAndAddValue(dataTypes, props, input, getPropertyValue(input))
182                 );
183
184                 if (props.isEmpty()) {
185                         return null;
186                 } else {
187                         return props;
188                 }       
189         }
190
191         private Supplier<String> getPropertyValue(PropertyDataDefinition propertyDataDefinition) {
192                 return () -> {
193                         if (isNotEmpty(propertyDataDefinition.getValue())) {
194                                 return propertyDataDefinition.getValue();
195                         } else {
196                                 return propertyDataDefinition.getDefaultValue();
197                         }
198                 };
199         }
200
201         private IToscaMetadata getToscaPolicyTemplateMetadata(PolicyDefinition policyDefinition) {
202                 IToscaMetadata metadata = new ToscaMetadata();
203                 metadata.setInvariantUUID(policyDefinition.getInvariantUUID());
204                 metadata.setUUID(policyDefinition.getPolicyUUID());
205                 metadata.setName(policyDefinition.getName());
206                 metadata.setVersion(policyDefinition.getVersion());
207                 return metadata;
208         }
209
210 }