Allow POST dr_sub using FeedName

Change-Id: I7237755d265fc14205d3c04173d170c250be4817
Signed-off-by: dglFromAtt <dgl@research.att.com>
Issue-ID: DMAAP-1045
diff --git a/src/main/java/org/onap/dmaap/dbcapi/client/DrProvConnection.java b/src/main/java/org/onap/dmaap/dbcapi/client/DrProvConnection.java
index 441596a..3cb3d67 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/client/DrProvConnection.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/client/DrProvConnection.java
@@ -411,7 +411,7 @@
             if (rc >= 200 && rc < 300 ) {
      			responseBody = bodyToString( uc.getInputStream() );
     			logger.info( "responseBody=" + responseBody );
-
+    			err.setCode( rc );
             } else if ( rc == 404 ) {
             	err.setCode( rc );
             	err.setFields( "feedid");
diff --git a/src/main/java/org/onap/dmaap/dbcapi/model/DR_Pub.java b/src/main/java/org/onap/dmaap/dbcapi/model/DR_Pub.java
index 06598f7..0d146b7 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/model/DR_Pub.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/model/DR_Pub.java
@@ -32,6 +32,10 @@
 	private String userpwd;
 	private String feedId;
 	private String pubId;
+	
+	// NOTE: the following fields are optional in the API but not stored in the DB
+	private	String	feedName;
+	private String	feedVersion;
 
 	
 	public DR_Pub() {
@@ -114,6 +118,22 @@
 	public void setNextPubId() {
 		this.pubId = this.feedId + "." +  DR_Pub.nextKey();
 	}
+	
+	public String getFeedName() {
+		return feedName;
+	}
+
+	public void setFeedName(String feedName) {
+		this.feedName = feedName;
+	}
+
+	public String getFeedVersion() {
+		return feedVersion;
+	}
+
+	public void setFeedVersion(String feedVersion) {
+		this.feedVersion = feedVersion;
+	}
 
 	public DR_Pub setRandomUserName() {
 		RandomString r = new RandomString(15);
diff --git a/src/main/java/org/onap/dmaap/dbcapi/model/DR_Sub.java b/src/main/java/org/onap/dmaap/dbcapi/model/DR_Sub.java
index dd0b85d..9b6e2d7 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/model/DR_Sub.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/model/DR_Sub.java
@@ -45,6 +45,10 @@
 	private boolean guaranteedDelivery;
 	private boolean guaranteedSequence;
 	private boolean privilegedSubscriber;
+	
+	// NOTE: the following fields are optional in the API but not stored in the DB
+	private	String	feedName;
+	private String	feedVersion;
 
 	public DR_Sub() {
 
@@ -230,6 +234,24 @@
 	public void setPrivilegedSubscriber(boolean privilegedSubscriber) {
 		this.privilegedSubscriber = privilegedSubscriber;
 	}
+	
+	
+
+	public String getFeedName() {
+		return feedName;
+	}
+
+	public void setFeedName(String feedName) {
+		this.feedName = feedName;
+	}
+
+	public String getFeedVersion() {
+		return feedVersion;
+	}
+
+	public void setFeedVersion(String feedVersion) {
+		this.feedVersion = feedVersion;
+	}
 
 	public byte[] getBytes(String provApi) {
 		if ( "AT&T".equals(provApi)) {
diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_PubResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_PubResource.java
index bcdb4b3..1673857 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_PubResource.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_PubResource.java
@@ -91,21 +91,42 @@
 			DR_Pub pub
 			) {
 		ApiService resp = new ApiService();
+		FeedService feeds = new FeedService();
+		Feed fnew = null;
 
 		logger.info( "Entry: POST /dr_pubs");
 
 		try {
 			resp.required( "feedId", pub.getFeedId(), "");
+		} catch ( RequiredFieldException rfe ) {
+			try {
+				resp.required( "feedName", pub.getFeedName(), "");
+			}catch ( RequiredFieldException rfe2 ) {
+				logger.debug( resp.toString() );
+				return resp.error();
+			}
+			// if we found a FeedName instead of a FeedId then try to look it up.
+			List<Feed> nfeeds =  feeds.getAllFeeds( pub.getFeedName(), pub.getFeedVersion(), "equals");
+			if ( nfeeds.size() != 1 ) {
+				logger.debug( "Attempt to match "+ pub.getFeedName() + " ver="+pub.getFeedVersion() + " matched " + nfeeds.size() );
+				return resp.error();
+			}
+			fnew = nfeeds.get(0);
+		}
+		try {
 			resp.required( "dcaeLocationName", pub.getDcaeLocationName(), "");
 		} catch ( RequiredFieldException rfe ) {
 			logger.debug( resp.getErr().toString() );
 			return resp.error();	
 		}
 
-		FeedService feeds = new FeedService();
-		Feed fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() );
+
+		// we may have fnew already if located by FeedName
 		if ( fnew == null ) {
-			logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller");	
+			fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() );
+		}
+		if ( fnew == null ) {
+			logger.info( "Specified feed " + pub.getFeedId() + " or " + pub.getFeedName() + " not known to Bus Controller");	
 			return resp.error();	
 		}
 
diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java
index 28bfdc5..498454e 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_SubResource.java
@@ -92,24 +92,41 @@
 			) {
 	
 		ApiService resp = new ApiService();
-
+		FeedService feeds = new FeedService();
+		Feed fnew = null;
 		try {
 			resp.required( "feedId", sub.getFeedId(), "");
+		} catch ( RequiredFieldException rfe ) {
+			try {
+				resp.required( "feedName", sub.getFeedName(), "");
+			}catch ( RequiredFieldException rfe2 ) {
+				logger.debug( resp.toString() );
+				return resp.error();
+			}
+			// if we found a FeedName instead of a FeedId then try to look it up.
+			List<Feed> nfeeds =  feeds.getAllFeeds( sub.getFeedName(), sub.getFeedVersion(), "equals");
+			if ( nfeeds.size() != 1 ) {
+				logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() );
+				return resp.error();
+			}
+			fnew = nfeeds.get(0);
+		}
+			
+		try {
 			resp.required( "dcaeLocationName", sub.getDcaeLocationName(), "");
-	
 		} catch ( RequiredFieldException rfe ) {
 			logger.debug( resp.toString() );
 			return resp.error();	
 		}
-		
-		FeedService feeds = new FeedService();
-		Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
+		// we may have fnew already if located by FeedName
 		if ( fnew == null ) {
-			logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
+			fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() );
+		}
+		if ( fnew == null ) {
+			logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller");
 			resp.setCode(Status.NOT_FOUND.getStatusCode());
 			return resp.error();
 		}
-
 		DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL());
 		ArrayList<DR_Sub> subs = fnew.getSubs();
 		logger.info( "num existing subs before = " + subs.size() );
@@ -157,7 +174,7 @@
 			logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller");
 			return resp.error();
 		}
