From 89e739fc9227bedf591fdd11517a4c8077e3a386 Mon Sep 17 00:00:00 2001 From: Vijay Date: Thu, 21 Sep 2017 22:37:28 +0000 Subject: junit addon for ves Issue-ID:DCAEGEN2-82 Change-Id: Ic1ddd426545b3ccce331ec753dd6dbb7077babc9 Signed-off-by: Vijay --- .../onap/dcae/commonFunction/CommonStartup.java | 2 +- .../dcae/commonFunction/CustomExceptionLoader.java | 159 +++++++-------- .../dcae/commonFunction/DmaapPropertyReader.java | 16 +- .../onap/dcae/controller/LoadDynamicConfig.java | 9 +- .../onap/dcae/restapi/RestfulCollectorServlet.java | 213 ++++++++++++--------- 5 files changed, 217 insertions(+), 182 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java b/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java index 4bcfaab5..878aaa43 100644 --- a/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java +++ b/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java @@ -335,7 +335,7 @@ public class CommonStartup extends NsaBaseEndpoint implements Runnable { return result; } - static LinkedBlockingQueue fProcessingInputQueue; + public static LinkedBlockingQueue fProcessingInputQueue; private static ApiServer fTomcatServer = null; private static final Logger log = LoggerFactory.getLogger(CommonStartup.class); diff --git a/src/main/java/org/onap/dcae/commonFunction/CustomExceptionLoader.java b/src/main/java/org/onap/dcae/commonFunction/CustomExceptionLoader.java index 0ed39710..c6666d63 100644 --- a/src/main/java/org/onap/dcae/commonFunction/CustomExceptionLoader.java +++ b/src/main/java/org/onap/dcae/commonFunction/CustomExceptionLoader.java @@ -20,31 +20,33 @@ package org.onap.dcae.commonFunction; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; + +import java.util.Map.Entry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonIOException; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map.Entry; public class CustomExceptionLoader { - public static HashMap map; - private static final Logger log = LoggerFactory.getLogger(CustomExceptionLoader.class); - + protected static HashMap map = null; + private static final Logger log = LoggerFactory.getLogger ( CustomExceptionLoader.class ); + //For standalone test //LoadMap Invoked from servletSetup /* - public static void main(String[] args) { + public static void main(String[] args) { System.out.println("CustomExceptionLoader.main --> Arguments -- ExceptionConfig file: " + args[0] + "StatusCode:" + args[1]+ " Error Msg:" + args[2]); CommonStartup.exceptionConfig = args[0]; @@ -60,73 +62,72 @@ public class CustomExceptionLoader { } */ + + public static void LoadMap () { + + map = new HashMap(); + FileReader fr = null; + try { + JsonElement root = null; + fr = new FileReader(CommonStartup.exceptionConfig); + root = new JsonParser().parse(fr); + JsonObject jsonObject = root.getAsJsonObject().get("code").getAsJsonObject(); + + for (Entry entry : jsonObject.entrySet()) { + map.put(entry.getKey(), (JsonArray) entry.getValue()); + } + + log.debug("CustomExceptionLoader.LoadMap --> Map loaded - " + map); + } catch (JsonIOException e) { + e.printStackTrace(); + } catch (JsonSyntaxException e) { + e.printStackTrace(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + finally { + if (fr != null) { + try { + fr.close(); + } catch (IOException e) { + log.error("Error closing file reader stream : " +e.toString()); + } + } + } + } - public static void LoadMap() { - - map = new HashMap(); - FileReader fr = null; - try { - JsonElement root = null; - fr = new FileReader(CommonStartup.exceptionConfig); - root = new JsonParser().parse(fr); - JsonObject jsonObject = root.getAsJsonObject().get("code").getAsJsonObject(); - - for (Entry entry : jsonObject.entrySet()) { - map.put(entry.getKey(), (JsonArray) entry.getValue()); - } - - log.debug("CustomExceptionLoader.LoadMap --> Map loaded - " + map); - fr.close(); - } catch (JsonIOException e) { - log.error("json IO exception occured " + e.getLocalizedMessage(), e); - } catch (JsonSyntaxException e) { - log.error("JsonSyntaxException occured " + e.getLocalizedMessage(), e); - } catch (FileNotFoundException e) { - log.error("FileNotFoundException occured " + e.getLocalizedMessage(), e); - } catch (Exception e) { - log.error("exception occured " + e.getLocalizedMessage(), e); - } finally { - if (fr != null) { - try { - fr.close(); - } catch (IOException e) { - log.error("Error closing file reader stream : " + e.getLocalizedMessage(), e); - } - } - } - } - - public static String[] LookupMap(String error, String errorMsg) { - - String[] retarray = null; - - log.debug("CustomExceptionLoader.LookupMap -->" + " HTTP StatusCode:" + error + " Msg:" - + errorMsg); - try { - - JsonArray jarray = map.get(error); - for (int i = 0; i < jarray.size(); i++) { - - JsonElement val = jarray.get(i).getAsJsonObject().get("Reason"); - JsonArray ec = (JsonArray) jarray.get(i).getAsJsonObject().get("ErrorCode"); - log.trace("CustomExceptionLoader.LookupMap Parameter -> Error msg : " + errorMsg - + " Reason text being matched:" + val); - if (errorMsg.contains(val.toString().replace("\"", ""))) { - log.trace( - "CustomExceptionLoader.LookupMap Successful! Exception matched to error message StatusCode:" - + ec.get(0) + "ErrorMessage:" + ec.get(1)); - retarray = new String[2]; - retarray[0] = ec.get(0).toString(); - retarray[1] = ec.get(1).toString(); - return retarray; - } - } - - } catch (Exception e) { - System.out.println(e.getMessage()); - } - - return retarray; - } + public static String[] LookupMap (String error, String errormsg) { + + String[] retarray = null; + + log.debug("CustomExceptionLoader.LookupMap -->" + " HTTP StatusCode:" + error + " Msg:" + errormsg); + try{ + + JsonArray jarray = map.get(error); + for (int i = 0; i < jarray.size(); i++) { + + JsonElement val = jarray.get(i).getAsJsonObject().get("Reason"); + JsonArray ec = (JsonArray) jarray.get(i).getAsJsonObject().get("ErrorCode"); + log.trace("CustomExceptionLoader.LookupMap Parameter -> Error msg : " + errormsg + " Reason text being matched:" + val); + if (errormsg.contains(val.toString().replace("\"", ""))){ + log.trace("CustomExceptionLoader.LookupMap Successful! Exception matched to error message StatusCode:" + ec.get(0).toString() + "ErrorMessage:" + ec.get(1).toString()); + retarray = new String[2]; + retarray[0]=ec.get(0).toString(); + retarray[1]=ec.get(1).toString(); + return retarray; + } + } + + } + catch (Exception e) + { + System.out.println(e.getMessage()); + } + + return retarray; + } } + diff --git a/src/main/java/org/onap/dcae/commonFunction/DmaapPropertyReader.java b/src/main/java/org/onap/dcae/commonFunction/DmaapPropertyReader.java index 971d61ab..cb4e3af6 100644 --- a/src/main/java/org/onap/dcae/commonFunction/DmaapPropertyReader.java +++ b/src/main/java/org/onap/dcae/commonFunction/DmaapPropertyReader.java @@ -46,8 +46,8 @@ public class DmaapPropertyReader { private static final String CAMBRIA_HOSTS = "cambria.hosts"; private static final String USER = " USER:"; private static final String PWD = " PWD:"; - private static final String BASIC_AUTH_PASSWORD = "basicAuthPassword"; - private static final String BASIC_AUTH_USERNAME = "basicAuthUsername"; + private static final String BA_PWD = "basicAuthPassword"; + private static final String BA_UNAME = "basicAuthUsername"; public HashMap dmaap_hash = new HashMap(); @@ -68,8 +68,8 @@ public class DmaapPropertyReader { jsonObject.get(i).getAsJsonObject().get("cambria.topic"), HOST_URL, jsonObject.get(i).getAsJsonObject().get(CAMBRIA_URL), jsonObject.get(i).getAsJsonObject().get(CAMBRIA_HOSTS), PWD, - jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_PASSWORD), USER, - jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_USERNAME), + jsonObject.get(i).getAsJsonObject().get(BA_PWD), USER, + jsonObject.get(i).getAsJsonObject().get(BA_UNAME), jsonObject.get(i).getAsJsonObject().get("name"))); String convertedname = jsonObject.get(i).getAsJsonObject().get("name") @@ -88,15 +88,15 @@ public class DmaapPropertyReader { jsonObject.get(i).getAsJsonObject().get(CAMBRIA_URL).toString() .replace("\"", "")); } - if (jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_PASSWORD) != null) { + if (jsonObject.get(i).getAsJsonObject().get(BA_PWD) != null) { dmaap_hash.put(convertedname + ".basicAuthPassword", jsonObject.get(i).getAsJsonObject() - .get(BASIC_AUTH_PASSWORD).toString().replace("\"", "")); + .get(BA_PWD).toString().replace("\"", "")); } - if (jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_USERNAME) != null) { + if (jsonObject.get(i).getAsJsonObject().get(BA_UNAME) != null) { dmaap_hash.put(convertedname + ".basicAuthUsername", jsonObject.get(i).getAsJsonObject() - .get(BASIC_AUTH_USERNAME).toString().replace("\"", "")); + .get(BA_UNAME).toString().replace("\"", "")); } } diff --git a/src/main/java/org/onap/dcae/controller/LoadDynamicConfig.java b/src/main/java/org/onap/dcae/controller/LoadDynamicConfig.java index 7134f3c2..a28bca86 100644 --- a/src/main/java/org/onap/dcae/controller/LoadDynamicConfig.java +++ b/src/main/java/org/onap/dcae/controller/LoadDynamicConfig.java @@ -39,8 +39,8 @@ public class LoadDynamicConfig { private static final Logger log = LoggerFactory.getLogger(LoadDynamicConfig.class); - public static String propFile = "collector.properties"; - public static String configFile = "/opt/app/KV-Configuration.json"; + public String propFile = "collector.properties"; + public String configFile = "/opt/app/KV-Configuration.json"; static String url; static String retString; @@ -60,11 +60,12 @@ public class LoadDynamicConfig { try { - String jsonData = readFile(configFile); + LoadDynamicConfig lc = new LoadDynamicConfig(); + String jsonData = readFile(lc.configFile); JSONObject jsonObject = new JSONObject(jsonData); PropertiesConfiguration conf; - conf = new PropertiesConfiguration(propFile); + conf = new PropertiesConfiguration(lc.propFile); conf.setEncoding(null); // update properties based on consul dynamic configuration diff --git a/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java b/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java index babb184b..fee9ce1c 100644 --- a/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java +++ b/src/main/java/org/onap/dcae/restapi/RestfulCollectorServlet.java @@ -21,6 +21,17 @@ package org.onap.dcae.restapi; +import java.io.IOException; +import java.net.URL; + +import javax.servlet.ServletException; + +import org.apache.tomcat.util.codec.binary.Base64; +import org.onap.dcae.commonFunction.CommonStartup; +import org.onap.dcae.commonFunction.VESLogger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.att.nsa.apiServer.CommonServlet; import com.att.nsa.configs.ConfigDbException; import com.att.nsa.drumlin.service.framework.DrumlinErrorHandler; @@ -32,97 +43,119 @@ import com.att.nsa.drumlin.till.nv.rrNvReadable; import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException; import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting; import com.att.nsa.security.NsaAuthenticator; + import com.att.nsa.security.authenticators.SimpleAuthenticator; import com.att.nsa.security.db.simple.NsaSimpleApiKey; -import org.apache.tomcat.util.codec.binary.Base64; -import org.onap.dcae.commonFunction.CommonStartup; -import org.onap.dcae.commonFunction.VESLogger; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.net.URL; -import javax.servlet.ServletException; - -public class RestfulCollectorServlet extends CommonServlet { - - String authid; - String authpwd; - String authlist; - - public RestfulCollectorServlet(rrNvReadable settings) throws loadException, missingReqdSetting { - super(settings, "collector", false); - authid = settings.getString(CommonStartup.kSetting_authid, null); - if (authid != null) { - String authpwdtemp = settings.getString(CommonStartup.kSetting_authpwd, null); - authpwd = new String(Base64.decodeBase64(authpwdtemp)); - } - authlist = settings.getString(CommonStartup.kSetting_authlist, null); - } - - - /** - * This is called once at server start. Use it to init any shared objects and setup the route mapping. - */ - @Override - protected void servletSetup() - throws rrNvReadable.missingReqdSetting, rrNvReadable.invalidSettingValue, ServletException { - super.servletSetup(); - - try { - // the base class provides a bunch of things like API authentication and ECOMP compliant - // logging. The Restful Collector likely doesn't need API authentication, so for now, - // we init the base class services with an in-memory (and empty!) config DB. - commonServletSetup(ConfigDbType.MEMORY); - - VESLogger.setUpEcompLogging(); - - // setup the servlet routing and error handling - final DrumlinRequestRouter drr = getRequestRouter(); - - // you can tell the request router what to do when a particular kind of exception is thrown. - drr.setHandlerForException(IllegalArgumentException.class, new DrumlinErrorHandler() { - @Override - public void handle(DrumlinRequestContext ctx, Throwable cause) { - sendJsonReply(ctx, HttpStatusCodes.k400_badRequest, cause.getMessage()); - } - }); - - // load the routes from the config file - final URL routes = findStream("routes.conf"); - if (routes == null) { - throw new rrNvReadable.missingReqdSetting("No routing configuration."); - } - final DrumlinPlayishRoutingFileSource drs = new DrumlinPlayishRoutingFileSource(routes); - drr.addRouteSource(drs); - - if (CommonStartup.authflag > 0) { - NsaAuthenticator nsaAuth = new SimpleAuthenticator(); - if (authlist != null) { - String[] authpair = authlist.split("\\|"); - for (String pair : authpair) { - String[] lineid = pair.split(","); - String listauthid = lineid[0]; - String listauthpwd = new String(Base64.decodeBase64(lineid[1])); - ((SimpleAuthenticator) nsaAuth).add(listauthid, listauthpwd); - } - - } else if (authid != null) { - ((SimpleAuthenticator) nsaAuth).add(authid, authpwd); - } else { - //add a default test account - ((SimpleAuthenticator) nsaAuth).add("admin", "collectorpasscode"); - } - this.getSecurityManager().addAuthenticator(nsaAuth); - } - - log.info("Restful Collector Servlet is up."); - } catch (SecurityException | IOException | ConfigDbException e) { - throw new ServletException(e); - } - } - - - private static final long serialVersionUID = 1L; - private static final Logger log = LoggerFactory.getLogger(RestfulCollectorServlet.class); +public class RestfulCollectorServlet extends CommonServlet +{ + String authid; + String authpwd; + public String authlist; + + public RestfulCollectorServlet ( rrNvReadable settings ) throws loadException, missingReqdSetting + { + super ( settings, "collector", false ); + authid = settings.getString(CommonStartup.kSetting_authid,null); + if (authid != null) + { + String authpwdtemp = settings.getString(CommonStartup.kSetting_authpwd,null); + authpwd = new String(Base64.decodeBase64(authpwdtemp)); + } + authlist = settings.getString(CommonStartup.kSetting_authlist,null); + } + + + + + /** + * This is called once at server start. Use it to init any shared objects and setup the route mapping. + */ + @Override + protected void servletSetup () throws rrNvReadable.missingReqdSetting, rrNvReadable.invalidSettingValue, ServletException + { + super.servletSetup (); + + try + { + // the base class provides a bunch of things like API authentication and ECOMP compliant + // logging. The Restful Collector likely doesn't need API authentication, so for now, + // we init the base class services with an in-memory (and empty!) config DB. + commonServletSetup ( ConfigDbType.MEMORY ); + + VESLogger.setUpEcompLogging(); + + // setup the servlet routing and error handling + final DrumlinRequestRouter drr = getRequestRouter (); + + // you can tell the request router what to do when a particular kind of exception is thrown. + drr.setHandlerForException( IllegalArgumentException.class, new DrumlinErrorHandler() + { + @Override + public void handle ( DrumlinRequestContext ctx, Throwable cause ) + { + sendJsonReply ( ctx, HttpStatusCodes.k400_badRequest, cause.getMessage() ); + } + }); + + // load the routes from the config file + final URL routes = findStream ( "routes.conf" ); + if ( routes == null ) throw new rrNvReadable.missingReqdSetting ( "No routing configuration." ); + final DrumlinPlayishRoutingFileSource drs = new DrumlinPlayishRoutingFileSource ( routes ); + drr.addRouteSource ( drs ); + + if (CommonStartup.authflag > 0) { + NsaAuthenticator NsaAuth; + NsaAuth = AuthlistHandler(authlist); + + this.getSecurityManager().addAuthenticator(NsaAuth); + } + + log.info ( "Restful Collector Servlet is up." ); + } + catch ( SecurityException e ) + { + throw new ServletException ( e ); + } + catch ( IOException e ) + { + throw new ServletException ( e ); + } + catch ( ConfigDbException e ) + { + throw new ServletException ( e ); + } + } + + public NsaAuthenticator AuthlistHandler (String authlist) + { + NsaAuthenticator NsaAuth = new SimpleAuthenticator (); + if (authlist != null) + { + String authpair[] = authlist.split("\\|"); + for (String pair: authpair) { + String lineid[] = pair.split(","); + String listauthid = lineid[0]; + String listauthpwd = new String(Base64.decodeBase64(lineid[1])); + ((SimpleAuthenticator) NsaAuth).add(listauthid,listauthpwd); + } + + } + else if (authid != null) + { + ((SimpleAuthenticator) NsaAuth).add(authid,authpwd); + } + else + { + //add a default test account + ((SimpleAuthenticator) NsaAuth).add("admin","collectorpasscode"); + } + return NsaAuth; + + } + + + private static final long serialVersionUID = 1L; + private static final Logger log = LoggerFactory.getLogger ( RestfulCollectorServlet.class ); } + -- cgit 1.2.3-korg