6b9ea28f05f87826508848b51de8ceba70be50a8
[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          * This is a validateModelTypeDefinition
35          *
36          * @param definitionType definitionType
37          * @param definitionContent definitionContent
38          * @return boolean
39          */
40         fun validateModelTypeDefinition(definitionType: String, definitionContent: JsonNode): Boolean {
41
42             when (definitionType) {
43                 BluePrintConstants.MODEL_DEFINITION_TYPE_DATA_TYPE -> {
44                     JacksonUtils.readValue(definitionContent, DataType::class.java)
45                         ?: throw BluePrintException("Model type definition is not DataType valid content $definitionContent")
46                 }
47                 BluePrintConstants.MODEL_DEFINITION_TYPE_NODE_TYPE -> {
48                     JacksonUtils.readValue(definitionContent, NodeType::class.java)
49                         ?: throw BluePrintException("Model type definition is not NodeType valid content $definitionContent")
50                 }
51                 BluePrintConstants.MODEL_DEFINITION_TYPE_ARTIFACT_TYPE -> {
52                     JacksonUtils.readValue(definitionContent, ArtifactType::class.java)
53                         ?: throw BluePrintException("Model type definition is not ArtifactType valid content $definitionContent")
54                 }
55                 BluePrintConstants.MODEL_DEFINITION_TYPE_CAPABILITY_TYPE -> {
56                     JacksonUtils.readValue(definitionContent, CapabilityDefinition::class.java)
57                         ?: throw BluePrintException("Model type definition is not CapabilityDefinition valid content $definitionContent")
58                 }
59                 BluePrintConstants.MODEL_DEFINITION_TYPE_RELATIONSHIP_TYPE -> {
60                     JacksonUtils.readValue(definitionContent, RelationshipType::class.java)
61                         ?: throw BluePrintException("Model type definition is not RelationshipType valid content $definitionContent")
62                 }
63             }
64             return true
65         }
66
67         /**
68          * This is a validateModelType method
69          *
70          * @param modelType modelType
71          * @return boolean
72          */
73         fun validateModelType(modelType: ModelType?): Boolean {
74             checkNotNull(modelType) { "Model Type Information is missing." }
75
76             val validRootTypes = BluePrintTypes.validModelTypes()
77
78             check(validRootTypes.contains(modelType.definitionType)) {
79                 "Not Valid Model Root Type(${modelType.definitionType}), It should be $validRootTypes"
80             }
81
82             validateModelTypeDefinition(modelType.definitionType, modelType.definition)
83             return true
84         }
85     }
86 }