push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-translator-lib / openecomp-sdc-translator-core / src / main / java / org / openecomp / sdc / translator / services / heattotosca / impl / ResourceTranslationNovaServerGroupsImpl.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.translator.services.heattotosca.impl;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.sdc.heat.datatypes.model.Resource;
25 import org.openecomp.sdc.tosca.datatypes.ToscaGroupType;
26 import org.openecomp.sdc.tosca.datatypes.ToscaPolicyType;
27 import org.openecomp.sdc.tosca.datatypes.model.GroupDefinition;
28 import org.openecomp.sdc.tosca.datatypes.model.PolicyDefinition;
29 import org.openecomp.sdc.tosca.services.DataModelUtil;
30 import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
31 import org.openecomp.sdc.translator.services.heattotosca.mapping.TranslatorHeatToToscaPropertyConverter;
32
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Objects;
38
39 public class ResourceTranslationNovaServerGroupsImpl extends ResourceTranslationBase {
40   private static final String AFFINITY = "affinity";
41   private static final String ANTI_AFFINITY = "anti-affinity";
42   private static List<String> supportedPolicies = Arrays.asList(AFFINITY, ANTI_AFFINITY);
43
44   @Override
45   protected void translate(TranslateTo translateTo) {
46     String resourceId = translateTo.getResourceId();
47     List<String> toscaPolicyTypes = getToscaPolicies(translateTo.getResource(), resourceId);
48     if (!CollectionUtils.isEmpty(toscaPolicyTypes)) {
49       addGroupToTopology(translateTo, resourceId);
50       addPoliciesToTopology(translateTo, resourceId, toscaPolicyTypes);
51     }
52   }
53
54   private void addPoliciesToTopology(TranslateTo translateTo, String resourceId,
55                                      List<String> toscaPolicyTypes) {
56     logger.info("******** Start creating policies for resource '%s' ********", resourceId);
57     for (int i = 0; i < toscaPolicyTypes.size(); i++) {
58       String policy = toscaPolicyTypes.get(i);
59       logger.info("******** Creating policy '%s' ********", policy);
60       PolicyDefinition policyDefinition = new PolicyDefinition();
61       policyDefinition.setType(policy);
62       policyDefinition.setTargets(Arrays.asList(resourceId));
63       policyDefinition.setProperties(TranslatorHeatToToscaPropertyConverter
64           .getToscaPropertiesSimpleConversion(translateTo.getResource().getProperties(),
65               policyDefinition.getProperties(), translateTo.getHeatFileName(),
66               translateTo.getHeatOrchestrationTemplate(), translateTo.getResource().getType(),
67               policyDefinition, translateTo.getContext()));
68       policyDefinition.getProperties().put(
69           policy.equals(ToscaPolicyType.PLACEMENT_ANTILOCATE.getDisplayName()) ? "container_type"
70               : AFFINITY, "host");
71       String policyId = resourceId + (toscaPolicyTypes.size() > 1 ? i : "");
72       DataModelUtil
73           .addPolicyDefinition(translateTo.getServiceTemplate(), policyId, policyDefinition);
74       logger.info("******** Policy '%s' created ********", policy);
75     }
76
77     logger
78         .info("******** All policies for resource '%s' created successfully ********", resourceId);
79   }
80
81   private void addGroupToTopology(TranslateTo translateTo, String resourceId) {
82     logger.info("******** Start creating group for resource '%s' ********", resourceId);
83     GroupDefinition group = new GroupDefinition();
84     group.setMembers(new ArrayList<>());
85     group.setType(ToscaGroupType.ROOT.getDisplayName());
86     DataModelUtil
87         .addGroupDefinitionToTopologyTemplate(translateTo.getServiceTemplate(), resourceId, group);
88     logger.info("******** Creating group '%s' for resource '%s' ********", resourceId, resourceId);
89   }
90
91   private List<String> getToscaPolicies(Resource resource, String resourceId) {
92
93     Map<String, Object> properties = resource.getProperties();
94     if (Objects.isNull(properties) || Objects.isNull(properties.get("policies"))) {
95       return Arrays.asList(ToscaPolicyType.PLACEMENT_ANTILOCATE.getDisplayName());
96     }
97
98     List policies = (List) properties.get("policies");
99     List<String> retList = new ArrayList<>();
100     policies.forEach(policy -> {
101       if (!supportedPolicies.contains(policy)) {
102         logger.warn("Resource '" + resourceId + "'(" + resource.getType()
103             + ")  contains unsupported policy '" + policy.toString()
104             + "'. This resource is been ignored during the translation");
105       } else {
106         retList.add(getToscaPolicyByHotPolicy((String) policy));
107       }
108     });
109     return retList;
110   }
111
112   private String getToscaPolicyByHotPolicy(String name) {
113     if (Objects.equals(name, AFFINITY)) {
114       return ToscaPolicyType.PLACEMENT_COLOCATE.getDisplayName();
115     } else {
116       return ToscaPolicyType.PLACEMENT_ANTILOCATE.getDisplayName();
117     }
118   }
119 }