aboutsummaryrefslogtreecommitdiffstats
path: root/appc-directed-graph/dg-loader/provider/src/main/java/org/onap/sdnc/dg/loader/DGXMLLoad.java
blob: ced6b007cb6d50a9796db3ecea162162e0d94803 (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property.  All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * 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.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdnc.dg.loader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import org.onap.ccsdk.sli.core.sli.SvcLogicParser;
import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;

public class DGXMLLoad {
    private final static Logger logger = LoggerFactory.getLogger(DGXMLLoad.class);
    private final SvcLogicStore store;
    public static String STRING_ENCODING = "utf-8";

    public DGXMLLoad(String propfile) throws Exception {
        if (StringUtils.isBlank(propfile)) {
            throw new Exception(propfile + " Profile file is not defined");
        }
        this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
    }

    protected DGXMLLoad(SvcLogicStore store) throws Exception {
        this.store = store;
    }

    public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException {
        if (dgXMLpath != null) {
            SvcLogicParser.load(dgXMLpath, this.store);
        }
    }

    private void loadDGXMLDir(String xmlPath) throws Exception {
        try {
            logger.info(
                    "******************** Loading DG into Database *****************************");
            List<String> errors = new ArrayList<String>();
            if (this.store != null) {
                File xmlDir = new File(xmlPath);
                if (xmlDir != null && xmlDir.isDirectory()) {
                    String[] extensions = new String[] {"xml", "XML"};
                    List<File> files = (List<File>) FileUtils.listFiles(xmlDir, extensions, true);
                    for (File file : files) {
                        logger.info("Loading DG XML file :" + file.getCanonicalPath());
                        try {
                            SvcLogicParser.load(file.getCanonicalPath(), this.store);
                        } catch (Exception e) {
                            errors.add("Failed to load XML " + file.getCanonicalPath()
                                    + ", Exception : " + e.getMessage());
                        }
                    }
                } else {
                    throw new Exception(xmlPath + " is not a valid XML Directory");
                }
            } else {
                throw new Exception("Failed to initialise SvcLogicStore");
            }

            if (errors.size() > 0) {
                throw new Exception(errors.toString());
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    public static void main(String[] args) {
        try {
            String xmlPath = null;
            String propertyPath = null;

            if (args != null && args.length >= 2) {
                xmlPath = args[0];
                propertyPath = args[1];
            } else {
                throw new Exception(
                        "Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <dbPropertyfile>");
            }
            DGXMLLoad dgXMLLoadDB = new DGXMLLoad(propertyPath);
            dgXMLLoadDB.loadDGXMLDir(xmlPath);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(1);
        }
    }

}