[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-sdc-validation-lib / openecomp-sdc-validation-impl / src / main / java / org / openecomp / sdc / validation / impl / validators / ForbiddenResourceGuideLineValidator.java
1 package org.openecomp.sdc.validation.impl.validators;
2
3 import org.apache.commons.collections4.MapUtils;
4 import org.openecomp.sdc.validation.Validator;
5 import org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder;
6 import org.openecomp.core.validation.types.GlobalValidationContext;
7 import org.openecomp.sdc.common.errors.Messages;
8 import org.openecomp.sdc.datatypes.error.ErrorLevel;
9 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
10 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
11 import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
12 import org.openecomp.sdc.heat.datatypes.model.Resource;
13 import org.openecomp.sdc.heat.services.manifest.ManifestUtil;
14 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
15 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
16 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
17 import org.openecomp.sdc.validation.util.ValidationUtil;
18
19 import java.util.HashMap;
20 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Objects;
23 import java.util.Set;
24
25 /**
26  * Created by TALIO on 2/15/2017.
27  */
28 public class ForbiddenResourceGuideLineValidator implements Validator {
29   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
30   private static Set<String> forbiddenResources = new HashSet<>();
31
32   @Override
33   public void init(Map<String, Object> properties) {
34     Map<String, Map<String, Object>> forbiddenResourcesMap =
35         (Map<String, Map<String, Object>>) properties.get("forbiddenResourceTypes");
36
37     forbiddenResourcesMap.entrySet().stream()
38         .filter(entry -> isResourceEnabled(entry.getValue().get("enable")))
39         .forEach(entry -> forbiddenResources.add(entry.getKey()));
40
41
42
43
44   }
45
46   private boolean isResourceEnabled(Object enableValue){
47     if(Objects.isNull(enableValue)){
48       return true;
49     }
50
51     if(enableValue instanceof Boolean){
52       return (Boolean)enableValue;
53     }
54
55     return Boolean.valueOf((String) enableValue);
56   }
57
58
59   @Override
60   public void validate(GlobalValidationContext globalContext) {
61     ManifestContent manifestContent;
62     try {
63       manifestContent = ValidationUtil.checkValidationPreCondition(globalContext);
64     } catch (Exception exception) {
65       return;
66     }
67
68     Map<String, FileData.Type> fileTypeMap = ManifestUtil.getFileTypeMap(manifestContent);
69
70     globalContext.getFiles().stream()
71         .filter(fileName -> FileData
72             .isHeatFile(fileTypeMap.get(fileName)))
73         .forEach(fileName -> validate(fileName, globalContext));
74   }
75
76   private void validate(String fileName, GlobalValidationContext globalContext) {
77     HeatOrchestrationTemplate
78         heatOrchestrationTemplate = ValidationUtil.checkHeatOrchestrationPreCondition(fileName, globalContext);
79     if (heatOrchestrationTemplate == null) {
80       return;
81     }
82
83     validateResourceTypeIsForbidden(fileName, heatOrchestrationTemplate, globalContext);
84   }
85
86   private void validateResourceTypeIsForbidden(String fileName,
87                                                HeatOrchestrationTemplate heatOrchestrationTemplate,
88                                                GlobalValidationContext globalContext) {
89
90     mdcDataDebugMessage.debugEntryMessage("file", fileName);
91
92     Map<String, Resource> resourcesMap = heatOrchestrationTemplate.getResources();
93     if (MapUtils.isEmpty(resourcesMap)) {
94       mdcDataDebugMessage.debugExitMessage("file", fileName);
95       return;
96     }
97
98     for (Map.Entry<String, Resource> resourceEntry : resourcesMap.entrySet()) {
99       String resourceType = resourceEntry.getValue().getType();
100       if (Objects.isNull(resourceType)) {
101         globalContext.addMessage(fileName, ErrorLevel.WARNING, ErrorMessagesFormatBuilder
102                 .getErrorWithParameters(Messages.INVALID_RESOURCE_TYPE.getErrorMessage(), "null",
103                     resourceEntry.getKey()), LoggerTragetServiceName.VALIDATE_RESOURCE_TYPE,
104             LoggerErrorDescription.INVALID_RESOURCE_TYPE);
105       } else {
106         if(isResourceForbidden(resourceType)){
107           globalContext.addMessage(
108               fileName,
109               ErrorLevel.WARNING,
110               ErrorMessagesFormatBuilder
111                   .getErrorWithParameters(Messages.FORBIDDEN_RESOURCE_IN_USE.getErrorMessage(),
112                       resourceType,
113                       resourceEntry.getKey()),
114               LoggerTragetServiceName.VALIDATE_FORBIDDEN_RESOURCE,
115               LoggerErrorDescription.FLOATING_IP_IN_USE);
116         }
117       }
118     }
119     mdcDataDebugMessage.debugExitMessage("file", fileName);
120   }
121
122   private boolean isResourceForbidden(String resourceType){
123     return forbiddenResources.contains(resourceType);
124   }
125 }