Actually remove pubs and subs on Feed delete
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / FeedService.java
index f5ea4df..18ca2c7 100644 (file)
@@ -20,6 +20,8 @@
 
 package org.onap.dmaap.dbcapi.service;
 
+import org.onap.dmaap.dbcapi.util.RandomInteger;
+
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
@@ -27,8 +29,8 @@ import java.util.Map;
 import javax.ws.rs.core.Response.Status;
 
 import org.apache.log4j.Logger;
-import org.onap.dmaap.dbcapi.aaf.client.DrProvConnection;
-import org.onap.dmaap.dbcapi.aaf.database.DatabaseClass;
+import org.onap.dmaap.dbcapi.client.DrProvConnection;
+import org.onap.dmaap.dbcapi.database.DatabaseClass;
 import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
 import org.onap.dmaap.dbcapi.model.ApiError;
 import org.onap.dmaap.dbcapi.model.DR_Pub;
@@ -44,11 +46,13 @@ public class FeedService  extends BaseLoggingClass {
        private DR_SubService subService = new DR_SubService();
        private DcaeLocationService dcaeLocations = new DcaeLocationService();
        private String deleteHandling;
+       private String unit_test;
        
        public FeedService() {
                logger.info( "new FeedService");
                DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
                deleteHandling = p.getProperty("Feed.deleteHandling", "DeleteOnDR");
+               unit_test = p.getProperty( "UnitTest", "No" );
 
        }
        
@@ -63,11 +67,43 @@ public class FeedService  extends BaseLoggingClass {
                f.setSubs(subs);        
        }
                
-       public List<Feed> getAllFeeds() {
+       public List<Feed> getAllFeeds( String name, String ver, String match ) {
+               logger.info( "getAllFeeds: name=" + name + " ver=" + ver + " match=" + match);
                ArrayList<Feed> fatFeeds = new ArrayList<Feed>();
                for( Feed f:  feeds.values() ) {
-                       getSubObjects(f);
-                       fatFeeds.add(f);
+                       boolean keep = true;
+                       if ( name != null ) {
+                               if ( match != null && match.equals("startsWith") ) {
+                                       if ( ! f.getFeedName().startsWith( name ) ) {
+                                               logger.info( "getAllFeeds: feedName=" + f.getFeedName() + " doesn't start with=" + name);
+                                               keep = false;
+                                       }
+                               } else if ( match != null && match.equals("contains") ) {
+                                       if ( ! f.getFeedName().contains( name ) ) {
+                                               logger.info( "getAllFeeds: feedName=" + f.getFeedName() + " doesn't contain=" + name);
+                                               keep = false;
+                                       }
+                               } else {
+                                       if ( ! f.getFeedName().equals( name ) ) {
+                                               logger.info( "getAllFeeds: feedName=" + f.getFeedName() + " doesn't equal=" + name);
+                                               keep = false;
+                                       }
+                               }
+
+                       }
+                       if ( keep && ver != null ) {
+                               if ( ! f.getFeedVersion().equals(ver)) {
+                                       logger.info( "getAllFeeds: feedVersion=" + f.getFeedName() + " doesn't match " + ver);
+                                       keep = false;
+                               } else {
+                                       logger.info( "getAllFeeds: feedVersion=" + f.getFeedName() + " matches " + ver);
+                               }
+                       }
+                                       
+                       if (keep){
+                               getSubObjects(f);
+                               fatFeeds.add(f);
+                       }
                }
                return fatFeeds;
        }
@@ -157,7 +193,7 @@ public class FeedService  extends BaseLoggingClass {
        // need to save the Sub objects independently
        private boolean saveSubs( Feed fnew, Feed req ) {       
                ArrayList<DR_Sub> subs = req.getSubs();
-               if ( subs.size() == 0 ) {
+               if ( subs == null || subs.size() == 0 ) {
                        logger.info( "No subs specified");
                } else {
                        DR_SubService subSvc = new DR_SubService( fnew.getSubscribeURL() );
@@ -198,6 +234,10 @@ public class FeedService  extends BaseLoggingClass {
                DrProvConnection prov = new DrProvConnection();
                prov.makeFeedConnection();      
                String resp = prov.doPostFeed( req, err );
+               if ( unit_test.equals( "Yes" ) ) {
+                       // assume resp is null, so need to simulate it
+                       resp = simulateResp( req, "POST" );
+               }
                logger.info( "resp=" + resp );
                if ( resp == null ) {
                        switch( err.getCode() ) {
@@ -256,6 +296,10 @@ public class FeedService  extends BaseLoggingClass {
                DrProvConnection prov = new DrProvConnection();
                prov.makeFeedConnection( req.getFeedId() );
                String resp = prov.doPutFeed( req, err );
+               if ( unit_test.equals( "Yes" ) ) {
+                       // assume resp is null, so need to simulate it
+                       resp = simulateResp( req, "PUT" );
+               }
                logger.info( "resp=" + resp );
                if ( resp == null ) {
                        switch( err.getCode() ) {
@@ -325,6 +369,10 @@ public class FeedService  extends BaseLoggingClass {
                        DrProvConnection prov = new DrProvConnection();
                        prov.makeFeedConnection( req.getFeedId() );
                        String resp = prov.doDeleteFeed( req, err );
+                       if ( unit_test.equals( "Yes" ) ) {
+                               // assume resp is null, so need to simulate it
+                               resp = simulateDelResp( req );
+                       }
                        logger.info( "resp=" + resp );
                        if ( resp == null ) {
                                switch( err.getCode() ) {
@@ -345,15 +393,62 @@ public class FeedService  extends BaseLoggingClass {
                        }
                        return feeds.remove(req.getFeedId());
                } else {
-                       req.setStatus(DmaapObject_Status.DELETED);
-                       req.setPubs(null);
+               
+                       logger.info( "Disable pubs for deleted feed - creating tmp pub");
+                       ArrayList<DR_Pub> tmppub = new ArrayList<DR_Pub>();
+                       tmppub.add( new DR_Pub( dcaeLocations.getCentralLocation())
+                                                               .setRandomUserName()
+                                                               .setRandomPassword());
+                       req.setPubs(tmppub);
                        req.setSubs(null);
-                       req.setLastMod();
-                       feeds.put( req.getFeedId(), req );
-                       return null;
+                       Feed fnew = updateFeed( req, err );
+                       if ( ! err.is2xx()) {
+                               return req;
+                       }
+                       fnew.setStatus(DmaapObject_Status.DELETED);
+                       feeds.put( fnew.getFeedId(), fnew );
+                       return null;    
                }
 
                
        }       
 
+       private String simulateResp( Feed f, String action ){
+               String server = "localhost";
+               String feedid;
+               if ( action.equals( "POST" ) ) { 
+                       RandomInteger ran = new RandomInteger(10000);
+                       feedid = Integer.toString( ran.next() );
+               } else if ( action.equals( "PUT" ) ) {
+                       feedid = f.getFeedId();
+               } else {
+                       feedid = "99";
+               }
+               String ret = String.format( 
+"{\"suspend\":false,\"groupid\":0,\"description\":\"%s\",\"version\":\"1.0\",\"authorization\":{\"endpoint_addrs\":[],\"classification\":\"unclassified\",\"endpoint_ids\":[{\"password\":\"topSecret123\",\"id\":\"sim\"}]},\"name\":\"%s\",\"business_description\":\"\",\"publisher\":\"sim\",\"links\":{\"subscribe\":\"https://%s/subscribe/%s\",\"log\":\"https://%s/feedlog/%s\",\"publish\":\"https://%s/publish/%s\",\"self\":\"https://%s/feed/%s\"}}",
+               f.getFeedDescription(),
+               f.getFeedName(),
+               server, feedid,
+               server, feedid,
+               server, feedid,
+               server, feedid
+
+               );
+               return ret;
+       }
+       private String simulateDelResp( Feed f ){
+               String server = "localhost";
+               String feedid = f.getFeedId();
+               String ret = String.format( 
+"{\"suspend\":true,\"groupid\":0,\"description\":\"%s\",\"version\":\"1.0\",\"authorization\":{\"endpoint_addrs\":[],\"classification\":\"unclassified\",\"endpoint_ids\":[{\"password\":\"topSecret123\",\"id\":\"sim\"}]},\"name\":\"%s\",\"business_description\":\"\",\"publisher\":\"sim\",\"links\":{\"subscribe\":\"https://%s/subscribe/%s\",\"log\":\"https://%s/feedlog/%s\",\"publish\":\"https://%s/publish/%s\",\"self\":\"https://%s/feed/%s\"}}",
+               f.getFeedDescription(),
+               f.getFeedName(),
+               server, feedid,
+               server, feedid,
+               server, feedid,
+               server, feedid
+
+               );
+               return ret;
+       }
 }