Merge "IssueFix-Replace type with diamonds"
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / service / FeedService.java
index 5f1cfb8..de18d95 100644 (file)
@@ -3,6 +3,8 @@
  * org.onap.dmaap
  * ================================================================================
  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ *
+ * Modifications Copyright (C) 2019 IBM.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -29,8 +31,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;
@@ -73,7 +75,7 @@ public class FeedService  extends BaseLoggingClass {
                for( Feed f:  feeds.values() ) {
                        boolean keep = true;
                        if ( name != null ) {
-                               if ( match != null && match.equals("startsWith") ) {
+                               if ( match != null && "startsWith".equals(match) ) {
                                        if ( ! f.getFeedName().startsWith( name ) ) {
                                                logger.info( "getAllFeeds: feedName=" + f.getFeedName() + " doesn't start with=" + name);
                                                keep = false;
@@ -193,7 +195,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() );
@@ -299,6 +301,7 @@ public class FeedService  extends BaseLoggingClass {
                if ( unit_test.equals( "Yes" ) ) {
                        // assume resp is null, so need to simulate it
                        resp = simulateResp( req, "PUT" );
+                       err.setCode(200);
                }
                logger.info( "resp=" + resp );
                if ( resp == null ) {
@@ -393,12 +396,21 @@ 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;    
                }
 
                
@@ -416,15 +428,30 @@ public class FeedService  extends BaseLoggingClass {
                        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
+"{\"suspend\":false,\"groupid\":0,\"description\":\"%s\",\"version\":\"1.0\",\"authorization\":",
+                       f.getFeedDescription() );
 
-               );
+               String endpoints = "{\"endpoint_addrs\":[],\"classification\":\"unclassified\",\"endpoint_ids\":[";
+               String sep = "";
+               for( DR_Pub pub: f.getPubs()) {
+                       endpoints +=  String.format( "%s{\"password\":\"%s\",\"id\":\"%s\"}", 
+                                       sep, pub.getUserpwd(), pub.getUsername() );
+                       sep = ",";
+                       
+               }
+               endpoints += "]},";
+               ret += endpoints;
+               
+               ret += String.format(
+               "\"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.getFeedName(),
+                       server, feedid,
+                       server, feedid,
+                       server, feedid,
+                       server, feedid
+                               );
+               logger.info( "simulateResp ret=" + ret );
                return ret;
        }
        private String simulateDelResp( Feed f ){