Commit includes ControlLoopPolicy API and bugfixes
[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                         LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+"Exception while Importing Polcies"+e1);
90                 }
91                 //Create a loop to read every single entry in TAR file 
92                 try {
93                         while (extractFile!=null && (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                 }finally{
104                         try {
105                                 if(extractFile != null){
106                                         extractFile.close();
107                                 }
108                         } catch (IOException e) {
109                                 LOGGER.error("Exception Occured"+e);
110                         }
111                 }
112
113         }
114
115         //Copy files to Directorys 
116         public static void copyFileToLocation(TarArchiveInputStream extractFile, TarArchiveEntry entry, ArrayList<String> xacmlFiles, Set<String> finalScopes, Boolean superadminValue ) throws IOException{
117                 String individualFiles = "";
118                 int offset = 0;
119                 outputFile = null;
120                 // Get the name of the file
121                 if(superadminValue){
122                         individualFiles = entry.getName();
123                 }else{
124                         for(int i =0; i< finalScopes.size(); i++){
125                                 if(entry.getName().startsWith(finalScopes.toArray()[i].toString())){
126                                         individualFiles = entry.getName();
127                                 }
128                         }               
129                 }
130
131                 if(individualFiles.endsWith(".xls")){
132                         if(individualFiles.contains("\\")){
133                                 individualFiles = individualFiles.replace("\\", File.separator);
134                         }else if(individualFiles.contains("/")){
135                                 individualFiles = individualFiles.replace("/", File.separator);
136                         }
137                         return;
138                 }
139
140                 individualFiles = individualFiles.replace("/", File.separator);
141                 individualFiles = individualFiles.replace("\\", File.separator);
142
143                 //Create the path with the entry name 
144                 
145
146
147                 // Get Size of the file and create a byte array for the size 
148                 byte[] content = new byte[(int) entry.getSize()];
149
150                 offset=0;
151                 LOGGER.info("Size of the File is: " + entry.getSize());                  
152                 // Read file from the archive into byte array 
153                 extractFile.read(content, offset, content.length - offset);
154                 
155                 // Use IOUtiles to write content of byte array to physical file 
156                 IOUtils.write(content,outputFile); 
157
158                 // Close Output Stream 
159                 try {
160                         if(outputFile != null){
161                                 outputFile.close();
162                         }
163                 } catch (IOException e) {
164                         LOGGER.info("IOException:" +e);
165                         LOGGER.error("Exception Occured"+e);
166                 }
167         }
168
169
170         public Path     getUploadedFile() {
171                 if (this.succeeded) {
172                         return this.newfile;
173                 }
174                 return null;
175         }
176
177 }