[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / main / java / org / onap / dmaap / dbcapi / aaf / AafServiceFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2019 Nokia Intellectual Property. 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.dmaap.dbcapi.aaf;
22
23 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
24 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
25 import org.onap.dmaap.dbcapi.util.DmaapConfig;
26
27 public class AafServiceFactory extends BaseLoggingClass {
28
29     private final DmaapConfig dmaapConfig;
30
31     public AafServiceFactory() {
32         this((DmaapConfig) DmaapConfig.getConfig());
33     }
34
35     AafServiceFactory(DmaapConfig dmaapConfig) {
36         this.dmaapConfig = dmaapConfig;
37     }
38
39     public AafService initAafService(ServiceType serviceType) {
40         boolean useAaf = "true".equalsIgnoreCase(dmaapConfig.getProperty("UseAAF", "false"));
41         String aafUrl = dmaapConfig.getProperty("aaf.URL", "https://authentication.domain.netset.com:8100/proxy/");
42         logger.info("AafService initAafService: useAaf={}, aafUrl={}", useAaf, aafUrl);
43
44         AafCred cred = getCred(serviceType);
45         return new AafServiceImpl(useAaf, aafUrl, cred.getIdentity(), new AafConnection(cred.toString()));
46     }
47
48     AafCred getCred(ServiceType ctype) {
49         String mechIdProperty;
50         String secretProperty;
51         AafDecrypt decryptor = new AafDecrypt();
52
53         if (ctype == ServiceType.AAF_Admin) {
54             mechIdProperty = "aaf.AdminUser";
55             secretProperty = "aaf.AdminPassword";
56         } else if (ctype == ServiceType.AAF_TopicMgr) {
57             mechIdProperty = "aaf.TopicMgrUser";
58             secretProperty = "aaf.TopicMgrPassword";
59         } else {
60             logger.error("Unexpected case for AAF credential type: " + ctype);
61             return null;
62         }
63         String identity = dmaapConfig.getProperty(mechIdProperty, "noMechId@domain.netset.com");
64         String pwd = decryptor.decrypt(dmaapConfig.getProperty(secretProperty, "notSet"));
65
66         return new AafCred(identity, pwd);
67     }
68
69     class AafCred {
70         private final String identity;
71         private final String pwd;
72
73         AafCred(String identity, String pwd) {
74             this.identity = identity;
75             this.pwd = pwd;
76         }
77
78         public String getIdentity() {
79             return identity;
80         }
81
82         public String toString() {
83             return identity + ":" + pwd;
84         }
85     }
86 }