diff options
Diffstat (limited to 'mdsal-resource/provider')
3 files changed, 106 insertions, 176 deletions
diff --git a/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/ConfigResource.java b/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/ConfigResource.java index 66604468..e2e9bc23 100644 --- a/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/ConfigResource.java +++ b/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/ConfigResource.java @@ -39,8 +39,8 @@ public class ConfigResource implements SvcLogicResource { private RestService restService; public ConfigResource(MdsalResourcePropertiesProvider propProvider) { - LOG.info("Loading ConfigResource using property provider"); - Properties props = propProvider.getProperties(); + LOG.info("Loading ConfigResource using property provider"); + Properties props = propProvider.getProperties(); String sdncUser = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-user", "admin"); String sdncPasswd = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-passwd", "admin"); @@ -48,17 +48,16 @@ public class ConfigResource implements SvcLogicResource { String sdncProtocol = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-protocol", "https"); String sdncPort = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-port", "8443"); - restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, RestService.PayloadType.XML); - + restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, "XML", "XML"); } - + public ConfigResource(String sdncProtocol, String sdncHost, String sdncPort, String sdncUser, String sdncPasswd) { - restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, RestService.PayloadType.XML); + restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, "XML", "XML"); } public ConfigResource(RestService restService) { - this.restService = restService; + this.restService = restService; } @Override @@ -70,27 +69,21 @@ public class ConfigResource implements SvcLogicResource { @Override public QueryStatus exists(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException { - return(query(resource, false, null, key, prefix, null, null)); - } @Override public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix, - String orderBy, SvcLogicContext ctx) throws SvcLogicException { - - + String orderBy, SvcLogicContext ctx) throws SvcLogicException { String module = resource; StringBuffer restQuery = new StringBuffer(); String[] keyParts = key.split("/"); - for (String keyPart : keyParts) { if (restQuery.length() > 0) { restQuery.append("/"); } if (keyPart.startsWith("$")) { - restQuery.append(ctx.resolve(keyPart.substring(1))); } else { restQuery.append(keyPart); @@ -99,41 +92,33 @@ public class ConfigResource implements SvcLogicResource { String restQueryStr = restQuery.toString(); if ((restQueryStr.startsWith("'") && restQueryStr.endsWith("'")) || - (restQueryStr.startsWith("\"") && restQueryStr.endsWith("\""))) { + (restQueryStr.startsWith("\"") && restQueryStr.endsWith("\""))) { restQueryStr = restQueryStr.substring(1, restQueryStr.length()-1); } String urlString = "restconf/config/" + module + ":" + restQueryStr; - - LOG.info("Querying resource: " + resource + ". At URL: " + urlString); + LOG.info("Querying resource: " + resource + ". At URL: " + urlString); Document results = restService.get(urlString); - - if (results == null) { return(QueryStatus.NOT_FOUND); } else { - if (ctx != null) { ctx.mergeDocument(prefix, results); } return(QueryStatus.SUCCESS); } - } @Override public QueryStatus reserve(String resource, String select, String key, String prefix, - SvcLogicContext ctx) throws SvcLogicException { - - + SvcLogicContext ctx) throws SvcLogicException { return(QueryStatus.SUCCESS); } @Override public QueryStatus release(String resource, String key, SvcLogicContext ctx) throws SvcLogicException { - return(QueryStatus.SUCCESS); } @@ -146,7 +131,7 @@ public class ConfigResource implements SvcLogicResource { @Override public QueryStatus save(String arg0, boolean arg1, boolean localOnly, String arg2, - Map<String, String> arg3, String arg4, SvcLogicContext arg5) + Map<String, String> arg3, String arg4, SvcLogicContext arg5) throws SvcLogicException { // TODO Auto-generated method stub return(QueryStatus.SUCCESS); @@ -154,17 +139,15 @@ public class ConfigResource implements SvcLogicResource { @Override public QueryStatus notify(String resource, String action, String key, - SvcLogicContext ctx) throws SvcLogicException { + SvcLogicContext ctx) throws SvcLogicException { return(QueryStatus.SUCCESS); } public QueryStatus update(String resource, String key, - Map<String, String> parms, String prefix, SvcLogicContext ctx) + Map<String, String> parms, String prefix, SvcLogicContext ctx) throws SvcLogicException { return(QueryStatus.SUCCESS); } - - } diff --git a/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/OperationalResource.java b/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/OperationalResource.java index 7cd8a80a..32087812 100644 --- a/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/OperationalResource.java +++ b/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/OperationalResource.java @@ -21,17 +21,16 @@ package org.onap.ccsdk.sli.adaptors.resource.mdsal; -import java.util.Map; -import java.util.Properties; - import org.onap.ccsdk.sli.core.sli.SvcLogicContext; import org.onap.ccsdk.sli.core.sli.SvcLogicException; import org.onap.ccsdk.sli.core.sli.SvcLogicResource; -import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; +import java.util.Map; +import java.util.Properties; + public class OperationalResource implements SvcLogicResource { private static final Logger LOG = LoggerFactory.getLogger(OperationalResource.class); @@ -39,7 +38,7 @@ public class OperationalResource implements SvcLogicResource { private RestService restService; public OperationalResource(MdsalResourcePropertiesProvider propProvider) { - Properties props = propProvider.getProperties(); + Properties props = propProvider.getProperties(); String sdncUser = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-user", "admin"); String sdncPasswd = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-passwd", "admin"); @@ -47,51 +46,39 @@ public class OperationalResource implements SvcLogicResource { String sdncProtocol = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-protocol", "https"); String sdncPort = props.getProperty("org.onap.ccsdk.sli.adaptors.resource.mdsal.sdnc-port", "8443"); - restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, RestService.PayloadType.XML); - + restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, "XML", "XML"); } - - public OperationalResource(String sdncProtocol, String sdncHost, String sdncPort, String sdncUser, String sdncPasswd) - { - restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, RestService.PayloadType.XML); - + public OperationalResource(String sdncProtocol, String sdncHost, String sdncPort, String sdncUser, String sdncPasswd) { + restService = new RestService(sdncProtocol, sdncHost, sdncPort, sdncUser, sdncPasswd, "XML", "XML"); } public OperationalResource(RestService restService) { - this.restService = restService; + this.restService = restService; } @Override - public QueryStatus isAvailable(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException - { - return(query(resource, false, null, key, prefix, null, null)); + public QueryStatus isAvailable(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException { + return (query(resource, false, null, key, prefix, null, null)); } @Override - public QueryStatus exists(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException - { - - return(query(resource, false, null, key, prefix, null, null)); - + public QueryStatus exists(String resource, String key, String prefix, SvcLogicContext ctx) throws SvcLogicException { + return (query(resource, false, null, key, prefix, null, null)); } @Override public QueryStatus query(String resource, boolean localOnly, String select, String key, String prefix, - String orderBy, SvcLogicContext ctx) throws SvcLogicException { - - + String orderBy, SvcLogicContext ctx) throws SvcLogicException { String module = resource; StringBuffer restQuery = new StringBuffer(); String[] keyParts = key.split("/"); - for (String keyPart : keyParts) { if (restQuery.length() > 0) { restQuery.append("/"); } if (keyPart.startsWith("$")) { - restQuery.append(ctx.resolve(keyPart.substring(1))); } else { restQuery.append(keyPart); @@ -100,70 +87,61 @@ public class OperationalResource implements SvcLogicResource { String restQueryStr = restQuery.toString(); if ((restQueryStr.startsWith("'") && restQueryStr.endsWith("'")) || - (restQueryStr.startsWith("\"") && restQueryStr.endsWith("\""))) { - restQueryStr = restQueryStr.substring(1, restQueryStr.length()-1); + (restQueryStr.startsWith("\"") && restQueryStr.endsWith("\""))) { + restQueryStr = restQueryStr.substring(1, restQueryStr.length() - 1); } String urlString = "restconf/operational/" + module + ":" + restQueryStr; - - LOG.info("Querying resource: " + resource + ". At URL: " + urlString); + LOG.info("Querying resource: " + resource + ". At URL: " + urlString); Document results = restService.get(urlString); - if (results == null) { - return(QueryStatus.NOT_FOUND); + return (QueryStatus.NOT_FOUND); } else { - if (ctx != null) { ctx.mergeDocument(prefix, results); } - return(QueryStatus.SUCCESS); + return (QueryStatus.SUCCESS); } - } @Override public QueryStatus reserve(String resource, String select, String key, String prefix, - SvcLogicContext ctx) throws SvcLogicException { - - - return(QueryStatus.SUCCESS); - + SvcLogicContext ctx) throws SvcLogicException { + return (QueryStatus.SUCCESS); } @Override public QueryStatus release(String resource, String key, SvcLogicContext ctx) throws SvcLogicException { - - return(QueryStatus.SUCCESS); + return (QueryStatus.SUCCESS); } @Override public QueryStatus delete(String arg0, String arg1, SvcLogicContext arg2) throws SvcLogicException { // TODO Auto-generated method stub - return(QueryStatus.SUCCESS); + return (QueryStatus.SUCCESS); } @Override public QueryStatus save(String arg0, boolean arg1, boolean localOnly, String arg2, - Map<String, String> arg3, String arg4, SvcLogicContext arg5) + Map<String, String> arg3, String arg4, SvcLogicContext arg5) throws SvcLogicException { // TODO Auto-generated method stub - return(QueryStatus.SUCCESS); + return (QueryStatus.SUCCESS); } @Override public QueryStatus notify(String resource, String action, String key, - SvcLogicContext ctx) throws SvcLogicException { - return(QueryStatus.SUCCESS); + SvcLogicContext ctx) throws SvcLogicException { + return (QueryStatus.SUCCESS); } public QueryStatus update(String resource, String key, - Map<String, String> parms, String prefix, SvcLogicContext ctx) + Map<String, String> parms, String prefix, SvcLogicContext ctx) throws SvcLogicException { - return(QueryStatus.SUCCESS); + return (QueryStatus.SUCCESS); } - } diff --git a/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/RestService.java b/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/RestService.java index 30859a49..ebddc24a 100644 --- a/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/RestService.java +++ b/mdsal-resource/provider/src/main/java/org/onap/ccsdk/sli/adaptors/resource/mdsal/RestService.java @@ -8,9 +8,9 @@ * 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. @@ -21,131 +21,91 @@ package org.onap.ccsdk.sli.adaptors.resource.mdsal; -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.Authenticator; -import java.net.HttpURLConnection; -import java.net.PasswordAuthentication; -import java.net.URL; +import org.apache.commons.codec.binary.Base64; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLSession; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; - -import org.apache.commons.codec.binary.Base64; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; - - - +import java.io.*; +import java.net.Authenticator; +import java.net.HttpURLConnection; +import java.net.PasswordAuthentication; +import java.net.URL; public class RestService { private static final Logger LOG = LoggerFactory.getLogger(ConfigResource.class); - - public enum PayloadType { - XML, - JSON - } - - private class SdncAuthenticator extends Authenticator { - - private String user; - private String passwd; - - SdncAuthenticator(String user, String passwd) { - this.user = user; - this.passwd = passwd; - } - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(user, passwd.toCharArray()); - } - - } - private String user; private String passwd; - private PayloadType payloadType; - + private String contentType; + private String accept; private String protocol; private String host; private String port; - public RestService(String protocol, String host, String port, String user, String passwd, PayloadType payloadType) { + public RestService(String protocol, String host, String port, String user, String passwd, String accept, String contentType) { this.protocol = protocol; this.host = host; this.port = port; this.user = user; this.passwd = passwd; - this.payloadType = payloadType; + this.accept = accept; + this.contentType = contentType; } - private HttpURLConnection getRestConnection(String urlString, String method) throws IOException - { - + private HttpURLConnection getRestConnection(String urlString, String method) throws IOException { URL sdncUrl = new URL(urlString); Authenticator.setDefault(new SdncAuthenticator(user, passwd)); - HttpURLConnection conn = (HttpURLConnection) sdncUrl.openConnection(); - - String authStr = user+":"+passwd; + String authStr = user + ":" + passwd; String encodedAuthStr = new String(Base64.encodeBase64(authStr.getBytes())); - conn.addRequestProperty("Authentication", "Basic "+encodedAuthStr); - + HttpURLConnection conn = (HttpURLConnection) sdncUrl.openConnection(); + conn.addRequestProperty("Authentication", "Basic " + encodedAuthStr); conn.setRequestMethod(method); + conn.setDoInput(true); + conn.setDoOutput(true); + conn.setUseCaches(false); - if (payloadType == PayloadType.XML) { - conn.setRequestProperty("Content-Type", "application/xml"); + //Setting Accept header (doesn't dependent on Msg Body if present or not) + if ("XML".equalsIgnoreCase(accept)) { conn.setRequestProperty("Accept", "application/xml"); } else { - - conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Accept", "application/json"); } - conn.setDoInput(true); - conn.setDoOutput(true); - conn.setUseCaches(false); - - return(conn); - + return (conn); } - private Document send(String urlString, byte[] msgBytes, String method) { Document response = null; String fullUrl = protocol + "://" + host + ":" + port + "/" + urlString; - LOG.info("Sending REST "+method +" to "+fullUrl); - - if (msgBytes != null) { - LOG.info("Message body:\n"+msgBytes); - } + LOG.info("Sending REST {} to {}", method, fullUrl); try { HttpURLConnection conn = getRestConnection(fullUrl, method); - if (conn instanceof HttpsURLConnection) { - HostnameVerifier hostnameVerifier = new HostnameVerifier() { - @Override - public boolean verify(String hostname, SSLSession session) { - return true; - } - }; - ((HttpsURLConnection)conn).setHostnameVerifier(hostnameVerifier); + HostnameVerifier hostnameVerifier = (hostname, session) -> true; + ((HttpsURLConnection) conn).setHostnameVerifier(hostnameVerifier); } // Write message if (msgBytes != null) { - conn.setRequestProperty("Content-Length", ""+msgBytes.length); + LOG.info("Message body:\n{}", msgBytes); + conn.setRequestProperty("Content-Length", "" + msgBytes.length); + + // Setting Content-Type header only if Msg Body is present + if ("XML".equalsIgnoreCase(contentType)) { + conn.setRequestProperty("Content-Type", "application/xml"); + } else { + conn.setRequestProperty("Content-Type", "application/json"); + } + DataOutputStream outStr = new DataOutputStream(conn.getOutputStream()); outStr.write(msgBytes); outStr.close(); @@ -153,32 +113,25 @@ public class RestService { conn.setRequestProperty("Content-Length", "0"); } - // Read response - BufferedReader respRdr; - - LOG.info("Response: "+conn.getResponseCode()+" "+conn.getResponseMessage()); - + LOG.info("Response: {} {}", conn.getResponseCode(), conn.getResponseMessage()); + BufferedReader respRdr; if (conn.getResponseCode() < 300) { - respRdr = new BufferedReader(new InputStreamReader(conn.getInputStream())); } else { respRdr = new BufferedReader(new InputStreamReader(conn.getErrorStream())); } StringBuffer respBuff = new StringBuffer(); - String respLn; - while ((respLn = respRdr.readLine()) != null) { - respBuff.append(respLn+"\n"); + respBuff.append(respLn + "\n"); } respRdr.close(); String respString = respBuff.toString(); - - LOG.info("Response body :\n"+respString); + LOG.info("Response body :\n{}", respString); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); @@ -189,27 +142,43 @@ public class RestService { response = db.parse(new ByteArrayInputStream(respString.getBytes())); } catch (Exception e) { - LOG.error("Caught exception executing REST command", e); } return (response); } - public Document get(String urlString) { - return(send(urlString, null, "GET")); + return (send(urlString, null, "GET")); } public Document delete(String urlString) { - return(send(urlString, null, "DELETE")); + return (send(urlString, null, "DELETE")); } public Document post(String urlString, byte[] msgBytes) { - return(send(urlString, msgBytes, "POST")); + return (send(urlString, msgBytes, "POST")); } public Document put(String urlString, byte[] msgBytes) { - return(send(urlString, msgBytes, "PUT")); + return (send(urlString, msgBytes, "PUT")); + } + + + private class SdncAuthenticator extends Authenticator { + private String user; + private String passwd; + + SdncAuthenticator(String user, String passwd) { + this.user = user; + this.passwd = passwd; + } + + @Override + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(user, passwd.toCharArray()); + } + } + } |