aboutsummaryrefslogtreecommitdiffstats
path: root/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/validator/NodeTypeValidator.java
blob: f46d6483c9a3ec3b99b2000865e18069a9cdd772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * Modifications Copyright © 2018 IBM.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.config.model.validator;

import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.onap.ccsdk.config.model.ConfigModelConstant;
import org.onap.ccsdk.config.model.ConfigModelException;
import org.onap.ccsdk.config.model.ValidTypes;
import org.onap.ccsdk.config.model.data.CapabilityDefinition;
import org.onap.ccsdk.config.model.data.DataType;
import org.onap.ccsdk.config.model.data.InterfaceDefinition;
import org.onap.ccsdk.config.model.data.NodeType;
import org.onap.ccsdk.config.model.data.OperationDefinition;
import org.onap.ccsdk.config.model.data.PropertyDefinition;
import org.onap.ccsdk.config.model.data.RequirementDefinition;
import org.onap.ccsdk.config.model.data.ServiceTemplate;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

/**
 * NodeTypeValidator.java Purpose: Provide Configuration Generator NodeTypeValidator
 *
 * @version 1.0
 */
public class NodeTypeValidator {
    private static EELFLogger logger = EELFManager.getInstance().getLogger(NodeTypeValidator.class);
    private StringBuilder message;
    private Map<String, DataType> stDataTypes;
    private Map<String, NodeType> stNodeTypes;
    private ServiceTemplate serviceTemplate;
    private PropertyDefinitionValidator propertyDefinitionValidator;

    /**
     * This is a NodeTypeValidator
     *
     * @param serviceTemplate
     * @throws ConfigModelException
     */
    public NodeTypeValidator(ServiceTemplate serviceTemplate, StringBuilder message) throws ConfigModelException {
        this.serviceTemplate = serviceTemplate;
        this.message = message;
        propertyDefinitionValidator = new PropertyDefinitionValidator(this.message);
        stDataTypes = new HashMap<>();
        stNodeTypes = new HashMap<>();
        loadInitial();

    }

    private void loadInitial() {
        if (serviceTemplate != null) {

            if (serviceTemplate.getDataTypes() != null) {
                serviceTemplate.getDataTypes().forEach((dataTypeKey, dataType) -> {
                    stDataTypes.put(dataTypeKey, dataType);
                    logger.trace("Data Type ({}) loaded successfully.", dataTypeKey);
                });
            }

            if (serviceTemplate.getNodeTypes() != null) {
                serviceTemplate.getNodeTypes().forEach((nodeTypeKey, nodeType) -> {
                    stNodeTypes.put(nodeTypeKey, nodeType);
                    logger.trace("NodeType Type ({}) loaded successfully.", nodeTypeKey);
                });
            }

        }

    }

    /**
     * This is a validateNodeTypes to validate the Node Type
     *
     * @return boolean
     * @throws ConfigModelException
     */
    @SuppressWarnings({"squid:S00112", "squid:S3776"})
    public boolean validateNodeTypes() {
        if (serviceTemplate != null && serviceTemplate.getNodeTypes() != null) {
            serviceTemplate.getNodeTypes().forEach((nodeTypeKey, nodeType) -> {
                if (nodeType != null) {
                    message.append("\n ***** Validation Node Type  (" + nodeTypeKey + "), derived from ("
                            + nodeType.getDerivedFrom() + ")");
                    try {
                        validateNodeType(ConfigModelConstant.MODEL_DEFINITION_TYPE_NODE_TYPE,
                                nodeType.getDerivedFrom());

                        if (nodeType.getProperties() != null) {
                            checkValidProperties(nodeType.getProperties());
                        }

                        if (nodeType.getCapabilities() != null) {
                            validateNodeTypeCapabilities(nodeType.getCapabilities());
                        }

                        if (nodeType.getInterfaces() != null) {
                            validateNodeTypeInterface(nodeType.getInterfaces());
                        }

                        if (nodeType.getRequirements() != null) {
                            validateNodeTypeRequirement(nodeType.getRequirements());
                        }

                    } catch (ConfigModelException e) {
                        logger.error(e.getMessage());
                        throw new RuntimeException(e.getMessage());
                    }

                }
            });
        }
        return true;
    }

