2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.sdnc.dg.loader;
28 import java.util.ArrayList;
29 import java.util.List;
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.lang.StringUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import org.openecomp.sdnc.sli.SvcLogicException;
37 import org.openecomp.sdnc.sli.SvcLogicParser;
38 import org.openecomp.sdnc.sli.SvcLogicStore;
39 import org.openecomp.sdnc.sli.SvcLogicStoreFactory;
41 public class DGXMLLoad {
42 private final static Logger logger = LoggerFactory.getLogger(DGXMLLoad.class);
43 private final SvcLogicStore store;
44 public static String STRING_ENCODING = "utf-8";
46 public DGXMLLoad(String propfile) throws Exception{
47 if(StringUtils.isBlank(propfile)){
48 throw new Exception(propfile + " Profile file is not defined");
50 this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
53 protected DGXMLLoad(SvcLogicStore store) throws Exception {
57 public void loadDGXMLFile(String dgXMLpath) throws SvcLogicException{
58 if(dgXMLpath != null ){
59 SvcLogicParser.load(dgXMLpath, this.store);
63 private void loadDGXMLDir(String xmlPath) throws Exception {
65 logger.info("******************** Loading DG into Database *****************************");
66 List<String> errors = new ArrayList<String>();
67 if(this.store != null){
68 File xmlDir = new File(xmlPath);
69 if(xmlDir != null && xmlDir.isDirectory()){
70 String[] extensions = new String[] { "xml", "XML" };
71 List<File> files = (List<File>) FileUtils.listFiles(xmlDir, extensions, true);
72 for (File file : files) {
73 logger.info("Loading DG XML file :" + file.getCanonicalPath());
75 SvcLogicParser.load(file.getCanonicalPath(), this.store);
76 }catch (Exception e) {
77 errors.add("Failed to load XML "+file.getCanonicalPath() + ", Exception : "+e.getMessage());
81 throw new Exception(xmlPath + " is not a valid XML Directory");
84 throw new Exception("Failed to initialise SvcLogicStore");
87 if(errors.size() > 0){
88 throw new Exception(errors.toString());
90 } catch (Exception e) {
91 logger.error(e.getMessage());
95 public static void main(String[] args) {
97 String xmlPath = null;
98 String propertyPath = null;
100 if(args != null && args.length >= 2){
102 propertyPath = args[1];
104 throw new Exception("Sufficient inputs for DGXMLLoadNActivate are missing <xmlpath> <dbPropertyfile>");
106 DGXMLLoad dgXMLLoadDB = new DGXMLLoad(propertyPath);
107 dgXMLLoadDB.loadDGXMLDir(xmlPath);
108 } catch (Exception e) {