diff options
Diffstat (limited to 'src/main/java')
24 files changed, 1194 insertions, 589 deletions
diff --git a/src/main/java/org/onap/dmaap/dbcapi/aaf/AafConnection.java b/src/main/java/org/onap/dmaap/dbcapi/aaf/AafConnection.java index e22290a..934e541 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/aaf/AafConnection.java +++ b/src/main/java/org/onap/dmaap/dbcapi/aaf/AafConnection.java @@ -37,8 +37,11 @@ import java.net.UnknownHostException; import java.net.ConnectException; import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; import javax.net.ssl.SSLHandshakeException; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; import org.apache.commons.codec.binary.Base64; import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.logging.DmaapbcLogMessageEnum; @@ -129,6 +132,10 @@ public class AafConnection extends BaseLoggingClass { uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); uc.setUseCaches(false); uc.setDoOutput(true); + + SSLContext sc = SSLContext.getInstance("SSL"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + uc.setSSLSocketFactory(sc.getSocketFactory()); OutputStream os = null; @@ -296,6 +303,27 @@ public class AafConnection extends BaseLoggingClass { return rc; } + + private TrustManager[] trustAllCerts = new TrustManager[]{ + new X509TrustManager() { + + @Override + public java.security.cert.X509Certificate[] getAcceptedIssuers() + { + return null; + } + @Override + public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) + { + //No need to implement. + } + @Override + public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) + { + //No need to implement. + } + } + }; } 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 d603679..54fa84e 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/client/DrProvConnection.java +++ b/src/main/java/org/onap/dmaap/dbcapi/client/DrProvConnection.java @@ -49,6 +49,7 @@ public class DrProvConnection extends BaseLoggingClass { private String feedContentType; private String subContentType; private String unit_test; + private String provURI; private HttpsURLConnection uc; @@ -63,6 +64,7 @@ public class DrProvConnection extends BaseLoggingClass { behalfHeader = p.getProperty( "DR.onBehalfHeader", "X-DMAAP-DR-ON-BEHALF-OF"); feedContentType = p.getProperty( "DR.feedContentType", "application/vnd.dmaap-dr.feed"); subContentType = p.getProperty( "DR.subContentType", "application/vnd.dmaap-dr.subscription"); + provURI = p.getProperty( "DR.ProvisioningURI", "/internal/prov"); logger.info( "provURL=" + provURL + " provApi=" + provApi + " behalfHeader=" + behalfHeader + " feedContentType=" + feedContentType + " subContentType=" + subContentType ); unit_test = p.getProperty( "UnitTest", "No" ); @@ -97,6 +99,10 @@ public class DrProvConnection extends BaseLoggingClass { sub, nodep ); return makeConnection( provURL + uri ); } + public boolean makeDumpConnection() { + String url = provURL + provURI; + return makeConnection( url ); + } public boolean makeNodesConnection( String varName ) { String uri = String.format("/internal/api/%s", varName); @@ -923,5 +929,150 @@ public class DrProvConnection extends BaseLoggingClass { } + // add double-quotes around a value + // hope his is easier to read than in-line escaping... + private String dq( String v ) { + return ( "\"" + v + "\""); + } + private String dq( String k, String v) { + return( dq(k) + ":" + dq(v)); + } + private String dqc( String k, String v) { + return( dq(k) + ":" + dq(v) + ","); + } + + private String dumpSimulation() { + logger.info( "enter dumpSimulation()"); + String responseBody = + "{" + + dq("feeds") + ":[" + + "{" + dq( "suspend") + ":false," + + dq( "groupid") + ":0," + + dqc( "description", "Some description" ) + + dqc( "version", "m1.1") + + dq( "authorization") + ":" + + "{" + dq( "endpoint_addrs" ) + ":[]," + + dq( "classification", "unclassified") + + dq( "endpoint_ids") + ":[{" + + dqc( "password", "dradmin" ) + + dq( "id", "dradmin") + + "}]}" + + dq( "last_mod") + ":1553738110000," + + dq( "deleted") + ":false," + + dq( "feedid") + ":1," + + dqc( "name", "Default PM Feed") + + dq( "business_description") + ":\"\"," + + dqc( "publisher", "onap") + + dq( "links") + ":{" + + dqc( "subscribe", "https://dmaap-dr-prov/subscribe/1") + + dqc( "log", "https://dmaap-dr-prov/feedlog/1") + + dqc( "publish", "https://dmaap-dr-prov/publish/1") + + dq( "self", "https:/dmaap-dr-prov/feed/1") + + "}" + + dq( "created_date") + ":1553738110000 }" + + "]," + + dq( "groups") + ":[" + + "]," + + dq( "subscriptions") + ":[" + + "]," + + dq( "ingress") + ":[" + + "]," + + dq( "egress") + ":{" + + "}," + + dq( "routing") + ":[" + + "]," + + "}"; + return responseBody; + } + + public String doGetDump( ApiError err ) { + logger.info( "entry: doGetDump() " ); + + String responsemessage = null; + String responseBody = null; + + try { + + uc.setRequestMethod("GET"); + int rc = -1; + + + try { + uc.connect(); + + + } catch (ProtocolException pe) { + + // Rcvd error instead of 100-Continue + try { + // work around glitch in Java 1.7.0.21 and likely others + // without this, Java will connect multiple times to the server to run the same request + uc.setDoOutput(false); + } catch (Exception e) { + logger.error(e.getMessage(), e); + } + } + + rc = uc.getResponseCode(); + logger.info( "http response code:" + rc ); + responsemessage = uc.getResponseMessage(); + logger.info( "responsemessage=" + responsemessage ); + + + + if (responsemessage == null) { + + // work around for glitch in Java 1.7.0.21 and likely others + // When Expect: 100 is set and a non-100 response is received, the response message is not set but the response code is + String h0 = uc.getHeaderField(0); + if (h0 != null) { + int i = h0.indexOf(' '); + int j = h0.indexOf(' ', i + 1); + if (i != -1 && j != -1) { + responsemessage = h0.substring(j + 1); + } + } + } + + err.setCode(rc); // may not really be an error, but we save rc + if (rc == 200 ) { + responseBody = bodyToString( uc.getInputStream() ); + logger.info( "responseBody=" + responseBody ); + } else { + err.setMessage(responsemessage); + } + + + } catch (ConnectException ce) { + if ( unit_test.equals( "Yes" ) ) { + err.setCode(200); + err.setMessage( "simulated response"); + logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test ); + responseBody = dumpSimulation(); + + } else { + errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, ce.getMessage() ); + err.setCode( 500 ); + err.setMessage("Backend connection refused"); + logger.error(ce.getMessage(), ce); + } + } catch (Exception e) { + if ( unit_test.equals( "Yes" ) ) { + err.setCode(200); + err.setMessage( "simulated response"); + logger.info( "artificial 200 response from doGetNodes because unit_test =" + unit_test ); + responseBody = dumpSimulation(); + + } else { + logger.error("Unable to read response ", e.getMessage()); + } + } finally { + + if ( uc != null ) uc.disconnect(); + } + + return responseBody; + + } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/client/MrProvConnection.java b/src/main/java/org/onap/dmaap/dbcapi/client/MrProvConnection.java index 51bad4f..6e692fa 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/client/MrProvConnection.java +++ b/src/main/java/org/onap/dmaap/dbcapi/client/MrProvConnection.java @@ -36,131 +36,131 @@ import java.net.*; import java.util.Arrays; public class MrProvConnection extends BaseLoggingClass{ - - private String provURL; - - private HttpURLConnection uc; - - - private String topicMgrCred; - private boolean useAAF; - private String user; - private String encPwd; - - public MrProvConnection() { - String mechIdProperty = "aaf.TopicMgrUser"; - String pwdProperty = "aaf.TopicMgrPassword"; - DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig(); - user = p.getProperty( mechIdProperty, "noMechId@domain.netset.com" ); - encPwd = p.getProperty( pwdProperty, "notSet" ); - useAAF= "true".equalsIgnoreCase(p.getProperty("UseAAF", "false")); - topicMgrCred = getCred(); - - } - - private String getCred( ) { - - - String pwd = ""; - AafDecrypt decryptor = new AafDecrypt(); - pwd = decryptor.decrypt(encPwd); - return user + ":" + pwd; - } - - - public boolean makeTopicConnection( MR_Cluster cluster ) { - logger.info( "connect to cluster: " + cluster.getDcaeLocationName()); - - - provURL = cluster.getTopicProtocol() + "://" + cluster.getFqdn() + ":" + cluster.getTopicPort() + "/topics/create"; - - if ( cluster.getTopicProtocol().equals( "https" ) ) { - return makeSecureConnection( provURL ); - } - return makeConnection( provURL ); - } - - private boolean makeSecureConnection( String pURL ) { - logger.info( "makeConnection to " + pURL ); - - try { - URL u = new URL( pURL ); - uc = (HttpsURLConnection) u.openConnection(); - uc.setInstanceFollowRedirects(false); - logger.info( "open connect to " + pURL ); - return(true); - } catch( UnknownHostException uhe ){ - logger.error( "Caught UnknownHostException for " + pURL); - return(false); + + private String provURL; + + private HttpURLConnection uc; + + + private String topicMgrCred; + private boolean useAAF; + private String user; + private String encPwd; + + public MrProvConnection() { + String mechIdProperty = "aaf.TopicMgrUser"; + String pwdProperty = "aaf.TopicMgrPassword"; + DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig(); + user = p.getProperty( mechIdProperty, "noMechId@domain.netset.com" ); + encPwd = p.getProperty( pwdProperty, "notSet" ); + useAAF= "true".equalsIgnoreCase(p.getProperty("UseAAF", "false")); + topicMgrCred = getCred(); + + } + + private String getCred( ) { + + + String pwd = ""; + AafDecrypt decryptor = new AafDecrypt(); + pwd = decryptor.decrypt(encPwd); + return user + ":" + pwd; + } + + + public boolean makeTopicConnection( MR_Cluster cluster ) { + logger.info( "connect to cluster: " + cluster.getDcaeLocationName()); + + + provURL = cluster.getTopicProtocol() + "://" + cluster.getFqdn() + ":" + cluster.getTopicPort() + "/topics/create"; + + if ( cluster.getTopicProtocol().equals( "https" ) ) { + return makeSecureConnection( provURL ); + } + return makeConnection( provURL ); + } + + private boolean makeSecureConnection( String pURL ) { + logger.info( "makeConnection to " + pURL ); + + try { + URL u = new URL( pURL ); + uc = (HttpsURLConnection) u.openConnection(); + uc.setInstanceFollowRedirects(false); + logger.info( "open connect to " + pURL ); + return(true); + } catch( UnknownHostException uhe ){ + logger.error( "Caught UnknownHostException for " + pURL); + return(false); } catch (Exception e) { logger.error("Unexpected error during openConnection of " + pURL ); - e.printStackTrace(); + logger.error("Unexpected error during openConnection of ",e ); return(false); } - } - private boolean makeConnection( String pURL ) { - logger.info( "makeConnection to " + pURL ); - - try { - URL u = new URL( pURL ); - uc = (HttpURLConnection) u.openConnection(); - uc.setInstanceFollowRedirects(false); - logger.info( "open connect to " + pURL ); - return(true); - } catch( UnknownHostException uhe ){ - logger.error( "Caught UnknownHostException for " + pURL); - return(false); + } + private boolean makeConnection( String pURL ) { + logger.info( "makeConnection to " + pURL ); + + try { + URL u = new URL( pURL ); + uc = (HttpURLConnection) u.openConnection(); + uc.setInstanceFollowRedirects(false); + logger.info( "open connect to " + pURL ); + return(true); + } catch( UnknownHostException uhe ){ + logger.error( "Caught UnknownHostException for " + pURL); + return(false); } catch (Exception e) { logger.error("Unexpected error during openConnection of " + pURL ); - e.printStackTrace(); + logger.error("Unexpected error during openConnection of ",e ); return(false); } - } - - static String bodyToString( InputStream is ) { - StringBuilder sb = new StringBuilder(); - BufferedReader br = new BufferedReader( new InputStreamReader(is)); - String line; - try { - while ((line = br.readLine()) != null ) { - sb.append( line ); - } - } catch (IOException ex ) { - errorLogger.error( "IOexception:" + ex); - } - - return sb.toString(); - } - - public String doPostTopic( Topic postTopic, ApiError err ) { - String auth = "Basic " + Base64.encodeBase64String(topicMgrCred.getBytes()); - - - String responsemessage = null; - int rc = -1; - - - try { - byte[] postData = postTopic.getBytes(); - logger.info( "post fields=" + Arrays.toString(postData)); - - // when not using AAF, do not attempt Basic Authentication - if ( useAAF ) { - uc.setRequestProperty("Authorization", auth); - logger.info( "Authenticating with " + auth ); - } - uc.setRequestMethod("POST"); - uc.setRequestProperty("Content-Type", "application/json"); - uc.setRequestProperty( "charset", "utf-8"); - uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); - uc.setUseCaches(false); - uc.setDoOutput(true); - OutputStream os = null; - - - try { + } + + static String bodyToString( InputStream is ) { + StringBuilder sb = new StringBuilder(); + BufferedReader br = new BufferedReader( new InputStreamReader(is)); + String line; + try { + while ((line = br.readLine()) != null ) { + sb.append( line ); + } + } catch (IOException ex ) { + errorLogger.error( "IOexception:" + ex); + } + + return sb.toString(); + } + + public String doPostTopic( Topic postTopic, ApiError err ) { + String auth = "Basic " + Base64.encodeBase64String(topicMgrCred.getBytes()); + + + String responsemessage = null; + int rc = -1; + + + try { + byte[] postData = postTopic.getBytes(); + logger.info( "post fields=" + Arrays.toString(postData)); + + // when not using AAF, do not attempt Basic Authentication + if ( useAAF ) { + uc.setRequestProperty("Authorization", auth); + logger.info( "Authenticating with " + auth ); + } + uc.setRequestMethod("POST"); + uc.setRequestProperty("Content-Type", "application/json"); + uc.setRequestProperty( "charset", "utf-8"); + uc.setRequestProperty( "Content-Length", Integer.toString( postData.length )); + uc.setUseCaches(false); + uc.setDoOutput(true); + OutputStream os = null; + + + try { uc.connect(); os = uc.getOutputStream(); os.write( postData ); @@ -174,21 +174,21 @@ public class MrProvConnection extends BaseLoggingClass{ } catch (Exception e) { } } catch ( UnknownHostException uhe ) { - errorLogger.error( DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION , "Unknown Host Exception" , provURL ); - err.setCode(500); - err.setMessage("Unknown Host Exception"); - err.setFields( uc.getURL().getHost()); - return new String( "500: " + uhe.getMessage()); + errorLogger.error( DmaapbcLogMessageEnum.UNKNOWN_HOST_EXCEPTION , "Unknown Host Exception" , provURL ); + err.setCode(500); + err.setMessage("Unknown Host Exception"); + err.setFields( uc.getURL().getHost()); + return new String( "500: " + uhe.getMessage()); }catch ( ConnectException ce ) { - errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, "HTTP Connection Exception" ); - err.setCode(500); - err.setMessage("HTTP Connection Exception"); - err.setFields( uc.getURL().getHost()); - return new String( "500: " + ce.getMessage()); + errorLogger.error( DmaapbcLogMessageEnum.HTTP_CONNECTION_EXCEPTION, provURL, "HTTP Connection Exception" ); + err.setCode(500); + err.setMessage("HTTP Connection Exception"); + err.setFields( uc.getURL().getHost()); + return new String( "500: " + ce.getMessage()); } - rc = uc.getResponseCode(); - logger.info( "http response code:" + rc ); - err.setCode(rc); + rc = uc.getResponseCode(); + logger.info( "http response code:" + rc ); + err.setCode(rc); responsemessage = uc.getResponseMessage(); logger.info( "responsemessage=" + responsemessage ); err.setMessage(responsemessage); @@ -207,29 +207,29 @@ public class MrProvConnection extends BaseLoggingClass{ } } if (rc >= 200 && rc < 300 ) { - String responseBody = null; - responseBody = bodyToString( uc.getInputStream() ); - logger.info( "responseBody=" + responseBody ); - return responseBody; + String responseBody = null; + responseBody = bodyToString( uc.getInputStream() ); + logger.info( "responseBody=" + responseBody ); + return responseBody; } - } catch (Exception e) { - errorLogger.error("Unable to read response " ); + } catch (Exception e) { + errorLogger.error("Unable to read response " ); } - finally { - try { - uc.disconnect(); - } catch ( Exception e ) { - errorLogger.error("Unable to disconnect"); - } - } - return new String( rc +": " + responsemessage ); + finally { + try { + uc.disconnect(); + } catch ( Exception e ) { + errorLogger.error("Unable to disconnect"); + } + } + return new String( rc +": " + responsemessage ); - } - + } + - + } diff --git a/src/main/java/org/onap/dmaap/dbcapi/model/DR_Node.java b/src/main/java/org/onap/dmaap/dbcapi/model/DR_Node.java index a85f040..4b2ef90 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/model/DR_Node.java +++ b/src/main/java/org/onap/dmaap/dbcapi/model/DR_Node.java @@ -21,6 +21,7 @@ package org.onap.dmaap.dbcapi.model; import javax.xml.bind.annotation.XmlRootElement; +import java.util.Objects; @XmlRootElement public class DR_Node extends DmaapObject { @@ -75,4 +76,19 @@ public class DR_Node extends DmaapObject { this.version = version; } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + DR_Node dr_node = (DR_Node) o; + return Objects.equals(fqdn, dr_node.fqdn) && + Objects.equals(dcaeLocationName, dr_node.dcaeLocationName) && + Objects.equals(hostName, dr_node.hostName) && + Objects.equals(version, dr_node.version); + } + + @Override + public int hashCode() { + return Objects.hash(fqdn, dcaeLocationName, hostName, version); + } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java b/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java new file mode 100644 index 0000000..8739511 --- /dev/null +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthenticationFilter.java @@ -0,0 +1,128 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dmaap.dbcapi.resources; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; +import org.apache.log4j.Logger; +import org.eclipse.jetty.http.HttpStatus; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.filter.CadiFilter; +import org.onap.dmaap.dbcapi.model.ApiError; +import org.onap.dmaap.dbcapi.util.DmaapConfig; + +public class AAFAuthenticationFilter implements Filter { + + private static final Logger LOGGER = Logger.getLogger(AAFAuthenticationFilter.class.getName()); + static final String CADI_PROPERTIES = "cadi.properties"; + static final String AAF_AUTHN_FLAG = "UseAAF"; + + private boolean isAafEnabled; + private CadiFilter cadiFilter; + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + DmaapConfig dmaapConfig = getConfig(); + String flag = dmaapConfig.getProperty(AAF_AUTHN_FLAG, "false"); + isAafEnabled = "true".equalsIgnoreCase(flag); + initCadi(dmaapConfig); + } + + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) + throws IOException, ServletException { + + if(isAafEnabled) { + cadiFilter.doFilter(servletRequest, servletResponse, filterChain); + updateResponseBody((HttpServletResponse)servletResponse); + } else { + filterChain.doFilter(servletRequest, servletResponse); + } + } + + private void updateResponseBody(HttpServletResponse httpResponse) + throws IOException { + if(httpResponse.getStatus() == 401) { + String errorMsg = "invalid or no credentials provided"; + LOGGER.error(errorMsg); + httpResponse.setContentType("application/json"); + httpResponse.setCharacterEncoding("UTF-8"); + httpResponse.getWriter().print(buildErrorResponse(errorMsg)); + httpResponse.getWriter().flush(); + } + } + + private String buildErrorResponse(String msg) { + try { + return new ObjectMapper().writeValueAsString(new ApiError(HttpStatus.UNAUTHORIZED_401, msg, "Authentication")); + } catch (JsonProcessingException e) { + LOGGER.warn("Could not serialize response entity: " + e.getMessage()); + return ""; + } + } + + + @Override + public void destroy() { + //nothing to cleanup + } + + private void initCadi(DmaapConfig dmaapConfig) throws ServletException { + if(isAafEnabled) { + try { + String cadiPropertiesFile = dmaapConfig.getProperty(CADI_PROPERTIES); + if(cadiPropertiesFile != null && !cadiPropertiesFile.isEmpty()) { + cadiFilter = new CadiFilter(new PropAccess(cadiPropertiesFile)); + } else { + throw new ServletException("Cannot initialize CADI filter.CADI properties not available."); + } + } catch (ServletException e) { + LOGGER.error("CADI init error :" + e.getMessage()); + throw e; + } + } + } + + DmaapConfig getConfig() { + return (DmaapConfig) DmaapConfig.getConfig(); + } + + //tests only + CadiFilter getCadiFilter() { + return cadiFilter; + } + + void setCadiFilter(CadiFilter cadiFilter) { + this.cadiFilter = cadiFilter; + } + + boolean isAafEnabled() { + return isAafEnabled; + } +} diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthorizationFilter.java b/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthorizationFilter.java new file mode 100644 index 0000000..5bc3dec --- /dev/null +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/AAFAuthorizationFilter.java @@ -0,0 +1,116 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dmaap.dbcapi.resources; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.io.IOException; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.log4j.Logger; +import org.eclipse.jetty.http.HttpStatus; +import org.onap.dmaap.dbcapi.model.ApiError; +import org.onap.dmaap.dbcapi.service.DmaapService; +import org.onap.dmaap.dbcapi.util.DmaapConfig; +import org.onap.dmaap.dbcapi.util.PermissionBuilder; + +public class AAFAuthorizationFilter implements Filter{ + + private static final Logger LOGGER = Logger.getLogger(AAFAuthenticationFilter.class.getName()); + static final String AAF_AUTHZ_FLAG = "UseAAF"; + private boolean isAafEnabled = false; + + private PermissionBuilder permissionBuilder; + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + DmaapConfig dmaapConfig = getConfig(); + isAafEnabled = "true".equalsIgnoreCase(dmaapConfig.getProperty(AAF_AUTHZ_FLAG, "false")); + if(isAafEnabled) { + permissionBuilder = new PermissionBuilder(dmaapConfig, getDmaapService()); + } + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) + throws IOException, ServletException { + + if(isAafEnabled) { + HttpServletRequest httpRequest = (HttpServletRequest) servletRequest; + permissionBuilder.updateDmaapInstance(); + String permission = permissionBuilder.buildPermission(httpRequest); + + if (httpRequest.isUserInRole(permission)) { + LOGGER.info("User " + httpRequest.getUserPrincipal().getName() + " has permission " + permission); + filterChain.doFilter(servletRequest, servletResponse); + } else { + String msg = "User " + httpRequest.getUserPrincipal().getName() + " does not have permission " + permission; + LOGGER.error(msg); + ((HttpServletResponse) servletResponse).setStatus(HttpStatus.FORBIDDEN_403); + servletResponse.setContentType("application/json"); + servletResponse.setCharacterEncoding("UTF-8"); + servletResponse.getWriter().print(buildErrorResponse(msg)); + servletResponse.getWriter().flush(); + } + } else { + filterChain.doFilter(servletRequest, servletResponse); + } + } + + @Override + public void destroy() { + //nothing to cleanup + } + + DmaapConfig getConfig() { + return (DmaapConfig) DmaapConfig.getConfig(); + } + + DmaapService getDmaapService() { + return new DmaapService(); + } + + private String buildErrorResponse(String msg) { + try { + return new ObjectMapper().writeValueAsString(new ApiError(HttpStatus.FORBIDDEN_403, msg, "Authorization")); + } catch (JsonProcessingException e) { + LOGGER.warn("Could not serialize response entity: " + e.getMessage()); + return ""; + } + } + + PermissionBuilder getPermissionBuilder() { + return permissionBuilder; + } + + void setPermissionBuilder(PermissionBuilder permissionBuilder) { + this.permissionBuilder = permissionBuilder; + } + + void setAafEnabled(boolean aafEnabled) { + isAafEnabled = aafEnabled; + } +} diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/AuthorizationFilter.java b/src/main/java/org/onap/dmaap/dbcapi/resources/AuthorizationFilter.java index fd5b4aa..3ed5717 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/AuthorizationFilter.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/AuthorizationFilter.java @@ -26,33 +26,44 @@ import javax.ws.rs.container.ContainerRequestFilter; import org.apache.log4j.Logger; import org.onap.dmaap.dbcapi.authentication.AuthenticationErrorException; import org.onap.dmaap.dbcapi.service.ApiService; +import org.onap.dmaap.dbcapi.util.DmaapConfig; @Authorization public class AuthorizationFilter implements ContainerRequestFilter { - - private Logger logger = Logger.getLogger(AuthorizationFilter.class.getName()); - private ResponseBuilder responseBuilder = new ResponseBuilder(); - + + private static final String AAF_FLAG = "UseAAF"; + private final Logger logger = Logger.getLogger(AuthorizationFilter.class.getName()); + private final ResponseBuilder responseBuilder = new ResponseBuilder(); + private final boolean isAafEnabled; + + + public AuthorizationFilter() { + DmaapConfig dmaapConfig = (DmaapConfig) DmaapConfig.getConfig(); + String flag = dmaapConfig.getProperty(AAF_FLAG, "false"); + isAafEnabled = "true".equalsIgnoreCase(flag); + } + @Override public void filter(ContainerRequestContext requestContext) { - ApiService apiResp = new ApiService() - .setAuth( requestContext.getHeaderString("Authorization") ) - .setUriPath(requestContext.getUriInfo().getPath()) - .setHttpMethod( requestContext.getMethod() ) - .setRequestId( requestContext.getHeaderString("X-ECOMP-RequestID") ); - - try { - apiResp.checkAuthorization(); - } catch ( AuthenticationErrorException ae ) { - logger.error("Error", ae); - requestContext.abortWith( responseBuilder.unauthorized( apiResp.getErr().getMessage() ) ); - } catch ( Exception e ) { - logger.error("Error", e); - requestContext.abortWith( responseBuilder.unavailable() ); - } + if(!isAafEnabled) { + ApiService apiResp = new ApiService() + .setAuth(requestContext.getHeaderString("Authorization")) + .setUriPath(requestContext.getUriInfo().getPath()) + .setHttpMethod(requestContext.getMethod()) + .setRequestId(requestContext.getHeaderString("X-ECOMP-RequestID")); + try { + apiResp.checkAuthorization(); + } catch (AuthenticationErrorException ae) { + logger.error("Error", ae); + requestContext.abortWith(responseBuilder.unauthorized(apiResp.getErr().getMessage())); + } catch (Exception e) { + logger.error("Error", e); + requestContext.abortWith(responseBuilder.unavailable()); + } + } } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_NodeResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_NodeResource.java index 49dc69a..f001136 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/DR_NodeResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/DR_NodeResource.java @@ -42,7 +42,6 @@ import javax.ws.rs.core.Response; import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.DR_Node; -import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.DR_NodeService; import static javax.ws.rs.core.Response.Status.BAD_REQUEST; @@ -83,10 +82,9 @@ public class DR_NodeResource extends BaseLoggingClass { @ApiResponse( code = 200, message = "Success", response = DR_Node.class), @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) - public Response addDr_Node( - DR_Node node - ) { - ApiService resp = new ApiService(); + public Response addDr_Node(DR_Node node) { + + ApiError apiError = new ApiError(); try { checker.required( "dcaeLocation", node.getDcaeLocationName()); @@ -95,11 +93,11 @@ public class DR_NodeResource extends BaseLoggingClass { return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(), "missing required field", "dcaeLocation, fqdn")); } - DR_Node nNode = dr_nodeService.addDr_Node(node, resp.getErr()); - if ( resp.getErr().is2xx()) { + DR_Node nNode = dr_nodeService.addDr_Node(node, apiError); + if (apiError.is2xx()) { return responseBuilder.success(nNode); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } @PUT @@ -111,24 +109,23 @@ public class DR_NodeResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{fqdn}") - public Response updateDr_Node( - @PathParam("fqdn") String name, - DR_Node node - ) { - ApiService resp = new ApiService(); + public Response updateDr_Node(@PathParam("fqdn") String name, DR_Node node) { + + ApiError apiError = new ApiError(); try { - checker.required( "dcaeLocation", name); + checker.required( "dcaeLocation", node.getDcaeLocationName()); checker.required( "fqdn", node.getFqdn()); } catch ( RequiredFieldException rfe ) { - return responseBuilder.error(rfe.getApiError()); + return responseBuilder.error(new ApiError(BAD_REQUEST.getStatusCode(), + "missing required field", "dcaeLocation, fqdn")); } node.setFqdn(name); - DR_Node nNode = dr_nodeService.updateDr_Node(node, resp.getErr()); - if ( resp.getErr().is2xx()) { + DR_Node nNode = dr_nodeService.updateDr_Node(node, apiError); + if (apiError.is2xx()) { return responseBuilder.success(nNode); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } @DELETE @@ -141,22 +138,16 @@ public class DR_NodeResource extends BaseLoggingClass { }) @Path("/{fqdn}") public Response deleteDr_Node( - @PathParam("fqdn") String name - ){ + @PathParam("fqdn") String name){ - ApiService resp = new ApiService(); - try { - checker.required( "fqdn", name); - } catch ( RequiredFieldException rfe ) { - logger.debug( rfe.getApiError().toString() ); - return responseBuilder.error(rfe.getApiError()); - } - dr_nodeService.removeDr_Node(name, resp.getErr()); - if ( resp.getErr().is2xx() ) { + ApiError apiError = new ApiError(); + + dr_nodeService.removeDr_Node(name, apiError); + if (apiError.is2xx()) { return responseBuilder.success(NO_CONTENT.getStatusCode(), null); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } @GET @@ -168,15 +159,14 @@ public class DR_NodeResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{fqdn}") - public Response get( - @PathParam("fqdn") String name - ) { - ApiService resp = new ApiService(); + public Response get(@PathParam("fqdn") String name) { + + ApiError apiError = new ApiError(); - DR_Node nNode = dr_nodeService.getDr_Node( name, resp.getErr() ); - if ( resp.getErr().is2xx() ) { + DR_Node nNode = dr_nodeService.getDr_Node( name, apiError ); + if (apiError.is2xx()) { return responseBuilder.success(nNode); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } } 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 1054b03..928a7e2 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 @@ -46,7 +46,6 @@ 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.Feed; -import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.DR_PubService; import org.onap.dmaap.dbcapi.service.FeedService; @@ -87,10 +86,8 @@ public class DR_PubResource extends BaseLoggingClass { @ApiResponse( code = 200, message = "Success", response = DR_Pub.class), @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) - public Response addDr_Pub( - DR_Pub pub - ) { - ApiService resp = new ApiService(); + public Response addDr_Pub(DR_Pub pub) { + ApiError apiError = new ApiError(); FeedService feeds = new FeedService(); Feed fnew = null; @@ -109,7 +106,7 @@ public class DR_PubResource extends BaseLoggingClass { 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 responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } fnew = nfeeds.get(0); } @@ -123,11 +120,11 @@ public class DR_PubResource extends BaseLoggingClass { // we may have fnew already if located by FeedName if ( fnew == null ) { - fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() ); + fnew = feeds.getFeed(pub.getFeedId(), apiError); } if ( fnew == null ) { logger.info( "Specified feed " + pub.getFeedId() + " or " + pub.getFeedName() + " not known to Bus Controller"); - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } ArrayList<DR_Pub> pubs = fnew.getPubs(); @@ -143,15 +140,15 @@ public class DR_PubResource extends BaseLoggingClass { } pubs.add( pub ); fnew.setPubs(pubs); - fnew = feeds.updateFeed( fnew, resp.getErr() ); + fnew = feeds.updateFeed(fnew, apiError); - if ( ! resp.getErr().is2xx()) { - return responseBuilder.error(resp.getErr()); + if (!apiError.is2xx()) { + return responseBuilder.error(apiError); } pubs = fnew.getPubs(); logger.info( "num existing pubs after = " + pubs.size() ); - DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), resp.getErr()); + DR_Pub pnew = dr_pubService.getDr_Pub(pub.getPubId(), apiError); return responseBuilder.success(Status.CREATED.getStatusCode(), pnew); } @@ -164,10 +161,7 @@ public class DR_PubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{pubId}") - public Response updateDr_Pub( - @PathParam("pubId") String name, - DR_Pub pub - ) { + public Response updateDr_Pub(@PathParam("pubId") String name, DR_Pub pub) { logger.info( "Entry: PUT /dr_pubs"); pub.setPubId(name); DR_Pub res = dr_pubService.updateDr_Pub(pub); @@ -183,11 +177,9 @@ public class DR_PubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{pubId}") - public Response deleteDr_Pub( - @PathParam("pubId") String id - ){ + public Response deleteDr_Pub(@PathParam("pubId") String id){ - ApiService resp = new ApiService(); + ApiError apiError = new ApiError(); try { checker.required( "pubId", id); @@ -195,21 +187,21 @@ public class DR_PubResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } - DR_Pub pub = dr_pubService.getDr_Pub( id, resp.getErr() ); - if ( ! resp.getErr().is2xx()) { - return responseBuilder.error(resp.getErr()); + DR_Pub pub = dr_pubService.getDr_Pub(id, apiError); + if ( !apiError.is2xx()) { + return responseBuilder.error(apiError); } FeedService feeds = new FeedService(); - Feed fnew = feeds.getFeed( pub.getFeedId(), resp.getErr() ); + Feed fnew = feeds.getFeed(pub.getFeedId(), apiError); if ( fnew == null ) { logger.info( "Specified feed " + pub.getFeedId() + " not known to Bus Controller"); - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } ArrayList<DR_Pub> pubs = fnew.getPubs(); if ( pubs.size() == 1 ) { - resp.setCode(Status.BAD_REQUEST.getStatusCode()); - resp.setMessage( "Can't delete the last publisher of a feed"); - return responseBuilder.error(resp.getErr()); + apiError.setCode(Status.BAD_REQUEST.getStatusCode()); + apiError.setMessage( "Can't delete the last publisher of a feed"); + return responseBuilder.error(apiError); } for( Iterator<DR_Pub> i = pubs.iterator(); i.hasNext(); ) { @@ -219,14 +211,14 @@ public class DR_PubResource extends BaseLoggingClass { } } fnew.setPubs(pubs); - fnew = feeds.updateFeed( fnew, resp.getErr() ); - if ( ! resp.getErr().is2xx()) { - return responseBuilder.error(resp.getErr()); + fnew = feeds.updateFeed(fnew,apiError); + if (!apiError.is2xx()) { + return responseBuilder.error(apiError); } - dr_pubService.removeDr_Pub(id, resp.getErr() ); - if ( ! resp.getErr().is2xx()) { - return responseBuilder.error(resp.getErr()); + dr_pubService.removeDr_Pub(id, apiError); + if (!apiError.is2xx()) { + return responseBuilder.error(apiError); } return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null); } @@ -240,10 +232,8 @@ public class DR_PubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{pubId}") - public Response get( - @PathParam("pubId") String id - ) { - ApiService resp = new ApiService(); + public Response get(@PathParam("pubId") String id) { + ApiError apiError = new ApiError(); try { checker.required( "feedId", id); @@ -251,9 +241,9 @@ public class DR_PubResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } - DR_Pub pub = dr_pubService.getDr_Pub( id, resp.getErr() ); - if ( ! resp.getErr().is2xx()) { - resp.getErr(); + DR_Pub pub = dr_pubService.getDr_Pub(id, apiError); + if (!apiError.is2xx()) { + return responseBuilder.error(apiError); } return responseBuilder.success(Status.OK.getStatusCode(), pub); } 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 d5a96de..2fa6ccd 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 @@ -24,7 +24,6 @@ package org.onap.dmaap.dbcapi.resources; import com.google.common.collect.Iterables; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import javax.ws.rs.Consumes; @@ -44,7 +43,6 @@ import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.DR_Sub; import org.onap.dmaap.dbcapi.model.Feed; -import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.DR_SubService; import org.onap.dmaap.dbcapi.service.FeedService; @@ -91,11 +89,9 @@ public class DR_SubResource extends BaseLoggingClass { @ApiResponse( code = 200, message = "Success", response = DR_Sub.class), @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) - public Response addDr_Sub( - DR_Sub sub - ) { - - ApiService resp = new ApiService(); + public Response addDr_Sub(DR_Sub sub) { + + ApiError apiError = new ApiError(); FeedService feeds = new FeedService(); Feed fnew = null; try { @@ -110,14 +106,14 @@ public class DR_SubResource extends BaseLoggingClass { // 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.isEmpty() ) { - resp.setCode(Status.NOT_FOUND.getStatusCode()); - resp.setFields("feedName"); - return responseBuilder.error(resp.getErr()); + apiError.setCode(Status.NOT_FOUND.getStatusCode()); + apiError.setFields("feedName"); + return responseBuilder.error(apiError); } else if (nfeeds.size() > 1) { logger.debug( "Attempt to match "+ sub.getFeedName() + " ver="+sub.getFeedVersion() + " matched " + nfeeds.size() ); - resp.setCode(Status.CONFLICT.getStatusCode()); - resp.setFields("feedName"); - return responseBuilder.error(resp.getErr()); + apiError.setCode(Status.CONFLICT.getStatusCode()); + apiError.setFields("feedName"); + return responseBuilder.error(apiError); } fnew = Iterables.getOnlyElement(nfeeds); } @@ -130,19 +126,19 @@ public class DR_SubResource extends BaseLoggingClass { } // we may have fnew already if located by FeedName if ( fnew == null ) { - fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() ); + fnew = feeds.getFeed( sub.getFeedId(), apiError); } if ( fnew == null ) { logger.warn( "Specified feed " + sub.getFeedId() + " or " + sub.getFeedName() + " not known to Bus Controller"); - resp.setCode(Status.NOT_FOUND.getStatusCode()); - return responseBuilder.error(resp.getErr()); + apiError.setCode(Status.NOT_FOUND.getStatusCode()); + return responseBuilder.error(apiError); } DR_SubService dr_subService = new DR_SubService( fnew.getSubscribeURL()); ArrayList<DR_Sub> subs = fnew.getSubs(); logger.info( "num existing subs before = " + subs.size() ); - DR_Sub snew = dr_subService.addDr_Sub(sub, resp.getErr() ); - if ( ! resp.getErr().is2xx() ) { - return responseBuilder.error(resp.getErr()); + DR_Sub snew = dr_subService.addDr_Sub(sub, apiError); + if (!apiError.is2xx()) { + return responseBuilder.error(apiError); } subs.add( snew ); logger.info( "num existing subs after = " + subs.size() ); @@ -162,12 +158,9 @@ public class DR_SubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{subId}") - public Response updateDr_Sub( - @PathParam("subId") String name, - DR_Sub sub - ) { + public Response updateDr_Sub(@PathParam("subId") String name, DR_Sub sub) { - ApiService resp = new ApiService(); + ApiError apiError = new ApiError(); try { checker.required( "subId", name); @@ -179,19 +172,19 @@ public class DR_SubResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } FeedService feeds = new FeedService(); - Feed fnew = feeds.getFeed( sub.getFeedId(), resp.getErr() ); + Feed fnew = feeds.getFeed(sub.getFeedId(), apiError); if ( fnew == null ) { logger.warn( "Specified feed " + sub.getFeedId() + " not known to Bus Controller"); - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } DR_SubService dr_subService = new DR_SubService(); sub.setSubId(name); - DR_Sub nsub = dr_subService.updateDr_Sub(sub, resp.getErr() ); + DR_Sub nsub = dr_subService.updateDr_Sub(sub, apiError); if ( nsub != null && nsub.isStatusValid() ) { return responseBuilder.success(nsub); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } @DELETE @@ -203,11 +196,9 @@ public class DR_SubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{subId}") - public Response deleteDr_Sub( - @PathParam("subId") String id - ){ + public Response deleteDr_Sub(@PathParam("subId") String id){ - ApiService resp = new ApiService(); + ApiError apiError = new ApiError(); try { checker.required( "subId", id); @@ -216,9 +207,9 @@ public class DR_SubResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } DR_SubService dr_subService = new DR_SubService(); - dr_subService.removeDr_Sub(id, resp.getErr() ); - if ( ! resp.getErr().is2xx() ) { - return responseBuilder.error(resp.getErr()); + dr_subService.removeDr_Sub(id, apiError); + if (!apiError.is2xx() ) { + return responseBuilder.error(apiError); } return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null ); } @@ -232,10 +223,9 @@ public class DR_SubResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{subId}") - public Response get( - @PathParam("subId") String id - ) { - ApiService resp = new ApiService(); + public Response get(@PathParam("subId") String id) { + + ApiError apiError = new ApiError(); try { checker.required( "subId", id); @@ -244,10 +234,10 @@ public class DR_SubResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } DR_SubService dr_subService = new DR_SubService(); - DR_Sub sub = dr_subService.getDr_Sub( id, resp.getErr() ); + DR_Sub sub = dr_subService.getDr_Sub(id, apiError); if ( sub != null && sub.isStatusValid() ) { return responseBuilder.success(sub); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/FeedResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/FeedResource.java index 86a79f2..6589c0d 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/FeedResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/FeedResource.java @@ -46,7 +46,6 @@ import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.DR_Pub; import org.onap.dmaap.dbcapi.model.Feed; import org.onap.dmaap.dbcapi.model.DmaapObject.DmaapObject_Status; -import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.FeedService; @@ -94,7 +93,7 @@ public class FeedResource extends BaseLoggingClass { @WebParam(name = "feed") Feed feed, @QueryParam("useExisting") String useExisting) { - ApiService resp = new ApiService(); + ApiError apiError = new ApiError(); try { checker.required( "feedName", feed.getFeedName()); @@ -108,32 +107,32 @@ public class FeedResource extends BaseLoggingClass { FeedService feedService = new FeedService(); - Feed nfeed = feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), resp.getErr() ); + Feed nfeed = feedService.getFeedByName( feed.getFeedName(), feed.getFeedVersion(), apiError); if ( nfeed == null ) { - nfeed = feedService.addFeed( feed, resp.getErr() ); + nfeed = feedService.addFeed(feed, apiError); if ( nfeed != null ) { return responseBuilder.success(nfeed); } else { logger.error( "Unable to create: " + feed.getFeedName() + ":" + feed.getFeedVersion()); - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } } else if ( nfeed.getStatus() == DmaapObject_Status.DELETED ) { feed.setFeedId( nfeed.getFeedId()); - nfeed = feedService.updateFeed(feed, resp.getErr()); + nfeed = feedService.updateFeed(feed, apiError); if ( nfeed != null ) { return responseBuilder.success(nfeed); } else { logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion()); - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } } else if ( (useExisting != null) && ("true".compareToIgnoreCase( useExisting ) == 0)) { return responseBuilder.success(nfeed); } - resp.setCode(Status.CONFLICT.getStatusCode()); - return responseBuilder.error(resp.getErr()); + apiError.setCode(Status.CONFLICT.getStatusCode()); + return responseBuilder.error(apiError); } @PUT @@ -150,7 +149,7 @@ public class FeedResource extends BaseLoggingClass { @WebParam(name = "feed") Feed feed) { FeedService feedService = new FeedService(); - ApiService resp = new ApiService(); + ApiError apiError = new ApiError(); try { checker.required( "feedId", id); @@ -159,7 +158,7 @@ public class FeedResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } - Feed nfeed = feedService.getFeed( id, resp.getErr() ); + Feed nfeed = feedService.getFeed(id, apiError); if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) { return responseBuilder.notFound(); } @@ -170,13 +169,13 @@ public class FeedResource extends BaseLoggingClass { nfeed.setFeedDescription(feed.getFeedDescription()); nfeed.setFormatUuid(feed.getFormatUuid()); - nfeed = feedService.updateFeed(nfeed, resp.getErr()); + nfeed = feedService.updateFeed(nfeed, apiError); if ( nfeed != null ) { return responseBuilder.success(nfeed); } else { logger.info( "Unable to update: " + feed.getFeedName() + ":" + feed.getFeedVersion()); - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } } @@ -189,25 +188,23 @@ public class FeedResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{id}") - public Response deleteFeed( - @PathParam("id") String id - ){ - ApiService resp = new ApiService(); + public Response deleteFeed(@PathParam("id") String id){ + ApiError apiError = new ApiError(); logger.debug( "Entry: DELETE " + id); FeedService feedService = new FeedService(); - Feed nfeed = feedService.getFeed( id, resp.getErr() ); + Feed nfeed = feedService.getFeed(id, apiError); if ( nfeed == null ) { - resp.setCode(Status.NOT_FOUND.getStatusCode()); - return responseBuilder.error(resp.getErr()); + apiError.setCode(Status.NOT_FOUND.getStatusCode()); + return responseBuilder.error(apiError); } - nfeed = feedService.removeFeed( nfeed, resp.getErr() ); + nfeed = feedService.removeFeed(nfeed, apiError); if ( nfeed == null || nfeed.getStatus() == DmaapObject_Status.DELETED ) { return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null); } logger.info( "Unable to delete: " + id + ":" + nfeed.getFeedVersion()); - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } @GET @@ -219,17 +216,45 @@ public class FeedResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{id}") - public Response getFeed( - @PathParam("id") String id - ) { - ApiService resp = new ApiService(); + public Response getFeed(@PathParam("id") String id) { + ApiError apiError = new ApiError(); FeedService feedService = new FeedService(); - Feed nfeed = feedService.getFeed( id, resp.getErr() ); + Feed nfeed = feedService.getFeed(id, apiError); if ( nfeed == null ) { - resp.setCode(Status.NOT_FOUND.getStatusCode()); - return responseBuilder.error(resp.getErr()); + apiError.setCode(Status.NOT_FOUND.getStatusCode()); + return responseBuilder.error(apiError); } return responseBuilder.success(nfeed); } + + @PUT + @ApiOperation( value = "sync feeds to existing DR", + notes = "When Bus Controller is deployed after DR, then it is possible" + + "that DR has previous provisioning data that needs to be imported" + + "into Bus Controller.", + response = Feed.class ) + @ApiResponses( value = { + @ApiResponse( code = 200, message = "Success", response = Feed.class), + @ApiResponse( code = 400, message = "Error", response = ApiError.class ) + }) + @Path( "/sync") + public Response syncFeeds (@QueryParam("hard") String hardParam) { + ApiError error = new ApiError(); + + FeedService feedService = new FeedService(); + boolean hard = false; + if ( hardParam != null && hardParam.equalsIgnoreCase("true")) { + hard = true; + } + feedService.sync( hard, error ); + if ( error.is2xx()) { + List<Feed> nfeeds = feedService.getAllFeeds(); + GenericEntity<List<Feed>> list = new GenericEntity<List<Feed>>(nfeeds) { + }; + return responseBuilder.success(list); + } + return responseBuilder.error(error); + } + } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClientResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClientResource.java index a65007d..6df8ef6 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClientResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClientResource.java @@ -45,7 +45,6 @@ import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.MR_Client; import org.onap.dmaap.dbcapi.model.MR_Cluster; import org.onap.dmaap.dbcapi.model.Topic; -import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.MR_ClientService; import org.onap.dmaap.dbcapi.service.MR_ClusterService; import org.onap.dmaap.dbcapi.service.TopicService; @@ -91,9 +90,8 @@ public class MR_ClientResource extends BaseLoggingClass { @ApiResponse( code = 200, message = "Success", response = MR_Client.class), @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) - public Response addMr_Client( - MR_Client client) { - ApiService resp = new ApiService(); + public Response addMr_Client(MR_Client client) { + ApiError apiError = new ApiError(); try { checker.required( "fqtn", client.getFqtn()); @@ -111,38 +109,38 @@ public class MR_ClientResource extends BaseLoggingClass { } MR_ClusterService clusters = new MR_ClusterService(); - MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), resp.getErr()); + MR_Cluster cluster = clusters.getMr_Cluster(client.getDcaeLocationName(), apiError); if ( cluster == null ) { - resp.setCode(Status.BAD_REQUEST.getStatusCode()); - resp.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName()); - resp.setFields("dcaeLocationName"); - logger.warn( resp.toString() ); - return responseBuilder.error(resp.getErr()); + apiError.setCode(Status.BAD_REQUEST.getStatusCode()); + apiError.setMessage( "MR_Cluster alias not found for dcaeLocation: " + client.getDcaeLocationName()); + apiError.setFields("dcaeLocationName"); + logger.warn(apiError.toString()); + return responseBuilder.error(apiError); } String url = cluster.getFqdn(); if ( url == null || url.isEmpty() ) { - resp.setCode(Status.BAD_REQUEST.getStatusCode()); - resp.setMessage("FQDN not set for dcaeLocation " + client.getDcaeLocationName() ); - resp.setFields("fqdn"); - logger.warn( resp.toString() ); - return responseBuilder.error(resp.getErr()); + apiError.setCode(Status.BAD_REQUEST.getStatusCode()); + apiError.setMessage("FQDN not set for dcaeLocation " + client.getDcaeLocationName() ); + apiError.setFields("fqdn"); + logger.warn(apiError.toString()); + return responseBuilder.error(apiError); } TopicService topics = new TopicService(); - Topic t = topics.getTopic(client.getFqtn(), resp.getErr() ); + Topic t = topics.getTopic(client.getFqtn(), apiError); if ( t == null ) { - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } - MR_Client nClient = mr_clientService.addMr_Client(client, t, resp.getErr()); - if ( resp.getErr().is2xx()) { - t = topics.getTopic(client.getFqtn(), resp.getErr()); - topics.checkForBridge(t, resp.getErr()); + MR_Client nClient = mr_clientService.addMr_Client(client, t, apiError); + if (apiError.is2xx()) { + t = topics.getTopic(client.getFqtn(), apiError); + topics.checkForBridge(t, apiError); return responseBuilder.success(nClient); } else { - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } } @@ -155,11 +153,8 @@ public class MR_ClientResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{clientId}") - public Response updateMr_Client( - @PathParam("clientId") String clientId, - MR_Client client - ) { - ApiService resp = new ApiService(); + public Response updateMr_Client(@PathParam("clientId") String clientId, MR_Client client) { + ApiError apiError = new ApiError(); try { checker.required( "fqtn", client.getFqtn()); @@ -172,13 +167,13 @@ public class MR_ClientResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } client.setMrClientId(clientId); - MR_Client nClient = mr_clientService.updateMr_Client(client, resp.getErr() ); - if ( resp.getErr().is2xx()) { + MR_Client nClient = mr_clientService.updateMr_Client(client, apiError); + if (apiError.is2xx()) { return Response.ok(nClient) .build(); } - return Response.status(resp.getErr().getCode()) - .entity( resp.getErr() ) + return Response.status(apiError.getCode()) + .entity(apiError) .build(); } @@ -191,10 +186,8 @@ public class MR_ClientResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{subId}") - public Response deleteMr_Client( - @PathParam("subId") String id - ){ - ApiService resp = new ApiService(); + public Response deleteMr_Client(@PathParam("subId") String id){ + ApiError apiError = new ApiError(); try { checker.required( "clientId", id); @@ -202,12 +195,12 @@ public class MR_ClientResource extends BaseLoggingClass { logger.debug( rfe.getApiError().toString() ); return responseBuilder.error(rfe.getApiError()); } - mr_clientService.removeMr_Client(id, true, resp.getErr() ); - if ( resp.getErr().is2xx()) { + mr_clientService.removeMr_Client(id, true, apiError); + if (apiError.is2xx()) { return responseBuilder.success(NO_CONTENT.getStatusCode(), null); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } @GET @@ -219,10 +212,8 @@ public class MR_ClientResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{subId}") - public Response test( - @PathParam("subId") String id - ) { - ApiService resp = new ApiService(); + public Response test(@PathParam("subId") String id) { + ApiError apiError = new ApiError(); try { checker.required( "clientId", id); @@ -230,10 +221,10 @@ public class MR_ClientResource extends BaseLoggingClass { logger.debug( rfe.getApiError().toString() ); return responseBuilder.error(rfe.getApiError()); } - MR_Client nClient = mr_clientService.getMr_Client( id, resp.getErr() ); - if ( resp.getErr().is2xx()) { + MR_Client nClient = mr_clientService.getMr_Client(id, apiError); + if (apiError.is2xx()) { return responseBuilder.success(nClient); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClusterResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClusterResource.java index 598fcc2..0a361ff 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClusterResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/MR_ClusterResource.java @@ -43,7 +43,6 @@ import javax.ws.rs.core.Response.Status; import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.MR_Cluster; -import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.MR_ClusterService; @@ -82,9 +81,8 @@ public class MR_ClusterResource extends BaseLoggingClass { @ApiResponse( code = 200, message = "Success", response = MR_Cluster.class), @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) - public Response addMr_Cluster( - MR_Cluster cluster) { - ApiService resp = new ApiService(); + public Response addMr_Cluster(MR_Cluster cluster) { + ApiError apiError = new ApiError(); try { checker.required( "dcaeLocationName", cluster.getDcaeLocationName()); @@ -92,11 +90,11 @@ public class MR_ClusterResource extends BaseLoggingClass { } catch( RequiredFieldException rfe ) { return responseBuilder.error(rfe.getApiError()); } - MR_Cluster mrc = mr_clusterService.addMr_Cluster(cluster, resp.getErr() ); + MR_Cluster mrc = mr_clusterService.addMr_Cluster(cluster, apiError); if ( mrc != null && mrc.isStatusValid() ) { return responseBuilder.success(Status.CREATED.getStatusCode(), mrc); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } @@ -109,11 +107,8 @@ public class MR_ClusterResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{clusterId}") - public Response updateMr_Cluster( - @PathParam("clusterId") String clusterId, - MR_Cluster cluster - ) { - ApiService resp = new ApiService(); + public Response updateMr_Cluster(@PathParam("clusterId") String clusterId, MR_Cluster cluster) { + ApiError apiError = new ApiError(); try { checker.required( "fqdn", clusterId); @@ -122,11 +117,11 @@ public class MR_ClusterResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } cluster.setDcaeLocationName(clusterId); - MR_Cluster mrc = mr_clusterService.updateMr_Cluster(cluster, resp.getErr() ); + MR_Cluster mrc = mr_clusterService.updateMr_Cluster(cluster, apiError); if ( mrc != null && mrc.isStatusValid() ) { return responseBuilder.success(Status.CREATED.getStatusCode(), mrc); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } @DELETE @@ -138,21 +133,19 @@ public class MR_ClusterResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{clusterId}") - public Response deleteMr_Cluster( - @PathParam("clusterId") String id - ){ - ApiService resp = new ApiService(); + public Response deleteMr_Cluster(@PathParam("clusterId") String id){ + ApiError apiError = new ApiError(); try { checker.required( "fqdn", id); } catch( RequiredFieldException rfe ) { return responseBuilder.error(rfe.getApiError()); } - mr_clusterService.removeMr_Cluster(id, resp.getErr() ); - if ( resp.getErr().is2xx()) { + mr_clusterService.removeMr_Cluster(id, apiError); + if (apiError.is2xx()) { return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } @GET @@ -164,20 +157,18 @@ public class MR_ClusterResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{clusterId}") - public Response getMR_Cluster( - @PathParam("clusterId") String id - ) { - ApiService resp = new ApiService(); + public Response getMR_Cluster(@PathParam("clusterId") String id) { + ApiError apiError = new ApiError(); try { checker.required( "dcaeLocationName", id); } catch( RequiredFieldException rfe ) { return responseBuilder.error(rfe.getApiError()); } - MR_Cluster mrc = mr_clusterService.getMr_Cluster( id, resp.getErr() ); + MR_Cluster mrc = mr_clusterService.getMr_Cluster(id, apiError); if ( mrc != null && mrc.isStatusValid() ) { return responseBuilder.success(Status.CREATED.getStatusCode(), mrc); } - return responseBuilder.error(resp.getErr()); + return responseBuilder.error(apiError); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/RequestTimeLogFilter.java b/src/main/java/org/onap/dmaap/dbcapi/resources/RequestTimeLogFilter.java new file mode 100644 index 0000000..b2b98b6 --- /dev/null +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/RequestTimeLogFilter.java @@ -0,0 +1,55 @@ +/*-
+ * ============LICENSE_START=======================================================
+ * org.onap.dmaap
+ * ================================================================================
+ * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.dmaap.dbcapi.resources;
+
+import com.att.eelf.configuration.EELFLogger;
+import java.time.Clock;
+import javax.ws.rs.container.ContainerRequestContext;
+import javax.ws.rs.container.ContainerRequestFilter;
+import javax.ws.rs.container.ContainerResponseContext;
+import javax.ws.rs.container.ContainerResponseFilter;
+import org.onap.dmaap.dbcapi.logging.BaseLoggingClass;
+
+public class RequestTimeLogFilter extends BaseLoggingClass implements ContainerRequestFilter, ContainerResponseFilter {
+
+ private final EELFLogger log;
+ private Clock clock;
+
+ public RequestTimeLogFilter() {
+ this(auditLogger, Clock.systemDefaultZone());
+ }
+
+ RequestTimeLogFilter(EELFLogger logger, Clock clock) {
+ this.log = logger;
+ this.clock = clock;
+ }
+
+ @Override
+ public void filter(ContainerRequestContext requestContext) {
+ requestContext.setProperty("start", clock.millis());
+ }
+
+ @Override
+ public void filter(ContainerRequestContext requestContext, ContainerResponseContext containerResponseContext) {
+ long startTime = (long) requestContext.getProperty("start");
+ long elapsedTime = clock.millis() - startTime;
+ log.info("Request took {} ms", elapsedTime);
+ }
+}
diff --git a/src/main/java/org/onap/dmaap/dbcapi/resources/TopicResource.java b/src/main/java/org/onap/dmaap/dbcapi/resources/TopicResource.java index 96b6a4a..3206cf7 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/resources/TopicResource.java +++ b/src/main/java/org/onap/dmaap/dbcapi/resources/TopicResource.java @@ -46,7 +46,6 @@ import org.onap.dmaap.dbcapi.model.ApiError; import org.onap.dmaap.dbcapi.model.ReplicationType; import org.onap.dmaap.dbcapi.model.FqtnType; import org.onap.dmaap.dbcapi.model.Topic; -import org.onap.dmaap.dbcapi.service.ApiService; import org.onap.dmaap.dbcapi.service.TopicService; import org.onap.dmaap.dbcapi.util.DmaapConfig; @@ -102,12 +101,9 @@ public class TopicResource extends BaseLoggingClass { @ApiResponse( code = 200, message = "Success", response = Topic.class), @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) - public Response addTopic( - Topic topic, - @QueryParam("useExisting") String useExisting - ) { + public Response addTopic(Topic topic, @QueryParam("useExisting") String useExisting) { logger.info( "addTopic request: " + topic + " useExisting=" + useExisting ); - ApiService check = new ApiService(); + ApiError apiError = new ApiError(); try { checker.required( "topicName", topic.getTopicName(), "^\\S+$" ); //no white space allowed in topicName @@ -141,11 +137,11 @@ public class TopicResource extends BaseLoggingClass { flag = "true".compareToIgnoreCase( useExisting ) == 0; } - Topic mrc = mr_topicService.addTopic(topic, check.getErr(), flag); - if ( mrc != null && check.getErr().is2xx() ) { + Topic mrc = mr_topicService.addTopic(topic, apiError, flag); + if ( mrc != null && apiError.is2xx() ) { return responseBuilder.success(CREATED.getStatusCode(), mrc); } - return responseBuilder.error(check.getErr()); + return responseBuilder.error(apiError); } @PUT @@ -157,15 +153,13 @@ public class TopicResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{topicId}") - public Response updateTopic( - @PathParam("topicId") String topicId - ) { - ApiService check = new ApiService(); + public Response updateTopic(@PathParam("topicId") String topicId) { + ApiError apiError = new ApiError(); - check.setCode(Status.BAD_REQUEST.getStatusCode()); - check.setMessage( "Method /PUT not supported for /topics"); + apiError.setCode(Status.BAD_REQUEST.getStatusCode()); + apiError.setMessage( "Method /PUT not supported for /topics"); - return responseBuilder.error(check.getErr()); + return responseBuilder.error(apiError); } @DELETE @@ -177,10 +171,8 @@ public class TopicResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{topicId}") - public Response deleteTopic( - @PathParam("topicId") String id - ){ - ApiService check = new ApiService(); + public Response deleteTopic(@PathParam("topicId") String id){ + ApiError apiError = new ApiError(); try { checker.required( "fqtn", id); @@ -189,11 +181,11 @@ public class TopicResource extends BaseLoggingClass { return responseBuilder.error(rfe.getApiError()); } - mr_topicService.removeTopic(id, check.getErr()); - if ( check.getErr().is2xx()) { + mr_topicService.removeTopic(id, apiError); + if (apiError.is2xx()) { return responseBuilder.success(Status.NO_CONTENT.getStatusCode(), null); } - return responseBuilder.error(check.getErr()); + return responseBuilder.error(apiError); } @@ -206,11 +198,9 @@ public class TopicResource extends BaseLoggingClass { @ApiResponse( code = 400, message = "Error", response = ApiError.class ) }) @Path("/{topicId}") - public Response getTopic( - @PathParam("topicId") String id - ) { + public Response getTopic(@PathParam("topicId") String id) { logger.info("Entry: /GET " + id); - ApiService check = new ApiService(); + ApiError apiError = new ApiError(); try { checker.required( "topicName", id, "^\\S+$" ); //no white space allowed in topicName @@ -218,9 +208,9 @@ public class TopicResource extends BaseLoggingClass { logger.error("Error", rfe.getApiError()); return responseBuilder.error(rfe.getApiError()); } - Topic mrc = mr_topicService.getTopic( id, check.getErr() ); + Topic mrc = mr_topicService.getTopic(id, apiError); if ( mrc == null ) { - return responseBuilder.error(check.getErr()); + return responseBuilder.error(apiError); } return responseBuilder.success(mrc); } diff --git a/src/main/java/org/onap/dmaap/dbcapi/server/ApplicationConfig.java b/src/main/java/org/onap/dmaap/dbcapi/server/ApplicationConfig.java index 2283ea2..2244b73 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/server/ApplicationConfig.java +++ b/src/main/java/org/onap/dmaap/dbcapi/server/ApplicationConfig.java @@ -20,8 +20,8 @@ package org.onap.dmaap.dbcapi.server; import org.glassfish.jersey.server.ResourceConfig; - - +import org.onap.dmaap.dbcapi.resources.RequestTimeLogFilter; +import org.onap.dmaap.dbcapi.resources.AuthorizationFilter; public class ApplicationConfig extends ResourceConfig { @@ -30,7 +30,8 @@ public class ApplicationConfig extends ResourceConfig { */ public ApplicationConfig() { - register(org.onap.dmaap.dbcapi.resources.AuthorizationFilter.class); + register(AuthorizationFilter.class). + register(RequestTimeLogFilter.class); } } diff --git a/src/main/java/org/onap/dmaap/dbcapi/server/JettyServer.java b/src/main/java/org/onap/dmaap/dbcapi/server/JettyServer.java index 4b578b4..7457ce9 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/server/JettyServer.java +++ b/src/main/java/org/onap/dmaap/dbcapi/server/JettyServer.java @@ -22,7 +22,8 @@ package org.onap.dmaap.dbcapi.server; - +import com.google.common.collect.Sets; +import javax.servlet.DispatcherType; import org.eclipse.jetty.server.*; import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.ServletContextHandler; @@ -31,6 +32,7 @@ import org.eclipse.jetty.util.ssl.SslContextFactory; import org.onap.dmaap.dbcapi.logging.BaseLoggingClass; import java.util.Properties; + /** * A Jetty server which supports: * - http and https (simultaneously for dev env) @@ -38,48 +40,47 @@ import java.util.Properties; * - static html pages (for documentation). */ public class JettyServer extends BaseLoggingClass { - private Server server; + private Server server; - public Server getServer() { - return server; - } - public JettyServer( Properties params ) throws Exception { + public Server getServer() { + return server; + } + + public JettyServer(Properties params) throws Exception { server = new Server(); - int httpPort = Integer.valueOf(params.getProperty("IntHttpPort", "80" )); - int sslPort = Integer.valueOf(params.getProperty("IntHttpsPort", "443" )); - boolean allowHttp = Boolean.valueOf(params.getProperty("HttpAllowed", "false")); - serverLogger.info( "port params: http=" + httpPort + " https=" + sslPort ); - serverLogger.info( "allowHttp=" + allowHttp ); - - // HTTP Server - HttpConfiguration http_config = new HttpConfiguration(); - http_config.setSecureScheme("https"); - http_config.setSecurePort(sslPort); - http_config.setOutputBufferSize(32768); - - - - try(ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(http_config))) { - httpConnector.setPort(httpPort); - httpConnector.setIdleTimeout(30000); - - - // HTTPS Server - - HttpConfiguration https_config = new HttpConfiguration(http_config); - https_config.addCustomizer(new SecureRequestCustomizer()); - SslContextFactory sslContextFactory = new SslContextFactory(); - - setUpKeystore(params, sslContextFactory); - setUpTrustStore(params, sslContextFactory); - - if (sslPort != 0) { - try(ServerConnector sslConnector = new ServerConnector(server, - new SslConnectionFactory(sslContextFactory, "http/1.1"), - new HttpConnectionFactory(https_config))) { + int httpPort = Integer.valueOf(params.getProperty("IntHttpPort", "80")); + int sslPort = Integer.valueOf(params.getProperty("IntHttpsPort", "443")); + boolean allowHttp = Boolean.valueOf(params.getProperty("HttpAllowed", "false")); + serverLogger.info("port params: http=" + httpPort + " https=" + sslPort); + serverLogger.info("allowHttp=" + allowHttp); + + // HTTP Server + HttpConfiguration http_config = new HttpConfiguration(); + http_config.setSecureScheme("https"); + http_config.setSecurePort(sslPort); + http_config.setOutputBufferSize(32768); + + try (ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(http_config))) { + httpConnector.setPort(httpPort); + httpConnector.setIdleTimeout(30000); + + // HTTPS Server + + HttpConfiguration https_config = new HttpConfiguration(http_config); + https_config.addCustomizer(new SecureRequestCustomizer()); + SslContextFactory sslContextFactory = new SslContextFactory(); + sslContextFactory.setWantClientAuth(true); + + setUpKeystore(params, sslContextFactory); + setUpTrustStore(params, sslContextFactory); + + if (sslPort != 0) { + try (ServerConnector sslConnector = new ServerConnector(server, + new SslConnectionFactory(sslContextFactory, "http/1.1"), + new HttpConnectionFactory(https_config))) { sslConnector.setPort(sslPort); if (allowHttp) { logger.info("Starting httpConnector on port " + httpPort); @@ -91,61 +92,71 @@ public class JettyServer extends BaseLoggingClass { server.setConnectors(new Connector[]{sslConnector}); } } - } else { - serverLogger.info("NOT starting sslConnector on port " + sslPort + " for https"); - if (allowHttp) { - serverLogger.info("Starting httpConnector on port " + httpPort); - server.setConnectors(new Connector[]{httpConnector}); - } - } - } + } else { + serverLogger.info("NOT starting sslConnector on port " + sslPort + " for https"); + if (allowHttp) { + serverLogger.info("Starting httpConnector on port " + httpPort); + server.setConnectors(new Connector[]{httpConnector}); + } + } + } // Set context for servlet. This is shared for http and https - ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); - context.setContextPath("/"); - server.setHandler( context ); + ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); + context.setContextPath("/"); + server.setHandler(context); - ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/webapi/*"); + ServletHolder jerseyServlet = context + .addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/webapi/*"); jerseyServlet.setInitOrder(1); - jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "org.onap.dmaap.dbcapi.resources" ); - jerseyServlet.setInitParameter("javax.ws.rs.Application", "org.onap.dmaap.dbcapi.server.ApplicationConfig" ); - + jerseyServlet.setInitParameter("jersey.config.server.provider.packages", "org.onap.dmaap.dbcapi.resources"); + jerseyServlet.setInitParameter("javax.ws.rs.Application", "org.onap.dmaap.dbcapi.server.ApplicationConfig"); + // also serve up some static pages... - ServletHolder staticServlet = context.addServlet(DefaultServlet.class,"/*"); - staticServlet.setInitParameter("resourceBase","www"); - staticServlet.setInitParameter("pathInfoOnly","true"); + ServletHolder staticServlet = context.addServlet(DefaultServlet.class, "/*"); + staticServlet.setInitParameter("resourceBase", "www"); + staticServlet.setInitParameter("pathInfoOnly", "true"); + + registerAuthFilters(context); try { serverLogger.info("Starting jetty server"); - String unit_test = params.getProperty("UnitTest", "No"); + String unit_test = params.getProperty("UnitTest", "No"); serverLogger.info("UnitTest=" + unit_test); - if ( unit_test.equals( "No" ) ) { - server.start(); - server.dumpStdErr(); - server.join(); - } - } catch ( Exception e ) { - errorLogger.error( "Exception " + e ); + if (unit_test.equals("No")) { + server.start(); + server.dumpStdErr(); + server.join(); + } + } catch (Exception e) { + errorLogger.error("Exception " + e); } finally { - server.destroy(); + server.destroy(); } - + + } + + private void registerAuthFilters(ServletContextHandler context) { + context.addFilter("org.onap.dmaap.dbcapi.resources.AAFAuthenticationFilter", "/webapi/*", + Sets.newEnumSet(Sets.newHashSet(DispatcherType.FORWARD, DispatcherType.REQUEST), DispatcherType.class)); + context.addFilter("org.onap.dmaap.dbcapi.resources.AAFAuthorizationFilter", "/webapi/*", + Sets.newEnumSet(Sets.newHashSet(DispatcherType.FORWARD, DispatcherType.REQUEST), DispatcherType.class)); } - private void setUpKeystore(Properties params, SslContextFactory sslContextFactory) { - String keystore = params.getProperty("KeyStoreFile", "etc/keystore"); - logger.info("https Server using keystore at " + keystore); - sslContextFactory.setKeyStorePath(keystore); - sslContextFactory.setKeyStorePassword(params.getProperty("KeyStorePassword", "changeit")); - sslContextFactory.setKeyManagerPassword(params.getProperty("KeyPassword", "changeit")); - } - - private void setUpTrustStore(Properties params, SslContextFactory sslContextFactory) { - String truststore = params.getProperty("TrustStoreFile", "etc/org.onap.dmaap-bc.trust.jks"); - logger.info("https Server using truststore at " + truststore); - sslContextFactory.setTrustStorePath(truststore); - sslContextFactory.setTrustStoreType(params.getProperty("TrustStoreType", "jks")); - sslContextFactory.setTrustStorePassword(params.getProperty("TrustStorePassword", "changeit")); - } + private void setUpKeystore(Properties params, SslContextFactory sslContextFactory) { + String keystore = params.getProperty("KeyStoreFile", "etc/keystore"); + logger.info("https Server using keystore at " + keystore); + sslContextFactory.setKeyStorePath(keystore); + sslContextFactory.setKeyStorePassword(params.getProperty("KeyStorePassword", "changeit")); + sslContextFactory.setKeyManagerPassword(params.getProperty("KeyPassword", "changeit")); + } + + private void setUpTrustStore(Properties params, SslContextFactory sslContextFactory) { + String truststore = params.getProperty("TrustStoreFile", "etc/org.onap.dmaap-bc.trust.jks"); + logger.info("https Server using truststore at " + truststore); + sslContextFactory.setTrustStorePath(truststore); + sslContextFactory.setTrustStoreType(params.getProperty("TrustStoreType", "jks")); + sslContextFactory.setTrustStorePassword(params.getProperty("TrustStorePassword", "changeit")); + } } 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 35e61db..e1beb28 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/service/ApiService.java +++ b/src/main/java/org/onap/dmaap/dbcapi/service/ApiService.java @@ -100,25 +100,6 @@ public class ApiService extends BaseLoggingClass { return err; } - - public void setErr(ApiError err) { - this.err = err; - } - - public void setCode(int statusCode) { - err.setCode(statusCode); - } - - - public void setMessage(String string) { - err.setMessage(string); - } - - - public void setFields(String string) { - err.setFields(string); - } - public void checkAuthorization() throws Exception { MDC.put(MDC_KEY_REQUEST_ID, requestId); diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/DR_NodeService.java b/src/main/java/org/onap/dmaap/dbcapi/service/DR_NodeService.java index b478dca..9181154 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/service/DR_NodeService.java +++ b/src/main/java/org/onap/dmaap/dbcapi/service/DR_NodeService.java @@ -215,11 +215,11 @@ public class DR_NodeService extends BaseLoggingClass { } public DR_Node updateDr_Node( DR_Node node, ApiError apiError ) { - DR_Node old = dr_nodes.get( node ); + DR_Node old = dr_nodes.get( node.getFqdn() ); if ( old == null ) { apiError.setCode(Status.NOT_FOUND.getStatusCode()); apiError.setFields( "fqdn"); - apiError.setMessage( "Node " + node + " does not exist"); + apiError.setMessage( "Node " + node.getFqdn() + " does not exist"); return null; } node.setLastMod(); diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/DR_PubService.java b/src/main/java/org/onap/dmaap/dbcapi/service/DR_PubService.java index 13717da..eb5b514 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/service/DR_PubService.java +++ b/src/main/java/org/onap/dmaap/dbcapi/service/DR_PubService.java @@ -129,7 +129,11 @@ public class DR_PubService extends BaseLoggingClass{ } public DR_Pub removeDr_Pub( String pubId, ApiError err ) { - + return removeDr_Pub( pubId, err, true ); + } + + + public DR_Pub removeDr_Pub( String pubId, ApiError err, boolean hitDR ) { DR_Pub pub = dr_pubs.get( pubId ); if ( pub == null ) { err.setCode(Status.NOT_FOUND.getStatusCode()); 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 c14b1fd..0a583a0 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 @@ -173,6 +173,11 @@ public class DR_SubService extends BaseLoggingClass { } public void removeDr_Sub( String key, ApiError apiError ) { + removeDr_Sub( key, apiError, true ); + return; + } + + public void removeDr_Sub( String key, ApiError apiError, boolean hitDR ) { logger.debug( "enter removeDR_Subs()"); DR_Sub sub = dr_subs.get( key ); @@ -180,11 +185,15 @@ public class DR_SubService extends BaseLoggingClass { apiError.setCode(Status.NOT_FOUND.getStatusCode()); apiError.setFields( "subId"); apiError.setMessage("subId " + key + " not found"); - } else { - DrProvConnection prov = new DrProvConnection(); - prov.makeSubPutConnection( key ); - String resp = prov.doDeleteDr_Sub( sub, apiError ); - logger.debug( "resp=" + resp ); + } else { + if ( hitDR ) { + DrProvConnection prov = new DrProvConnection(); + prov.makeSubPutConnection( key ); + String resp = prov.doDeleteDr_Sub( sub, apiError ); + logger.debug( "resp=" + resp ); + } else { + apiError.setCode(200); + } if ( apiError.is2xx() || unit_test.equals( "Yes" ) ) { dr_subs.remove(key); 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 de18d95..77d11ab 100644 --- a/src/main/java/org/onap/dmaap/dbcapi/service/FeedService.java +++ b/src/main/java/org/onap/dmaap/dbcapi/service/FeedService.java @@ -25,12 +25,17 @@ 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.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; @@ -38,12 +43,14 @@ 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<String, Feed> feeds = DatabaseClass.getFeeds(); + private Map<String, DR_Sub> dr_subs = DatabaseClass.getDr_subs(); private DR_PubService pubService = new DR_PubService(); private DR_SubService subService = new DR_SubService(); private DcaeLocationService dcaeLocations = new DcaeLocationService(); @@ -68,6 +75,10 @@ public class FeedService extends BaseLoggingClass { ArrayList<DR_Sub> subs = subService.getDr_SubsByFeedId( f.getFeedId() ); f.setSubs(subs); } + + public List<Feed> getAllFeeds(){ + return getAllFeeds(null, null, null); + } public List<Feed> getAllFeeds( String name, String ver, String match ) { logger.info( "getAllFeeds: name=" + name + " ver=" + ver + " match=" + match); @@ -351,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<DR_Pub> 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<DR_Sub> 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(); @@ -415,6 +434,92 @@ public class FeedService extends BaseLoggingClass { } + + + /* + * 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<Feed> flist = new ArrayList<Feed>(this.getAllFeeds()); + for ( Iterator<Feed> 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"; diff --git a/src/main/java/org/onap/dmaap/dbcapi/service/StopWatch.java b/src/main/java/org/onap/dmaap/dbcapi/service/StopWatch.java deleted file mode 100644 index 6dc8fe9..0000000 --- a/src/main/java/org/onap/dmaap/dbcapi/service/StopWatch.java +++ /dev/null @@ -1,64 +0,0 @@ -/*-
- * ============LICENSE_START=======================================================
- * org.onap.dmaap
- * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ============LICENSE_END=========================================================
- */
-package org.onap.dmaap.dbcapi.service;
-
-import static com.att.eelf.configuration.Configuration.MDC_BEGIN_TIMESTAMP;
-import static com.att.eelf.configuration.Configuration.MDC_ELAPSED_TIME;
-import static com.att.eelf.configuration.Configuration.MDC_END_TIMESTAMP;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.TimeZone;
-import org.slf4j.MDC;
-
-
-public class StopWatch {
-
- private static final String ISO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
- private final static SimpleDateFormat isoFormatter = new SimpleDateFormat(ISO_FORMAT);
- private final static TimeZone utc = TimeZone.getTimeZone("UTC");
-
- private long startTimestamp;
- private long elapsedTime;
-
- static {
- isoFormatter.setTimeZone(utc);
- }
-
- public void start() {
- startTimestamp = System.currentTimeMillis();
- MDC.put(MDC_BEGIN_TIMESTAMP, isoFormatter.format(new Date(startTimestamp)));
- }
-
- public void stop() {
- long endTimestamp = System.currentTimeMillis();
- elapsedTime = endTimestamp - startTimestamp;
- MDC.put(MDC_END_TIMESTAMP, isoFormatter.format(new Date(endTimestamp)));
- MDC.put(MDC_ELAPSED_TIME, String.valueOf(elapsedTime));
- }
-
- public void resetElapsedTime() {
- elapsedTime = 0;
- }
-
- public long getElapsedTime() {
- return elapsedTime;
- }
-}
diff --git a/src/main/java/org/onap/dmaap/dbcapi/util/PermissionBuilder.java b/src/main/java/org/onap/dmaap/dbcapi/util/PermissionBuilder.java new file mode 100644 index 0000000..44c94af --- /dev/null +++ b/src/main/java/org/onap/dmaap/dbcapi/util/PermissionBuilder.java @@ -0,0 +1,86 @@ +/*- + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright (C) 2019 Nokia Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dmaap.dbcapi.util; + +import javax.servlet.http.HttpServletRequest; +import org.onap.dmaap.dbcapi.model.Dmaap; +import org.onap.dmaap.dbcapi.service.DmaapService; + +public class PermissionBuilder { + + static final String API_NS_PROP = "ApiNamespace"; + static final String DEFAULT_API_NS = "org.onap.dmaap-bc.api"; + static final String BOOT_INSTANCE = "boot"; + private static final String PERM_SEPARATOR = "|"; + private static final String NS_SEPARATOR = "."; + private DmaapConfig dmaapConfig; + private DmaapService dmaapService; + private String instance; + private String apiNamespace; + + public PermissionBuilder(DmaapConfig dmaapConfig, DmaapService dmaapService) { + this.dmaapConfig = dmaapConfig; + this.dmaapService = dmaapService; + initFields(); + } + + public synchronized void updateDmaapInstance() { + if(instance == null || instance.isEmpty() || instance.equalsIgnoreCase(BOOT_INSTANCE)) { + String dmaapName = getDmaapName(); + instance = (dmaapName == null || dmaapName.isEmpty()) ? BOOT_INSTANCE : dmaapName; + } + } + + public String buildPermission(HttpServletRequest httpRequest) { + + StringBuilder sb = new StringBuilder(apiNamespace); + sb.append(NS_SEPARATOR) + .append(getPermissionType(httpRequest.getPathInfo())) + .append(PERM_SEPARATOR) + .append(instance) + .append(PERM_SEPARATOR) + .append(httpRequest.getMethod()); + return sb.toString(); + } + + + private void initFields() { + apiNamespace = dmaapConfig.getProperty(API_NS_PROP, DEFAULT_API_NS); + updateDmaapInstance(); + } + + private String getDmaapName() { + Dmaap dmaap = dmaapService.getDmaap(); + return ( dmaap != null ) ? dmaap.getDmaapName() : BOOT_INSTANCE; + } + + private String getPermissionType(String pathInfo) { + char pathSeparator = '/'; + String relativePath = (pathInfo.charAt(pathInfo.length()-1) == pathSeparator) ? + pathInfo.substring(0,pathInfo.length()-1) : pathInfo; + + String[] pathSlices = relativePath.split(String.valueOf(pathSeparator)); + return pathSlices[pathSlices.length-1]; + } + + String getInstance() { + return instance; + } +} |