From 974b67dd4021e6e839eaad25366bffe6d7a414c8 Mon Sep 17 00:00:00 2001 From: Dan Timoney Date: Fri, 31 Mar 2017 15:03:13 -0400 Subject: [SDNC-5] Rebase sdnc-core Upgrade to OpenDaylight Boron release, and sync changes made since 16.10 release to ONAP SDN-C distribution Change-Id: I20bef9e6d0008c4436b5624ce839bbb70ecc20a5 Signed-off-by: Dan Timoney --- sli/common/pom.xml | 2 +- .../org/openecomp/sdnc/sli/BreakNodeException.java | 46 ++++ .../java/org/openecomp/sdnc/sli/MessageWriter.java | 302 +++++++++++++++++++++ .../openecomp/sdnc/sli/SvcLogicExprListener.java | 60 ++-- .../sdnc/sli/SvcLogicExpressionFactory.java | 2 +- 5 files changed, 380 insertions(+), 32 deletions(-) create mode 100644 sli/common/src/main/java/org/openecomp/sdnc/sli/BreakNodeException.java create mode 100644 sli/common/src/main/java/org/openecomp/sdnc/sli/MessageWriter.java (limited to 'sli/common') diff --git a/sli/common/pom.xml b/sli/common/pom.xml index 7a08c34..eb06e6b 100755 --- a/sli/common/pom.xml +++ b/sli/common/pom.xml @@ -44,7 +44,7 @@ org.opendaylight.mdsal yang-binding - ${odl.yangtools.version} + ${odl.mdsal.yang.binding.version} org.opendaylight.yangtools diff --git a/sli/common/src/main/java/org/openecomp/sdnc/sli/BreakNodeException.java b/sli/common/src/main/java/org/openecomp/sdnc/sli/BreakNodeException.java new file mode 100644 index 0000000..3e355ba --- /dev/null +++ b/sli/common/src/main/java/org/openecomp/sdnc/sli/BreakNodeException.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : SDN-C + * ================================================================================ + * Copyright (C) 2017 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.openecomp.sdnc.sli; + +public class BreakNodeException extends SvcLogicException { + + /** + * + */ + private static final long serialVersionUID = 1L; + + public BreakNodeException() + { + super(); + } + + public BreakNodeException(String message) + { + super(message); + } + + public BreakNodeException(String message, Throwable t) + { + super(message, t); + } + +} diff --git a/sli/common/src/main/java/org/openecomp/sdnc/sli/MessageWriter.java b/sli/common/src/main/java/org/openecomp/sdnc/sli/MessageWriter.java new file mode 100644 index 0000000..5e5b621 --- /dev/null +++ b/sli/common/src/main/java/org/openecomp/sdnc/sli/MessageWriter.java @@ -0,0 +1,302 @@ +/*- + * ============LICENSE_START======================================================= + * openECOMP : SDN-C + * ================================================================================ + * Copyright (C) 2017 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.openecomp.sdnc.sli; + +import java.io.File; +import java.io.FileInputStream; +import java.sql.SQLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.Properties; + +import javax.sql.rowset.CachedRowSet; + +import org.openecomp.sdnc.sli.resource.dblib.DbLibService; +import org.osgi.framework.BundleContext; +import org.osgi.framework.FrameworkUtil; +import org.osgi.framework.ServiceReference; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class MessageWriter { + + private static final Logger LOG = LoggerFactory.getLogger(MessageWriter.class); + + private static final String DBLIB_SERVICE = "org.openecomp.sdnc.sli.resource.dblib.DBResourceManager"; + private static final String SVCLOGIC_PROP_VAR = "SDNC_SLI_PROPERTIES"; + private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR"; + + private static final String INCOMING_PROPERTY_NAME = "org.openecomp.sdnc.sli.MessageWriter.writeIncomingRequests"; + private static final String OUTGOING_PROPERTY_NAME = "org.openecomp.sdnc.sli.MessageWriter.writeOutgoingRequests"; + + private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + + private static DbLibService dbLibService = null; + + private static boolean incomingEnabled = false; + private static boolean outgoingEnabled = false; + + private static boolean initialized = false; + + @SuppressWarnings({ "rawtypes", "unchecked" }) + private static void init() { + if (initialized) + return; + + initialized = true; + + // Read properties + Properties props = new Properties(); + String propPath = System.getenv(SVCLOGIC_PROP_VAR); + + if (propPath == null) { + String propDir = System.getenv(SDNC_CONFIG_DIR); + if (propDir == null) { + propDir = "/opt/sdnc/data/properties"; + } + propPath = propDir + "/svclogic.properties"; + LOG.warn("Environment variable " + SVCLOGIC_PROP_VAR + " unset - defaulting to " + propPath); + } + + File propFile = new File(propPath); + + if (!propFile.exists()) { + LOG.warn("Property file does not exist: " + propPath); + } + + try { + props.load(new FileInputStream(propFile)); + } catch (Exception e) { + LOG.warn("Error loading property file: " + propPath, e); + } + + incomingEnabled = Boolean.valueOf(props.getProperty(INCOMING_PROPERTY_NAME, "false")); + outgoingEnabled = Boolean.valueOf(props.getProperty(OUTGOING_PROPERTY_NAME, "false")); + + LOG.info(INCOMING_PROPERTY_NAME + ": " + incomingEnabled); + LOG.info(OUTGOING_PROPERTY_NAME + ": " + outgoingEnabled); + + if (dbLibService != null) + return; + + BundleContext bctx = FrameworkUtil.getBundle(MessageWriter.class).getBundleContext(); + + ServiceReference sref = bctx.getServiceReference(DBLIB_SERVICE); + + if (sref == null) { + LOG.warn("Could not find service reference for DBLIB service (" + DBLIB_SERVICE + ")"); + } else { + dbLibService = (DbLibService) bctx.getService(sref); + if (dbLibService == null) { + LOG.warn("Could not find service reference for DBLIB service (" + DBLIB_SERVICE + ")"); + } + } + } + + public static void saveOutgoingRequest( + String requestId, + String serviceInstanceId, + String targetUrl, + String request) { + try { + init(); + + if (!outgoingEnabled) + return; + + if (serviceInstanceId == null || serviceInstanceId.trim().length() == 0) + serviceInstanceId = "NA"; + + int seqnum = getLastSequenceNumber("OUTGOING_MESSAGE", requestId) + 1; + String now = df.format(new Date()); + + String sql = "INSERT INTO OUTGOING_MESSAGE (\n" + + " request_id, sequence_number, service_instance_id, target_url, request, start_time)\n" + + "VALUES (?, ?, ?, ?, ?, ?)"; + + ArrayList data = new ArrayList<>(); + data.add(requestId); + data.add(String.valueOf(seqnum)); + data.add(serviceInstanceId); + data.add(targetUrl); + data.add(request); + data.add(now); + + dbLibService.writeData(sql, data, null); + + } catch (Exception e) { + LOG.warn("Failed to save outgoing request for request-id: " + requestId, e); + } + } + + public static void saveOutgoingResponse(String requestId, int httpResponseCode, String response) { + try { + init(); + + if (!outgoingEnabled) + return; + + int seqnum = getLastSequenceNumber("OUTGOING_MESSAGE", requestId); + if (seqnum == 0) { + LOG.warn("Failed to save outgoing response for request-id: " + requestId + + ": Request record not found in OUTGOING_MESSAGE"); + return; + } + + String now = df.format(new Date()); + + String sql = "UPDATE OUTGOING_MESSAGE SET http_response_code = ?, response = ?,\n" + + " duration = timestampdiff(MICROSECOND, start_time, ?) / 1000\n" + + "WHERE request_id = ? AND sequence_number = ?"; + + ArrayList data = new ArrayList<>(); + data.add(String.valueOf(httpResponseCode)); + data.add(response); + data.add(now); + data.add(requestId); + data.add(String.valueOf(seqnum)); + + dbLibService.writeData(sql, data, null); + + } catch (Exception e) { + LOG.warn("Failed to save outgoing response for request-id: " + requestId, e); + } + } + + public static void saveIncomingRequest( + String requestId, + String serviceInstanceId, + String requestHost, + String request) { + try { + init(); + + if (!incomingEnabled) + return; + + if (serviceInstanceId == null || serviceInstanceId.trim().length() == 0) + serviceInstanceId = "NA"; + + int seqnum = getLastSequenceNumber("INCOMING_MESSAGE", requestId) + 1; + String now = df.format(new Date()); + + String sql = "INSERT INTO INCOMING_MESSAGE (\n" + + " request_id, sequence_number, service_instance_id, request_host, request, start_time)\n" + + "VALUES (?, ?, ?, ?, ?, ?)"; + + ArrayList data = new ArrayList<>(); + data.add(requestId); + data.add(String.valueOf(seqnum)); + data.add(serviceInstanceId); + data.add(requestHost); + data.add(request); + data.add(now); + + dbLibService.writeData(sql, data, null); + + } catch (Exception e) { + LOG.warn("Failed to save incoming request for request-id: " + requestId, e); + } + } + + public static void saveIncomingResponse(String requestId, int httpResponseCode, String response) { + try { + init(); + + if (!incomingEnabled) + return; + + int seqnum = getLastSequenceNumber("INCOMING_MESSAGE", requestId); + if (seqnum == 0) { + LOG.warn("Failed to save response for request-id: " + requestId + + ": Request record not found in INCOMING_MESSAGE"); + return; + } + + String now = df.format(new Date()); + + String sql = "UPDATE INCOMING_MESSAGE SET http_response_code = ?, response = ?,\n" + + " duration = timestampdiff(MICROSECOND, start_time, ?) / 1000\n" + + "WHERE request_id = ? AND sequence_number = ?"; + + ArrayList data = new ArrayList<>(); + data.add(String.valueOf(httpResponseCode)); + data.add(response); + data.add(now); + data.add(requestId); + data.add(String.valueOf(seqnum)); + + dbLibService.writeData(sql, data, null); + + } catch (Exception e) { + LOG.warn("Failed to save response for request-id: " + requestId, e); + } + } + + public static String getServiceInstanceId(String requestId) throws SQLException { + init(); + + String sql = "SELECT service_instance_id FROM OUTGOING_MESSAGE WHERE request_id = '" + requestId + + "' ORDER BY sequence_number DESC"; + + CachedRowSet rs = null; + try { + rs = dbLibService.getData(sql, null, null); + if (rs.next()) { + return rs.getString("service_instance_id"); + } + } finally { + if (rs != null) { + try { + rs.close(); + } catch (Exception e) { + LOG.warn("Failed to close CachedRowSet", e); + } + } + } + return null; + } + + private static int getLastSequenceNumber(String tableName, String requestId) throws SQLException { + String sql = "SELECT sequence_number FROM " + tableName + " WHERE request_id = '" + requestId + + "' ORDER BY sequence_number DESC"; + + CachedRowSet rs = null; + try { + rs = dbLibService.getData(sql, null, null); + if (rs.next()) { + return rs.getInt("sequence_number"); + } + } finally { + if (rs != null) { + try { + rs.close(); + } catch (Exception e) { + LOG.warn("Failed to close CachedRowSet", e); + } + } + } + return 0; + } +} diff --git a/sli/common/src/main/java/org/openecomp/sdnc/sli/SvcLogicExprListener.java b/sli/common/src/main/java/org/openecomp/sdnc/sli/SvcLogicExprListener.java index d8d3384..4fef12f 100644 --- a/sli/common/src/main/java/org/openecomp/sdnc/sli/SvcLogicExprListener.java +++ b/sli/common/src/main/java/org/openecomp/sdnc/sli/SvcLogicExprListener.java @@ -77,7 +77,7 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener private void pushExpr(SvcLogicExpression expr) { - LOG.debug("Pushing expression ["+expr.getClass().getName()+"]"); + LOG.trace("Pushing expression ["+expr.getClass().getName()+"]"); if (curExpr != null) { exprStack.push(curExpr); @@ -89,7 +89,7 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener { if (exprStack.isEmpty()) { - LOG.debug("Popping last expression"); + LOG.trace("Popping last expression"); topExpr = curExpr; } else @@ -97,7 +97,7 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener SvcLogicExpression lastExpr = curExpr; curExpr = exprStack.pop(); curExpr.addOperand(lastExpr); - LOG.debug("New curExpr is ["+curExpr.getClass().getName()+"]"); + LOG.trace("New curExpr is ["+curExpr.getClass().getName()+"]"); } } @@ -107,7 +107,7 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener String atomText = ctx.getText(); - LOG.debug("enterAtom: text = "+atomText); + LOG.trace("enterAtom: text = "+atomText); SvcLogicAtom newAtom = new SvcLogicAtom(atomText); @@ -118,7 +118,7 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener @Override public void enterMultExpr(MultExprContext ctx) { - LOG.debug("enterMultExpr: text = "+ctx.getText()); + LOG.trace("enterMultExpr: text = "+ctx.getText()); SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression(); pushExpr(curBinExpr); @@ -127,7 +127,7 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener for (TerminalNode nd : opList) { - LOG.debug("enterMultExpr: operator - "+nd.getText()); + LOG.trace("enterMultExpr: operator - "+nd.getText()); curBinExpr.addOperator(nd.getText()); } @@ -136,7 +136,7 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener @Override public void exitMultExpr(MultExprContext ctx) { - LOG.debug("exitMultExpr: text = "+ctx.getText()); + LOG.trace("exitMultExpr: text = "+ctx.getText()); popExpr(); @@ -144,13 +144,13 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener @Override public void exitAtom(AtomContext ctx) { - LOG.debug("exitAtom: text = "+ctx.getText()); + LOG.trace("exitAtom: text = "+ctx.getText()); popExpr(); } @Override public void enterAddExpr(AddExprContext ctx) { - LOG.debug("enterAddExpr: text = "+ctx.getText()); + LOG.trace("enterAddExpr: text = "+ctx.getText()); List opList = ctx.ADDOP(); @@ -160,7 +160,7 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener for (TerminalNode nd : opList) { - LOG.debug("enterAddExpr: operator - "+nd.getText()); + LOG.trace("enterAddExpr: operator - "+nd.getText()); curBinExpr.addOperator(nd.getText()); } @@ -168,19 +168,19 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener @Override public void exitAddExpr(AddExprContext ctx) { - LOG.debug("exitAddExpr: text = "+ctx.getText()); + LOG.trace("exitAddExpr: text = "+ctx.getText()); popExpr(); } @Override public void enterFuncExpr(FuncExprContext ctx) { - LOG.debug("enterFuncExpr: text = "+ctx.getText()); - LOG.debug("enterFuncExpr - IDENTIFIER : "+ctx.IDENTIFIER().getText()); + LOG.trace("enterFuncExpr: text = "+ctx.getText()); + LOG.trace("enterFuncExpr - IDENTIFIER : "+ctx.IDENTIFIER().getText()); for (ExprContext expr: ctx.expr()) { - LOG.debug("enterFuncExpr - expr = "+expr.getText()); + LOG.trace("enterFuncExpr - expr = "+expr.getText()); } @@ -189,25 +189,25 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener @Override public void exitFuncExpr(FuncExprContext ctx) { - LOG.debug("exitFuncExpr: text = "+ctx.getText()); + LOG.trace("exitFuncExpr: text = "+ctx.getText()); popExpr(); } @Override public void enterParenExpr(ParenExprContext ctx) { - LOG.debug("enterParenExpr: text = "+ctx.getText()); - LOG.debug("enterParenExpr: expr = "+ctx.expr().getText()); + LOG.trace("enterParenExpr: text = "+ctx.getText()); + LOG.trace("enterParenExpr: expr = "+ctx.expr().getText()); } @Override public void exitParenExpr(ParenExprContext ctx) { - LOG.debug("exitParenExpr: text = "+ctx.getText()); + LOG.trace("exitParenExpr: text = "+ctx.getText()); } @Override public void enterRelExpr(RelExprContext ctx) { - LOG.debug("enterRelExpr: text = "+ctx.getText()); + LOG.trace("enterRelExpr: text = "+ctx.getText()); List opList = ctx.RELOP(); @@ -218,7 +218,7 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener for (TerminalNode nd : opList) { - LOG.debug("enterRelExpr: operator - "+nd.getText()); + LOG.trace("enterRelExpr: operator - "+nd.getText()); curBinExpr.addOperator(nd.getText()); } @@ -226,28 +226,28 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener @Override public void exitRelExpr(RelExprContext ctx) { - LOG.debug("exitRelExpr: text = "+ctx.getText()); + LOG.trace("exitRelExpr: text = "+ctx.getText()); popExpr(); } @Override public void enterCompareExpr(CompareExprContext ctx) { - LOG.debug("enterCompareExpr: text = "+ctx.getText()); + LOG.trace("enterCompareExpr: text = "+ctx.getText()); TerminalNode nd = ctx.COMPAREOP(); SvcLogicBinaryExpression curBinExpr = new SvcLogicBinaryExpression(); pushExpr(curBinExpr); - LOG.debug("enterCompareExpr: operator - "+nd.getText()); + LOG.trace("enterCompareExpr: operator - "+nd.getText()); curBinExpr.addOperator(nd.getText()); } @Override public void exitCompareExpr(CompareExprContext ctx) { - LOG.debug("exitCompareExpr : text = "+ctx.getText()); + LOG.trace("exitCompareExpr : text = "+ctx.getText()); popExpr(); } @@ -256,18 +256,18 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener @Override public void enterConstant(ConstantContext ctx) { - LOG.debug("enterConstant: text = "+ctx.getText()); + LOG.trace("enterConstant: text = "+ctx.getText()); } @Override public void exitConstant(ConstantContext ctx) { - LOG.debug("exitConstant: text = "+ctx.getText()); + LOG.trace("exitConstant: text = "+ctx.getText()); } @Override public void enterVariable(VariableContext ctx) { - LOG.debug("enterVariable: text = "+ctx.getText()); + LOG.trace("enterVariable: text = "+ctx.getText()); } @@ -290,12 +290,12 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener @Override public void exitVariableLead(VariableLeadContext ctx) { - LOG.debug("exitVariableLead: text ="+ctx.getText()); + LOG.trace("exitVariableLead: text ="+ctx.getText()); } @Override public void enterVariableTerm(VariableTermContext ctx) { - LOG.debug("enterVariableTerm: text ="+ctx.getText()); + LOG.trace("enterVariableTerm: text ="+ctx.getText()); String name = ctx.getText(); @@ -310,7 +310,7 @@ public class SvcLogicExprListener extends ExprGrammarBaseListener @Override public void exitVariableTerm(VariableTermContext ctx) { - LOG.debug("exitVariableTerm: text="+ctx.getText()); + LOG.trace("exitVariableTerm: text="+ctx.getText()); popExpr(); } } diff --git a/sli/common/src/main/java/org/openecomp/sdnc/sli/SvcLogicExpressionFactory.java b/sli/common/src/main/java/org/openecomp/sdnc/sli/SvcLogicExpressionFactory.java index 751564b..cce8e04 100644 --- a/sli/common/src/main/java/org/openecomp/sdnc/sli/SvcLogicExpressionFactory.java +++ b/sli/common/src/main/java/org/openecomp/sdnc/sli/SvcLogicExpressionFactory.java @@ -42,7 +42,7 @@ public class SvcLogicExpressionFactory { public static SvcLogicExpression parse(String exprStr) throws IOException { - LOG.debug("parse("+exprStr+")"); + LOG.trace("parse("+exprStr+")"); InputStream exprStream = new ByteArrayInputStream(exprStr.getBytes()); CharStream input = new ANTLRInputStream(exprStream); ExprGrammarLexer lexer = new ExprGrammarLexer(input); -- cgit