Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / utils / ModelTypeValidator.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ModelType
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.CapabilityDefinition
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30
31 class ModelTypeValidator {
32     companion object {
33
34         /**
35          * This is a validateModelTypeDefinition
36          *
37          * @param definitionType definitionType
38          * @param definitionContent definitionContent
39          * @return boolean
40          */
41         fun validateModelTypeDefinition(definitionType: String, definitionContent: JsonNode): Boolean {
42
43             when (definitionType) {
44                 BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE -> {
45                     JacksonUtils.readValue(definitionContent, DataType::class.java)
46                         ?: throw BluePrintException("Model type definition is not DataType valid content $definitionContent")
47                 }
48                 BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE -> {
49                     JacksonUtils.readValue(definitionContent, NodeType::class.java)
50                         ?: throw BluePrintException("Model type definition is not NodeType valid content $definitionContent")
51                 }
52                 BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE -> {
53                     JacksonUtils.readValue(definitionContent, ArtifactType::class.java)
54                         ?: throw BluePrintException("Model type definition is not ArtifactType valid content $definitionContent")
55                 }
56                 BluePrintConstants.MODEL_DEFINITION_TYPE_CAPABILITY_TYPE -> {
57                     JacksonUtils.readValue(definitionContent, CapabilityDefinition::class.java)
58                         ?: throw BluePrintException("Model type definition is not CapabilityDefinition valid content $definitionContent")
59                 }
60                 BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TYPE -> {
61                     JacksonUtils.readValue(definitionContent, RelationshipType::class.java)
62                         ?: throw BluePrintException("Model type definition is not RelationshipType valid content $definitionContent")
63                 }
64             }
65             return true
66         }
67
68         /**
69          * This is a validateModelType method
70          *
71          * @param modelType modelType
72          * @return boolean
73          */
74         fun validateModelType(modelType: ModelType?): Boolean {
75             checkNotNull(modelType) { "Model Type Information is missing." }
76
77             val validRootTypes = BluePrintTypes.validModelTypes()
78
79             check(validRootTypes.contains(modelType.definitionType)) {
80                 "Not Valid Model Root Type(${modelType.definitionType}), It should be $validRootTypes"
81             }
82
83             validateModelTypeDefinition(modelType.definitionType, modelType.definition)
84             return true
85         }
86     }
87 }