X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fdmaap%2Fdbcapi%2Fservice%2FFeedService.java;h=77d11ab894edfc3c4a711feef2443de7601e77d8;hb=e2e2bcd054957077817b4e9f441ca069ad452d2d;hp=5f1cfb8631a1ddf47fb67dde26faa2b3ac53f74c;hpb=41317d52ae9991ff49ca21c72aceb6037dffc9ad;p=dmaap%2Fdbcapi.git diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/FeedService.java b/src/main/java/org/onap/dmaap/dbcapi/service/FeedService.java index 5f1cfb8..77d11ab 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/service/FeedService.java +++ b/src/main/java/org/onap/dmaap/dbcapi/service/FeedService.java @@ -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. @@ -23,25 +25,32 @@ package org.onap.dmaap.dbcapi.service; import org.onap.dmaap.dbcapi.util.RandomInteger; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; 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.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; +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; import org.onap.dmaap.dbcapi.model.DR_Sub; import org.onap.dmaap.dbcapi.model.Feed; +import org.onap.dmaap.dbcapi.model.MR_Client; import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status; import org.onap.dmaap.dbcapi.util.DmaapConfig; public class FeedService extends BaseLoggingClass { private Map feeds = DatabaseClass.getFeeds(); + private Map dr_subs = DatabaseClass.getDr_subs(); private DR_PubService pubService = new DR_PubService(); private DR_SubService subService = new DR_SubService(); private DcaeLocationService dcaeLocations = new DcaeLocationService(); @@ -66,6 +75,10 @@ public class FeedService extends BaseLoggingClass { ArrayList subs = subService.getDr_SubsByFeedId( f.getFeedId() ); f.setSubs(subs); } + + public List getAllFeeds(){ + return getAllFeeds(null, null, null); + } public List getAllFeeds( String name, String ver, String match ) { logger.info( "getAllFeeds: name=" + name + " ver=" + ver + " match=" + match); @@ -73,7 +86,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 +206,7 @@ public class FeedService extends BaseLoggingClass { // need to save the Sub objects independently private boolean saveSubs( Feed fnew, Feed req ) { ArrayList 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 +312,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 ) { @@ -348,22 +362,30 @@ public class FeedService extends BaseLoggingClass { // 2) Call the DR Delete function. Feed with the same name and version can never be added again // public Feed removeFeed( Feed req, ApiError err ) { + return removeFeed( req, err, true ); + } + + public Feed removeFeed( Feed req, ApiError err, boolean hitDR ) { // strip pubs and subs from feed first no matter what ArrayList pubs = pubService.getDr_PubsByFeedId( req.getFeedId() ); for( DR_Pub pub: pubs ) { - pubService.removeDr_Pub(pub.getPubId(), err); + pubService.removeDr_Pub(pub.getPubId(), err, hitDR); if ( ! err.is2xx()) { return req; } } ArrayList subs = subService.getDr_SubsByFeedId( req.getFeedId() ); for ( DR_Sub sub: subs ) { - subService.removeDr_Sub(sub.getSubId(), err); + subService.removeDr_Sub(sub.getSubId(), err, hitDR); if ( ! err.is2xx()) { return req; } } + + if ( ! hitDR ) { + return feeds.remove(req.getFeedId()); + } if ( deleteHandling.equalsIgnoreCase("DeleteOnDR")) { DrProvConnection prov = new DrProvConnection(); @@ -393,16 +415,111 @@ 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 tmppub = new ArrayList(); + 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; } } + + + /* + * sync will retrieve current config from DR and add it to the DB + * when hard = true, then first git rid of current DR provisioning data (from the DB) + */ + public void sync( boolean hard, ApiError err ) { + + if ( hard ) { + + ArrayList flist = new ArrayList(this.getAllFeeds()); + for ( Iterator it = flist.iterator(); it.hasNext(); ) { + Feed f = it.next(); + + @SuppressWarnings("unused") + Feed old = removeFeed( f, err, false ); + if (! err.is2xx()) { + return; + } + } + } + + DrProvConnection prov = new DrProvConnection(); + prov.makeDumpConnection(); + String resp = prov.doGetDump( err ); + if (! err.is2xx()) { + return; + } + logger.debug("sync: resp from DR is: " + resp); + + JSONParser parser = new JSONParser(); + JSONObject jsonObj; + try { + jsonObj = (JSONObject) parser.parse( resp ); + } catch ( ParseException pe ) { + logger.error( "Error parsing provisioning data: " + resp ); + err.setCode(500); + return; + } + + int i; + + JSONArray feedsArray = (JSONArray) jsonObj.get( "feeds"); + for( i = 0; i < feedsArray.size(); i++ ) { + JSONObject entry = (JSONObject) feedsArray.get(i); + Feed fnew = new Feed( entry.toJSONString() ); + + logger.info( "fnew status is:" + fnew.getStatus() ); + if ( ! fnew.isStatusValid()) { + err.setCode(500); + err.setMessage( "Unexpected response from DR backend" ); + err.setFields("response"); + return; + } + + if ( ! savePubs( fnew ) ) { + err.setCode(Status.BAD_REQUEST.getStatusCode()); + err.setMessage("Unable to save Pub or Sub objects"); + return; + } + fnew.setFormatUuid(fnew.getFormatUuid()); + fnew.setLastMod(); + feeds.put( fnew.getFeedId(), fnew ); + + } + + JSONArray subArray = (JSONArray) jsonObj.get( "subscriptions"); + for( i = 0; i < subArray.size(); i++ ) { + JSONObject entry = (JSONObject) subArray.get(i); + DR_Sub snew = new DR_Sub( entry.toJSONString() ); + + logger.info( "snew status is:" + snew.getStatus() ); + if ( ! snew.isStatusValid()) { + err.setCode(500); + err.setMessage( "Unexpected response from DR backend" ); + err.setFields("response"); + return; + } + + dr_subs.put( snew.getSubId(), snew ); + + } + err.setCode(200); + return; + + } private String simulateResp( Feed f, String action ){ String server = "localhost"; @@ -416,15 +533,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 ){