Controller Blueprints Nitrogen to Oxygen Migration
[ccsdk/features.git] / blueprints-processor / plugin / model-provider / src / main / java / org / onap / ccsdk / features / model / service / ComponentNodeServiceImpl.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.features.model.service;\r
19 \r
20 import java.util.HashMap;\r
21 import java.util.Map;\r
22 import org.apache.commons.lang3.StringUtils;\r
23 import org.onap.ccsdk.features.data.adaptor.DataAdaptorConstants;\r
24 import org.onap.ccsdk.features.data.adaptor.domain.TransactionLog;\r
25 import org.onap.ccsdk.features.data.adaptor.service.ConfigResourceService;\r
26 import org.onap.ccsdk.features.model.ConfigModelConstant;\r
27 import org.onap.ccsdk.features.model.data.InterfaceAssignment;\r
28 import org.onap.ccsdk.features.model.data.NodeTemplate;\r
29 import org.onap.ccsdk.features.model.utils.TransformationUtils;\r
30 import org.onap.ccsdk.features.rest.adaptor.service.ConfigRestAdaptorService;\r
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
33 import org.osgi.framework.BundleContext;\r
34 import org.osgi.framework.ServiceReference;\r
35 import com.att.eelf.configuration.EELFLogger;\r
36 import com.att.eelf.configuration.EELFManager;\r
37 \r
38 public class ComponentNodeServiceImpl implements ComponentNodeService {\r
39 \r
40     private static EELFLogger logger = EELFManager.getInstance().getLogger(ComponentNodeServiceImpl.class);\r
41 \r
42     private BundleContext bcontext;\r
43     private ConfigResourceService configResourceService;\r
44     private ConfigModelService configModelService;\r
45 \r
46     public ComponentNodeServiceImpl(BundleContext blueprintBundleContext, ConfigResourceService configResourceService,\r
47             ConfigRestAdaptorService configRestAdaptorService) {\r
48         logger.info("{} Constructor Initiated", "ComponentNodeServiceImpl");\r
49         this.bcontext = blueprintBundleContext;\r
50         this.configResourceService = configResourceService;\r
51         this.configModelService = new ConfigModelServiceImpl(configRestAdaptorService);\r
52     }\r
53 \r
54     @Override\r
55     public void process(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
56             throws SvcLogicException {\r
57         String requestId = null;\r
58         String selector = null;\r
59         try {\r
60             selector = inParams.get(ConfigModelConstant.PROPERTY_SELECTOR);\r
61             requestId = ctx.getAttribute(ConfigModelConstant.PROPERTY_REQUEST_ID);\r
62 \r
63             logger.info("Component execution started with input params  ({})", inParams);\r
64             configModelService.assignInParamsFromModel(ctx, inParams);\r
65 \r
66             String currentInterface = inParams.get(ConfigModelConstant.PROPERTY_CURRENT_INTERFACE);\r
67             String currentNodeDerivedFrom = inParams.get(ConfigModelConstant.PROPERTY_CURRENT_NODETYPE_DERIVED_FROM);\r
68 \r
69             configResourceService.save(new TransactionLog(requestId, DataAdaptorConstants.LOG_MESSAGE_TYPE_COMPONENT,\r
70                     String.format("Executing Component (%s) derived from (%s) with Params :  (%s) ", currentInterface,\r
71                             currentNodeDerivedFrom, inParams)));\r
72 \r
73             ComponentNode handler = getComponentNodeInterface(currentInterface, currentNodeDerivedFrom);\r
74 \r
75             if (handler == null) {\r
76                 throw new SvcLogicException(\r
77                         String.format("Could not find Component for Interface %s", currentInterface));\r
78             }\r
79             if (componentContext == null) {\r
80                 componentContext = new HashMap<>();\r
81             }\r
82 \r
83             logger.debug("Executing component ({})", currentInterface);\r
84 \r
85             if (handler.preCondition(inParams, ctx, componentContext)) {\r
86                 handler.preProcess(inParams, ctx, componentContext);\r
87                 handler.process(inParams, ctx, componentContext);\r
88                 handler.postProcess(inParams, ctx, componentContext);\r
89                 logger.debug("Executed component ({}) successfully.", currentInterface);\r
90                 configResourceService\r
91                         .save(new TransactionLog(requestId, DataAdaptorConstants.LOG_MESSAGE_TYPE_COMPONENT,\r
92                                 String.format("Component (%s) executed successfully. ", currentInterface)));\r
93 \r
94                 ctx.setAttribute(selector + ConfigModelConstant.PROPERTY_DOT_STATUS,\r
95                         ConfigModelConstant.STATUS_SUCCESS);\r
96             } else {\r
97                 logger.info("Skipped component execution  ({})", handler.getClass());\r
98                 configResourceService\r
99                         .save(new TransactionLog(requestId, DataAdaptorConstants.LOG_MESSAGE_TYPE_COMPONENT,\r
100                                 String.format("Skipping component (%s) execution.", handler.getClass())));\r
101                 ctx.setAttribute(selector + ConfigModelConstant.PROPERTY_DOT_STATUS,\r
102                         ConfigModelConstant.STATUS_SKIPPED);\r
103             }\r
104 \r
105             configModelService.assignOutParamsFromModel(ctx, inParams);\r
106             ctx.setStatus(ConfigModelConstant.STATUS_SUCCESS);\r
107         } catch (Exception e) {\r
108             logger.error(String.format("Failed in component (%s) execution for request id (%s) with error %s", selector,\r
109                     requestId, e.getMessage()));\r
110             configResourceService.save(new TransactionLog(requestId, DataAdaptorConstants.LOG_MESSAGE_TYPE_COMPONENT,\r
111                     String.format("Failed in component (%s) execution for request id (%s) with error %s", selector,\r
112                             requestId, e.getMessage())));\r
113 \r
114             ctx.setAttribute(selector + ConfigModelConstant.PROPERTY_DOT_STATUS, ConfigModelConstant.STATUS_FAILURE);\r
115             ctx.setAttribute(selector + ConfigModelConstant.PROPERTY_DOT_ERROR_MESSAGE, e.getMessage());\r
116             ctx.setAttribute(ConfigModelConstant.PROPERTY_ERROR_MESSAGE, e.getMessage());\r
117             ctx.setStatus(ConfigModelConstant.STATUS_FAILURE);\r
118             throw new SvcLogicException(e.getMessage(), e);\r
119         }\r
120     }\r
121 \r
122     @Override\r
123     public ComponentNode getComponentNodeInterface(String pluginName, String componentType) throws SvcLogicException {\r
124 \r
125         logger.info("Searching for component node plugin ({}) component type ({})", pluginName, componentType);\r
126 \r
127         if (StringUtils.isBlank(pluginName)) {\r
128             throw new SvcLogicException(\r
129                     String.format("Could not get Interface Name from Service Template :  %s ", pluginName));\r
130         }\r
131 \r
132         pluginName = pluginName.replace("-", ".");\r
133         ServiceReference sref = bcontext.getServiceReference(pluginName);\r
134 \r
135         if (sref == null) {\r
136             throw new SvcLogicException(\r
137                     String.format("Could not find service reference object for plugin %s", pluginName));\r
138         }\r
139         return (ComponentNode) bcontext.getService(sref);\r
140     }\r
141 \r
142     @Override\r
143     public ComponentNode getComponentNode(SvcLogicContext ctx, String componentKey) throws SvcLogicException {\r
144 \r
145         if (StringUtils.isBlank(componentKey)) {\r
146             logger.warn("Can't get node template content for a component key ({})", componentKey);\r
147             return null;\r
148         }\r
149 \r
150         String nodeTemplateContent = ctx.getAttribute(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + componentKey);\r
151         logger.info("Processing component template : ({})", nodeTemplateContent);\r
152 \r
153         if (StringUtils.isBlank(nodeTemplateContent)) {\r
154             logger.warn("Couldn't get node template content for component key ({})", componentKey);\r
155             return null;\r
156         }\r
157 \r
158         NodeTemplate nodeTemplate = TransformationUtils.readValue(nodeTemplateContent, NodeTemplate.class);\r
159         if (nodeTemplate == null || StringUtils.isBlank(nodeTemplate.getType())) {\r
160             logger.warn("Failed to convert content ({}) to node template.", nodeTemplateContent);\r
161             return null;\r
162         }\r
163 \r
164         ComponentNode componentNode = null;\r
165         for (Map.Entry<String, InterfaceAssignment> nodeTemplateInterface : nodeTemplate.getInterfaces().entrySet()) {\r
166             if (nodeTemplateInterface != null && nodeTemplateInterface.getValue() != null) {\r
167                 String pluginName = nodeTemplateInterface.getKey();\r
168                 componentNode = getComponentNodeInterface(pluginName, ConfigModelConstant.MODEL_TYPE_NODE_COMPONENT);\r
169             }\r
170         }\r
171         return componentNode;\r
172     }\r
173 \r
174 }\r