aboutsummaryrefslogtreecommitdiffstats
path: root/ecomp-sdk-app/src/main/java/org/openecomp/policy/components/PolicyImportWindow.java
diff options
context:
space:
mode:
Diffstat (limited to 'ecomp-sdk-app/src/main/java/org/openecomp/policy/components/PolicyImportWindow.java')
-rw-r--r--ecomp-sdk-app/src/main/java/org/openecomp/policy/components/PolicyImportWindow.java226
1 files changed, 226 insertions, 0 deletions
diff --git a/ecomp-sdk-app/src/main/java/org/openecomp/policy/components/PolicyImportWindow.java b/ecomp-sdk-app/src/main/java/org/openecomp/policy/components/PolicyImportWindow.java
new file mode 100644
index 000000000..c70e331d0
--- /dev/null
+++ b/ecomp-sdk-app/src/main/java/org/openecomp/policy/components/PolicyImportWindow.java
@@ -0,0 +1,226 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ECOMP Policy Engine
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.policy.components;
+
+
+/*
+ *
+ */
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Set;
+
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.io.IOUtils;
+import org.openecomp.policy.controller.PolicyController;
+
+import org.openecomp.policy.xacml.api.XACMLErrorConstants;
+
+import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
+import org.openecomp.policy.common.logging.flexlogger.Logger;
+
+
+
+public class PolicyImportWindow{
+
+ private static final Logger logger = FlexLogger.getLogger(PolicyImportWindow.class);
+ private static final int BUFFER_SIZE = 4096;
+ private static Path directory = PolicyController.getGitPath();
+ private Path newfile = null;
+ private boolean succeeded = false;
+ public static String CONFIG_HOME = PolicyController.getConfigHome();
+ public static String ACTION_HOME = PolicyController.getActionHome();
+ private Boolean superadmin = false;
+ private ArrayList<String> xacmlFiles = new ArrayList<String>();
+ /**
+ * The constructor should first build the main layout, set the
+ * composition root and then do any custom initialization.
+ *
+ * The constructor will not be automatically regenerated by the
+ * visual editor.
+ */
+
+ public OutputStream receiveUpload(String filename, String mimeType) {
+
+ //
+ // Create its new full path
+ //
+ this.newfile = Paths.get(PolicyImportWindow.directory.toString(), filename);
+ //
+ // Does it already exist?
+ //
+ if (Files.exists(this.newfile)) {
+ return null;
+ }
+ //
+ // Try to create the output stream
+ //
+ try {
+ return new FileOutputStream(this.newfile.toFile());
+ } catch (FileNotFoundException e) {
+ logger.error("Failed to create uploaded file", e);
+ }
+ return null;
+ }
+
+ public void Upload(){
+ TarArchiveEntry entry = null;
+ TarArchiveInputStream extractFile = null;
+ try {
+ extractFile = new TarArchiveInputStream (new FileInputStream(this.newfile.toFile()));
+ } catch (FileNotFoundException e1) {
+ e1.printStackTrace();
+ }
+ //Create a loop to read every single entry in TAR file
+ try {
+ while ((entry = extractFile.getNextTarEntry()) != null) {
+ this.superadmin = true;
+ try{
+ copyFileToLocation(extractFile, entry, xacmlFiles, null, superadmin);
+ }catch(Exception e){
+ logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Exception while Importing Polcies"+e);
+ }
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+
+
+
+
+ //Copy files to Directorys
+ public static void copyFileToLocation(TarArchiveInputStream extractFile, TarArchiveEntry entry, ArrayList<String> xacmlFiles, Set<String> finalScopes, Boolean superadminValue ) throws IOException{
+ String individualFiles = "";
+ int offset = 0;
+ FileOutputStream outputFile=null;
+ // Get the name of the file
+ if(superadminValue){
+ individualFiles = entry.getName();
+ }else{
+ for(int i =0; i< finalScopes.size(); i++){
+ if(entry.getName().startsWith(finalScopes.toArray()[i].toString())){
+ individualFiles = entry.getName();
+ }
+ }
+ }
+
+ if(individualFiles.endsWith(".xls")){
+ if(individualFiles.contains("\\")){
+ individualFiles = individualFiles.replace("\\", File.separator);
+ }else if(individualFiles.contains("/")){
+ individualFiles = individualFiles.replace("/", File.separator);
+ }
+ return;
+ }
+
+ individualFiles = individualFiles.replace("/", File.separator);
+ individualFiles = individualFiles.replace("\\", File.separator);
+
+ //Create the path with the entry name
+ String filePath = directory.toAbsolutePath() + File.separator + individualFiles;
+ String configPath = CONFIG_HOME + File.separator + individualFiles;
+ String actionPath = ACTION_HOME + File.separator + individualFiles;
+ logger.info("File Name in TAR File is: " + individualFiles);
+ logger.info("Xml directory file path: " + filePath);
+ logger.info("Config Home directory file path: " + configPath);
+ logger.info("Action Home directory file path: " + actionPath);
+
+
+ // Get Size of the file and create a byte array for the size
+ byte[] content = new byte[(int) entry.getSize()];
+
+ offset=0;
+ logger.info("File Name in TAR File is: " + individualFiles);
+ logger.info("Size of the File is: " + entry.getSize());
+ // Read file from the archive into byte array
+ extractFile.read(content, offset, content.length - offset);
+ if (!entry.isDirectory()) {
+ if(!individualFiles.contains(".Config_") || !individualFiles.contains(".Action_")){
+ // if the entry is a file, extracts it
+ String filePath1 = filePath.substring(0, filePath.lastIndexOf(File.separator));
+ File newFile = new File(filePath1);
+ if(!(newFile.exists())) {
+ File dir = new File(filePath1);
+ dir.mkdir();
+ extractFile(extractFile, filePath);
+ }
+ }
+ } else {
+ // if the entry is a directory, make the director
+ File dir = new File(filePath);
+ dir.mkdir();
+ }
+ // Define OutputStream for writing the file
+ if(individualFiles.contains(".Config_")){
+ outputFile=new FileOutputStream(new File(configPath));
+ }else if(individualFiles.contains(".Action_")){
+ outputFile=new FileOutputStream(new File(actionPath));
+ }else{
+ if(filePath != null){
+ outputFile=new FileOutputStream(new File(filePath));
+ xacmlFiles.add(filePath);
+ }
+ }
+
+ // Use IOUtiles to write content of byte array to physical file
+ IOUtils.write(content,outputFile);
+
+ // Close Output Stream
+ try {
+ outputFile.close();
+ } catch (IOException e) {
+ logger.info("IOException:" +e);
+ e.printStackTrace();
+ }
+ }
+
+ private static void extractFile(TarArchiveInputStream extractFile, String filePath) throws IOException {
+ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
+ byte[] bytesIn = new byte[BUFFER_SIZE];
+ int read = 0;
+ while ((read = extractFile.read(bytesIn)) != -1) {
+ bos.write(bytesIn, 0, read);
+ }
+ bos.close();
+
+ }
+
+ public Path getUploadedFile() {
+ if (this.succeeded) {
+ return this.newfile;
+ }
+ return null;
+ }
+
+}