afc9e3cef6cd303c6b76fedc8b10cf3057d212a1
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.validation.impl.validators.heatresource;
18
19 import org.apache.commons.collections4.MapUtils;
20 import org.openecomp.core.validation.ErrorMessageCode;
21 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
22 import org.openecomp.core.validation.types.GlobalValidationContext;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
26 import org.openecomp.sdc.heat.datatypes.model.PolicyTypes;
27 import org.openecomp.sdc.heat.datatypes.model.Resource;
28 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
29 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
30 import org.openecomp.sdc.validation.ResourceValidator;
31 import org.openecomp.sdc.validation.ValidationContext;
32 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
33 import org.openecomp.sdc.validation.type.ValidatorConstants;
34
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Objects;
38
39 public class NovaServerGroupResourceValidator implements ResourceValidator {
40   private static final ErrorMessageCode ERROR_CODE_HNG1 = new ErrorMessageCode("HNG1");
41   private static final ErrorMessageCode ERROR_CODE_HNG2 = new ErrorMessageCode("HNG2");
42   private static final ErrorMessageCode ERROR_CODE_HNG3 = new ErrorMessageCode("HNG3");
43
44   @Override
45   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
46                        GlobalValidationContext globalContext, ValidationContext validationContext) {
47     validateNovaServerGroupPolicy(fileName, resourceEntry, globalContext);
48     validateServerGroupIsUsed(fileName, resourceEntry, globalContext,
49             (HeatResourceValidationContext) validationContext);
50   }
51
52   @SuppressWarnings("unchecked")
53   private static void validateNovaServerGroupPolicy(String fileName,
54                                                     Map.Entry<String, Resource> resourceEntry,
55                                                     GlobalValidationContext globalContext) {
56     Resource resource = resourceEntry.getValue();
57     Object policies =
58             resource.getProperties() == null ? null : resource.getProperties().get("policies");
59
60     if (Objects.nonNull(policies) && policies instanceof List) {
61       List<Object> policiesList = (List<Object>) policies;
62       if (policiesList.size() == 1) {
63         Object policy = policiesList.get(0);
64         if (!isGivenPolicyValid(policy)) {
65           globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
66                           .getErrorWithParameters(
67                                   ERROR_CODE_HNG1, Messages.WRONG_POLICY_IN_SERVER_GROUP.getErrorMessage(),
68                                   resourceEntry.getKey()),
69                   LoggerTragetServiceName.VALIDATE_NOVA_SEVER_GROUP_POLICY,
70                   LoggerErrorDescription.WRONG_POLICY_SERVER_GROUP);
71         }
72       } else {
73         globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
74                         .getErrorWithParameters(ERROR_CODE_HNG1,
75                                 Messages.WRONG_POLICY_IN_SERVER_GROUP.getErrorMessage(),
76                                 resourceEntry.getKey()),
77                 LoggerTragetServiceName.VALIDATE_NOVA_SEVER_GROUP_POLICY,
78                 LoggerErrorDescription.WRONG_POLICY_SERVER_GROUP);
79       }
80     }
81   }
82
83   private static boolean isGivenPolicyValid(Object policy) {
84     if (policy instanceof Map) {
85       return true;
86     }
87     if (policy instanceof String) {
88       return PolicyTypes.isGivenPolicyValid((String) policy);
89     }
90     return false;
91   }
92
93   public void validateServerGroupIsUsed(String fileName,
94                                         Map.Entry<String, Resource> resourceEntry,
95                                         GlobalValidationContext globalContext,
96                                         HeatResourceValidationContext validationContext) {
97
98     Map<String, Map<String, List<String>>> pointedServerGroups =
99             validationContext.getFileLevelResourceDependencies().get(HeatResourcesTypes
100                     .NOVA_SERVER_GROUP_RESOURCE_TYPE.getHeatResource());
101
102     if (MapUtils.isEmpty(pointedServerGroups)) {
103       globalContext
104               .addMessage(
105                       fileName,
106                       ErrorLevel.WARNING,
107                       ErrorMessagesFormatBuilder
108                               .getErrorWithParameters(
109                                       ERROR_CODE_HNG2, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
110                                       ValidatorConstants.Server_Group, resourceEntry.getKey()),
111                       LoggerTragetServiceName.VALIDATE_ALL_SERVER_GROUP_OR_SECURITY_GROUP_IN_USE,
112                       LoggerErrorDescription.SERVER_GROUP_SECURITY_GROUP_NOT_IN_USE);
113       return;
114     }
115
116     handleServerGroupReferences(fileName, resourceEntry, pointedServerGroups, globalContext);
117   }
118
119   private void handleServerGroupReferences(String fileName, Map.Entry<String, Resource>
120           resourceEntry, Map<String, Map<String, List<String>>> pointedServerGroups,
121                                            GlobalValidationContext globalContext) {
122     Map<String, List<String>> resourcesPointingToCurrServerGroup =
123             pointedServerGroups.get(resourceEntry.getKey());
124
125     if (MapUtils.isEmpty(resourcesPointingToCurrServerGroup)) {
126       globalContext
127               .addMessage(
128                       fileName,
129                       ErrorLevel.WARNING,
130                       ErrorMessagesFormatBuilder
131                               .getErrorWithParameters(
132                                       ERROR_CODE_HNG3, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
133                                       ValidatorConstants.Server_Group, resourceEntry.getKey()),
134                       LoggerTragetServiceName.VALIDATE_ALL_SERVER_GROUP_OR_SECURITY_GROUP_IN_USE,
135                       LoggerErrorDescription.SERVER_GROUP_SECURITY_GROUP_NOT_IN_USE);
136     }
137
138   }
139 }