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