bbd81e13278c6ea11047007721a8d9eca919c748
[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;
18
19 import org.apache.commons.collections4.MapUtils;
20 import org.openecomp.core.validation.ErrorMessageCode;
21 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
22 import org.openecomp.core.validation.types.GlobalValidationContext;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
26 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
27 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
28 import org.openecomp.sdc.heat.datatypes.model.Resource;
29 import org.openecomp.sdc.heat.services.manifest.ManifestUtil;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
33 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
34 import org.openecomp.sdc.validation.Validator;
35 import org.openecomp.sdc.validation.util.ValidationUtil;
36
37 import java.util.HashSet;
38 import java.util.Map;
39 import java.util.Objects;
40 import java.util.Set;
41
42 public class ForbiddenResourceGuideLineValidator implements Validator {
43   private static Set<String> forbiddenResources = new HashSet<>();
44   private static final ErrorMessageCode ERROR_CODE_FRG_1 = new ErrorMessageCode("FRG1");
45   private static final ErrorMessageCode ERROR_CODE_FRG_2 = new ErrorMessageCode("FRG2");
46   private static final ErrorMessageCode ERROR_CODE_FRG_3 = new ErrorMessageCode("FRG3");
47
48   private static final Logger LOGGER =  LoggerFactory
49           .getLogger(ForbiddenResourceGuideLineValidator.class);
50
51   @Override
52   public void init(Map<String, Object> properties) {
53     Map<String, Map<String, Object>> forbiddenResourcesMap =
54         (Map<String, Map<String, Object>>) properties.get("forbiddenResourceTypes");
55
56     forbiddenResourcesMap.entrySet().stream()
57         .filter(entry -> isResourceEnabled(entry.getValue().get("enable")))
58         .forEach(entry -> forbiddenResources.add(entry.getKey()));
59   }
60
61   private boolean isResourceEnabled(Object enableValue) {
62     if (Objects.isNull(enableValue)) {
63       return true;
64     }
65
66     if (enableValue instanceof Boolean) {
67       return (Boolean)enableValue;
68     }
69
70     return Boolean.valueOf((String) enableValue);
71   }
72
73
74   @Override
75   public void validate(GlobalValidationContext globalContext) {
76     ManifestContent manifestContent;
77     try {
78       manifestContent = ValidationUtil.validateManifest(globalContext);
79     } catch (Exception exception) {
80       LOGGER.debug("",exception);
81       return;
82     }
83
84     Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent);
85
86     globalContext.getFiles().stream()
87         .filter(fileName -> FileData
88             .isHeatFile(fileTypeMap.get(fileName)))
89         .forEach(fileName -> validate(fileName, globalContext));
90   }
91
92   private void validate(String fileName, GlobalValidationContext globalContext) {
93     globalContext.setMessageCode(ERROR_CODE_FRG_3);
94     HeatOrchestrationTemplate
95         heatOrchestrationTemplate = ValidationUtil.checkHeatOrchestrationPreCondition(
96         fileName, globalContext);
97     if (heatOrchestrationTemplate == null) {
98       return;
99     }
100
101     validateResourceTypeIsForbidden(fileName, heatOrchestrationTemplate, globalContext);
102   }
103
104   private void validateResourceTypeIsForbidden(String fileName,
105                                                HeatOrchestrationTemplate heatOrchestrationTemplate,
106                                                GlobalValidationContext globalContext) {
107     Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
108     if (MapUtils.isEmpty(resourcesMap)) {
109       return;
110     }
111
112     for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
113       String resourceType = resourceEntry.getValue().getType();
114       if (Objects.isNull(resourceType)) {
115         globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
116                 .getErrorWithParameters(ERROR_CODE_FRG_1,
117                         Messages.INVALID_RESOURCE_TYPE.getErrorMessage(),"null",
118                     resourceEntry.getKey()), LoggerTragetServiceName.VALIDATE_RESOURCE_TYPE,
119             LoggerErrorDescription.INVALID_RESOURCE_TYPE);
120       } else {
121         if (isResourceForbidden(resourceType)) {
122            globalContext.addMessage(
123               fileName,
124               ErrorLevel.WARNING,
125               ErrorMessagesFormatBuilder
126                   .getErrorWithParameters(ERROR_CODE_FRG_2, Messages.FORBIDDEN_RESOURCE_IN_USE
127                           .getErrorMessage(),
128                       resourceType, resourceEntry.getKey()),
129               LoggerTragetServiceName.VALIDATE_FORBIDDEN_RESOURCE,
130               LoggerErrorDescription.FLOATING_IP_IN_USE);
131         }
132       }
133     }
134   }
135
136   private boolean isResourceForbidden(String resourceType) {
137     return forbiddenResources.contains(resourceType);
138   }
139 }