Second part of onap rename
[appc.git] / appc-directed-graph / dg-loader / provider / src / main / java / org / openecomp / sdnc / dg / loader / DGXMLActivate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.sdnc.dg.loader;
26
27 import java.io.File;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.apache.commons.io.FileUtils;
32 import org.apache.commons.lang.StringUtils;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicGraph;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicStore;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicStoreFactory;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39
40
41 public class DGXMLActivate {
42
43     private final static Logger logger = LoggerFactory.getLogger(DGXMLLoadNActivate.class);
44     private final SvcLogicStore store;
45     public static String STRING_ENCODING = "utf-8";
46
47     public DGXMLActivate(String propfile) throws Exception{
48         if(StringUtils.isBlank(propfile)){
49             throw new Exception(propfile + " Profile file is not defined");
50         }
51         this.store = SvcLogicStoreFactory.getSvcLogicStore(propfile);
52     }
53     
54     protected DGXMLActivate(SvcLogicStore store) {
55         this.store = store;
56     }
57
58
59     public void activateDg(String activateFilePath) throws Exception {
60         logger.info("******************** Activating DG into Database *****************************");
61         try {
62             List<String> errors = new ArrayList<String>();
63             if(this.store != null){
64                 File activateFile = new File(activateFilePath);
65                 if(activateFile != null && activateFile.isFile()){
66                     List<String> fileLines = FileUtils.readLines(activateFile,STRING_ENCODING);
67                     if(fileLines != null ){
68                         for (String line : fileLines) {
69                             if(line != null && ! line.trim().startsWith("#")){
70                                 String lineArray[] = line.trim().split(":");
71                                 try {
72                                     if(lineArray != null && lineArray.length >= 4){
73                                         String module = lineArray[0];
74                                         String rpc = lineArray[1];
75                                         String version = lineArray[2];
76                                         String mode = lineArray[3];
77                                         if(StringUtils.isNotBlank(module) && StringUtils.isNotBlank(rpc)
78                                                 && StringUtils.isNotBlank(version) && StringUtils.isNotBlank(mode)){
79                                             logger.info("Activating DG :" + line);
80                                             SvcLogicGraph graph = this.store.fetch(module, rpc, version, mode);
81                                             if(graph != null){
82                                                 logger.info("Found Graph :" + line + " Activating ...");
83                                                 this.store.activate(graph);
84                                             }else{
85                                                 throw new Exception("Failed to fetch from Database");
86                                             }
87                                         }
88                                     }
89                                 } catch (Exception e) {
90                                     e.printStackTrace();
91                                     errors.add("Failed to Activate "+line + ", "+e.getMessage());
92                                 }
93                             }
94                         }
95                     }
96                 }else{
97                     throw new Exception(activateFile + " is not a valid Activate file Path");
98                 }
99             }else{
100                 throw new Exception("Failed to initialise SvcLogicStore");
101             }
102
103             if(errors.size() > 0){
104                 throw new Exception(errors.toString());
105             }
106         } catch (Exception e) {
107             logger.error(e.getMessage());
108         }
109     }
110
111
112     public static void main(String[] args) {
113         try {
114             String activateFile = null;
115             String propertyPath = null;
116
117             if(args != null && args.length >= 2){
118                 activateFile = args[0];
119                 propertyPath = args[1];
120             }else{
121                 throw new Exception("Sufficient inputs for DGXMLActivate are missing <activatefile> <dbPropertyfile>");
122             }
123
124             DGXMLActivate dgXmlActivate = new DGXMLActivate(propertyPath);
125             dgXmlActivate.activateDg(activateFile);
126         } catch (Exception e) {
127             e.printStackTrace();
128         }finally {
129             System.exit(1);
130         }
131     }
132
133 }