aboutsummaryrefslogtreecommitdiffstats
path: root/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java
blob: 3d9caffbd3ecbf2af91e1fa54b72733cb15ac3a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/*-
 * ============LICENSE_START=======================================================
 * ONAP : CCSDK
 * ================================================================================
 * 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.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.ParserConfigurationException;
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.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author dt5972
 *
 */
public class SvcLogicParser {

    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";
    static final String JAXP_DYNAMIC_VALIDATION = "http://apache.org/xml/features/validation/dynamic";
    static final String JAXP_SCHEMA_VALIDATION = "http://apache.org/xml/features/validation/schema";

    private static final String LOAD_MESSAGE = "Getting SvcLogicGraph from database - {}";
    private static final String LOAD_ERROR_MESSAGE = "SvcLogicGraph not found - {}";
    private static final String ACTIVATION_ERROR_MESSAGE = "Could not activate SvcLogicGraph - {}";
    private static final String PRINT_ERROR_MESSAGE = "Could not print SvcLogicGraph - {}";
    private static final String SVC_LOGIC_STORE_ERROR = "Could not get service logic store";

    private static final Logger LOGGER = LoggerFactory.getLogger(SvcLogicParser.class);
    private static final String SLI_VALIDATING_PARSER = "org.onap.ccsdk.sli.parser.validate";
    private static final String SVCLOGIC_XSD = "/svclogic.xsd";
    private SAXParser saxParser;

    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;

        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 startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {

            // 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");
                }

                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;
            }

            if ("method".equalsIgnoreCase(qName)) {

                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;

                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);

                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");

                return;
            }

            // 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 {

                        curNode.mapParameter(parmName, parmValue);
                    } catch (Exception e) {
                        throw new SAXException("line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " "
                                + " cannot set parameter " + parmName + " to " + parmValue + " [" + e.getMessage()
                                + "]");
                    }
                }

                return;
            }

            // Handle node tags
            String nodeName = attributes.getValue("name");
            SvcLogicNode thisNode;


            try {
                if (nodeName != null && nodeName.length() > 0) {
                    thisNode = new SvcLogicNode(curNodeId++, qName, nodeName, curGraph);
                } else {
                    thisNode = new SvcLogicNode(curNodeId++, qName, curGraph);
                }

                if (curGraph.getRootNode() == null) {
                    curGraph.setRootNode(thisNode);
                }
            } catch (SvcLogicException e) {
                throw new SAXException(
                        "line " + locator.getLineNumber() + ":" + locator.getColumnNumber() + " " + e.getMessage());

            }

            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);

                        } 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 (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);
            }
            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 outcome tag
            if ("outcome".equalsIgnoreCase(qName)) {
                // Finished this outcome - pop the outcome stack
                if (outcomeStack.isEmpty()) {
                    outcomeValue = null;
                } else {
                    outcomeValue = outcomeStack.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 LinkedList<SvcLogicGraph> parse(String fileName) throws SvcLogicException {
        LinkedList<SvcLogicGraph> graphs;

        try {
            if (saxParser == null) {
                saxParser = initParser();
            }

            graphs = new LinkedList<>();
            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);
            String msg = e.getMessage();
            if (msg != null) {
                throw new SvcLogicException("Compiler error: " + fileName + " @ " + msg);
            } else {
                throw new SvcLogicException("Compiler error: " + fileName, e);
            }
        }
        return graphs;
    }

    public static void main(String argv[]) {

        if (argv.length == 0) {
            SvcLogicParser.usage();
        }

        if ("load".equalsIgnoreCase(argv[0])) {
            if (argv.length == 3) {
                String xmlfile = argv[1];
                String propfile = argv[2];

                SvcLogicStore store = SvcLogicParser.getStore(propfile);
                try {
                    SvcLogicParser.load(xmlfile, store);
                } catch (Exception e) {
                    LOGGER.error("Load failed ", e);
                }
            } else {
                SvcLogicParser.usage();
            }
        } else if ("print".equalsIgnoreCase(argv[0])) {
            String version = null;
            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();
            }
        } else if ("get-source".equalsIgnoreCase(argv[0])) {

            if (argv.length == 6) {
                SvcLogicStore store = SvcLogicParser.getStore(argv[5]);
                SvcLogicParser.getSource(argv[1], argv[2], argv[3], argv[4], store);
            } else {
                SvcLogicParser.usage();
            }
        } 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);
            } else {
                SvcLogicParser.usage();
            }
        } else if ("validate".equalsIgnoreCase(argv[0])) {
            if (argv.length == 3) {
                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);
                }
            } 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();
            }
        }

        System.exit(0);
    }

    protected static SvcLogicStore getStore(String propfile) {

        SvcLogicStore store = null;

        try {
            store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
        } catch (Exception e) {
            LOGGER.error(SVC_LOGIC_STORE_ERROR, e);
            System.exit(1);
        }

        return store;

    }

    
    public static void load(String xmlfile, SvcLogicStore store) throws SvcLogicException {
        File xmlFile = new File(xmlfile);
        if (!xmlFile.canRead()) {
            throw new ConfigurationException("Cannot read xml file (" + xmlfile + ")");
        }

        SvcLogicParser parser = new SvcLogicParser();
        LinkedList<SvcLogicGraph> graphs;
        try {
            LOGGER.info("Loading {}", xmlfile);
            graphs = parser.parse(xmlfile);
        } catch (Exception e) {
            throw new SvcLogicException(e.getMessage(), e);
        }

        if (graphs == null) {
            throw new SvcLogicException("Could not parse " + xmlfile);
        }

        for (SvcLogicGraph graph : graphs) {

            try {
                LOGGER.info("Saving " + graph.toString() + " to database.");
                store.store(graph);
            } catch (Exception e) {
                throw new SvcLogicException(e.getMessage(), e);
            }

        }

    }

    public static void validate(String xmlfile, SvcLogicStore store) throws SvcLogicException {
        File xmlFile = new File(xmlfile);
        if (!xmlFile.canRead()) {
            throw new ConfigurationException("Cannot read xml file (" + xmlfile + ")");
        }

        SvcLogicParser parser = new SvcLogicParser();
        LinkedList<SvcLogicGraph> graphs;
        try {
            LOGGER.info("Validating {}", xmlfile);
            graphs = parser.parse(xmlfile);
        } catch (Exception e) {
            throw new SvcLogicException(e.getMessage(), e);
        }

        if (graphs == null) {
            throw new SvcLogicException("Could not parse " + xmlfile);
        } else {
            LOGGER.info("Compilation successful for {}", xmlfile);
        }

    }

    private static void print(String module, String rpc, String mode, String version, SvcLogicStore store) {
        String details = "(module:" + module + ", rpc:" + rpc + ", version:" + version + ", mode:" + mode + ")";

        try {
            LOGGER.info(LOAD_MESSAGE, details);

            SvcLogicGraph graph = store.fetch(module, rpc, version, mode);
            if (graph == null) {
                LOGGER.error(LOAD_ERROR_MESSAGE, details);
                System.exit(1);
            }
            graph.printAsGv(System.out);
        } catch (Exception e) {
            LOGGER.error(PRINT_ERROR_MESSAGE, details, e);
            System.exit(1);
        }

    }

    private static void getSource(String module, String rpc, String mode, String version, SvcLogicStore store) {
        String details = "(module:" + module + ", rpc:" + rpc + ", version:" + version + ", mode:" + mode + ")";

        try {
            LOGGER.info(LOAD_MESSAGE, details);

            SvcLogicGraph graph = store.fetch(module, rpc, version, mode);
            if (graph == null) {
                LOGGER.error(LOAD_ERROR_MESSAGE, details);
                System.exit(1);
            }
            graph.printAsXml(System.out);
        } catch (Exception e) {
            LOGGER.error(PRINT_ERROR_MESSAGE, details, e);
            System.exit(1);
        }

    }

    private static void activate(String module, String rpc, String version, String mode, SvcLogicStore store) {
        String details = "(module:" + module + ", rpc:" + rpc + ", version:" + version + ", mode:" + mode + ")";

        try {
            LOGGER.info(LOAD_MESSAGE, details);

            SvcLogicGraph graph = store.fetch(module, rpc, version, mode);
            if (graph == null) {
                LOGGER.error(LOAD_ERROR_MESSAGE, details);
                System.exit(1);
            }
            store.activate(graph);
        } catch (Exception e) {
            LOGGER.error(ACTIVATION_ERROR_MESSAGE, details, e);
            System.exit(1);
        }

    }

    private static void usage() {
        System.err.println("Usage: SvcLogicParser load <xml-file> <prop-file>");
        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);
    }
    
    protected SAXParser initParser() throws ParserConfigurationException, SAXException {
        URL xsdUrl = null;
        Schema schema = null;
        String validateSchema = System.getProperty(SLI_VALIDATING_PARSER, "true");

        if ("true".equalsIgnoreCase(validateSchema)) {
            xsdUrl = getClass().getResource(SVCLOGIC_XSD);
        }

        if (xsdUrl != null) {
            try {
                SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                schema = schemaFactory.newSchema(xsdUrl);
                LOGGER.info("Schema path {}", xsdUrl.getPath());
            } catch (Exception e) {
                LOGGER.warn("Could not validate using schema {}", xsdUrl.getPath(), e);
            }
        } else {
            LOGGER.warn("Could not find resource {}", SVCLOGIC_XSD);
        }

        SAXParserFactory factory = SAXParserFactory.newInstance();

        if (schema != null) {
            factory.setNamespaceAware(true);
            factory.setSchema(schema);
        }

        SAXParser saxParser = factory.newSAXParser();
        if (saxParser.isValidating()) {
            LOGGER.info("Parser configured to validate XML {}", (xsdUrl != null ? xsdUrl.getPath() : null));
        }
        return saxParser;
    }


}