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