diff options
Diffstat (limited to 'sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java')
-rw-r--r-- | sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java | 516 |
1 files changed, 250 insertions, 266 deletions
diff --git a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java index a33ba476..ee260433 100644 --- a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java +++ b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java @@ -21,32 +21,29 @@ package org.onap.ccsdk.sli.core.sli; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.LinkedList; +import javax.xml.XMLConstants; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.Attributes; import org.xml.sax.Locator; import org.xml.sax.SAXException; -import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXParseException; import org.xml.sax.helpers.DefaultHandler; -import javax.xml.XMLConstants; -import javax.xml.parsers.SAXParser; -import javax.xml.parsers.SAXParserFactory; -import javax.xml.validation.Schema; -import javax.xml.validation.SchemaFactory; -import java.io.File; -import java.io.InputStream; -import java.net.URL; -import java.util.LinkedList; - /** * @author dt5972 * */ public class SvcLogicParser { - private SvcLogicStore store = null; static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; @@ -64,282 +61,247 @@ public class SvcLogicParser { private static final String SVCLOGIC_XSD = "/svclogic.xsd"; private class SvcLogicHandler extends DefaultHandler { - private Locator locator = null; - private String module = null; - private String version = null; - private LinkedList<SvcLogicGraph> graphs = null; - private SvcLogicGraph curGraph = null; - private SvcLogicNode curNode = null; - private LinkedList<SvcLogicNode> nodeStack = null; - private int curNodeId = 0; - private String outcomeValue = null; - private LinkedList<String> outcomeStack = null; - private SvcLogicStore svcLogicStore = null; - - public SvcLogicHandler(LinkedList<SvcLogicGraph> graphs, SvcLogicStore store) { - this.graphs = graphs; - this.curNode = null; - this.nodeStack = new LinkedList<>(); - this.outcomeStack = new LinkedList<>(); - this.curNodeId = 1; - this.outcomeValue = null; - this.svcLogicStore = store; + private Locator locator = null; + private String module = null; + private String version = null; + private LinkedList<SvcLogicGraph> graphs = null; + private SvcLogicGraph curGraph = null; + private SvcLogicNode curNode = null; + private LinkedList<SvcLogicNode> nodeStack = null; + private int curNodeId = 0; + private String outcomeValue = null; + private LinkedList<String> outcomeStack = null; + + public SvcLogicHandler(LinkedList<SvcLogicGraph> graphs) { + this.graphs = graphs; + this.curNode = null; + this.nodeStack = new LinkedList<>(); + this.outcomeStack = new LinkedList<>(); + this.curNodeId = 1; + this.outcomeValue = null; + } - } + @Override + public void setDocumentLocator(Locator locator) { + this.locator = locator; + } - @Override - public void setDocumentLocator(Locator locator) { - this.locator = locator; - } + @Override + public void startElement(String uri, String localName, String qName, Attributes attributes) + throws SAXException { - @Override - public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { + // Handle service-logic (graph) tag + if ("service-logic".equalsIgnoreCase(qName)) { - // Handle service-logic (graph) tag - if ("service-logic".equalsIgnoreCase(qName)) { + module = attributes.getValue("module"); + if (module == null || module.length() == 0) { + throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " + + "Missing 'module' attribute from service-logic tag"); + } - module = attributes.getValue("module"); - if (module == null || module.length() == 0) { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + "Missing 'module' attribute from service-logic tag"); - } + version = attributes.getValue("version"); + if (version == null || version.length() == 0) { + throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " + + "Missing 'version' attribute from service-logic tag"); + } - version = attributes.getValue("version"); - if (version == null || version.length() == 0) { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + "Missing 'version' attribute from service-logic tag"); + return; } - return; - } + if ("method".equalsIgnoreCase(qName)) { - if ("method".equalsIgnoreCase(qName)) { + if (curGraph != null) { + throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " + + "Cannot nest module tags"); + } - if (curGraph != null) { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + "Cannot nest module tags"); - } + curGraph = new SvcLogicGraph(); + curGraph.setModule(module); + curGraph.setVersion(version); + this.curNodeId = 1; - curGraph = new SvcLogicGraph(); - curGraph.setModule(module); - curGraph.setVersion(version); - this.curNodeId = 1; + String attrValue = attributes.getValue("rpc"); + if (attrValue == null || attrValue.length() == 0) { + throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " + + "Missing 'rpc' attribute for method tag"); + } + curGraph.setRpc(attrValue); - String attrValue = attributes.getValue("rpc"); - if (attrValue == null || attrValue.length() == 0) { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + "Missing 'rpc' attribute for method tag"); - } - curGraph.setRpc(attrValue); + attrValue = attributes.getValue("mode"); + if (attrValue == null || attrValue.length() == 0) { + throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " + + "Missing 'mode' attribute for method tag"); + } + curGraph.setMode(attrValue); - attrValue = attributes.getValue("mode"); - if (attrValue == null || attrValue.length() == 0) { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + "Missing 'mode' attribute for method tag"); + return; } - curGraph.setMode(attrValue); - return; - } + // Handle outcome (edge) tag + if ("outcome".equalsIgnoreCase(qName)) { + String refValue = attributes.getValue("ref"); + + if (refValue != null) { + SvcLogicNode refNode = curGraph.getNamedNode(refValue); + + if (refNode != null) { + try { + curNode.addOutcome(attributes.getValue("value"), refNode); + } catch (SvcLogicException e) { + throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + + " " + "Cannot add outcome", e); + } + } else { + throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " + + "ref to unknown node " + refValue); + } + return; + } + + if (outcomeValue != null) { + outcomeStack.push(outcomeValue); + } + outcomeValue = attributes.getValue("value"); - // Handle outcome (edge) tag - if ("outcome".equalsIgnoreCase(qName)) { - String refValue = attributes.getValue("ref"); + return; + } - if (refValue != null) { - SvcLogicNode refNode = curGraph.getNamedNode(refValue); + // Handle parameter tag + if ("parameter".equalsIgnoreCase(qName)) { + String parmName = attributes.getValue("name"); + String parmValue = attributes.getValue("value"); - if (refNode != null) { + if (parmName != null && parmName.length() > 0 && parmValue != null) { try { - curNode.addOutcome(attributes.getValue("value"), refNode); - } catch (SvcLogicException e) { + + curNode.mapParameter(parmName, parmValue); + } catch (Exception e) { throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + "Cannot add outcome", e); + + " cannot set parameter " + parmName + " to " + parmValue + " [" + e.getMessage() + + "]"); } - } else { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + "ref to unknown node " + refValue); } - return; - } - if (outcomeValue != null) { - outcomeStack.push(outcomeValue); + return; } - outcomeValue = attributes.getValue("value"); - return; - } + // Handle node tags + String nodeName = attributes.getValue("name"); + SvcLogicNode thisNode; - // Handle parameter tag - if ("parameter".equalsIgnoreCase(qName)) { - String parmName = attributes.getValue("name"); - String parmValue = attributes.getValue("value"); - if (parmName != null && parmName.length() > 0 && parmValue != null) { - try { + try { + if (nodeName != null && nodeName.length() > 0) { + thisNode = new SvcLogicNode(curNodeId++, qName, nodeName, curGraph); + } else { + thisNode = new SvcLogicNode(curNodeId++, qName, curGraph); + } - curNode.mapParameter(parmName, parmValue); - } catch (Exception e) { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + " cannot set parameter " + parmName + " to " + parmValue + " [" + e.getMessage() + "]"); + if (curGraph.getRootNode() == null) { + curGraph.setRootNode(thisNode); } - } + } catch (SvcLogicException e) { + throw new SAXException( + "line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " + e.getMessage()); - return; - } + } - // Handle node tags - String nodeName = attributes.getValue("name"); - SvcLogicNode thisNode; + int numAttributes = attributes.getLength(); - try { - if (!svcLogicStore.isValidNodeType(qName)) { - throw new SAXNotRecognizedException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() - + " " + "Unknown tag " + qName); - } - } catch (Exception e) { - throw new SAXNotRecognizedException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() - + " " + "Cannot validate node type " + qName); - } + for (int i = 0; i < numAttributes; i++) { + String attrName = attributes.getQName(i); + if (!"name".equalsIgnoreCase(attrName)) { + try { - try { - if (nodeName != null && nodeName.length() > 0) { - thisNode = new SvcLogicNode(curNodeId++, qName, nodeName, curGraph); - } else { - thisNode = new SvcLogicNode(curNodeId++, qName, curGraph); + String attrValueStr = attributes.getValue(i); + SvcLogicExpression attrValue; + if (attrValueStr.trim().startsWith("`")) { + int lastParen = attrValueStr.lastIndexOf('`'); + String evalExpr = attrValueStr.trim().substring(1, lastParen); + attrValue = SvcLogicExpressionFactory.parse(evalExpr); + + } else { + if (Character.isDigit(attrValueStr.charAt(0))) { + attrValue = new SvcLogicAtom("NUMBER", attrValueStr); + } else { + attrValue = new SvcLogicAtom("STRING", attrValueStr); + } + } + thisNode.setAttribute(attrName, attrValue); + } catch (Exception e) { + throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " + + "Cannot set attribute " + attrName, e); + } + } } - if (curGraph.getRootNode() == null) { - curGraph.setRootNode(thisNode); + if (curNode != null) { + try { + if ("block".equalsIgnoreCase(curNode.getNodeType()) || "for".equalsIgnoreCase(curNode.getNodeType()) + || "while".equalsIgnoreCase(curNode.getNodeType())) { + curNode.addOutcome(Integer.toString(curNode.getNumOutcomes() + 1), thisNode); + } else { + if (outcomeValue == null) { + throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + + " " + curNode.getNodeType() + " node expects outcome, instead found " + + thisNode.getNodeType()); + } + curNode.addOutcome(outcomeValue, thisNode); + } + } catch (SvcLogicException e) { + throw new SAXException( + "line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " + e.getMessage()); + } + nodeStack.push(curNode); } - } catch (SvcLogicException e) { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + e.getMessage()); + curNode = thisNode; } - int numAttributes = attributes.getLength(); - - for (int i = 0; i < numAttributes; i++) { - String attrName = attributes.getQName(i); - if (!"name".equalsIgnoreCase(attrName)) { - try { - - String attrValueStr = attributes.getValue(i); - SvcLogicExpression attrValue; - if (attrValueStr.trim().startsWith("`")) { - int lastParen = attrValueStr.lastIndexOf('`'); - String evalExpr = attrValueStr.trim().substring(1, lastParen); - attrValue = SvcLogicExpressionFactory.parse(evalExpr); + @Override + public void endElement(String uri, String localName, String qName) throws SAXException { - } else { - if (Character.isDigit(attrValueStr.charAt(0))) { - attrValue = new SvcLogicAtom("NUMBER", attrValueStr); - } else { - attrValue = new SvcLogicAtom("STRING", attrValueStr); - } + // Handle close of service-logic tag + if ("service-logic".equalsIgnoreCase(qName)) { + // Nothing more to do + return; } - thisNode.setAttribute(attrName, attrValue); - } catch (Exception e) { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + "Cannot set attribute " + attrName, e); + + // Handle close of method tag + if ("method".equalsIgnoreCase(qName)) { + graphs.add(curGraph); + curGraph = null; + return; } - } - } - if (curNode != null) { - try { - if ("block".equalsIgnoreCase(curNode.getNodeType()) || "for".equalsIgnoreCase(curNode.getNodeType()) - || "while".equalsIgnoreCase(curNode.getNodeType())) { - curNode.addOutcome(Integer.toString(curNode.getNumOutcomes() + 1), thisNode); + // Handle close of outcome tag + if ("outcome".equalsIgnoreCase(qName)) { + // Finished this outcome - pop the outcome stack + if (outcomeStack.isEmpty()) { + outcomeValue = null; } else { - if (outcomeValue == null) { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + curNode.getNodeType() + " node expects outcome, instead found " + thisNode.getNodeType()); - } - curNode.addOutcome(outcomeValue, thisNode); + outcomeValue = outcomeStack.pop(); } - } catch (SvcLogicException e) { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + e.getMessage()); + return; } - nodeStack.push(curNode); - } - curNode = thisNode; - - } - - @Override - public void endElement(String uri, String localName, String qName) throws SAXException { - // Handle close of service-logic tag - if ("service-logic".equalsIgnoreCase(qName)) { - // Nothing more to do - return; - } - - // Handle close of method tag - if ("method".equalsIgnoreCase(qName)) { - graphs.add(curGraph); - curGraph = null; - return; - } + // Handle close of parameter tag - do nothing + if ("parameter".equalsIgnoreCase(qName)) { + return; + } - // Handle close of outcome tag - if ("outcome".equalsIgnoreCase(qName)) { - // Finished this outcome - pop the outcome stack - if (outcomeStack.isEmpty()) { - outcomeValue = null; + // Handle close of a node tag + if (nodeStack.isEmpty()) { + curNode = null; } else { - outcomeValue = outcomeStack.pop(); + curNode = nodeStack.pop(); } - return; - } - - // Handle close of parameter tag - do nothing - if ("parameter".equalsIgnoreCase(qName)) { - return; - } - - // Handle close of a node tag - if (nodeStack.isEmpty()) { - curNode = null; - } else { - curNode = nodeStack.pop(); - } - } - - @Override - public void error(SAXParseException arg0) throws SAXException { - throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " - + arg0.getMessage()); - } - - } - - public SvcLogicParser(SvcLogicStore store) { - this.store = store; - } - - public SvcLogicParser(String propFile) { - - try { - this.store = SvcLogicStoreFactory.getSvcLogicStore(propFile); - } catch (Exception e) { - LOGGER.error(SVC_LOGIC_STORE_ERROR, e); - } - } - - public SvcLogicParser(InputStream propStr) { - - try { - this.store = SvcLogicStoreFactory.getSvcLogicStore(propStr); - } catch (Exception e) { - LOGGER.error(SVC_LOGIC_STORE_ERROR, e); - + @Override + public void error(SAXParseException arg0) throws SAXException { + throw new SAXException( + "line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " + arg0.getMessage()); } } @@ -382,7 +344,15 @@ public class SvcLogicParser { graphs = new LinkedList<>(); - saxParser.parse(fileName, new SvcLogicHandler(graphs, store)); + saxParser.parse(fileName, new SvcLogicHandler(graphs)); + + try { + for (SvcLogicGraph graph : graphs) { + graph.setMd5sum(CheckSumHelper.md5SumFromFile(fileName)); + } + } catch (Exception exc) { + LOGGER.error("Couldn't set md5sum on graphs", exc); + } } catch (Exception e) { LOGGER.error("Parsing failed ", e); @@ -422,18 +392,18 @@ public class SvcLogicParser { String propfile = null; switch (argv.length) { - case 6: - version = argv[4]; - propfile = argv[5]; - case 5: - if (propfile == null) { - propfile = argv[4]; - } - SvcLogicStore store = SvcLogicParser.getStore(propfile); - SvcLogicParser.print(argv[1], argv[2], argv[3], version, store); - break; - default: - SvcLogicParser.usage(); + case 6: + version = argv[4]; + propfile = argv[5]; + case 5: + if (propfile == null) { + propfile = argv[4]; + } + SvcLogicStore store = SvcLogicParser.getStore(propfile); + SvcLogicParser.print(argv[1], argv[2], argv[3], version, store); + break; + default: + SvcLogicParser.usage(); } } else if ("get-source".equalsIgnoreCase(argv[0])) { @@ -445,32 +415,43 @@ public class SvcLogicParser { } } else if ("activate".equalsIgnoreCase(argv[0])) { if (argv.length == 6) { - SvcLogicStore store = SvcLogicParser.getStore(argv[5]); - SvcLogicParser.activate(argv[1], argv[2], argv[3], argv[4], store); + SvcLogicStore store = SvcLogicParser.getStore(argv[5]); + SvcLogicParser.activate(argv[1], argv[2], argv[3], argv[4], store); } else { - SvcLogicParser.usage(); + SvcLogicParser.usage(); } } else if ("validate".equalsIgnoreCase(argv[0])) { if (argv.length == 3) { - String xmlfile = argv[1]; - String propfile = argv[2]; + String xmlfile = argv[1]; + String propfile = argv[2]; - System.setProperty(SLI_VALIDATING_PARSER, "true"); - SvcLogicStore store = SvcLogicParser.getStore(propfile); - try { - SvcLogicParser.validate(xmlfile, store); - } catch (Exception e) { - LOGGER.error("Validate failed", e); + System.setProperty(SLI_VALIDATING_PARSER, "true"); + SvcLogicStore store = SvcLogicParser.getStore(propfile); + try { + SvcLogicParser.validate(xmlfile, store); + } catch (Exception e) { + LOGGER.error("Validate failed", e); + } + } else { + SvcLogicParser.usage(); } + } else if ("install".equalsIgnoreCase(argv[0])) { + if (argv.length == 3) { + SvcLogicLoader loader = new SvcLogicLoader(argv[1], argv[2]); + try { + loader.loadAndActivate(); + } catch (IOException e) { + LOGGER.error(e.getMessage(), e); + } } else { - SvcLogicParser.usage(); + SvcLogicParser.usage(); } } System.exit(0); } - private static SvcLogicStore getStore(String propfile) { + protected static SvcLogicStore getStore(String propfile) { SvcLogicStore store = null; @@ -491,7 +472,7 @@ public class SvcLogicParser { throw new ConfigurationException("Cannot read xml file (" + xmlfile + ")"); } - SvcLogicParser parser = new SvcLogicParser(store); + SvcLogicParser parser = new SvcLogicParser(); LinkedList<SvcLogicGraph> graphs; try { LOGGER.info("Loading {}", xmlfile); @@ -511,8 +492,8 @@ public class SvcLogicParser { String version = graph.getVersion(); String mode = graph.getMode(); try { - LOGGER.info("Saving SvcLogicGraph to database (module:{},rpc:{},mode:{},version:{})", module, rpc, - mode, version); + LOGGER.info("Saving SvcLogicGraph to database (module:{},rpc:{},mode:{},version:{})", module, rpc, mode, + version); store.store(graph); } catch (Exception e) { throw new SvcLogicException(e.getMessage(), e); @@ -528,7 +509,7 @@ public class SvcLogicParser { throw new ConfigurationException("Cannot read xml file (" + xmlfile + ")"); } - SvcLogicParser parser = new SvcLogicParser(store); + SvcLogicParser parser = new SvcLogicParser(); LinkedList<SvcLogicGraph> graphs; try { LOGGER.info("Validating {}", xmlfile); @@ -607,6 +588,9 @@ public class SvcLogicParser { System.err.println(" OR SvcLogicParser print <module> <rpc> <mode> [<version>] <prop-file>"); System.err.println(" OR SvcLogicParser get-source <module> <rpc> <mode> <version> <prop-file>"); System.err.println(" OR SvcLogicParser activate <module> <rpc> <version> <mode>"); + System.err.println(" OR SvcLogicParser validate <file path to graph> <prop-file>"); + System.err.println(" OR SvcLogicParser install <service-logic directory path> <prop-file>"); + System.exit(1); } |