Register SqlResource in SvcLogicMap
[ccsdk/apps.git] / services / src / main / java / org / onap / ccsdk / apps / services / SvcLogicFactory.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - CCSDK\r
4  * ================================================================================\r
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.onap.ccsdk.apps.services;\r
22 \r
23 import java.io.FileInputStream;\r
24 import java.io.IOException;\r
25 import java.util.List;\r
26 import java.util.Properties;\r
27 \r
28 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;\r
29 import org.onap.ccsdk.sli.core.sli.ConfigurationException;\r
30 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;\r
31 import org.onap.ccsdk.sli.core.sli.SvcLogicLoader;\r
32 import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder;\r
33 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;\r
34 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;\r
35 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;\r
36 import org.onap.ccsdk.sli.core.sli.provider.base.HashMapResolver;\r
37 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider;\r
38 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;\r
39 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceImplBase;\r
40 import org.onap.ccsdk.sli.core.sli.recording.Slf4jRecorder;\r
41 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;\r
42 import org.onap.ccsdk.sli.core.slipluginutils.SliStringUtils;\r
43 import org.onap.ccsdk.sli.plugins.prop.PropertiesNode;\r
44 import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode;\r
45 import org.slf4j.Logger;\r
46 import org.slf4j.LoggerFactory;\r
47 import org.springframework.beans.factory.annotation.Autowired;\r
48 import org.springframework.context.annotation.Bean;\r
49 import org.springframework.context.annotation.Configuration;\r
50 import org.springframework.stereotype.Service;\r
51 \r
52 @Configuration\r
53 @Service\r
54 public class SvcLogicFactory {\r
55   private static final Logger log = LoggerFactory.getLogger(SvcLogicFactory.class);\r
56 \r
57   @Autowired\r
58   List<SvcLogicRecorder> recorders;\r
59 \r
60   @Autowired\r
61   List<SvcLogicJavaPlugin> plugins;\r
62 \r
63   @Autowired\r
64   List<SvcLogicResource> svcLogicResources;\r
65 \r
66   @Bean\r
67   public SvcLogicStore getStore() throws Exception {\r
68     SvcLogicPropertiesProvider propProvider = new SvcLogicPropertiesProvider() {\r
69 \r
70       @Override\r
71       public Properties getProperties() {\r
72         Properties props = new Properties();\r
73 \r
74 \r
75         String propPath = System.getProperty("serviceLogicProperties", "");\r
76 \r
77         if ("".equals(propPath)) {\r
78           propPath = System.getenv("SVCLOGIC_PROPERTIES");\r
79         }\r
80 \r
81 \r
82         if ((propPath == null) || propPath.length() == 0) {\r
83           propPath = "src/main/resources/svclogic.properties";\r
84         }\r
85         System.out.println(propPath);\r
86         try (FileInputStream fileInputStream = new FileInputStream(propPath)) {\r
87           props = new Properties();\r
88           props.load(fileInputStream);\r
89         } catch (final IOException e) {\r
90           log.error("Failed to load properties for file: {}", propPath,\r
91               new ConfigurationException("Failed to load properties for file: " + propPath, e));\r
92         }\r
93         return props;\r
94       }\r
95     };\r
96     SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(propProvider.getProperties());\r
97     return store;\r
98   }\r
99 \r
100   @Bean\r
101   public SvcLogicLoader createLoader() throws Exception {\r
102     String serviceLogicDirectory = System.getProperty("serviceLogicDirectory");\r
103     if (serviceLogicDirectory == null) {\r
104       serviceLogicDirectory = "src/main/resources";\r
105     }\r
106 \r
107     System.out.println("serviceLogicDirectory is " + serviceLogicDirectory);\r
108     SvcLogicLoader loader = new SvcLogicLoader(serviceLogicDirectory, getStore());\r
109 \r
110     try {\r
111       loader.loadAndActivate();\r
112     } catch (IOException e) {\r
113       log.error("Cannot load directed graphs", e);\r
114     }\r
115     return loader;\r
116   }\r
117 \r
118   @Bean\r
119   public SvcLogicServiceBase createService() throws Exception {\r
120     HashMapResolver resolver = new HashMapResolver();\r
121     for (SvcLogicRecorder recorder : recorders) {\r
122       resolver.addSvcLogicRecorder(recorder.getClass().getName(), recorder);\r
123 \r
124     }\r
125     for (SvcLogicJavaPlugin plugin : plugins) {\r
126       resolver.addSvcLogicSvcLogicJavaPlugin(plugin.getClass().getName(), plugin);\r
127 \r
128     }\r
129     for (SvcLogicResource svcLogicResource : svcLogicResources) {\r
130       resolver.addSvcLogicResource(svcLogicResource.getClass().getName(), svcLogicResource);\r
131     }\r
132 \r
133     return new SvcLogicServiceImplBase(getStore(), resolver);\r
134   }\r
135 \r
136   @Bean\r
137   public Slf4jRecorder slf4jRecorderNode() {\r
138     return new Slf4jRecorder();\r
139   }\r
140 \r
141   @Bean\r
142   public SliPluginUtils sliPluginUtil() {\r
143     return new SliPluginUtils();\r
144   }\r
145 \r
146   @Bean\r
147   public SliStringUtils sliStringUtils() {\r
148     return new SliStringUtils();\r
149   }\r
150   \r
151   @Bean\r
152   public RestapiCallNode restapiCallNode() {\r
153       return new RestapiCallNode();\r
154   }\r
155   \r
156   @Bean\r
157   public PropertiesNode propertiesNode() {\r
158       return new PropertiesNode();\r
159   }\r
160 \r
161   @Bean\r
162   public SqlResource sqlResource() {\r
163     return new SqlResource();\r
164   }\r
165   \r
166 \r
167 }\r