f7e3662f47cd6c6b55aecbe23ab427e5155bde37
[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.messagerouter.publisher.api.PublisherApi;\r
29 import org.onap.ccsdk.sli.adaptors.messagerouter.publisher.provider.impl.PublisherApiImpl;\r
30 import org.onap.ccsdk.sli.adaptors.netbox.api.NetboxClient;\r
31 import org.onap.ccsdk.sli.adaptors.netbox.impl.NetboxClientImpl;\r
32 import org.onap.ccsdk.sli.adaptors.netbox.impl.NetboxHttpClient;\r
33 import org.onap.ccsdk.sli.adaptors.netbox.property.NetboxProperties;\r
34 import org.onap.ccsdk.sli.adaptors.resource.mdsal.ConfigResource;\r
35 import org.onap.ccsdk.sli.adaptors.resource.mdsal.MdsalResourcePropertiesProviderImpl;\r
36 import org.onap.ccsdk.sli.adaptors.resource.mdsal.OperationalResource;\r
37 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;\r
38 import org.onap.ccsdk.sli.core.dblib.DBLIBResourceProvider;\r
39 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;\r
40 import org.onap.ccsdk.sli.core.dblib.DbLibService;\r
41 import org.onap.ccsdk.sli.core.sli.ConfigurationException;\r
42 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;\r
43 import org.onap.ccsdk.sli.core.sli.SvcLogicLoader;\r
44 import org.onap.ccsdk.sli.core.sli.SvcLogicRecorder;\r
45 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;\r
46 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;\r
47 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;\r
48 import org.onap.ccsdk.sli.core.sli.provider.base.HashMapResolver;\r
49 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicPropertiesProvider;\r
50 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceBase;\r
51 import org.onap.ccsdk.sli.core.sli.provider.base.SvcLogicServiceImplBase;\r
52 import org.onap.ccsdk.sli.core.sli.recording.Slf4jRecorder;\r
53 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils;\r
54 import org.onap.ccsdk.sli.core.slipluginutils.SliStringUtils;\r
55 import org.onap.ccsdk.sli.plugins.prop.PropertiesNode;\r
56 import org.onap.ccsdk.sli.plugins.restapicall.RestapiCallNode;\r
57 import org.slf4j.Logger;\r
58 import org.slf4j.LoggerFactory;\r
59 import org.springframework.beans.factory.annotation.Autowired;\r
60 import org.springframework.context.annotation.Bean;\r
61 import org.springframework.context.annotation.Configuration;\r
62 import org.springframework.stereotype.Service;\r
63 \r
64 @Configuration\r
65 @Service\r
66 public class SvcLogicFactory {\r
67   private static final Logger log = LoggerFactory.getLogger(SvcLogicFactory.class);\r
68 \r
69   @Autowired\r
70   List<SvcLogicRecorder> recorders;\r
71 \r
72   @Autowired\r
73   List<SvcLogicJavaPlugin> plugins;\r
74 \r
75   @Autowired\r
76   List<SvcLogicResource> svcLogicResources;\r
77 \r
78   @Bean\r
79   public SvcLogicStore getStore() throws Exception {\r
80     SvcLogicPropertiesProvider propProvider = new SvcLogicPropertiesProvider() {\r
81 \r
82       @Override\r
83       public Properties getProperties() {\r
84         Properties props = new Properties();\r
85 \r
86 \r
87         String propPath = System.getProperty("serviceLogicProperties", "");\r
88 \r
89         if ("".equals(propPath)) {\r
90           propPath = System.getenv("SVCLOGIC_PROPERTIES");\r
91         }\r
92 \r
93 \r
94         if ((propPath == null) || propPath.length() == 0) {\r
95           propPath = "src/main/resources/svclogic.properties";\r
96         }\r
97         System.out.println(propPath);\r
98         try (FileInputStream fileInputStream = new FileInputStream(propPath)) {\r
99           props = new Properties();\r
100           props.load(fileInputStream);\r
101         } catch (final IOException e) {\r
102           log.error("Failed to load properties for file: {}", propPath,\r
103               new ConfigurationException("Failed to load properties for file: " + propPath, e));\r
104         }\r
105         return props;\r
106       }\r
107     };\r
108     SvcLogicStore store = SvcLogicStoreFactory.getSvcLogicStore(propProvider.getProperties());\r
109     return store;\r
110   }\r
111 \r
112   @Bean\r
113   public SvcLogicLoader createLoader() throws Exception {\r
114     String serviceLogicDirectory = System.getProperty("serviceLogicDirectory");\r
115     if (serviceLogicDirectory == null) {\r
116       serviceLogicDirectory = "src/main/resources";\r
117     }\r
118 \r
119     System.out.println("serviceLogicDirectory is " + serviceLogicDirectory);\r
120     SvcLogicLoader loader = new SvcLogicLoader(serviceLogicDirectory, getStore());\r
121 \r
122     try {\r
123       loader.loadAndActivate();\r
124     } catch (IOException e) {\r
125       log.error("Cannot load directed graphs", e);\r
126     }\r
127     return loader;\r
128   }\r
129 \r
130   @Bean\r
131   public SvcLogicServiceBase createService() throws Exception {\r
132     HashMapResolver resolver = new HashMapResolver();\r
133     for (SvcLogicRecorder recorder : recorders) {\r
134       resolver.addSvcLogicRecorder(recorder.getClass().getName(), recorder);\r
135 \r
136     }\r
137     for (SvcLogicJavaPlugin plugin : plugins) {\r
138       resolver.addSvcLogicSvcLogicJavaPlugin(plugin.getClass().getName(), plugin);\r
139 \r
140     }\r
141     for (SvcLogicResource svcLogicResource : svcLogicResources) {\r
142       resolver.addSvcLogicResource(svcLogicResource.getClass().getName(), svcLogicResource);\r
143     }\r
144 \r
145     return new SvcLogicServiceImplBase(getStore(), resolver);\r
146   }\r
147 \r
148   @Bean\r
149   public Slf4jRecorder slf4jRecorderNode() {\r
150     return new Slf4jRecorder();\r
151   }\r
152 \r
153   // Beans from sli/core\r
154 \r
155   @Bean\r
156   public SliPluginUtils sliPluginUtil() {\r
157     return new SliPluginUtils();\r
158   }\r
159 \r
160   @Bean\r
161   public SliStringUtils sliStringUtils() {\r
162     return new SliStringUtils();\r
163   }\r
164 \r
165   // Beans from sli/adaptors\r
166   \r
167   @Bean\r
168   public ConfigResource configResource() {\r
169     return new ConfigResource(new MdsalResourcePropertiesProviderImpl());\r
170   }\r
171 \r
172   @Bean\r
173   public OperationalResource operationalResource() {\r
174     return new OperationalResource(new MdsalResourcePropertiesProviderImpl());\r
175   }\r
176 \r
177   @Bean \r
178   public PublisherApi publisherApi() {\r
179     return new PublisherApiImpl();\r
180   }\r
181   \r
182   \r
183   @Bean \r
184   public NetboxClient netboxClient() {\r
185     return new NetboxClientImpl();\r
186   }\r
187   \r
188   \r
189   @Bean\r
190   public SqlResource sqlResource() {\r
191     return new SqlResource();\r
192   }\r
193 \r
194   \r
195   @Bean\r
196   public RestapiCallNode restapiCallNode() {\r
197       return new RestapiCallNode();\r
198   }\r
199   \r
200   @Bean\r
201   public PropertiesNode propertiesNode() {\r
202       return new PropertiesNode();\r
203   }\r
204 \r
205   \r
206 \r
207 }\r