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