Update third party versions in ccsdk/apps
[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.File;\r
24 import java.io.FileInputStream;\r
25 import java.io.FileReader;\r
26 import java.io.IOException;\r
27 import java.io.InputStream;\r
28 import java.util.List;\r
29 import java.util.Properties;\r
30 \r
31 import org.onap.ccsdk.sli.adaptors.aai.AAIService;\r
32 import org.onap.ccsdk.sli.adaptors.aai.AAIServiceProvider;\r
33 import org.onap.ccsdk.sli.adaptors.messagerouter.publisher.api.PublisherApi;\r
34 import org.onap.ccsdk.sli.adaptors.messagerouter.publisher.provider.impl.PublisherApiImpl;\r
35 import org.onap.ccsdk.sli.adaptors.netbox.api.NetboxClient;\r
36 import org.onap.ccsdk.sli.adaptors.netbox.impl.NetboxClientImpl;\r
37 import org.onap.ccsdk.sli.adaptors.netbox.impl.NetboxHttpClient;\r
38 import org.onap.ccsdk.sli.adaptors.netbox.property.NetboxProperties;\r
39 import org.onap.ccsdk.sli.adaptors.resource.mdsal.ConfigResource;\r
40 import org.onap.ccsdk.sli.adaptors.resource.mdsal.MdsalResourcePropertiesProviderImpl;\r
41 import org.onap.ccsdk.sli.adaptors.resource.mdsal.OperationalResource;\r
42 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;\r
43 import org.onap.ccsdk.sli.core.dblib.DBLIBResourceProvider;\r
44 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;\r
45 import org.onap.ccsdk.sli.core.dblib.DbLibService;\r
46 import org.onap.ccsdk.sli.core.sli.ConfigurationException;\r
47 import org.onap.ccsdk.sli.core.sli.SvcLogicAdaptor;\r
48 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
49 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;\r
50 import org.onap.ccsdk.sli.core.sli.SvcLogicLoader;\r
51 import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder;\r
52 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;\r
53 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;\r
54 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;\r
55 import org.onap.ccsdk.sli.core.sli.provider.base.HashMapResolver;\r
56 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider;\r
57 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;\r
58 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceImplBase;\r
59 import org.onap.ccsdk.sli.core.sli.recording.Slf4jRecorder;\r
60 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;\r
61 import org.onap.ccsdk.sli.core.slipluginutils.SliStringUtils;\r
62 import org.onap.ccsdk.sli.core.utils.common.EnvProperties;\r
63 import org.onap.ccsdk.sli.plugins.prop.PropertiesNode;\r
64 import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode;\r
65 import org.slf4j.Logger;\r
66 import org.slf4j.LoggerFactory;\r
67 import org.springframework.beans.factory.annotation.Autowired;\r
68 import org.springframework.context.annotation.Bean;\r
69 import org.springframework.context.annotation.Configuration;\r
70 import org.springframework.stereotype.Service;\r
71 \r
72 @Configuration\r
73 @Service\r
74 public class SvcLogicFactory {\r
75     private static final Logger log = LoggerFactory.getLogger(SvcLogicFactory.class);\r
76     private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";\r
77     private static final String CONTRAIL_PROPERTIES = "contrail-adaptor.properties";\r
78 \r
79  /*    \r
80   * In Springboot 2.6, these autowired lists become a circular dependency with the RestconfApiController.\r
81   * For now, comment these out and instead just explicitly add wiring for the sli classes\r
82     @Autowired\r
83     List<SvcLogicRecorder> recorders;\r
84 \r
85     @Autowired\r
86     List<SvcLogicJavaPlugin> plugins;\r
87 \r
88     @Autowired\r
89     List<SvcLogicResource> svcLogicResources;\r
90 \r
91  */\r
92 \r
93  \r
94 \r
95 \r
96     @Bean\r
97     public SvcLogicStore getStore() throws Exception {\r
98         SvcLogicPropertiesProvider propProvider = new SvcLogicPropertiesProvider() {\r
99 \r
100             @Override\r
101             public Properties getProperties() {\r
102                 Properties props = new Properties();\r
103 \r
104                 String propPath = System.getProperty("serviceLogicProperties", "");\r
105 \r
106                 if ("".equals(propPath)) {\r
107                     propPath = System.getenv("SVCLOGIC_PROPERTIES");\r
108                 }\r
109 \r
110                 if ((propPath == null) || propPath.length() == 0) {\r
111                     propPath = "src/main/resources/svclogic.properties";\r
112                 }\r
113                 System.out.println(propPath);\r
114                 try (FileInputStream fileInputStream = new FileInputStream(propPath)) {\r
115                     props = new EnvProperties();\r
116                     props.load(fileInputStream);\r
117                 } catch (final IOException e) {\r
118                     log.error("Failed to load properties for file: {}", propPath,\r
119                             new ConfigurationException("Failed to load properties for file: " + propPath, e));\r
120                 }\r
121                 return props;\r
122             }\r
123         };\r
124         SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(propProvider.getProperties());\r
125         return store;\r
126     }\r
127 \r
128     @Bean\r
129     public SvcLogicLoader createLoader() throws Exception {\r
130         String serviceLogicDirectory = System.getProperty("serviceLogicDirectory");\r
131         if (serviceLogicDirectory == null) {\r
132             serviceLogicDirectory = "src/main/resources";\r
133         }\r
134 \r
135         System.out.println("serviceLogicDirectory is " + serviceLogicDirectory);\r
136         SvcLogicLoader loader = new SvcLogicLoader(serviceLogicDirectory, getStore());\r
137 \r
138         try {\r
139             loader.loadAndActivate();\r
140         } catch (IOException e) {\r
141             log.error("Cannot load directed graphs", e);\r
142         }\r
143         return loader;\r
144     }\r
145 \r
146     @Bean\r
147     public SvcLogicServiceBase createService() throws Exception {\r
148         HashMapResolver resolver = new HashMapResolver();\r
149 \r
150         /**\r
151          * See comment above re: autowired lists. Need to explicitly register\r
152          * SLI features to avoid circular dependency issue in springboot 2.6\r
153          * \r
154          * for (SvcLogicRecorder recorder : recorders) {\r
155          * log.info("Registering SvcLogicRecorder {}", recorder.getClass().getName());\r
156          * resolver.addSvcLogicRecorder(recorder.getClass().getName(), recorder);\r
157          * \r
158          * }\r
159          * \r
160          * for (SvcLogicJavaPlugin plugin : plugins) {\r
161          * log.info("Registering SvcLogicJavaPlugin {}", plugin.getClass().getName());\r
162          * resolver.addSvcLogicSvcLogicJavaPlugin(plugin.getClass().getName(), plugin);\r
163          * \r
164          * }\r
165          * for (SvcLogicResource svcLogicResource : svcLogicResources) {\r
166          * log.info("Registering SvcLogicResource {}",\r
167          * svcLogicResource.getClass().getName());\r
168          * resolver.addSvcLogicResource(svcLogicResource.getClass().getName(),\r
169          * svcLogicResource);\r
170          * }\r
171          */\r
172 \r
173         Slf4jRecorder slf4jRecorder = slf4jRecorderNode();\r
174 \r
175         SliPluginUtils sliPluginUtils = sliPluginUtil();\r
176 \r
177         SliStringUtils sliStringUtils = sliStringUtils();\r
178 \r
179         AAIService aaiService = aaiService();\r
180 \r
181         ConfigResource configResource = configResource();\r
182 \r
183         OperationalResource operationalResource = operationalResource();\r
184 \r
185         NetboxClient netboxClient = netboxClient();\r
186 \r
187         SqlResource sqlResource = sqlResource();\r
188 \r
189         RestapiCallNode restapiCallNode = restapiCallNode();\r
190 \r
191         PropertiesNode propertiesNode = propertiesNode();\r
192         // Register recorder (there is only one)\r
193         resolver.addSvcLogicRecorder(Slf4jRecorder.class.getName(), new Slf4jRecorder());\r
194 \r
195         // Register plugins\r
196         resolver.addSvcLogicSvcLogicJavaPlugin(sliPluginUtils.getClass().getName(), sliPluginUtils);\r
197         resolver.addSvcLogicSvcLogicJavaPlugin(sliStringUtils.getClass().getName(), sliStringUtils);\r
198         resolver.addSvcLogicSvcLogicJavaPlugin(restapiCallNode.getClass().getName(), restapiCallNode);\r
199         resolver.addSvcLogicSvcLogicJavaPlugin(propertiesNode.getClass().getName(), propertiesNode);\r
200         resolver.addSvcLogicSvcLogicJavaPlugin(netboxClient.getClass().getName(), netboxClient);\r
201 \r
202         // Register resources\r
203         resolver.addSvcLogicResource(aaiService.getClass().getName(), aaiService);\r
204         resolver.addSvcLogicResource(configResource.getClass().getName(), configResource);\r
205         resolver.addSvcLogicResource(operationalResource.getClass().getName(), operationalResource);\r
206         resolver.addSvcLogicResource(sqlResource.getClass().getName(), sqlResource);\r
207 \r
208         return new SvcLogicServiceImplBase(getStore(), resolver);\r
209     }\r
210     \r
211 \r
212     @Bean\r
213     public Slf4jRecorder slf4jRecorderNode() {\r
214         return new Slf4jRecorder();\r
215     }\r
216 \r
217     // Beans from sli/core\r
218 \r
219     @Bean\r
220     public SliPluginUtils sliPluginUtil() {\r
221         return new SliPluginUtils();\r
222     }\r
223 \r
224     @Bean\r
225     public SliStringUtils sliStringUtils() {\r
226         return new SliStringUtils();\r
227     }\r
228 \r
229     // Beans from sli/adaptors\r
230 \r
231     @Bean\r
232     AAIService aaiService() {\r
233         return new AAIService(new AAIServiceProvider());\r
234     }\r
235 \r
236     @Bean\r
237     public ConfigResource configResource() {\r
238         return new ConfigResource(new MdsalResourcePropertiesProviderImpl());\r
239     }\r
240 \r
241     @Bean\r
242     public OperationalResource operationalResource() {\r
243         return new OperationalResource(new MdsalResourcePropertiesProviderImpl());\r
244     }\r
245 \r
246     @Bean\r
247     public PublisherApi publisherApi() {\r
248         return new PublisherApiImpl();\r
249     }\r
250 \r
251     @Bean\r
252     public NetboxClient netboxClient() {\r
253         return new NetboxClientImpl();\r
254     }\r
255 \r
256     @Bean\r
257     public SqlResource sqlResource() {\r
258         return new SqlResource();\r
259     }\r
260 \r
261     @Bean\r
262     public RestapiCallNode restapiCallNode() {\r
263         return new RestapiCallNode();\r
264     }\r
265 \r
266     @Bean\r
267     public PropertiesNode propertiesNode() {\r
268         return new PropertiesNode();\r
269     }\r
270 \r
271 }\r