fc817c467de669170c12c50250d277cac3ec7f79
[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.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;
30
31 import java.util.Map;
32 import java.util.Objects;
33 import java.util.Optional;
34
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");
38
39
40   @Override
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());
47
48       switch (status) {
49         case BOTH_PROPERTIES_PRESENT:
50           validateHasSingleParentPort(fileName, resourceEntry, globalContext,
51               heatResourceValidationContext);
52           break;
53         case REFS_PROPERTY_MISSING:
54           globalContext
55               .addMessage(fileName, ErrorLevel.WARNING,
56                   ErrorMessagesFormatBuilder
57                       .getErrorWithParameters(
58                           ERROR_CODE_VLAN2,
59                           Messages.VLAN_SUBINTERFACE_MISSING_REFS_PROPERTY.getErrorMessage(),
60                           resourceEntry.getKey()));
61           break;
62         case VLAN_TAG_PROPERTY_MISSING:
63           globalContext
64               .addMessage(fileName, ErrorLevel.WARNING,
65                   ErrorMessagesFormatBuilder
66                       .getErrorWithParameters(
67                           ERROR_CODE_VLAN2,
68                           Messages.VLAN_SUBINTERFACE_MISSING_TAG_PROPERTY.getErrorMessage(),
69                           resourceEntry.getKey()));
70           validateHasSingleParentPort(fileName, resourceEntry, globalContext,
71               heatResourceValidationContext);
72           break;
73         case BOTH_PROPERTIES_MISSING:
74           // this is a port and not a VLAN, no further validation required
75           break;
76         default :
77           throw new IllegalArgumentException("Received a value for which no handling is " +
78               "available " + status);
79       }
80     }
81   }
82
83   private ValidityStatus calculateValidityStatus(Resource resource) {
84     Optional<Object> refsPropertyValue = getRefsPropertyValue(resource);
85     Optional<Object> tagPropertyValue = getVlanTagPropertyValue(resource);
86
87     if (refsPropertyValue.isPresent() && tagPropertyValue.isPresent()) {
88       return ValidityStatus.BOTH_PROPERTIES_PRESENT;
89     }
90     if (!refsPropertyValue.isPresent() && !tagPropertyValue.isPresent()) {
91       return ValidityStatus.BOTH_PROPERTIES_MISSING;
92     }
93     return refsPropertyValue.map(o -> ValidityStatus.VLAN_TAG_PROPERTY_MISSING)
94         .orElse(ValidityStatus.REFS_PROPERTY_MISSING);
95   }
96
97
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)) {
105       return;
106     }
107     boolean hasSingleParentPort = HeatValidationService.hasSingleParentPort(fileName, globalContext,
108         heatResourceValidationContext,
109         refsPropertyValue);
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()));
115     }
116
117
118   }
119
120
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));
127     }
128     return Optional.empty();
129   }
130
131   private Optional<Object> getRefsPropertyValue(Resource resource) {
132     Object refsProperty = resource.getProperties()
133         .get(HeatConstants.VMI_REFS_PROPERTY_NAME);
134     return Optional.ofNullable(refsProperty);
135
136   }
137
138
139   private enum ValidityStatus {
140     BOTH_PROPERTIES_MISSING,
141     BOTH_PROPERTIES_PRESENT,
142     REFS_PROPERTY_MISSING,
143     VLAN_TAG_PROPERTY_MISSING
144
145   }
146
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]");
154
155     String getErrorMessage() {
156       return errorMessage;
157     }
158
159     private final String errorMessage;
160
161     Messages(String errorMessage) {
162       this.errorMessage = errorMessage;
163     }
164
165   }
166
167 }