    private boolean validateNodeType(String definitionType, String derivedFrom) throws ConfigModelException {
        boolean valid = true;
        if (!ConfigModelConstant.MODEL_DEFINITION_TYPE_DATA_TYPE.equalsIgnoreCase(definitionType)
                && !ValidTypes.getValidNodeTypes().contains(derivedFrom)) {
            throw new ConfigModelException("Not Valid Model Type (" + derivedFrom + ")");
        }
        return valid;
    }

    private boolean checkValidProperties(Map<String, PropertyDefinition> properties) {
        if (properties != null) {
            propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);
        }
        return true;
    }

    @SuppressWarnings("squid:S00112")
    private boolean validateNodeTypeCapabilities(Map<String, CapabilityDefinition> capabilities) {
        if (capabilities != null) {
            capabilities.forEach((capabilityKey, capability) -> {
                if (capability != null) {
                    Map<String, PropertyDefinition> properties = capability.getProperties();
                    message.append("\n Validation Capability (" + capabilityKey + ") properties :");
                    propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, properties);
                }
            });
        }
        return true;
    }

    @SuppressWarnings("squid:S00112")
    private boolean validateNodeTypeInterface(Map<String, InterfaceDefinition> interfaces) {
        if (interfaces != null) {
            interfaces.forEach((interfaceKey, interfaceDefinition) -> {
                if (interfaceDefinition != null && interfaceDefinition.getOperations() != null) {
                    validateNodeTypeInterfaceOperation(interfaceDefinition.getOperations());
                }
            });
        }
        return true;
    }

    @SuppressWarnings("squid:S00112")
    private boolean validateNodeTypeInterfaceOperation(Map<String, OperationDefinition> operations) {
        if (operations != null) {
            operations.forEach((operationKey, operation) -> {
                if (operation != null) {
                    Map<String, PropertyDefinition> inputs = operation.getInputs();
                    message.append("\n Validation Operation (" + operationKey + ") Inputs :");
                    propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, inputs);
                    message.append("\n Validation Operation (" + operationKey + ") output :");
                    Map<String, PropertyDefinition> outputs = operation.getOutputs();
                    propertyDefinitionValidator.validatePropertyDefinition(stDataTypes, outputs);
                }
            });
        }
        return true;
    }

    @SuppressWarnings("squid:S00112")
    private boolean validateNodeTypeRequirement(Map<String, RequirementDefinition> requirements) {
        if (requirements != null) {
            requirements.forEach((requirementKey, requirement) -> {
                if (requirement != null) {
                    String nodeTypeName = requirement.getNode();
                    String capabilityName = requirement.getCapability();
                    try {
                        checkCapabilityPresentInNodeType(nodeTypeName, capabilityName);
                    } catch (ConfigModelException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }
        return true;
    }

    private boolean checkCapabilityPresentInNodeType(String nodeTypeName, String capabilityName)
            throws ConfigModelException {
        if (StringUtils.isNotBlank(nodeTypeName) && StringUtils.isNotBlank(capabilityName)) {

            if (!stNodeTypes.containsKey(nodeTypeName)) {
                throw new ConfigModelException(nodeTypeName + " Node Type not Defined.");
            } else {
                message.append("\n Node Type  (" + nodeTypeName + ") Defined.");
            }

            NodeType relationalNodeType = stNodeTypes.get(nodeTypeName);

            if (relationalNodeType.getCapabilities() == null) {
                throw new ConfigModelException(
                        "Node Type  (" + nodeTypeName + "), doesn't have Capability Definitions.");
            }

            if (!relationalNodeType.getCapabilities().containsKey(capabilityName)) {
                throw new ConfigModelException("Node Type (" + nodeTypeName + ") doesn't have  (" + capabilityName
                        + ") Capability Definitions.");
            } else {
                message.append(
                        "\n Node Type (" + nodeTypeName + ") has (" + capabilityName + ") Capability Definitions.");
            }

        }
        return true;
    }

}