cde4987ff2c004df9961a764d1d3b625d724db9f
[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.BluePrintPropertiesService
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.*
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 bluePrintPropertiesService: BluePrintPropertiesService) {
50
51     /**
52      * Static variable for logging.
53      */
54     companion object {
55         var log = LoggerFactory.getLogger(
56                 BluePrintDmaapLibPropertyService::class.java)!!
57     }
58
59     /**
60      * Environment entity to derive it from the system to load a specific
61      * property file.
62      */
63     @Autowired
64     lateinit var env: Environment
65
66     /**
67      * Returns the DMAAP client by providing the input properties as a JSON
68      * node.
69      */
70     fun blueprintDmaapClientService(jsonNode: JsonNode):
71             BluePrintDmaapClientService {
72         val dmaapProps = dmaapClientProperties(jsonNode)
73         return blueprintDmaapClientService(dmaapProps)
74     }
75
76     /**
77      * Returns the DMAAP client by providing the input properties as a
78      * selector string.
79      */
80     fun blueprintDmaapClientService(selector: String):
81             BluePrintDmaapClientService {
82         val prefix = "blueprintsprocessor.dmaapclient.$selector"
83         val dmaapProps = dmaapClientProperties(prefix)
84         return blueprintDmaapClientService(dmaapProps)
85     }
86
87     /**
88      * Returns the DMAAP client properties from the type of connection it
89      * requires.
90      */
91     fun dmaapClientProperties(prefix: String): DmaapClientProperties {
92         val type = bluePrintPropertiesService.propertyBeanType(
93                 "$prefix.type", String::class.java)
94         val clientProps: DmaapClientProperties
95
96         when (type) {
97             TYPE_HTTP_NO_AUTH -> {
98                 clientProps = bluePrintPropertiesService.propertyBeanType(
99                         prefix, HttpNoAuthDmaapClientProperties::class.java)
100                 clientProps.props = parseEventProps()
101             }
102
103             TYPE_HTTP_AAF_AUTH -> {
104                 clientProps = bluePrintPropertiesService.propertyBeanType(
105                         prefix, AafAuthDmaapClientProperties::class.java)
106                 clientProps.props = parseEventProps()
107             }
108
109             else -> {
110                 throw BluePrintProcessorException("DMAAP adaptor($type) is " +
111                         "not supported")
112             }
113         }
114         return clientProps
115     }
116
117     /**
118      * Returns the DMAAP client properties from the type of connection it
119      * requires.
120      */
121     fun dmaapClientProperties(jsonNode: JsonNode): DmaapClientProperties {
122         val type = jsonNode.get("type").textValue()
123         val clientProps: DmaapClientProperties
124
125         when (type) {
126             TYPE_HTTP_NO_AUTH -> {
127                 clientProps = JacksonUtils.readValue(jsonNode,
128                         HttpNoAuthDmaapClientProperties::class.java)!!
129                 clientProps.props = parseEventProps()
130             }
131
132             TYPE_HTTP_AAF_AUTH -> {
133                 clientProps = JacksonUtils.readValue(jsonNode,
134                         AafAuthDmaapClientProperties::class.java)!!
135                 clientProps.props = parseEventProps()
136             }
137
138             else -> {
139                 throw BluePrintProcessorException("DMAAP adaptor($type) is " +
140                         "not supported")
141             }
142         }
143         return clientProps
144     }
145
146     /**
147      * Returns DMAAP client service according to the type of client properties.
148      */
149     private fun blueprintDmaapClientService(clientProps: DmaapClientProperties):
150             BluePrintDmaapClientService {
151         when (clientProps) {
152             is HttpNoAuthDmaapClientProperties -> {
153                 return HttpNoAuthDmaapClientService(clientProps)
154             }
155
156             is AafAuthDmaapClientProperties -> {
157                 return AafAuthDmaapClientService(clientProps)
158             }
159
160             else -> {
161                 throw BluePrintProcessorException("Unable to get the DMAAP " +
162                         "client")
163             }
164         }
165     }
166
167     /**
168      * Parses the event.properties file which contains the default values for
169      * the connection required.
170      */
171     private fun parseEventProps(): Properties {
172         val prodProps = Properties()
173         val proProps = (env as ConfigurableEnvironment).propertySources.get(
174                 "class path resource [event.properties]")
175
176         if (proProps != null) {
177             val entries = (proProps as ResourcePropertySource).source.entries
178             for (e in entries) {
179                 prodProps.put(e.key, e.value)
180             }
181         } else {
182             log.error("Unable to load the event.properties file")
183         }
184         return prodProps
185     }
186 }