AafServiceFactory implementation 76/89476/1
authorpkaras <piotr.karas@nokia.com>
Wed, 5 Jun 2019 13:02:22 +0000 (15:02 +0200)
committerpkaras <piotr.karas@nokia.com>
Wed, 5 Jun 2019 13:02:22 +0000 (15:02 +0200)
Change-Id: I95748319111087b991dabc08e9c918601c8defee
Issue-ID: DMAAP-1217
Signed-off-by: piotr.karas <piotr.karas@nokia.com>
src/main/java/org/onap/dmaap/dbcapi/aaf/AafServiceFactory.java [new file with mode: 0644]
src/main/java/org/onap/dmaap/dbcapi/aaf/AafServiceImpl.java
src/main/java/org/onap/dmaap/dbcapi/authentication/ApiPerms.java
src/main/java/org/onap/dmaap/dbcapi/service/AafPermissionService.java
src/main/java/org/onap/dmaap/dbcapi/service/DmaapService.java
src/main/java/org/onap/dmaap/dbcapi/service/MR_ClientService.java
src/main/java/org/onap/dmaap/dbcapi/service/TopicService.java
src/test/java/org/onap/dmaap/dbcapi/aaf/AafServiceFactoryTest.java [new file with mode: 0644]

diff --git a/src/main/java/org/onap/dmaap/dbcapi/aaf/AafServiceFactory.java b/src/main/java/org/onap/dmaap/dbcapi/aaf/AafServiceFactory.java
new file mode 100644 (file)
index 0000000..cfde19b
--- /dev/null
@@ -0,0 +1,86 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dmaap.dbcapi.aaf;
+
+import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
+import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
+import org.onap.dmaap.dbcapi.util.DmaapConfig;
+
+public class AafServiceFactory extends BaseLoggingClass {
+
+    private final DmaapConfig dmaapConfig;
+
+    public AafServiceFactory() {
+        this((DmaapConfig) DmaapConfig.getConfig());
+    }
+
+    AafServiceFactory(DmaapConfig dmaapConfig) {
+        this.dmaapConfig = dmaapConfig;
+    }
+
+    public AafService initAafService(ServiceType serviceType) {
+        boolean useAaf = "true".equalsIgnoreCase(dmaapConfig.getProperty("UseAAF", "false"));
+        String aafUrl = dmaapConfig.getProperty("aaf.URL", "https://authentication.domain.netset.com:8100/proxy/");
+        logger.info("AafService initAafService: useAaf={}, aafUrl={}", useAaf, aafUrl);
+
+        AafCred cred = getCred(serviceType);
+        return new AafServiceImpl(useAaf, aafUrl, cred.getIdentity(), new AafConnection(cred.toString()));
+    }
+
+    AafCred getCred(ServiceType ctype) {
+        String mechIdProperty;
+        String secretProperty;
+        AafDecrypt decryptor = new AafDecrypt();
+
+        if (ctype == ServiceType.AAF_Admin) {
+            mechIdProperty = "aaf.AdminUser";
+            secretProperty = "aaf.AdminPassword";
+        } else if (ctype == ServiceType.AAF_TopicMgr) {
+            mechIdProperty = "aaf.TopicMgrUser";
+            secretProperty = "aaf.TopicMgrPassword";
+        } else {
+            logger.error("Unexpected case for AAF credential type: " + ctype);
+            return null;
+        }
+        String identity = dmaapConfig.getProperty(mechIdProperty, "noMechId@domain.netset.com");
+        String pwd = decryptor.decrypt(dmaapConfig.getProperty(secretProperty, "notSet"));
+
+        return new AafCred(identity, pwd);
+    }
+
+    class AafCred {
+        private final String identity;
+        private final String pwd;
+
+        AafCred(String identity, String pwd) {
+            this.identity = identity;
+            this.pwd = pwd;
+        }
+
+        public String getIdentity() {
+            return identity;
+        }
+
+        public String toString() {
+            return identity + ":" + pwd;
+        }
+    }
+}
index 7d2c018..49810f3 100644 (file)
@@ -22,58 +22,19 @@ package org.onap.dmaap.dbcapi.aaf;
 
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
-import org.onap.dmaap.dbcapi.util.DmaapConfig;
 
 public class AafServiceImpl extends BaseLoggingClass implements AafService {
 
-    private AafConnection aaf;
-    private AafService.ServiceType ctype;
-    private String aafURL;
+    private String aafUrl;
     private String identity;
-    private boolean useAAF = false;
-
-    public AafServiceImpl(AafService.ServiceType t) {
-        DmaapConfig p = (DmaapConfig) DmaapConfig.getConfig();
-        aafURL = p.getProperty("aaf.URL", "https://authentication.domain.netset.com:8100/proxy/");
-        initAafService(t);
-    }
-
-    private void initAafService(AafService.ServiceType t) {
-        DmaapConfig p = (DmaapConfig) DmaapConfig.getConfig();
-        useAAF = "true".equalsIgnoreCase(p.getProperty("UseAAF", "false"));
-        logger.info("AafService initAafService: useAAF=" + useAAF);
-
-        ctype = t;
-        aaf = new AafConnection(getCred(true));
-    }
-
-    private String getCred(boolean wPwd) {
-        String mechIdProperty;
-        String secretProperty;
-        DmaapConfig p = (DmaapConfig) DmaapConfig.getConfig();
-        AafDecrypt decryptor = new AafDecrypt();
-
-        if (ctype == AafService.ServiceType.AAF_Admin) {
-            mechIdProperty = "aaf.AdminUser";
-            secretProperty = "aaf.AdminPassword";
-        } else if (ctype == AafService.ServiceType.AAF_TopicMgr) {
-            mechIdProperty = "aaf.TopicMgrUser";
-            secretProperty = "aaf.TopicMgrPassword";
-        } else {
-            logger.error("Unexpected case for AAF credential type: " + ctype);
-            return null;
-        }
-        identity = p.getProperty(mechIdProperty, "noMechId@domain.netset.com");
-
-        String encPwd = p.getProperty(secretProperty, "notSet");
-
-        String pwd = decryptor.decrypt(encPwd);
-
-        if (wPwd) {
-            return identity + ":" + pwd;
-        } else {
-            return identity;
-        }
+    private boolean useAAF;
+    private AafConnection aafConnection;
+
+    AafServiceImpl(boolean useAaf, String aafUrl, String identity, AafConnection aafConnection) {
+        this.useAAF = useAaf;
+        this.aafUrl = aafUrl;
+        this.identity = identity;
+        this.aafConnection = aafConnection;
     }
 
     @Override
@@ -130,18 +91,18 @@ public class AafServiceImpl extends BaseLoggingClass implements AafService {
     private int doPost(AafObject obj, String uri, int expect) {
         int rc;
         logger.info("entry: doPost() ");
-        String pURL = aafURL + uri;
+        String pURL = aafUrl + uri;
         logger.info("doPost: useAAF=" + useAAF);
         if (useAAF) {
             logger.info("doPost: " + obj.toJSON());
-            rc = aaf.postAaf(obj, pURL);
+            rc = aafConnection.postAaf(obj, pURL);
         } else {
             rc = expect;
         }
         switch (rc) {
             case 401:
             case 403:
-                errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR, getCred(false));
+                errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR, identity);
                 break;
             case 409:
                 logger.warn("Object for " + uri + " already exists. Possible conflict.");
@@ -160,17 +121,17 @@ public class AafServiceImpl extends BaseLoggingClass implements AafService {
 
     private int doDelete(AafObject obj, String uri, int expect) {
         int rc;
-        String pURL = aafURL + uri;
+        String pURL = aafUrl + uri;
         if (useAAF) {
             logger.info("doDelete: " + obj.toJSON());
-            rc = aaf.delAaf(obj, pURL);
+            rc = aafConnection.delAaf(obj, pURL);
         } else {
             rc = expect;
         }
         switch (rc) {
             case 401:
             case 403:
-                errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR, getCred(false));
+                errorLogger.error(DmaapbcLogMessageEnum.AAF_CREDENTIAL_ERROR, identity);
                 break;
             case 404:
                 logger.warn("Object not found...ignore");
@@ -185,4 +146,13 @@ public class AafServiceImpl extends BaseLoggingClass implements AafService {
 
         return rc;
     }
+
+    String getAafUrl() {
+        return aafUrl;
+    }
+
+    boolean isUseAAF() {
+        return useAAF;
+    }
+
 }
\ No newline at end of file
index 02bab63..b082102 100644 (file)
@@ -26,7 +26,7 @@ import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 
 import org.onap.dmaap.dbcapi.aaf.AafService;
-import org.onap.dmaap.dbcapi.aaf.AafServiceImpl;
+import org.onap.dmaap.dbcapi.aaf.AafServiceFactory;
 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
@@ -90,7 +90,7 @@ public  class ApiPerms extends BaseLoggingClass {
                        DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
                        String api = p.getProperty("ApiNamespace", "apiNamespace.not.set");
 
-                       AafService aaf = new AafServiceImpl(ServiceType.AAF_Admin);
+                       AafService aaf = new AafServiceFactory().initAafService(ServiceType.AAF_Admin);
                        
                        for ( int i = 0; i < pmap.length ; i++ ) {
                                String uri = new String( api + "." + pmap[i].getUri());
index 0be6c28..51941d9 100644 (file)
@@ -21,7 +21,6 @@
 package org.onap.dmaap.dbcapi.service;
 
 import org.onap.dmaap.dbcapi.aaf.AafService;
-import org.onap.dmaap.dbcapi.aaf.AafServiceImpl;
 import org.onap.dmaap.dbcapi.aaf.AafUserRole;
 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
@@ -32,16 +31,12 @@ import org.onap.dmaap.dbcapi.model.MR_Client;
 
 import static java.lang.String.format;
 
-public class AafPermissionService extends BaseLoggingClass {
+class AafPermissionService extends BaseLoggingClass {
 
     private static final String INSTANCE_PREFIX = ":topic.";
     private final AafService aafService;
     private final DmaapService dmaapService;
 
-    public AafPermissionService() {
-        this(new AafServiceImpl(AafService.ServiceType.AAF_TopicMgr), new DmaapService());
-    }
-
     AafPermissionService(AafService aafService, DmaapService dmaapService) {
         this.aafService = aafService;
         this.dmaapService = dmaapService;
index 92455cd..c54fce8 100644 (file)
@@ -24,7 +24,7 @@ package org.onap.dmaap.dbcapi.service;
 
 import java.util.ArrayList;
 import org.onap.dmaap.dbcapi.aaf.AafService;
-import org.onap.dmaap.dbcapi.aaf.AafServiceImpl;
+import org.onap.dmaap.dbcapi.aaf.AafServiceFactory;
 import org.onap.dmaap.dbcapi.aaf.DmaapGrant;
 import org.onap.dmaap.dbcapi.aaf.DmaapPerm;
 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
@@ -86,7 +86,7 @@ public class DmaapService  extends BaseLoggingClass  {
                        nd.setLastMod();
                        dmaapholder.update(nd);
                        
-                       AafService aaf = new AafServiceImpl( ServiceType.AAF_Admin);
+                       AafService aaf = new AafServiceFactory().initAafService(ServiceType.AAF_Admin);
                        ApiPolicy apiPolicy = new ApiPolicy();
                        if ( apiPolicy.isPermissionClassSet() ) {
                                ApiPerms p = new ApiPerms();
@@ -135,7 +135,7 @@ public class DmaapService  extends BaseLoggingClass  {
                                ApiPerms p = new ApiPerms();
                                p.setEnvMap();
                        }
-                       AafService aaf = new AafServiceImpl( ServiceType.AAF_Admin);
+                       AafService aaf = new AafServiceFactory().initAafService(ServiceType.AAF_Admin);
                        if ( multiSite ) {
                                anythingWrong = setTopicMgtPerms(  nd,  aaf ) || createMmaTopic();
                        }
index 5fe6b66..d3278f5 100644 (file)
@@ -23,7 +23,7 @@
 package org.onap.dmaap.dbcapi.service;
 
 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
-import org.onap.dmaap.dbcapi.aaf.AafServiceImpl;
+import org.onap.dmaap.dbcapi.aaf.AafServiceFactory;
 import org.onap.dmaap.dbcapi.client.MrProvConnection;
 import org.onap.dmaap.dbcapi.database.DatabaseClass;
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
@@ -49,7 +49,7 @@ public class MR_ClientService extends BaseLoggingClass {
     private Map<String, DcaeLocation> locations = DatabaseClass.getDcaeLocations();
     private DmaapService dmaap = new DmaapService();
     private AafPermissionService aafPermissionService =
-            new AafPermissionService(new AafServiceImpl(ServiceType.AAF_TopicMgr), dmaap);
+            new AafPermissionService(new AafServiceFactory().initAafService(ServiceType.AAF_TopicMgr), dmaap);
     private String centralCname;
 
     public MR_ClientService() {
@@ -85,7 +85,6 @@ public class MR_ClientService extends BaseLoggingClass {
         return results;
     }
 
-
     public MR_Client getMr_Client(String key, ApiError apiError) {
         MR_Client c = mr_clients.get(key);
         if (c == null) {
index 2065754..c432254 100644 (file)
@@ -23,7 +23,7 @@
 package org.onap.dmaap.dbcapi.service;
 
 import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
-import org.onap.dmaap.dbcapi.aaf.AafServiceImpl;
+import org.onap.dmaap.dbcapi.aaf.AafServiceFactory;
 import org.onap.dmaap.dbcapi.database.DatabaseClass;
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
 import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum;
@@ -71,7 +71,7 @@ public class TopicService extends BaseLoggingClass {
         this(DatabaseClass.getTopics(), new MR_ClientService(), (DmaapConfig) DmaapConfig.getConfig(),
                 new MR_ClusterService(), new DcaeLocationService(), new MirrorMakerService(),
                 new AafTopicSetupService(
-                        new AafServiceImpl(ServiceType.AAF_TopicMgr),
+                        new AafServiceFactory().initAafService(ServiceType.AAF_TopicMgr),
                         dmaapSvc,
                         "true".equalsIgnoreCase(DmaapConfig.getConfig().getProperty("aaf.CreateTopicRoles", "true"))));
 
diff --git a/src/test/java/org/onap/dmaap/dbcapi/aaf/AafServiceFactoryTest.java b/src/test/java/org/onap/dmaap/dbcapi/aaf/AafServiceFactoryTest.java
new file mode 100644 (file)
index 0000000..45ff2b1
--- /dev/null
@@ -0,0 +1,103 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.dmaap.dbcapi.aaf;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.onap.dmaap.dbcapi.aaf.AafService.ServiceType;
+import org.onap.dmaap.dbcapi.util.DmaapConfig;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.BDDMockito.given;
+
+@RunWith(MockitoJUnitRunner.class)
+public class AafServiceFactoryTest {
+
+    private static final String USE_AAF = "true";
+    private static final String AAF_URL = "https://aaf.url/api";
+    private static final String ADMIN_USER = "admin_user";
+    private static final String TOPIC_MANAGER = "topic_manager";
+    private static final String ADMIN_PASS = "admin_pass";
+    private static final String MANAGER_PASS = "manager_pass";
+    @Mock
+    private DmaapConfig dmaapConfig;
+    private AafServiceFactory aafServiceFactory;
+
+    @Before
+    public void setUp() throws Exception {
+        aafServiceFactory = new AafServiceFactory(dmaapConfig);
+    }
+
+    @Test
+    public void shouldBuildAafServiceForAafAdmin() {
+        givenDmaapConfig();
+
+        AafServiceImpl aafService = (AafServiceImpl) aafServiceFactory.initAafService(ServiceType.AAF_Admin);
+
+        assertEquals(ADMIN_USER, aafService.getIdentity());
+        assertEquals(AAF_URL, aafService.getAafUrl());
+        assertTrue(aafService.isUseAAF());
+    }
+
+    @Test
+    public void shouldBuildAafServiceForTopicManager() {
+        givenDmaapConfig();
+
+        AafServiceImpl aafService = (AafServiceImpl) aafServiceFactory.initAafService(ServiceType.AAF_TopicMgr);
+
+        assertEquals(TOPIC_MANAGER, aafService.getIdentity());
+        assertEquals(AAF_URL, aafService.getAafUrl());
+        assertTrue(aafService.isUseAAF());
+    }
+
+    @Test
+    public void shouldCorrectlyCreateCredentialsForAafAdmin() {
+        givenDmaapConfig();
+
+        AafServiceFactory.AafCred cred = aafServiceFactory.getCred(ServiceType.AAF_Admin);
+
+        assertEquals(ADMIN_USER, cred.getIdentity());
+        assertEquals(ADMIN_USER + ":" + new AafDecrypt().decrypt(ADMIN_PASS), cred.toString());
+    }
+
+    @Test
+    public void shouldCorrectlyCreateCredentialsForTopicManager() {
+        givenDmaapConfig();
+
+        AafServiceFactory.AafCred cred = aafServiceFactory.getCred(ServiceType.AAF_TopicMgr);
+
+        assertEquals(TOPIC_MANAGER, cred.getIdentity());
+        assertEquals(TOPIC_MANAGER + ":" + new AafDecrypt().decrypt(MANAGER_PASS), cred.toString());
+    }
+
+    private void givenDmaapConfig() {
+        given(dmaapConfig.getProperty("UseAAF", "false")).willReturn(USE_AAF);
+        given(dmaapConfig.getProperty("aaf.URL", "https://authentication.domain.netset.com:8100/proxy/")).willReturn(AAF_URL);
+        given(dmaapConfig.getProperty("aaf.AdminUser", "noMechId@domain.netset.com")).willReturn(ADMIN_USER);
+        given(dmaapConfig.getProperty("aaf.TopicMgrUser", "noMechId@domain.netset.com")).willReturn(TOPIC_MANAGER);
+        given(dmaapConfig.getProperty("aaf.AdminPassword", "notSet")).willReturn(ADMIN_PASS);
+        given(dmaapConfig.getProperty("aaf.TopicMgrPassword", "notSet")).willReturn(MANAGER_PASS);
+    }
+}
\ No newline at end of file