Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / assignment-provider / src / main / java / org / onap / ccsdk / features / assignment / processor / DefaultResourceProcessor.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.processor;\r
19 \r
20 import java.util.List;\r
21 import java.util.Map;\r
22 import org.apache.commons.lang3.StringUtils;\r
23 import org.onap.ccsdk.features.assignment.service.ConfigAssignmentUtils;\r
24 import org.onap.ccsdk.features.data.adaptor.service.ConfigResourceService;\r
25 import org.onap.ccsdk.features.model.ConfigModelConstant;\r
26 import org.onap.ccsdk.features.model.ConfigModelException;\r
27 import org.onap.ccsdk.features.model.data.ResourceAssignment;\r
28 import org.onap.ccsdk.features.model.service.ComponentNode;\r
29 import org.onap.ccsdk.features.model.utils.ResourceAssignmentUtils;\r
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
31 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
32 \r
33 public class DefaultResourceProcessor implements ComponentNode {\r
34 \r
35     public DefaultResourceProcessor(ConfigResourceService configResourceService) {}\r
36 \r
37     @Override\r
38     public Boolean preCondition(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
39             throws SvcLogicException {\r
40         return Boolean.TRUE;\r
41     }\r
42 \r
43     @Override\r
44     public void preProcess(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
45             throws SvcLogicException {\r
46         // Auto-generated method stub\r
47     }\r
48 \r
49     @Override\r
50     public void process(Map<String, String> inParams, SvcLogicContext ctx) throws SvcLogicException {\r
51         // Auto-generated method stub\r
52     }\r
53 \r
54     @SuppressWarnings("unchecked")\r
55     @Override\r
56     public void process(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
57             throws SvcLogicException {\r
58         try {\r
59             List<ResourceAssignment> batchResourceAssignment =\r
60                     (List<ResourceAssignment>) componentContext.get(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS);\r
61 \r
62             if (batchResourceAssignment != null && !batchResourceAssignment.isEmpty()) {\r
63                 for (ResourceAssignment resourceAssignment : batchResourceAssignment) {\r
64                     processResourceAssignment(ctx, componentContext, resourceAssignment);\r
65                 }\r
66             }\r
67         } catch (Exception e) {\r
68             throw new SvcLogicException(String.format("DefaultResourceProcessor Exception : (%s)", e), e);\r
69         }\r
70 \r
71     }\r
72 \r
73     private void processResourceAssignment(SvcLogicContext ctx, Map<String, Object> componentContext,\r
74             ResourceAssignment resourceAssignment) throws ConfigModelException, SvcLogicException {\r
75         if (resourceAssignment != null && StringUtils.isNotBlank(resourceAssignment.getName())\r
76                 && resourceAssignment.getProperty() != null) {\r
77             try {\r
78                 // Check if It has Input\r
79                 Object value = ConfigAssignmentUtils.getContextKeyValue(ctx, resourceAssignment.getName());\r
80                 if (value == null) {\r
81                     value = resourceAssignment.getProperty().getDefaultValue();\r
82                 }\r
83 \r
84                 // if value is null don't call setResourceDataValue to populate the value\r
85                 if (value != null) {\r
86                     ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, value);\r
87                 }\r
88 \r
89                 // Check the value has populated for mandatory case\r
90                 ResourceAssignmentUtils.assertTemplateKeyValueNotNull(componentContext, resourceAssignment);\r
91             } catch (Exception e) {\r
92                 ResourceAssignmentUtils.setFailedResourceDataValue(componentContext, resourceAssignment,\r
93                         e.getMessage());\r
94                 throw new SvcLogicException(\r
95                         String.format("Failed in template key (%s) assignments with : (%s)", resourceAssignment, e), e);\r
96             }\r
97         }\r
98     }\r
99 \r
100     @Override\r
101     public void postProcess(Map<String, String> inParams, SvcLogicContext ctx, Map<String, Object> componentContext)\r
102             throws SvcLogicException {\r
103         // Auto-generated method stub\r
104 \r
105     }\r
106 \r
107 }\r