2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.validation.impl.validators.heatresource;
19 import org.openecomp.core.validation.ErrorMessageCode;
20 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
21 import org.openecomp.core.validation.types.GlobalValidationContext;
22 import org.openecomp.sdc.datatypes.error.ErrorLevel;
23 import org.openecomp.sdc.heat.datatypes.model.Resource;
24 import org.openecomp.sdc.heat.services.HeatConstants;
25 import org.openecomp.sdc.validation.ResourceValidator;
26 import org.openecomp.sdc.validation.ValidationContext;
27 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
28 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
31 import java.util.Objects;
32 import java.util.Optional;
34 public class VirtualMachineInterfaceValidator implements ResourceValidator {
35 private static final ErrorMessageCode ERROR_CODE_VLAN1 = new ErrorMessageCode("VLAN1");
36 private static final ErrorMessageCode ERROR_CODE_VLAN2 = new ErrorMessageCode("VLAN2");
40 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
41 GlobalValidationContext globalContext, ValidationContext validationContext) {
42 HeatResourceValidationContext heatResourceValidationContext =
43 (HeatResourceValidationContext) validationContext;
44 final ValidityStatus status = calculateValidityStatus(resourceEntry.getValue());
47 case BOTH_PROPERTIES_PRESENT:
48 validateHasSingleParentPort(fileName, resourceEntry, globalContext,
49 heatResourceValidationContext);
51 case REFS_PROPERTY_MISSING:
53 .addMessage(fileName, ErrorLevel.WARNING,
54 ErrorMessagesFormatBuilder
55 .getErrorWithParameters(
57 Messages.VLAN_SUBINTERFACE_MISSING_REFS_PROPERTY.getErrorMessage(),
58 resourceEntry.getKey()));
60 case VLAN_TAG_PROPERTY_MISSING:
62 .addMessage(fileName, ErrorLevel.WARNING,
63 ErrorMessagesFormatBuilder
64 .getErrorWithParameters(
66 Messages.VLAN_SUBINTERFACE_MISSING_TAG_PROPERTY.getErrorMessage(),
67 resourceEntry.getKey()));
68 validateHasSingleParentPort(fileName, resourceEntry, globalContext,
69 heatResourceValidationContext);
71 case BOTH_PROPERTIES_MISSING:
72 // this is a port and not a VLAN, no further validation required
75 throw new IllegalArgumentException("Received a value for which no handling is " +
76 "available " + status);
80 private ValidityStatus calculateValidityStatus(Resource resource) {
81 Optional<Object> refsPropertyValue = getRefsPropertyValue(resource);
82 Optional<Object> tagPropertyValue = getVlanTagPropertyValue(resource);
84 if (refsPropertyValue.isPresent() && tagPropertyValue.isPresent()) {
85 return ValidityStatus.BOTH_PROPERTIES_PRESENT;
87 if (!refsPropertyValue.isPresent() && !tagPropertyValue.isPresent()) {
88 return ValidityStatus.BOTH_PROPERTIES_MISSING;
90 return refsPropertyValue.map(o -> ValidityStatus.VLAN_TAG_PROPERTY_MISSING)
91 .orElse(ValidityStatus.REFS_PROPERTY_MISSING);
95 private void validateHasSingleParentPort(String fileName,
96 Map.Entry<String, Resource> resourceEntry,
97 GlobalValidationContext globalContext,
98 HeatResourceValidationContext heatResourceValidationContext) {
99 Object refsPropertyValue = resourceEntry.getValue().getProperties()
100 .get(HeatConstants.VMI_REFS_PROPERTY_NAME);
101 if (Objects.isNull(refsPropertyValue)) {
104 boolean hasSingleParentPort = HeatValidationService.hasSingleParentPort(fileName, globalContext,
105 heatResourceValidationContext,
107 if (!hasSingleParentPort) {
108 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
109 .getErrorWithParameters(ERROR_CODE_VLAN1,
110 Messages.VLAN_SUBINTERFACE_MORE_THAN_ONE_PORT.getErrorMessage(),
111 resourceEntry.getKey()));
118 private Optional<Object> getVlanTagPropertyValue(Resource resource) {
119 Object vmiProperties = resource.getProperties()
120 .get(HeatConstants.VMI_PROPERTIES_PROPERTY_NAME);
121 if (Objects.nonNull(vmiProperties) && vmiProperties instanceof Map) {
122 return Optional.ofNullable(((Map) vmiProperties)
123 .get(HeatConstants.VMI_SUB_INTERFACE_VLAN_TAG_PROPERTY_NAME));
125 return Optional.empty();
128 private Optional<Object> getRefsPropertyValue(Resource resource) {
129 Object refsProperty = resource.getProperties()
130 .get(HeatConstants.VMI_REFS_PROPERTY_NAME);
131 return Optional.ofNullable(refsProperty);
136 private enum ValidityStatus {
137 BOTH_PROPERTIES_MISSING,
138 BOTH_PROPERTIES_PRESENT,
139 REFS_PROPERTY_MISSING,
140 VLAN_TAG_PROPERTY_MISSING
144 private enum Messages {
145 VLAN_SUBINTERFACE_MORE_THAN_ONE_PORT(
146 "More than one parent port found, there should be only one parent port for a VLAN sub-interface ID [%s]"),
147 VLAN_SUBINTERFACE_MISSING_TAG_PROPERTY("VLAN Tag property " +
148 "virtual_machine_interface_properties_sub_interface_vlan_tag is missing in VLAN Resource ID [%s]"),
149 VLAN_SUBINTERFACE_MISSING_REFS_PROPERTY("Parent port property virtual_machine_interface_refs " +
150 "is missing in VLAN Resource ID [%s]");
152 String getErrorMessage() {
156 private final String errorMessage;
158 Messages(String errorMessage) {
159 this.errorMessage = errorMessage;