Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / dmaap-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / dmaap / BluePrintDmaapLibPropertyService.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - CDS
4  * ================================================================================
5  * Copyright (C) 2019 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.cds.blueprintsprocessor.dmaap
22
23 import com.fasterxml.jackson.databind.JsonNode
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties
25 import org.onap.ccsdk.cds.blueprintsprocessor.dmaap.DmaapLibConstants.Companion.SERVICE_BLUEPRINT_DMAAP_LIB_PROPERTY
26 import org.onap.ccsdk.cds.blueprintsprocessor.dmaap.DmaapLibConstants.Companion.TYPE_HTTP_AAF_AUTH
27 import org.onap.ccsdk.cds.blueprintsprocessor.dmaap.DmaapLibConstants.Companion.TYPE_HTTP_NO_AUTH
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.slf4j.LoggerFactory
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.context.annotation.Configuration
33 import org.springframework.context.annotation.PropertySource
34 import org.springframework.context.annotation.PropertySources
35 import org.springframework.core.env.ConfigurableEnvironment
36 import org.springframework.core.env.Environment
37 import org.springframework.core.io.support.ResourcePropertySource
38 import org.springframework.stereotype.Service
39 import java.util.Properties
40
41 /**
42  * Representation of DMAAP lib property service to load the properties
43  * according to the connection type to the DMAAP server and returning back
44  * the appropriate DMAAP client to send messages DMAAP client.
45  */
46 @Service(SERVICE_BLUEPRINT_DMAAP_LIB_PROPERTY)
47 @Configuration
48 @PropertySources(PropertySource("classpath:event.properties"))
49 open class BluePrintDmaapLibPropertyService(private var bluePrintProperties:
50                                             BluePrintProperties) {
51
52     /**
53      * Static variable for logging.
54      */
55     companion object {
56         var log = LoggerFactory.getLogger(
57             BluePrintDmaapLibPropertyService::class.java)!!
58     }
59
60     /**
61      * Environment entity to derive it from the system to load a specific
62      * property file.
63      */
64     @Autowired
65     lateinit var env: Environment
66
67     /**
68      * Returns the DMAAP client by providing the input properties as a JSON
69      * node.
70      */
71     fun blueprintDmaapClientService(jsonNode: JsonNode):
72             BluePrintDmaapClientService {
73         val dmaapProps = dmaapClientProperties(jsonNode)
74         return blueprintDmaapClientService(dmaapProps)
75     }
76
77     /**
78      * Returns the DMAAP client by providing the input properties as a
79      * selector string.
80      */
81     fun blueprintDmaapClientService(selector: String):
82             BluePrintDmaapClientService {
83         val prefix = "blueprintsprocessor.dmaapclient.$selector"
84         val dmaapProps = dmaapClientProperties(prefix)
85         return blueprintDmaapClientService(dmaapProps)
86     }
87
88     /**
89      * Returns the DMAAP client properties from the type of connection it
90      * requires.
91      */
92     fun dmaapClientProperties(prefix: String): DmaapClientProperties {
93         val type = bluePrintProperties.propertyBeanType(
94             "$prefix.type", String::class.java)
95         val clientProps : DmaapClientProperties
96
97         when (type) {
98             TYPE_HTTP_NO_AUTH -> {
99                 clientProps =  bluePrintProperties.propertyBeanType(
100                     prefix, HttpNoAuthDmaapClientProperties::class.java)
101                 clientProps.props = parseEventProps()
102             }
103
104             TYPE_HTTP_AAF_AUTH -> {
105                 clientProps =  bluePrintProperties.propertyBeanType(
106                     prefix, AafAuthDmaapClientProperties::class.java)
107                 clientProps.props = parseEventProps()
108             }
109
110             else -> {
111                 throw BluePrintProcessorException("DMAAP adaptor($type) is " +
112                         "not supported")
113             }
114         }
115         return clientProps
116     }
117
118     /**
119      * Returns the DMAAP client properties from the type of connection it
120      * requires.
121      */
122     fun dmaapClientProperties(jsonNode: JsonNode): DmaapClientProperties {
123         val type = jsonNode.get("type").textValue()
124         val clientProps : DmaapClientProperties
125
126         when (type) {
127             TYPE_HTTP_NO_AUTH -> {
128                 clientProps = JacksonUtils.readValue(jsonNode,
129                     HttpNoAuthDmaapClientProperties::class.java)!!
130                 clientProps.props = parseEventProps()
131             }
132
133             TYPE_HTTP_AAF_AUTH -> {
134                 clientProps = JacksonUtils.readValue(jsonNode,
135                     AafAuthDmaapClientProperties::class.java)!!
136                 clientProps.props = parseEventProps()
137             }
138
139             else -> {
140                 throw BluePrintProcessorException("DMAAP adaptor($type) is " +
141                         "not supported")
142             }
143         }
144         return clientProps
145     }
146
147     /**
148      * Returns DMAAP client service according to the type of client properties.
149      */
150     private fun blueprintDmaapClientService(clientProps: DmaapClientProperties):
151             BluePrintDmaapClientService {
152         when (clientProps) {
153             is HttpNoAuthDmaapClientProperties -> {
154                 return HttpNoAuthDmaapClientService(clientProps)
155             }
156
157             is AafAuthDmaapClientProperties -> {
158                 return AafAuthDmaapClientService(clientProps)
159             }
160
161             else -> {
162                 throw BluePrintProcessorException("Unable to get the DMAAP " +
163                         "client")
164             }
165         }
166     }
167
168     /**
169      * Parses the event.properties file which contains the default values for
170      * the connection required.
171      */
172     private fun parseEventProps(): Properties {
173         val prodProps = Properties()
174         val proProps = (env as ConfigurableEnvironment).propertySources.get(
175             "class path resource [event.properties]")
176
177         if (proProps != null) {
178             val entries = (proProps as ResourcePropertySource).source.entries
179             for (e in entries) {
180                 prodProps.put(e.key, e.value)
181             }
182         } else {
183             log.error("Unable to load the event.properties file")
184         }
185         return prodProps
186     }
187 }