9641f8973beec33707213683cf91015de4ec15ba
[ccsdk/cds.git] /
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.apps.controllerblueprints.service.validator;\r
19 \r
20 import com.fasterxml.jackson.databind.JsonNode;\r
21 import org.apache.commons.lang3.StringUtils;\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants;\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.ArtifactType;\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.CapabilityDefinition;\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.DataType;\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType;\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;\r
29 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ModelType;\r
30 \r
31 import java.util.ArrayList;\r
32 import java.util.List;\r
33 \r
34 /**\r
35  * ModelTypeValidation.java Purpose: Provide Validation Service for Model Type ModelTypeValidation\r
36  *\r
37  * @author Brinda Santh\r
38  * @version 1.0\r
39  */\r
40 \r
41 public class ModelTypeValidator {\r
42 \r
43     private ModelTypeValidator() {\r
44 \r
45     }\r
46 \r
47     private static List<String> getValidModelDefinitionType() {\r
48         List<String> validTypes = new ArrayList<>();\r
49         validTypes.add(BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE);\r
50         validTypes.add(BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE);\r
51         validTypes.add(BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE);\r
52         validTypes.add(BluePrintConstants.MODEL_DEFINITION_TYPE_CAPABILITY_TYPE);\r
53         validTypes.add(BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TYPE);\r
54         return validTypes;\r
55     }\r
56 \r
57     /**\r
58      * This is a validateModelTypeDefinition\r
59      * \r
60      * @param definitionType definitionType\r
61      * @param definitionContent definitionContent\r
62      * @return boolean\r
63      * @throws BluePrintException BluePrintException\r
64      */\r
65     public static boolean validateModelTypeDefinition(String definitionType, JsonNode definitionContent)\r
66             throws BluePrintException {\r
67         if (definitionContent != null) {\r
68             if (BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE.equalsIgnoreCase(definitionType)) {\r
69                 DataType dataType = JacksonUtils.readValue(definitionContent, DataType.class);\r
70                 if (dataType == null) {\r
71                     throw new BluePrintException(\r
72                             "Model type definition is not DataType valid content " + definitionContent);\r
73                 }\r
74             } else if (BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE.equalsIgnoreCase(definitionType)) {\r
75                 NodeType nodeType = JacksonUtils.readValue(definitionContent, NodeType.class);\r
76                 if (nodeType == null) {\r
77                     throw new BluePrintException(\r
78                             "Model type definition is not NodeType valid content " + definitionContent);\r
79                 }\r
80             } else if (BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE.equalsIgnoreCase(definitionType)) {\r
81                 ArtifactType artifactType = JacksonUtils.readValue(definitionContent, ArtifactType.class);\r
82                 if (artifactType == null) {\r
83                     throw new BluePrintException(\r
84                             "Model type definition is not ArtifactType valid content " + definitionContent);\r
85                 }\r
86             }else if (BluePrintConstants.MODEL_DEFINITION_TYPE_CAPABILITY_TYPE.equalsIgnoreCase(definitionType)) {\r
87                 CapabilityDefinition capabilityDefinition =\r
88                         JacksonUtils.readValue(definitionContent, CapabilityDefinition.class);\r
89                 if (capabilityDefinition == null) {\r
90                     throw new BluePrintException(\r
91                             "Model type definition is not CapabilityDefinition valid content " + definitionContent);\r
92                 }\r
93             }\r
94 \r
95         }\r
96         return true;\r
97     }\r
98 \r
99     /**\r
100      * This is a validateModelType method\r
101      * \r
102      * @param modelType modelType\r
103      * @return boolean\r
104      * @throws BluePrintException BluePrintException\r
105      */\r
106     public static boolean validateModelType(ModelType modelType) throws BluePrintException {\r
107         if (modelType != null) {\r
108 \r
109             if (StringUtils.isBlank(modelType.getModelName())) {\r
110                 throw new BluePrintException("Model Name Information is missing.");\r
111             }\r
112 \r
113             if (StringUtils.isBlank(modelType.getDefinitionType())) {\r
114                 throw new BluePrintException("Model Root Type Information is missing.");\r
115             }\r
116             if (StringUtils.isBlank(modelType.getDerivedFrom())) {\r
117                 throw new BluePrintException("Model Type Information is missing.");\r
118             }\r
119 \r
120             if (modelType.getDefinition() == null) {\r
121                 throw new BluePrintException("Model Definition Information is missing.");\r
122             }\r
123             if (StringUtils.isBlank(modelType.getDescription())) {\r
124                 throw new BluePrintException("Model Description Information is missing.");\r
125             }\r
126 \r
127             if (StringUtils.isBlank(modelType.getVersion())) {\r
128                 throw new BluePrintException("Model Version Information is missing.");\r
129             }\r
130 \r
131             if (StringUtils.isBlank(modelType.getUpdatedBy())) {\r
132                 throw new BluePrintException("Model Updated By Information is missing.");\r
133             }\r
134 \r
135             List<String> validRootTypes = getValidModelDefinitionType();\r
136             if (!validRootTypes.contains(modelType.getDefinitionType())) {\r
137                 throw new BluePrintException("Not Valid Model Root Type(" + modelType.getDefinitionType()\r
138                         + "), It should be " + validRootTypes);\r
139             }\r
140 \r
141             validateModelTypeDefinition(modelType.getDefinitionType(), modelType.getDefinition());\r
142 \r
143         } else {\r
144             throw new BluePrintException("Model Type Information is missing.");\r
145         }\r
146 \r
147         return true;\r
148 \r
149     }\r
150 \r
151 }\r