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