Policy TestSuite Enabled
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / components / PolicyImportWindow.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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.policy.components;
22
23
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.FileNotFoundException;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.util.ArrayList;
33 import java.util.Set;
34
35 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
36 import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
37 import org.apache.commons.io.IOUtils;
38 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
39 import org.openecomp.policy.common.logging.flexlogger.Logger;
40 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
41
42
43
44 public class PolicyImportWindow{
45
46         private static final Logger LOGGER      = FlexLogger.getLogger(PolicyImportWindow.class);
47         private Path newfile = null;
48         private boolean succeeded = false;
49         private Boolean superadmin = false;
50         private ArrayList<String> xacmlFiles = new ArrayList<String>();
51         private static FileOutputStream outputFile;
52         /**
53          * The constructor should first build the main layout, set the
54          * composition root and then do any custom initialization.
55          *
56          * The constructor will not be automatically regenerated by the
57          * visual editor.
58          */
59         
60         public OutputStream receiveUpload(String filename, String mimeType) {
61
62                 //
63                 // Create its new full path
64                 //
65                 this.newfile = null;
66                 //
67                 // Does it already exist?
68                 //
69                 if (Files.exists(this.newfile)) {
70                         return null;
71                 }
72                 //
73                 // Try to create the output stream
74                 //
75                 try {
76                         return new FileOutputStream(this.newfile.toFile());
77                 } catch (FileNotFoundException e) {
78                         LOGGER.error("Failed to create uploaded file", e);
79                 }
80                 return null;
81         }
82
83         public void Upload(){
84                 TarArchiveEntry entry = null;
85                 TarArchiveInputStream extractFile = null;
86                 try {
87                         extractFile = new TarArchiveInputStream (new FileInputStream(this.newfile.toFile()));
88                 } catch (FileNotFoundException e1) {
89                         e1.printStackTrace();
90                 }
91                 //Create a loop to read every single entry in TAR file 
92                 try {
93                         while ((entry = extractFile.getNextTarEntry()) != null) {
94                                 this.superadmin = true;
95                                 try{
96                                         copyFileToLocation(extractFile, entry, xacmlFiles, null, superadmin);
97                                 }catch(Exception e){
98                                         LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Exception while Importing Polcies"+e);
99                                 }
100                         }
101                 } catch (IOException e) {
102                         LOGGER.error("Exception Occured"+e);
103                 }
104
105         }
106
107         //Copy files to Directorys 
108         public static void copyFileToLocation(TarArchiveInputStream extractFile, TarArchiveEntry entry, ArrayList<String> xacmlFiles, Set<String> finalScopes, Boolean superadminValue ) throws IOException{
109                 String individualFiles = "";
110                 int offset = 0;
111                 outputFile = null;
112                 // Get the name of the file
113                 if(superadminValue){
114                         individualFiles = entry.getName();
115                 }else{
116                         for(int i =0; i< finalScopes.size(); i++){
117                                 if(entry.getName().startsWith(finalScopes.toArray()[i].toString())){
118                                         individualFiles = entry.getName();
119                                 }
120                         }               
121                 }
122
123                 if(individualFiles.endsWith(".xls")){
124                         if(individualFiles.contains("\\")){
125                                 individualFiles = individualFiles.replace("\\", File.separator);
126                         }else if(individualFiles.contains("/")){
127                                 individualFiles = individualFiles.replace("/", File.separator);
128                         }
129                         return;
130                 }
131
132                 individualFiles = individualFiles.replace("/", File.separator);
133                 individualFiles = individualFiles.replace("\\", File.separator);
134
135                 //Create the path with the entry name 
136                 
137
138
139                 // Get Size of the file and create a byte array for the size 
140                 byte[] content = new byte[(int) entry.getSize()];
141
142                 offset=0;
143                 LOGGER.info("Size of the File is: " + entry.getSize());                  
144                 // Read file from the archive into byte array 
145                 extractFile.read(content, offset, content.length - offset);
146                 
147                 // Use IOUtiles to write content of byte array to physical file 
148                 IOUtils.write(content,outputFile); 
149
150                 // Close Output Stream 
151                 try {
152                         outputFile.close();
153                 } catch (IOException e) {
154                         LOGGER.info("IOException:" +e);
155                         LOGGER.error("Exception Occured"+e);
156                 }
157         }
158
159
160         public Path     getUploadedFile() {
161                 if (this.succeeded) {
162                         return this.newfile;
163                 }
164                 return null;
165         }
166
167 }