Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / model-provider / src / main / java / org / onap / ccsdk / config / model / utils / NodePropertyUtils.java
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.config.model.utils;\r
19 \r
20 import java.io.IOException;\r
21 import java.util.HashMap;\r
22 import java.util.Map;\r
23 import org.apache.commons.lang3.StringUtils;\r
24 import org.onap.ccsdk.config.model.ConfigModelConstant;\r
25 import org.onap.ccsdk.config.model.data.CapabilityAssignment;\r
26 import org.onap.ccsdk.config.model.data.CapabilityDefinition;\r
27 import org.onap.ccsdk.config.model.data.InterfaceAssignment;\r
28 import org.onap.ccsdk.config.model.data.InterfaceDefinition;\r
29 import org.onap.ccsdk.config.model.data.NodeTemplate;\r
30 import org.onap.ccsdk.config.model.data.NodeType;\r
31 import org.onap.ccsdk.config.model.data.OperationAssignment;\r
32 import org.onap.ccsdk.config.model.data.OperationDefinition;\r
33 import org.onap.ccsdk.config.model.data.PropertyDefinition;\r
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
35 import com.att.eelf.configuration.EELFLogger;\r
36 import com.att.eelf.configuration.EELFManager;\r
37 \r
38 public class NodePropertyUtils {\r
39 \r
40     private static EELFLogger logger = EELFManager.getInstance().getLogger(NodePropertyUtils.class);\r
41     private final SvcLogicContext context;\r
42     private final Map<String, String> inParams;\r
43     private ExpressionUtils jsonExpressionUtils;\r
44 \r
45     public NodePropertyUtils(final SvcLogicContext context, final Map<String, String> inParams) {\r
46         this.context = context;\r
47         this.inParams = inParams;\r
48         jsonExpressionUtils = new ExpressionUtils(this.context, this.inParams);\r
49     }\r
50 \r
51     @SuppressWarnings("squid:S3776")\r
52     public SvcLogicContext assignInParamsFromModel(NodeType nodetype, NodeTemplate nodeTemplate) throws IOException {\r
53         if (nodeTemplate != null) {\r
54             Map<String, Object> inputs = null;\r
55             // Populate the Type inputs\r
56             Map<String, PropertyDefinition> nodeTypeinputs = new HashMap<>();\r
57             for (Map.Entry<String, InterfaceDefinition> nodeTypeInterface : nodetype.getInterfaces().entrySet()) {\r
58                 if (nodeTypeInterface != null && nodeTypeInterface.getValue() != null) {\r
59                     for (Map.Entry<String, OperationDefinition> nodeTypeOperation : nodeTypeInterface.getValue()\r
60                             .getOperations().entrySet()) {\r
61                         nodeTypeinputs = nodeTypeOperation.getValue().getInputs();\r
62                         logger.trace("Populated node type input ({}).", nodeTypeinputs);\r
63                     }\r
64                 }\r
65             }\r
66 \r
67             if (!nodeTypeinputs.isEmpty()) {\r
68                 // Populate the Template inputs\r
69                 for (Map.Entry<String, InterfaceAssignment> nodeTemplateInterface : nodeTemplate.getInterfaces()\r
70                         .entrySet()) {\r
71                     if (nodeTemplateInterface != null && nodeTemplateInterface.getValue() != null) {\r
72                         this.inParams.put(ConfigModelConstant.PROPERTY_CURRENT_INTERFACE,\r
73                                 nodeTemplateInterface.getKey());\r
74                         for (Map.Entry<String, OperationAssignment> nodeTemplateOperation : nodeTemplateInterface\r
75                                 .getValue().getOperations().entrySet()) {\r
76                             if (nodeTemplateOperation != null) {\r
77                                 this.inParams.put(ConfigModelConstant.PROPERTY_CURRENT_OPERATION,\r
78                                         nodeTemplateOperation.getKey());\r
79                                 if (StringUtils.isNotBlank(nodeTemplateOperation.getValue().getImplementation())) {\r
80                                     this.inParams.put(ConfigModelConstant.PROPERTY_CURRENT_IMPLEMENTATION,\r
81                                             nodeTemplateOperation.getValue().getImplementation());\r
82                                 }\r
83                                 inputs = nodeTemplateOperation.getValue().getInputs();\r
84                                 logger.trace("Populated node template input({}).", inputs);\r
85                                 // Assign Default Values & Types\r
86                                 jsonExpressionUtils.populatePropertAssignments(nodeTypeinputs, inputs);\r
87                             }\r
88                         }\r
89                     }\r
90                 }\r
91             }\r
92 \r
93             if (nodeTemplate.getRequirements() != null && !nodeTemplate.getRequirements().isEmpty()) {\r
94                 assignInParamsFromRequirementModel(nodeTemplate, context, inParams);\r
95             }\r
96         }\r
97         return context;\r
98     }\r
99 \r
100     @SuppressWarnings("squid:S3776")\r
101     public SvcLogicContext assignOutParamsFromModel(NodeType nodetype, NodeTemplate nodeTemplate) throws IOException {\r
102 \r
103         if (nodeTemplate != null) {\r
104             Map<String, Object> outputs = null;\r
105             // Populate the Type Outputs\r
106             Map<String, PropertyDefinition> nodeTypeOutputs = new HashMap<>();\r
107 \r
108             for (Map.Entry<String, InterfaceDefinition> nodeTypeInterface : nodetype.getInterfaces().entrySet()) {\r
109                 if (nodeTypeInterface != null && nodeTypeInterface.getValue() != null) {\r
110                     for (Map.Entry<String, OperationDefinition> nodeTypeOperation : nodeTypeInterface.getValue()\r
111                             .getOperations().entrySet()) {\r
112                         nodeTypeOutputs = nodeTypeOperation.getValue().getOutputs();\r
113                         logger.info("Populated node type output ({}).", nodeTypeOutputs);\r
114                     }\r
115                 }\r
116             }\r
117 \r
118             if (!nodeTypeOutputs.isEmpty()) {\r
119                 // Populate the Template Outputs\r
120                 for (Map.Entry<String, InterfaceAssignment> nodeTemplateInterface : nodeTemplate.getInterfaces()\r
121                         .entrySet()) {\r
122                     if (nodeTemplateInterface != null && nodeTemplateInterface.getValue() != null) {\r
123                         for (Map.Entry<String, OperationAssignment> nodeTemplateOperation : nodeTemplateInterface\r
124                                 .getValue().getOperations().entrySet()) {\r
125                             outputs = nodeTemplateOperation.getValue().getOutputs();\r
126                             logger.info("Populated node template output ({}).", outputs);\r
127                             // Assign Default Values & Types\r
128                             jsonExpressionUtils.populateOutPropertAssignments(nodeTypeOutputs, outputs);\r
129                             // TO DO\r
130                         }\r
131                     }\r
132                 }\r
133             }\r
134         }\r
135         return context;\r
136     }\r
137 \r
138     @SuppressWarnings({"squid:S3776", "squid:S00112"})\r
139     public SvcLogicContext assignInParamsFromRequirementModel(NodeTemplate nodeTemplate, final SvcLogicContext context,\r
140             final Map<String, String> inParams) {\r
141         if (nodeTemplate != null && nodeTemplate.getRequirements() != null && context != null && inParams != null) {\r
142             nodeTemplate.getRequirements().forEach((requrementKey, requirement) -> {\r
143                 if (requirement != null && StringUtils.isNotBlank(requirement.getNode())\r
144                         && StringUtils.isNotBlank(requirement.getCapability())) {\r
145                     String requirementNodeTemplateContent = context\r
146                             .getAttribute(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + requirement.getNode());\r
147                     logger.info("Processing requirement node ({}) template content ({}) ", requirement.getNode(),\r
148                             requirementNodeTemplateContent);\r
149                     NodeTemplate requirementNodeTemplate =\r
150                             TransformationUtils.readValue(requirementNodeTemplateContent, NodeTemplate.class);\r
151                     if (requirementNodeTemplate != null && requirementNodeTemplate.getCapabilities() != null) {\r
152                         String nodeTypeContent = context.getAttribute(\r
153                                 ConfigModelConstant.PROPERTY_NODE_TYPES_DOT + requirementNodeTemplate.getType());\r
154                         NodeType nodeType = TransformationUtils.readValue(nodeTypeContent, NodeType.class);\r
155                         if (nodeType != null && nodeType.getCapabilities() != null\r
156                                 && nodeType.getCapabilities().containsKey(requirement.getCapability())) {\r
157                             CapabilityDefinition capabilityDefinition =\r
158                                     nodeType.getCapabilities().get(requirement.getCapability());\r
159                             if (capabilityDefinition != null) {\r
160                                 CapabilityAssignment capabilityAssignment =\r
161                                         requirementNodeTemplate.getCapabilities().get(requirement.getCapability());\r
162                                 try {\r
163                                     assignInParamsFromCapabilityModel(requrementKey, capabilityDefinition,\r
164                                             capabilityAssignment, context);\r
165                                 } catch (Exception e) {\r
166                                     throw new RuntimeException(e);\r
167                                 }\r
168                             }\r
169                         }\r
170                     }\r
171                 }\r
172             });\r
173         }\r
174         return context;\r
175     }\r
176 \r
177     public SvcLogicContext assignInParamsFromCapabilityModel(String requirementName,\r
178             CapabilityDefinition capabilityDefinition, CapabilityAssignment capabilityAssignment,\r
179             final SvcLogicContext context) throws IOException {\r
180         if (capabilityAssignment != null && capabilityAssignment.getProperties() != null) {\r
181             jsonExpressionUtils.populatePropertAssignmentsWithPrefix(requirementName,\r
182                     capabilityDefinition.getProperties(), capabilityAssignment.getProperties());\r
183         }\r
184         return context;\r
185     }\r
186 \r
187 }\r