-		
+
 		DR_SubService dr_subService = new DR_SubService();
 		sub.setSubId(name);
 		DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() );
diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/ApiService.java b/src/main/java/org/onap/dmaap/dbcapi/service/ApiService.java
index 6ae639d..48f81db 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/service/ApiService.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/service/ApiService.java
@@ -165,6 +165,7 @@
 
 	// test for presence of a required field
 	public void required( String name, Object val, String expr ) throws RequiredFieldException {
+		err.setCode(0);
 		if ( val == null  ) {
 			err.setCode(Status.BAD_REQUEST.getStatusCode());
 			err.setMessage("missing required field");
diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/DR_SubService.java b/src/main/java/org/onap/dmaap/dbcapi/service/DR_SubService.java
index 99c8f31..9d233a5 100644
--- a/src/main/java/org/onap/dmaap/dbcapi/service/DR_SubService.java
+++ b/src/main/java/org/onap/dmaap/dbcapi/service/DR_SubService.java
@@ -98,7 +98,7 @@
 		String resp = prov.doPostDr_Sub( sub, apiError );
 		if ( "Yes".equals(unit_test) ) {
 			resp = simulateResp( sub, "POST" );
-			apiError.setCode(200);
+			apiError.setCode(201);
 		}
 		logger.debug( "addDr_Sub resp=" + resp );
 
@@ -110,7 +110,7 @@
 			snew.setLastMod();
 			addEgressRoute( snew, apiError );
 			dr_subs.put( snew.getSubId(), snew );	
-			apiError.setCode(200);
+			apiError.setCode(201);
 		} else {
 			apiError.setCode(400);
 		}
diff --git a/src/test/java/org/onap/dmaap/dbcapi/resources/DR_PubResourceTest.java b/src/test/java/org/onap/dmaap/dbcapi/resources/DR_PubResourceTest.java
index 42c8014..d4f71bb 100644
--- a/src/test/java/org/onap/dmaap/dbcapi/resources/DR_PubResourceTest.java
+++ b/src/test/java/org/onap/dmaap/dbcapi/resources/DR_PubResourceTest.java
@@ -98,6 +98,18 @@
 		return dr_pub;
 	}
 	
