Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / model-provider / src / main / java / org / onap / ccsdk / config / model / validator / PropertyDefinitionValidator.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  * \r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  * \r
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  * \r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.config.model.validator;\r
19 \r
20 import java.util.Map;\r
21 import org.apache.commons.lang3.StringUtils;\r
22 import org.onap.ccsdk.config.model.ConfigModelException;\r
23 import org.onap.ccsdk.config.model.ValidTypes;\r
24 import org.onap.ccsdk.config.model.data.DataType;\r
25 import org.onap.ccsdk.config.model.data.PropertyDefinition;\r
26 \r
27 /**\r
28  * PropertyDefinitionValidator.java Purpose: Provide Configuration Generator\r
29  * PropertyDefinitionValidator\r
30  *\r
31  * @version 1.0\r
32  */\r
33 public class PropertyDefinitionValidator {\r
34 \r
35     StringBuilder message = new StringBuilder();\r
36 \r
37     public PropertyDefinitionValidator(StringBuilder message) {\r
38         this.message = message;\r
39 \r
40     }\r
41 \r
42     /**\r
43      * This is a validatePropertyDefinition stored in database\r
44      *\r
45      * @param stDataTypes\r
46      * @param properties\r
47      * @return boolean\r
48      * @throws ConfigModelException\r
49      */\r
50     @SuppressWarnings({"squid:S00112", "squid:S3776", "squid:S1192"})\r
51     public boolean validatePropertyDefinition(Map<String, DataType> stDataTypes,\r
52             Map<String, PropertyDefinition> properties) {\r
53 \r
54         if (stDataTypes != null && properties != null) {\r
55             properties.forEach((propertyKey, prop) -> {\r
56                 if (propertyKey != null && prop != null) {\r
57                     try {\r
58                         String propertType = prop.getType();\r
59                         message.append("\n Validating (" + propertyKey + ") " + prop);\r
60 \r
61                         if (!ValidTypes.getValidPropertType().contains(propertType)\r
62                                 && !stDataTypes.containsKey(propertType)) {\r
63                             throw new ConfigModelException("Data Type (" + propertyKey + ") -> type(" + propertType\r
64                                     + ") is not a valid type.");\r
65                         } else if (ValidTypes.getListPropertType().contains(propertType)) {\r
66                             if (prop.getEntrySchema() == null || StringUtils.isBlank(prop.getEntrySchema().getType())) {\r
67                                 throw new ConfigModelException("Data Type (" + propertyKey + ") -> type (" + propertType\r
68                                         + ") Entity Schema is not defined.");\r
69                             }\r
70 \r
71                             String entitySchemaType = prop.getEntrySchema().getType();\r
72 \r
73                             if (!ValidTypes.getValidPropertType().contains(entitySchemaType)\r
74                                     && !stDataTypes.containsKey(entitySchemaType)) {\r
75                                 message.append("\n Present Data Type " + stDataTypes);\r
76                                 throw new ConfigModelException("Data Type (" + propertyKey + ") -> type(" + propertType\r
77                                         + ") -> entitySchema(" + entitySchemaType + ") is not defined.");\r
78                             }\r
79                         }\r
80                     } catch (ConfigModelException e) {\r
81                         throw new RuntimeException(e);\r
82                     }\r
83                 }\r
84             });\r
85         }\r
86         return true;\r
87     }\r
88 \r
89 }\r