e70b884805b28e5be823b267ccbf0dd9f4a03efc
[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.context.impl.MdcDataDebugMessage;
29 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
30 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
31 import org.openecomp.sdc.validation.ResourceValidator;
32 import org.openecomp.sdc.validation.ValidationContext;
33 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
34 import org.openecomp.sdc.validation.type.ValidatorConstants;
35
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Objects;
39
40 public class NovaServerGroupResourceValidator implements ResourceValidator {
41   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
42   private static final ErrorMessageCode ERROR_CODE_HNG1 = new ErrorMessageCode("HNG1");
43   private static final ErrorMessageCode ERROR_CODE_HNG2 = new ErrorMessageCode("HNG2");
44   private static final ErrorMessageCode ERROR_CODE_HNG3 = new ErrorMessageCode("HNG3");
45
46   @Override
47   public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
48                        GlobalValidationContext globalContext, ValidationContext validationContext) {
49     validateNovaServerGroupPolicy(fileName, resourceEntry, globalContext);
50     validateServerGroupIsUsed(fileName, resourceEntry, globalContext,
51             (HeatResourceValidationContext) validationContext);
52   }
53
54   @SuppressWarnings("unchecked")
55   private static void validateNovaServerGroupPolicy(String fileName,
56                                                     Map.Entry<String, Resource> resourceEntry,
57                                                     GlobalValidationContext globalContext) {
58
59     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("file", fileName);
60
61     Resource resource = resourceEntry.getValue();
62     Object policies =
63             resource.getProperties() == null ? null : resource.getProperties().get("policies");
64
65     if (Objects.nonNull(policies) && policies instanceof List) {
66       List<Object> policiesList = (List<Object>) policies;
67       if (policiesList.size() == 1) {
68         Object policy = policiesList.get(0);
69         if (!isGivenPolicyValid(policy)) {
70           globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
71                           .getErrorWithParameters(
72                                   ERROR_CODE_HNG1, Messages.WRONG_POLICY_IN_SERVER_GROUP.getErrorMessage(),
73                                   resourceEntry.getKey()),
74                   LoggerTragetServiceName.VALIDATE_NOVA_SEVER_GROUP_POLICY,
75                   LoggerErrorDescription.WRONG_POLICY_SERVER_GROUP);
76         }
77       } else {
78         globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
79                         .getErrorWithParameters(ERROR_CODE_HNG1,
80                                 Messages.WRONG_POLICY_IN_SERVER_GROUP.getErrorMessage(),
81                                 resourceEntry.getKey()),
82                 LoggerTragetServiceName.VALIDATE_NOVA_SEVER_GROUP_POLICY,
83                 LoggerErrorDescription.WRONG_POLICY_SERVER_GROUP);
84       }
85     }
86
87     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("file", fileName);
88   }
89
90   private static boolean isGivenPolicyValid(Object policy) {
91     if (policy instanceof Map) {
92       return true;
93     }
94     if (policy instanceof String) {
95       return PolicyTypes.isGivenPolicyValid((String) policy);
96     }
97     return false;
98   }
99
100   public void validateServerGroupIsUsed(String fileName,
101                                         Map.Entry<String, Resource> resourceEntry,
102                                         GlobalValidationContext globalContext,
103                                         HeatResourceValidationContext validationContext) {
104
105     Map<String, Map<String, List<String>>> pointedServerGroups =
106             validationContext.getFileLevelResourceDependencies().get(HeatResourcesTypes
107                     .NOVA_SERVER_GROUP_RESOURCE_TYPE.getHeatResource());
108
109     if (MapUtils.isEmpty(pointedServerGroups)) {
110       globalContext
111               .addMessage(
112                       fileName,
113                       ErrorLevel.WARNING,
114                       ErrorMessagesFormatBuilder
115                               .getErrorWithParameters(
116                                       ERROR_CODE_HNG2, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
117                                       ValidatorConstants.Server_Group, resourceEntry.getKey()),
118                       LoggerTragetServiceName.VALIDATE_ALL_SERVER_GROUP_OR_SECURITY_GROUP_IN_USE,
119                       LoggerErrorDescription.SERVER_GROUP_SECURITY_GROUP_NOT_IN_USE);
120       return;
121     }
122
123     handleServerGroupReferences(fileName, resourceEntry, pointedServerGroups, globalContext);
124   }
125
126   private void handleServerGroupReferences(String fileName, Map.Entry<String, Resource>
127           resourceEntry, Map<String, Map<String, List<String>>> pointedServerGroups,
128                                            GlobalValidationContext globalContext) {
129     Map<String, List<String>> resourcesPointingToCurrServerGroup =
130             pointedServerGroups.get(resourceEntry.getKey());
131
132     if (MapUtils.isEmpty(resourcesPointingToCurrServerGroup)) {
133       globalContext
134               .addMessage(
135                       fileName,
136                       ErrorLevel.WARNING,
137                       ErrorMessagesFormatBuilder
138                               .getErrorWithParameters(
139                                       ERROR_CODE_HNG3, Messages.RESOURCE_NOT_IN_USE.getErrorMessage(),
140                                       ValidatorConstants.Server_Group, resourceEntry.getKey()),
141                       LoggerTragetServiceName.VALIDATE_ALL_SERVER_GROUP_OR_SECURITY_GROUP_IN_USE,
142                       LoggerErrorDescription.SERVER_GROUP_SECURITY_GROUP_NOT_IN_USE);
143     }
144
145   }
146 }