+	private DR_Pub addPubByName( String d, String un, String up, String feedName) {
+		DR_Pub dr_pub = new DR_Pub( d, un, up, null, "" );
+		dr_pub.setFeedName(feedName);
+		Entity<DR_Pub> reqEntity2 = Entity.entity( dr_pub, MediaType.APPLICATION_JSON);
+		Response resp = target( "dr_pubs").request().post( reqEntity2, Response.class);
+		System.out.println( "POST dr_pubs resp=" + resp.getStatus() );
+		assertTrue( resp.getStatus() == 201 );
+		dr_pub = resp.readEntity( DR_Pub.class );
+		
+		return dr_pub;
+	}
+	
 	@Test
 	public void GetTest() {
 		Response resp = target( "dr_pubs").request().get( Response.class );
@@ -119,6 +131,20 @@
 
 		DR_Pub dr_pub = addPub( d, un, up, feed.getFeedId() );
 	}
+	
+	@Test
+	public void PostTestByName() {
+
+		Feed feed = addFeed( "pubPostTest2", "post unit test" );
+		System.out.println( "fpubPostTest: feedId=" + feed.getFeedId());
+		
+		String d, un, up;
+		d = "central-onap";
+		un = "user1";
+		up = "secretW0rd";
+
+		DR_Pub dr_pub = addPubByName( d, un, up, "pubPostTest2" );	
+	}
 
 	@Test
 	public void PutTest() {
diff --git a/src/test/java/org/onap/dmaap/dbcapi/resources/DR_SubResourceTest.java b/src/test/java/org/onap/dmaap/dbcapi/resources/DR_SubResourceTest.java
index 917507f..2227870 100644
--- a/src/test/java/org/onap/dmaap/dbcapi/resources/DR_SubResourceTest.java
+++ b/src/test/java/org/onap/dmaap/dbcapi/resources/DR_SubResourceTest.java
@@ -129,7 +129,21 @@
 		
 		return dr_sub;
 	}
-	
+	private DR_Sub addSubByName( String d, String un, String up, String feedName ) {
+		DR_Sub dr_sub = new DR_Sub( d, un, up, null, 
+				"https://subscriber.onap.org/foo", "https://dr-prov/sublog", true );
+		
+		dr_sub.setFeedName(feedName);
+
+		Entity<DR_Sub> reqEntity2 = Entity.entity( dr_sub, MediaType.APPLICATION_JSON);
+		Response resp = target( "dr_subs").request().post( reqEntity2, Response.class);
+		System.out.println( "POST dr_subs resp=" + resp.getStatus() );
+		assertTrue( resp.getStatus() == 201 );
+		dr_sub = resp.readEntity( DR_Sub.class );
+		
+		return dr_sub;
+	}
+
 	@Test
 	public void GetTest() {
 		Response resp = target( "dr_subs").request().get( Response.class );
@@ -153,6 +167,20 @@
 	}
 
 	@Test
+	public void PostTestByName() {
+
+		Feed feed = addFeed( "subPostTest2", "post unit test" );
+		System.out.println( "subPostTest2: feedId=" + feed.getFeedId());
+		
+		String d, un, up;
+		d = "central-onap";
+		un = "user1";
+		up = "secretW0rd";
+
+		DR_Sub dr_pub = addSubByName( d, un, up, feed.getFeedName() );
+	}
+
+	@Test
 	public void PutTest() {
 
 		Feed feed = addFeed( "subPutTest", "put unit test");
@@ -172,6 +200,7 @@
 		System.out.println( "PUT dr_subs resp=" + resp.getStatus() );
 		assertTrue( resp.getStatus() == 200 );
 	}
+
 /*
  * TODO: figure out how to check delete() response
 	@Test