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