Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / assignment-provider / src / main / java / org / onap / ccsdk / features / assignment / service / ConfigAssignmentNode.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.assignment.service;\r
19 \r
20 import java.util.HashMap;\r
21 import java.util.List;\r
22 import java.util.Map;\r
23 import org.apache.commons.collections.MapUtils;\r
24 import org.apache.commons.lang3.StringUtils;\r
25 import org.onap.ccsdk.features.assignment.ConfigAssignmentConstants;\r
26 import org.onap.ccsdk.features.assignment.data.ResourceAssignmentData;\r
27 import org.onap.ccsdk.features.data.adaptor.service.ConfigResourceService;\r
28 import org.onap.ccsdk.features.generator.service.ConfigGeneratorService;\r
29 import org.onap.ccsdk.features.model.ConfigModelConstant;\r
30 import org.onap.ccsdk.features.model.service.ComponentNode;\r
31 import org.onap.ccsdk.features.model.service.ComponentNodeService;\r
32 import org.onap.ccsdk.features.model.service.ConfigModelService;\r
33 import org.onap.ccsdk.features.model.utils.TransformationUtils;\r
34 import org.onap.ccsdk.features.rest.adaptor.service.ConfigRestAdaptorService;\r
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
37 import com.att.eelf.configuration.EELFLogger;\r
38 import com.att.eelf.configuration.EELFManager;\r
39 \r
40 public class ConfigAssignmentNode implements ComponentNode {\r
41     private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigAssignmentNode.class);\r
42 \r
43     private ComponentNodeService componentNodeService;\r
44     private ConfigResourceService configResourceService;\r
45     private ConfigModelService configModelService;\r
46     private ConfigRestAdaptorService configRestAdaptorService;\r
47     private ConfigGeneratorService configGeneratorService;\r
48 \r
49     public ConfigAssignmentNode(ConfigResourceService configResourceService,\r
50             ConfigRestAdaptorService configRestAdaptorService, ConfigModelService configModelService,\r
51             ComponentNodeService componentNodeService, ConfigGeneratorService configGeneratorService) {\r
52         logger.info("{} Constrctor Initiated", "ConfigAssignmentNode");\r
53         this.componentNodeService = componentNodeService;\r
54         this.configResourceService = configResourceService;\r
55         this.configModelService = configModelService;\r
56         this.configRestAdaptorService = configRestAdaptorService;\r
57         this.configGeneratorService = configGeneratorService;\r
58     }\r
59 \r
60     @Override\r
61     public Boolean preCondition(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
62             throws SvcLogicException {\r
63         return Boolean.TRUE;\r
64     }\r
65 \r
66     @Override\r
67     public void preProcess(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
68             throws SvcLogicException {\r
69         // Auto-generated method stub\r
70     }\r
71 \r
72     /**\r
73      * This method is used to resolve the resources defined in the template. Generic Resource API DG\r
74      * calls this execute node.\r
75      *\r
76      * @param inParams This is the input parameter to process this node\r
77      * \r
78      *        <pre>\r
79     request-id                  (string):           Tracking Id \r
80     resource-type               (string):           Resource Type ( ex : vnf-type) \r
81     resource-id                 (string):           Resource Id \r
82     service-template-name       (string):           Blueprint Name \r
83     service-template-version    (string):           Blueprint Version \r
84     action-name                 (string): \r
85     template-names              (List of string):   Template Names / Artifact Node Names to resolve. ["template1", "template2"] \r
86     input-data                  (string):           Input Data in JSON String, for the substitution in the Template. \r
87     prifix                      (string):           Return Value selector\r
88      *        </pre>\r
89      * \r
90      * @param ctx This is the service logger context, Output will be stored (\r
91      *        <responsePrefix>.resource-assignment-params.<template-name> : Output Data in JSON String.\r
92      *        <responsePrefix>.status <responsePrefix>.error-message )\r
93      * @throws SvcLogicException On processing error.\r
94      */\r
95 \r
96     @Override\r
97     public void process(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {\r
98 \r
99         final String responsePrefix = StringUtils.isNotBlank(inParams.get(ConfigModelConstant.PROPERTY_SELECTOR))\r
100                 ? (inParams.get(ConfigModelConstant.PROPERTY_SELECTOR) + ".")\r
101                 : "";\r
102         try {\r
103 \r
104             ResourceAssignmentData resourceAssignmentData = populateResourceData(inParams);\r
105             resourceAssignmentData.setSvcLogicContext(ctx);\r
106             Map<String, Object> componentContext = new HashMap<>();\r
107             resourceAssignmentData.setContext(componentContext);\r
108             resourceAssignmentData.setReloadModel(true);\r
109 \r
110             ConfigAssignmentProcessService configAssignmentProcessService =\r
111                     new ConfigAssignmentProcessService(configResourceService, configRestAdaptorService,\r
112                             configModelService, componentNodeService, configGeneratorService);\r
113             configAssignmentProcessService.resolveResources(resourceAssignmentData);\r
114 \r
115             if (MapUtils.isNotEmpty(resourceAssignmentData.getTemplatesMashedContents())) {\r
116                 resourceAssignmentData.getTemplatesMashedContents().forEach((templateName, previewContent) -> {\r
117                     logger.debug("For Template name : ({}),\n Preview Content is : ({})", templateName, previewContent);\r
118                     ctx.setAttribute(\r
119                             responsePrefix + ConfigAssignmentConstants.OUTPUT_PARAM_MASHED_DATA + "." + templateName,\r
120                             previewContent);\r
121                 });\r
122             }\r
123             ctx.setAttribute(responsePrefix + ConfigAssignmentConstants.OUTPUT_PARAM_STATUS,\r
124                     ConfigAssignmentConstants.OUTPUT_STATUS_SUCCESS);\r
125         } catch (Exception e) {\r
126             ctx.setAttribute(responsePrefix + ConfigAssignmentConstants.OUTPUT_PARAM_STATUS,\r
127                     ConfigAssignmentConstants.OUTPUT_STATUS_FAILURE);\r
128             ctx.setAttribute(responsePrefix + ConfigAssignmentConstants.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());\r
129             throw new SvcLogicException(e.getMessage(), e);\r
130         }\r
131     }\r
132 \r
133     @Override\r
134     public void process(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
135             throws SvcLogicException {\r
136 \r
137         final String responsePrefix = StringUtils.isNotBlank(inParams.get(ConfigModelConstant.PROPERTY_SELECTOR))\r
138                 ? (inParams.get(ConfigModelConstant.PROPERTY_SELECTOR) + ".")\r
139                 : "";\r
140         try {\r
141 \r
142             ResourceAssignmentData resourceAssignmentData = populateResourceData(inParams);\r
143             resourceAssignmentData.setSvcLogicContext(ctx);\r
144             resourceAssignmentData.setContext(componentContext);\r
145             resourceAssignmentData.setReloadModel(false);\r
146 \r
147             ConfigAssignmentProcessService configAssignmentProcessService =\r
148                     new ConfigAssignmentProcessService(configResourceService, configRestAdaptorService,\r
149                             configModelService, componentNodeService, configGeneratorService);\r
150             configAssignmentProcessService.resolveResources(resourceAssignmentData);\r
151             ctx.setAttribute(responsePrefix + ConfigAssignmentConstants.OUTPUT_PARAM_STATUS,\r
152                     ConfigAssignmentConstants.OUTPUT_STATUS_SUCCESS);\r
153 \r
154         } catch (Exception e) {\r
155             ctx.setAttribute(responsePrefix + ConfigAssignmentConstants.OUTPUT_PARAM_STATUS,\r
156                     ConfigAssignmentConstants.OUTPUT_STATUS_FAILURE);\r
157             ctx.setAttribute(responsePrefix + ConfigAssignmentConstants.OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());\r
158             throw new SvcLogicException(e.getMessage(), e);\r
159         }\r
160     }\r
161 \r
162     @Override\r
163     public void postProcess(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
164             throws SvcLogicException {\r
165         // Do Nothing\r
166     }\r
167 \r
168     private ResourceAssignmentData populateResourceData(Map<String, String> inParams) throws SvcLogicException {\r
169         validateInputParams(inParams);\r
170 \r
171         String requestId = inParams.get(ConfigAssignmentConstants.INPUT_PARAM_REQUEST_ID);\r
172         String resourceId = inParams.get(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_ID);\r
173         String resourceType = inParams.get(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_TYPE);\r
174         String serviceTemplateName = inParams.get(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_NAME);\r
175         String serviceTemplateVersion = inParams.get(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_VERSION);\r
176         String actionName = inParams.get(ConfigModelConstant.PROPERTY_ACTION_NAME);\r
177         String inputData = inParams.get(ConfigAssignmentConstants.INPUT_PARAM_INPUT_DATA);\r
178 \r
179         String templateNamesStr = inParams.get(ConfigAssignmentConstants.INPUT_PARAM_TEMPLATE_NAMES);\r
180         List<String> templateNames = TransformationUtils.getListfromJson(templateNamesStr, String.class);\r
181 \r
182         ResourceAssignmentData resourceAssignmentData = new ResourceAssignmentData();\r
183         resourceAssignmentData.setRequestId(requestId);\r
184         resourceAssignmentData.setResourceId(resourceId);\r
185         resourceAssignmentData.setResourceType(resourceType);\r
186         resourceAssignmentData.setServiceTemplateName(serviceTemplateName);\r
187         resourceAssignmentData.setServiceTemplateVersion(serviceTemplateVersion);\r
188         resourceAssignmentData.setActionName(actionName);\r
189         resourceAssignmentData.setInputData(inputData);\r
190         resourceAssignmentData.setTemplateNames(templateNames);\r
191 \r
192         return resourceAssignmentData;\r
193     }\r
194 \r
195     private void validateInputParams(Map<String, String> inParams) throws SvcLogicException {\r
196         if (inParams == null) {\r
197             throw new SvcLogicException("Input parameters missing");\r
198         }\r
199 \r
200         String requestId = inParams.get(ConfigAssignmentConstants.INPUT_PARAM_REQUEST_ID);\r
201         if (StringUtils.isBlank(requestId)) {\r
202             throw new SvcLogicException("Request id parameters missing");\r
203         }\r
204         String resourceId = inParams.get(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_ID);\r
205         if (StringUtils.isBlank(resourceId)) {\r
206             throw new SvcLogicException("Resource id parameter is missing");\r
207         }\r
208         String resourceType = inParams.get(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_TYPE);\r
209         if (StringUtils.isBlank(resourceType)) {\r
210             throw new SvcLogicException("Resource type parameter is missing");\r
211         }\r
212         String recipeName = inParams.get(ConfigAssignmentConstants.INPUT_PARAM_ACTION_NAME);\r
213         if (StringUtils.isBlank(recipeName)) {\r
214             throw new SvcLogicException("Action name is parameter is missing");\r
215         }\r
216         String templateNames = inParams.get(ConfigAssignmentConstants.INPUT_PARAM_TEMPLATE_NAMES);\r
217         if (StringUtils.isBlank(templateNames)) {\r
218             throw new SvcLogicException("Template names parameter missing");\r
219         }\r
220         String serviceTemplateName = inParams.get(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_NAME);\r
221         if (StringUtils.isBlank(serviceTemplateName)) {\r
222             throw new SvcLogicException("Service Template name parameter missing");\r
223         }\r
224         String serviceTemplateVersion = inParams.get(ConfigModelConstant.SERVICE_TEMPLATE_KEY_ARTIFACT_VERSION);\r
225         if (StringUtils.isBlank(serviceTemplateVersion)) {\r
226             throw new SvcLogicException("Service Template version parameter missing");\r
227         }\r
228 \r
229     }\r
230 \r
231 }\r