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.common.togglz.ToggleableFeature;
23 import org.openecomp.sdc.datatypes.error.ErrorLevel;
24 import org.openecomp.sdc.heat.datatypes.model.Resource;
25 import org.openecomp.sdc.heat.services.HeatConstants;
26 import org.openecomp.sdc.validation.ResourceValidator;
27 import org.openecomp.sdc.validation.ValidationContext;
28 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
29 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
32 import java.util.Objects;
33 import java.util.Optional;
35 public class VirtualMachineInterfaceValidator implements ResourceValidator {
36 private static final ErrorMessageCode ERROR_CODE_VLAN1 = new ErrorMessageCode("VLAN1");
37 private static final ErrorMessageCode ERROR_CODE_VLAN2 = new ErrorMessageCode("VLAN2");
41 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
42 GlobalValidationContext globalContext, ValidationContext validationContext) {
43 if (ToggleableFeature.VLAN_TAGGING.isActive()) {
44 HeatResourceValidationContext heatResourceValidationContext =
45 (HeatResourceValidationContext) validationContext;
46 final ValidityStatus status = calculateValidityStatus(resourceEntry.getValue());
49 case BOTH_PROPERTIES_PRESENT:
50 validateHasSingleParentPort(fileName, resourceEntry, globalContext,
51 heatResourceValidationContext);
53 case REFS_PROPERTY_MISSING:
55 .addMessage(fileName, ErrorLevel.WARNING,
56 ErrorMessagesFormatBuilder
57 .getErrorWithParameters(
59 Messages.VLAN_SUBINTERFACE_MISSING_REFS_PROPERTY.getErrorMessage(),
60 resourceEntry.getKey()));
62 case VLAN_TAG_PROPERTY_MISSING:
64 .addMessage(fileName, ErrorLevel.WARNING,
65 ErrorMessagesFormatBuilder
66 .getErrorWithParameters(
68 Messages.VLAN_SUBINTERFACE_MISSING_TAG_PROPERTY.getErrorMessage(),
69 resourceEntry.getKey()));
70 validateHasSingleParentPort(fileName, resourceEntry, globalContext,
71 heatResourceValidationContext);
73 case BOTH_PROPERTIES_MISSING:
74 // this is a port and not a VLAN, no further validation required
77 throw new IllegalArgumentException("Received a value for which no handling is " +
78 "available " + status);
83 private ValidityStatus calculateValidityStatus(Resource resource) {
84 Optional<Object> refsPropertyValue = getRefsPropertyValue(resource);
85 Optional<Object> tagPropertyValue = getVlanTagPropertyValue(resource);
87 if (refsPropertyValue.isPresent() && tagPropertyValue.isPresent()) {
88 return ValidityStatus.BOTH_PROPERTIES_PRESENT;
90 if (!refsPropertyValue.isPresent() && !tagPropertyValue.isPresent()) {
91 return ValidityStatus.BOTH_PROPERTIES_MISSING;
93 return refsPropertyValue.map(o -> ValidityStatus.VLAN_TAG_PROPERTY_MISSING)
94 .orElse(ValidityStatus.REFS_PROPERTY_MISSING);
98 private void validateHasSingleParentPort(String fileName,
99 Map.Entry<String, Resource> resourceEntry,
100 GlobalValidationContext globalContext,
101 HeatResourceValidationContext heatResourceValidationContext) {
102 Object refsPropertyValue = resourceEntry.getValue().getProperties()
103 .get(HeatConstants.VMI_REFS_PROPERTY_NAME);
104 if (Objects.isNull(refsPropertyValue)) {
107 boolean hasSingleParentPort = HeatValidationService.hasSingleParentPort(fileName, globalContext,
108 heatResourceValidationContext,
110 if (!hasSingleParentPort) {
111 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
112 .getErrorWithParameters(ERROR_CODE_VLAN1,
113 Messages.VLAN_SUBINTERFACE_MORE_THAN_ONE_PORT.getErrorMessage(),
114 resourceEntry.getKey()));
121 private Optional<Object> getVlanTagPropertyValue(Resource resource) {
122 Object vmiProperties = resource.getProperties()
123 .get(HeatConstants.VMI_PROPERTIES_PROPERTY_NAME);
124 if (Objects.nonNull(vmiProperties) && vmiProperties instanceof Map) {
125 return Optional.ofNullable(((Map) vmiProperties)
126 .get(HeatConstants.VMI_SUB_INTERFACE_VLAN_TAG_PROPERTY_NAME));
128 return Optional.empty();
131 private Optional<Object> getRefsPropertyValue(Resource resource) {
132 Object refsProperty = resource.getProperties()
133 .get(HeatConstants.VMI_REFS_PROPERTY_NAME);
134 return Optional.ofNullable(refsProperty);
139 private enum ValidityStatus {
140 BOTH_PROPERTIES_MISSING,
141 BOTH_PROPERTIES_PRESENT,
142 REFS_PROPERTY_MISSING,
143 VLAN_TAG_PROPERTY_MISSING
147 private enum Messages {
148 VLAN_SUBINTERFACE_MORE_THAN_ONE_PORT(
149 "More than one parent port found, there should be only one parent port for a VLAN sub-interface ID [%s]"),
150 VLAN_SUBINTERFACE_MISSING_TAG_PROPERTY("VLAN Tag property " +
151 "virtual_machine_interface_properties_sub_interface_vlan_tag is missing in VLAN Resource ID [%s]"),
152 VLAN_SUBINTERFACE_MISSING_REFS_PROPERTY("Parent port property virtual_machine_interface_refs " +
153 "is missing in VLAN Resource ID [%s]");
155 String getErrorMessage() {
159 private final String errorMessage;
161 Messages(String errorMessage) {
162 this.errorMessage = errorMessage;