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.DefinedHeatParameterTypes;
25 import org.openecomp.sdc.heat.datatypes.model.Resource;
26 import org.openecomp.sdc.heat.services.HeatConstants;
27 import org.openecomp.sdc.heat.services.HeatStructureUtil;
28 import org.openecomp.sdc.validation.ResourceValidator;
29 import org.openecomp.sdc.validation.ValidationContext;
30 import org.openecomp.sdc.validation.impl.util.HeatValidationService;
31 import org.openecomp.sdc.validation.type.HeatResourceValidationContext;
33 import java.util.List;
35 import java.util.Objects;
36 import java.util.Optional;
39 public class VirtualMachineInterfaceValidator implements ResourceValidator {
40 private static final ErrorMessageCode ERROR_CODE_VLAN1 = new ErrorMessageCode("VLAN1");
41 private static final ErrorMessageCode ERROR_CODE_VLAN2 = new ErrorMessageCode("VLAN2");
45 public void validate(String fileName, Map.Entry<String, Resource> resourceEntry,
46 GlobalValidationContext globalContext, ValidationContext validationContext) {
47 if (ToggleableFeature.VLAN_TAGGING.isActive()) {
48 HeatResourceValidationContext heatResourceValidationContext =
49 (HeatResourceValidationContext) validationContext;
50 Optional<Object> tagPropertyValue = getVlanTagPropertyValue(resourceEntry.getValue());
53 .ifPresent(o -> validateHasSingleParentPort(fileName, resourceEntry, globalContext,
54 heatResourceValidationContext));
55 validateHasTwoProperties(fileName, resourceEntry, globalContext);
61 private void validateHasSingleParentPort(String fileName,
62 Map.Entry<String, Resource> resourceEntry,
63 GlobalValidationContext globalContext,
64 HeatResourceValidationContext heatResourceValidationContext) {
65 Object refsPropertyValue = resourceEntry.getValue().getProperties()
66 .get(HeatConstants.VMI_REFS_PROPERTY_NAME);
67 if (Objects.isNull(refsPropertyValue)) {
70 boolean hasSingleParentPort= HeatValidationService.hasSingleParentPort(fileName, globalContext,
71 heatResourceValidationContext,
73 if (!hasSingleParentPort) {
74 globalContext.addMessage(fileName, ErrorLevel.ERROR, ErrorMessagesFormatBuilder
75 .getErrorWithParameters(ERROR_CODE_VLAN1,
76 Messages.VLAN_SUBINTERFACE_MORE_THAN_ONE_PORT.getErrorMessage(),
77 resourceEntry.getKey()));
84 private void validateHasTwoProperties(String fileName, Map.Entry<String, Resource> resourceEntry,
85 GlobalValidationContext globalContext) {
87 Optional<Object> refsPropertyValue = getRefsPropertyValue(resourceEntry.getValue());
88 Optional<Object> tagPropertyValue = getVlanTagPropertyValue(resourceEntry.getValue());
91 if (refsPropertyValue.isPresent() && !tagPropertyValue.isPresent()) {
93 .addMessage(fileName, ErrorLevel.WARNING,
94 ErrorMessagesFormatBuilder
95 .getErrorWithParameters(
97 Messages.VLAN_SUBINTERFACE_MISSING_TAG_PROPERTY.getErrorMessage(),
98 resourceEntry.getKey())
101 } else if (!refsPropertyValue.isPresent() && tagPropertyValue.isPresent()) {
103 .addMessage(fileName, ErrorLevel.WARNING,
104 ErrorMessagesFormatBuilder
105 .getErrorWithParameters(
107 Messages.VLAN_SUBINTERFACE_MISSING_REFS_PROPERTY.getErrorMessage(),
108 resourceEntry.getKey()));
115 private Optional<Object> getVlanTagPropertyValue(Resource resource) {
116 Object vmiProperties = resource.getProperties()
117 .get(HeatConstants.VMI_PROPERTIES_PROPERTY_NAME);
118 if (Objects.nonNull(vmiProperties) && vmiProperties instanceof Map) {
119 return Optional.ofNullable(((Map) vmiProperties)
120 .get(HeatConstants.VMI_SUB_INTERFACE_VLAN_TAG_PROPERTY_NAME));
122 return Optional.empty();
125 private Optional<Object> getRefsPropertyValue(Resource resource) {
126 Object refsProperty = resource.getProperties()
127 .get(HeatConstants.VMI_REFS_PROPERTY_NAME);
128 return Optional.ofNullable(refsProperty);
133 private enum Messages {
134 VLAN_SUBINTERFACE_MORE_THAN_ONE_PORT(
135 "More than one parent port found, there should be only one parent port for a VLAN sub-interface ID [%s]"),
136 VLAN_SUBINTERFACE_MISSING_TAG_PROPERTY("VLAN Tag property " +
137 "virtual_machine_interface_properties_sub_interface_vlan_tag is missing in VLAN Resource ID [%s]"),
138 VLAN_SUBINTERFACE_MISSING_REFS_PROPERTY("Parent port property virtual_machine_interface_refs " +
139 "is missing in VLAN Resource ID [%s]");
141 String getErrorMessage() {
145 private final String errorMessage;
147 Messages(String errorMessage) {
148 this.errorMessage = errorMessage;