b5fa80e381c025c48a74a8828e9b15773ce06937
[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.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;
32
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Objects;
36 import java.util.Optional;
37 import java.util.Set;
38
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");
42
43
44   @Override
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());
51
52       tagPropertyValue
53           .ifPresent(o -> validateHasSingleParentPort(fileName, resourceEntry, globalContext,
54               heatResourceValidationContext));
55       validateHasTwoProperties(fileName, resourceEntry, globalContext);
56
57     }
58   }
59
60
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)) {
68       return;
69     }
70     boolean hasSingleParentPort= HeatValidationService.hasSingleParentPort(fileName, globalContext,
71         heatResourceValidationContext,
72             refsPropertyValue);
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()));
78     }
79
80
81   }
82
83
84   private void validateHasTwoProperties(String fileName, Map.Entry<String, Resource> resourceEntry,
85                                         GlobalValidationContext globalContext) {
86
87     Optional<Object> refsPropertyValue = getRefsPropertyValue(resourceEntry.getValue());
88     Optional<Object> tagPropertyValue = getVlanTagPropertyValue(resourceEntry.getValue());
89
90
91     if (refsPropertyValue.isPresent() && !tagPropertyValue.isPresent()) {
92       globalContext
93           .addMessage(fileName, ErrorLevel.WARNING,
94               ErrorMessagesFormatBuilder
95                   .getErrorWithParameters(
96                       ERROR_CODE_VLAN2,
97                       Messages.VLAN_SUBINTERFACE_MISSING_TAG_PROPERTY.getErrorMessage(),
98                       resourceEntry.getKey())
99           );
100
101     } else if (!refsPropertyValue.isPresent() && tagPropertyValue.isPresent()) {
102       globalContext
103           .addMessage(fileName, ErrorLevel.WARNING,
104               ErrorMessagesFormatBuilder
105                   .getErrorWithParameters(
106                       ERROR_CODE_VLAN2,
107                       Messages.VLAN_SUBINTERFACE_MISSING_REFS_PROPERTY.getErrorMessage(),
108                       resourceEntry.getKey()));
109
110     }
111
112   }
113
114
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));
121     }
122     return Optional.empty();
123   }
124
125   private Optional<Object> getRefsPropertyValue(Resource resource) {
126     Object refsProperty = resource.getProperties()
127         .get(HeatConstants.VMI_REFS_PROPERTY_NAME);
128     return Optional.ofNullable(refsProperty);
129
130   }
131
132
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]");
140
141     String getErrorMessage() {
142       return errorMessage;
143     }
144
145     private final String errorMessage;
146
147     Messages(String errorMessage) {
148       this.errorMessage = errorMessage;
149     }
150
151   }
152
153 }