Merge "Multiple Sonar Fixes - AafNamespace.java"
authorvarun gudisena <varuneshwar.gudisena@att.com>
Mon, 28 Jan 2019 17:38:53 +0000 (17:38 +0000)
committerGerrit Code Review <gerrit@onap.org>
Mon, 28 Jan 2019 17:38:53 +0000 (17:38 +0000)
pom.xml
src/main/java/org/onap/dmaap/dbcapi/client/DrProvConnection.java
src/main/java/org/onap/dmaap/dbcapi/model/DR_Sub.java
src/main/java/org/onap/dmaap/dbcapi/model/Feed.java
src/test/java/org/onap/dmaap/dbcapi/aaf/AafUserRoleTest.java [new file with mode: 0644]
version.properties

diff --git a/pom.xml b/pom.xml
index 4592e2d..2650e46 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <jettyVersion>9.4.12.RC2</jettyVersion> 
                <eelf.version>1.0.0</eelf.version>
-               <artifact.version>1.0.20-SNAPSHOT</artifact.version>
+               <artifact.version>1.0.21-SNAPSHOT</artifact.version>
                <!-- SONAR -->
                <jacoco.version>0.7.7.201606060606</jacoco.version>
                <sonar-jacoco-listeners.version>3.2</sonar-jacoco-listeners.version>
index cc6f02c..eda3653 100644 (file)
@@ -28,6 +28,7 @@ import org.onap.dmaap.dbcapi.model.ApiError;
 import org.onap.dmaap.dbcapi.model.DR_Sub;
 import org.onap.dmaap.dbcapi.model.Feed;
 import org.onap.dmaap.dbcapi.service.DmaapService;
+import org.onap.dmaap.dbcapi.util.DmaapConfig;
 
 import javax.net.ssl.HttpsURLConnection;
 import java.io.*;
