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