@@ -43,6 +44,10 @@ public class DrProvConnection extends BaseLoggingClass {
           
    
        private String provURL;
+       private String provApi;
+       private String  behalfHeader;
+       private String  feedContentType;
+       private String  subContentType;
        
        private HttpsURLConnection uc;
 
@@ -52,6 +57,13 @@ public class DrProvConnection extends BaseLoggingClass {
                if ( provURL.length() < 1 ) {
                        errorLogger.error( DmaapbcLogMessageEnum.PREREQ_DMAAP_OBJECT, "getDrProvUrl");
                }
+               DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
+               provApi = p.getProperty( "DR.provApi", "ONAP" );
+               behalfHeader = p.getProperty( "DR.onBehalfHeader", "X-DR-ON-BEHALF-OF");
+               feedContentType = p.getProperty( "DR.feedContentType", "application/vnc.dr.feed");
+               subContentType = p.getProperty( "DR.subContentType", "application/vnd.dr.subscription");
+               logger.info( "provURL=" + provURL + " provApi=" + provApi + " behalfHeader=" + behalfHeader
+                               + " feedContentType=" + feedContentType + " subContentType=" + subContentType );
                        
        }
        
@@ -143,9 +155,9 @@ public class DrProvConnection extends BaseLoggingClass {
                try {
                        logger.info( "uc=" + uc );
                        uc.setRequestMethod("POST");
-                       uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed");
+                       uc.setRequestProperty("Content-Type", feedContentType);
                        uc.setRequestProperty( "charset", "utf-8");
-                       uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", postFeed.getOwner() );
+                       uc.setRequestProperty( behalfHeader, postFeed.getOwner() );
                        uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
                        uc.setUseCaches(false);
                        uc.setDoOutput(true);
@@ -231,9 +243,9 @@ public class DrProvConnection extends BaseLoggingClass {
 
                try {
                        uc.setRequestMethod("POST");
-//                     uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed");
+//                     uc.setRequestProperty("Content-Type", feedContenType );
 //                     uc.setRequestProperty( "charset", "utf-8");
-//                     uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", postFeed.getOwner() );
+//                     uc.setRequestProperty( behalfHeader, postFeed.getOwner() );
 //                     uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
 //                     uc.setUseCaches(false);
 //                     uc.setDoOutput(true);   
@@ -280,7 +292,7 @@ public class DrProvConnection extends BaseLoggingClass {
        
        public String doPostDr_Sub( DR_Sub postSub, ApiError err ) {
                logger.info( "entry: doPostDr_Sub() "  );
-               byte[] postData = postSub.getBytes();
+               byte[] postData = postSub.getBytes(provApi );
                logger.info( "post fields=" + postData );
                String responsemessage = null;
                String responseBody = null;
@@ -289,9 +301,9 @@ public class DrProvConnection extends BaseLoggingClass {
        
                        uc.setRequestMethod("POST");
                
-                       uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription");
+                       uc.setRequestProperty("Content-Type", subContentType );
                        uc.setRequestProperty( "charset", "utf-8");
-                       uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" );
+                       uc.setRequestProperty( behalfHeader, "DGL" );
                        uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
                        uc.setUseCaches(false);
                        uc.setDoOutput(true);
@@ -361,9 +373,9 @@ public class DrProvConnection extends BaseLoggingClass {
                try {
                        logger.info( "uc=" + uc );
                        uc.setRequestMethod("PUT");
-                       uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed");
+                       uc.setRequestProperty("Content-Type", feedContentType );
                        uc.setRequestProperty( "charset", "utf-8");
-                       uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", putFeed.getOwner() );
+                       uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
                        uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
                        uc.setUseCaches(false);
                        uc.setDoOutput(true);
@@ -445,7 +457,7 @@ public class DrProvConnection extends BaseLoggingClass {
        }
        public String doPutDr_Sub(DR_Sub postSub, ApiError err) {
                logger.info( "entry: doPutDr_Sub() "  );
-               byte[] postData = postSub.getBytes();
+               byte[] postData = postSub.getBytes(provApi);
                logger.info( "post fields=" + postData );
                String responsemessage = null;
                String responseBody = null;
@@ -454,9 +466,9 @@ public class DrProvConnection extends BaseLoggingClass {
        
                        uc.setRequestMethod("PUT");
                
-                       uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription");
+                       uc.setRequestProperty("Content-Type", subContentType );
                        uc.setRequestProperty( "charset", "utf-8");
-                       uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" );
+                       uc.setRequestProperty( behalfHeader, "DGL" );
                        uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
                        uc.setUseCaches(false);
                        uc.setDoOutput(true);
@@ -531,9 +543,9 @@ public class DrProvConnection extends BaseLoggingClass {
        
                        uc.setRequestMethod("GET");
                
-                       //uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription");
+                       //uc.setRequestProperty("Content-Type", subContentType );
                        //uc.setRequestProperty( "charset", "utf-8");
-                       //uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" );
+                       //uc.setRequestProperty( behalfHeader, "DGL" );
                        //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
                        //uc.setUseCaches(false);
                        //uc.setDoOutput(true);
@@ -616,9 +628,9 @@ public class DrProvConnection extends BaseLoggingClass {
        
                        uc.setRequestMethod("PUT");
                
-                       //uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription");
+                       //uc.setRequestProperty("Content-Type", subContentType );
                        //uc.setRequestProperty( "charset", "utf-8");
-                       //uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" );
+                       //uc.setRequestProperty( behalfHeader, "DGL" );
                        //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
                        uc.setUseCaches(false);
                        //uc.setDoOutput(true);
@@ -688,9 +700,9 @@ public class DrProvConnection extends BaseLoggingClass {
                try {
                        logger.info( "uc=" + uc );
                        uc.setRequestMethod("DELETE");
-                       uc.setRequestProperty("Content-Type", "application/vnd.att-dr.feed");
+                       uc.setRequestProperty("Content-Type", feedContentType );
                        uc.setRequestProperty( "charset", "utf-8");
-                       uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", putFeed.getOwner() );
+                       uc.setRequestProperty( behalfHeader, putFeed.getOwner() );
                        //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
                        uc.setUseCaches(false);
                        uc.setDoOutput(true);
@@ -773,7 +785,7 @@ public class DrProvConnection extends BaseLoggingClass {
        
        public String doDeleteDr_Sub(DR_Sub delSub, ApiError err) {
                logger.info( "entry: doDeleteDr_Sub() "  );
-               byte[] postData = delSub.getBytes();
+               byte[] postData = delSub.getBytes(provApi);
                logger.info( "post fields=" + postData );
                String responsemessage = null;
                String responseBody = null;
@@ -782,9 +794,9 @@ public class DrProvConnection extends BaseLoggingClass {
        
                        uc.setRequestMethod("DELETE");
                
-                       uc.setRequestProperty("Content-Type", "application/vnd.att-dr.subscription");
+                       uc.setRequestProperty("Content-Type", subContentType);
                        uc.setRequestProperty( "charset", "utf-8");
-                       uc.setRequestProperty( "X-ATT-DR-ON-BEHALF-OF", "DGL" );
+                       uc.setRequestProperty( behalfHeader, "DGL" );
                        //uc.setRequestProperty( "Content-Length", Integer.toString( postData.length ));
                        uc.setUseCaches(false);
                        uc.setDoOutput(true);
index e089881..a5d9999 100644 (file)
@@ -184,12 +184,35 @@ public class DR_Sub extends DmaapObject {
 
 
 
-       public byte[] getBytes() {
+       public byte[] getBytes(String provApi) {
+               if ( "AT&T".equals(provApi)) {
+                       return toProvJSONforATT().getBytes(StandardCharsets.UTF_8);
+               }
                return toProvJSON().getBytes(StandardCharsets.UTF_8);
        }
-       // returns the DR_Sub object in JSON that conforms to DR Prov Server expectations
-       public String toProvJSON() {
+       // returns the DR_Sub object in JSON that conforms to ONAP DR Prov Server expectations
+       public String toProvJSON() {    
+               // this is the original DR API that was contributed to ONAP
+               String postJSON = String.format("{\"suspend\": \"%s\", \"delivery\": "
+                               + "{\"url\": \"%s\", \"user\": \"%s\", \"password\": \"%s\", \"use100\":  \"%s\"}"
+                               + ", \"metadataOnly\": %s, \"groupid\": \"%s\", \"follow_redirect\": %s "
+                               + "}", 
+                               this.suspended,
+                               this.getDeliveryURL(), 
+                               this.getUsername(),
+                               this.getUserpwd(),
+                               this.isUse100(),
+                               "false",
+                               "0",
+                               "true");        
                
+               logger.info( postJSON );
+               return postJSON;
+       }
+       // returns the DR_Sub object in JSON that conforms to AT&T DR Prov Server expectations
+       // In Jan, 2019, the DR API used internally at AT&T diverged, so this function can be used in
+       // that runtime environment
+       public String toProvJSONforATT() {      
                // in DR 3.0, API v2.1 a new groupid field is added.  We are not using this required field so just set it to 0.
                // we send this regardless of DR Release because older versions of DR seem to safely ignore it
                // and soon those versions won't be around anyway...
@@ -197,10 +220,16 @@ public class DR_Sub extends DmaapObject {
                // We are setting it to "true" because that is the general behavior desired in OpenDCAE.
                // But it is really a no-op for OpenDCAE because we've deployed DR with the SYSTEM-level parameter for FOLLOW_REDIRECTS set to true.
                // In the event we abandon that, then setting the sub attribute to true will be a good thing.
+               // Update Jan, 2019: added guaranteed_delivery and guaranteed_sequence with value false for
+               // backwards compatibility
                // TODO:
                //   - introduce Bus Controller API support for these attributes
                //   - store the default values in the DB
-               String postJSON = String.format("{\"suspend\": \"%s\", \"delivery\": {\"url\": \"%s\", \"user\": \"%s\", \"password\": \"%s\", \"use100\":  \"%s\"}, \"metadataOnly\": %s, \"groupid\": \"%s\", \"follow_redirect\": %s }", 
+               String postJSON = String.format("{\"suspend\": \"%s\", \"delivery\": "
+                               + "{\"url\": \"%s\", \"user\": \"%s\", \"password\": \"%s\", \"use100\":  \"%s\"}"
+                               + ", \"metadataOnly\": %s, \"groupid\": \"%s\", \"follow_redirect\": %s "
+                               + ", \"guaranteed_delivery\": %s, \"guaranteed_sequence\": %s "
+                               + "}", 
                                this.suspended,
                                this.getDeliveryURL(), 
                                this.getUsername(),
@@ -208,7 +237,9 @@ public class DR_Sub extends DmaapObject {
                                this.isUse100(),
                                "false",
                                "0",
-                               "true");        
+                               "true",
+                               "false",
+                               "false");       
                
                logger.info( postJSON );
                return postJSON;
index b78377b..b3c7332 100644 (file)
@@ -48,52 +48,32 @@ public class Feed extends DmaapObject {
                private String formatUuid;
 
                private ArrayList<DR_Pub> pubs;
-               private ArrayList<DR_Sub> subs; 
+               private ArrayList<DR_Sub> subs;
 
-               
-
-               public boolean isSuspended() {
-                       return suspended;
-               }
-
-               public void setSuspended(boolean suspended) {
-                       this.suspended = suspended;
-               }
-
-               public String getSubscribeURL() {
-                       return subscribeURL;
-               }
 
-               public void setSubscribeURL(String subscribeURL) {
-                       this.subscribeURL = subscribeURL;
-               }
-
-
-               
                public Feed() {
-                       this.pubs = new ArrayList<DR_Pub>();
-                       this.subs = new ArrayList<DR_Sub>();
+                       this.pubs = new ArrayList<>();
+                       this.subs = new ArrayList<>();
                        this.setStatus( DmaapObject_Status.EMPTY );
-                       
+
                }
-               
+
                public  Feed( String name,
-                                       String version,
-                                       String description,
-                                       String owner,
-                                       String aspr
-                                        ) {
+                                               String version,
+                                               String description,
+                                               String owner,
+                                               String aspr) {
                        this.feedName = name;
                        this.feedVersion = version;
                        this.feedDescription = description;
                        this.owner = owner;
                        this.asprClassification = aspr;
-                       this.pubs = new ArrayList<DR_Pub>();
-                       this.subs = new ArrayList<DR_Sub>();
+                       this.pubs = new ArrayList<>();
+                       this.subs = new ArrayList<>();
                        this.setStatus( DmaapObject_Status.NEW );
-                       
+
                }
-               
+
                // expects a String in JSON format, with known fields to populate Feed object
                public Feed ( String json ) {
                        JSONParser parser = new JSONParser();
@@ -101,10 +81,10 @@ public class Feed extends DmaapObject {
                        try {
                                jsonObj = (JSONObject) parser.parse( json );
                        } catch ( ParseException pe ) {
-                   logger.error( "Error parsing provisioning data: " + json );
-                   this.setStatus( DmaapObject_Status.INVALID );
-                   return;
-               }
+                               logger.error( "Error parsing provisioning data: " + json );
+                               this.setStatus( DmaapObject_Status.INVALID );
+                               return;
+                       }
                        this.setFeedName( (String) jsonObj.get("name"));
 
                        this.setFeedVersion( (String) jsonObj.get("version"));
@@ -117,30 +97,48 @@ public class Feed extends DmaapObject {
                        this.setPublishURL( url );
                        this.setFeedId( url.substring( url.lastIndexOf('/')+1, url.length() ));
                        logger.info( "feedid="+ this.getFeedId() );
-                       this.setSubscribeURL( (String) links.get("subscribe") );                                        
+                       this.setSubscribeURL( (String) links.get("subscribe") );
                        this.setLogURL( (String) links.get("log") );
                        JSONObject auth = (JSONObject) jsonObj.get("authorization");
                        this.setAsprClassification( (String) auth.get("classification"));
                        JSONArray pubs = (JSONArray) auth.get( "endpoint_ids");
                        int i;
-                       ArrayList<DR_Pub> dr_pub = new ArrayList<DR_Pub>();
-                       this.subs = new ArrayList<DR_Sub>();
+                       ArrayList<DR_Pub> dr_pub = new ArrayList<>();
+                       this.subs = new ArrayList<>();
 
                        for( i = 0; i < pubs.size(); i++ ) {
                                JSONObject entry = (JSONObject) pubs.get(i);
-                               dr_pub.add(  new DR_Pub( "someLocation", 
-                                                                       (String) entry.get("id"),
-                                                                       (String) entry.get("password"),
-                                                                       this.getFeedId(),
-                                                                       this.getFeedId() + "." +  DR_Pub.nextKey() ));
-                       
+                               dr_pub.add(  new DR_Pub( "someLocation",
+                                               (String) entry.get("id"),
+                                               (String) entry.get("password"),
+                                               this.getFeedId(),
+                                               this.getFeedId() + "." +  DR_Pub.nextKey() ));
+
                        }
                        this.setPubs( dr_pub );
-       
+
                        this.setStatus( DmaapObject_Status.VALID );
 
                }
 
+               
+
+               public boolean isSuspended() {
+                       return suspended;
+               }
+
+               public void setSuspended(boolean suspended) {
+                       this.suspended = suspended;
+               }
+
+               public String getSubscribeURL() {
+                       return subscribeURL;
+               }
+
+               public void setSubscribeURL(String subscribeURL) {
+                       this.subscribeURL = subscribeURL;
+               }
+
                public String getFeedId() {
                        return feedId;
                }
@@ -218,8 +216,7 @@ public class Feed extends DmaapObject {
                // returns the Feed object in JSON that conforms to DR Prov Server expectations
                public String toProvJSON() {
 
-                       ArrayList<DR_Pub> pubs = this.getPubs();
-                       String postJSON = String.format("{\"name\": \"%s\", \"version\": \"%s\", \"description\": \"%s\", \"suspend\": %s, \"authorization\": { \"classification\": \"%s\", ",  
+                       String postJSON = String.format("{\"name\": \"%s\", \"version\": \"%s\", \"description\": \"%s\", \"suspend\": %s, \"authorization\": { \"classification\": \"%s\", ",
                                        this.getFeedName(), 
                                        this.getFeedVersion(),
                                        this.getFeedDescription(),
@@ -264,8 +261,7 @@ public class Feed extends DmaapObject {
                }
                
                public static String getSubProvURL( String feedId ) {
-                       String ret = new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId ;
-                       return ret;
+                       return new DmaapService().getDmaap().getDrProvUrl() + "/subscribe/" + feedId;
                }
 
                @Override
diff --git a/src/test/java/org/onap/dmaap/dbcapi/aaf/AafUserRoleTest.java b/src/test/java/org/onap/dmaap/dbcapi/aaf/AafUserRoleTest.java
new file mode 100644 (file)
index 0000000..6328c21
--- /dev/null
@@ -0,0 +1,35 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 IBM 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.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.*;
+
+public class AafUserRoleTest {
+
+    @Test
+    public void toJSON() {
+        AafUserRole role = new AafUserRole("test","admin");
+        assertThat(role.toJSON(), is(" { \"user\": \"test\", \"role\": \"admin\" }"));
+    }
+}
\ No newline at end of file
index 6623284..e04b5bc 100644 (file)
@@ -27,7 +27,7 @@
 
 major=1
 minor=0
-patch=20
+patch=21
 base_version=${major}.${minor}.${patch}
 
 # Release must be completed with git revision # in Jenkins