View Javadoc

1   /*
2    * JBoss, Home of Professional Open Source.
3    * Copyright 2013, Red Hat Middleware LLC, and individual contributors
4    * as indicated by the @author tags. See the copyright.txt file in the
5    * distribution for a full listing of individual contributors.
6    *
7    * This is free software; you can redistribute it and/or modify it
8    * under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */
22  
23  package org.redhat.vmtruckloader.util;
24  
25  import static org.redhat.vmtruckloader.util.StringUtil.checkIfStringValid;
26  
27  import java.io.FileReader;
28  import java.io.FileWriter;
29  import java.io.IOException;
30  import java.io.StringReader;
31  import java.util.List;
32  
33  import org.redhat.vmtruckloader.service.MachineSpecification;
34  
35  import au.com.bytecode.opencsv.CSVReader;
36  import au.com.bytecode.opencsv.CSVWriter;
37  import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
38  import au.com.bytecode.opencsv.bean.CsvToBean;
39  
40  /**
41   * 
42   * @author Romain Pelisse - romain@redhat.com
43   *
44   */
45  public final class CSVUtils {
46  
47  	private static final char CSV_SEPARATOR = ';';
48  	
49  	private CSVUtils() {}
50  	
51  	private static String[] turnSpecIntoCsvLine(MachineSpecification spec, String[] line) {
52  		int i = 0;
53  		line[i++] = spec.getEnv();
54  		line[i++] = spec.getHostname();
55  		line[i++] = spec.getRole();
56  		line[i++] = spec.getMAC();
57  		line[i++] = spec.getIpAddress();
58  		line[i++] = spec.getVLAN();
59  		line[i++] = spec.getResourcePoolName();
60  		line[i++] = spec.getDatastoreName();
61  		line[i++] = spec.getFolder();
62  		line[i++] = String.valueOf(spec.getNbCpu());
63  		line[i++] = String.valueOf(spec.getVRAM());
64  		return line;
65  	}
66  
67  	private static final String VALIDATION_ERROR_MESSAGE_PREFIX = "Invalid " + MachineSpecification.class.getSimpleName() +  " -";
68  	private static void validateSpec(MachineSpecification spec) {		
69  		checkIfStringValid(spec.getVmName(),VALIDATION_ERROR_MESSAGE_PREFIX +  " VM name is empty or 'null'.");
70  		checkIfStringValid(spec.getEnv(),VALIDATION_ERROR_MESSAGE_PREFIX +  " no Env specified for VM " + spec.getVmName());
71  		checkIfStringValid(spec.getRole(),VALIDATION_ERROR_MESSAGE_PREFIX +  "  no Role specified for VM " + spec.getVmName());
72  		checkIfStringValid(spec.getMAC(),VALIDATION_ERROR_MESSAGE_PREFIX +  "  no MAC Address specified for VM " + spec.getVmName());
73  		checkIfStringValid(spec.getVLAN(),VALIDATION_ERROR_MESSAGE_PREFIX +  "  no VLAN specified for VM " + spec.getVmName());
74  		checkIfStringValid(spec.getResourcePoolName(),VALIDATION_ERROR_MESSAGE_PREFIX +  "  no Resource Pool name specified for VM " + spec.getVmName());
75  		checkIfStringValid(spec.getDatastoreName(),VALIDATION_ERROR_MESSAGE_PREFIX +  "  no Datastore name specified for VM " + spec.getVmName());
76  		checkIfStringValid(spec.getFolder(),VALIDATION_ERROR_MESSAGE_PREFIX +  "  no Folder name specified for VM " + spec.getVmName());
77  		if ( spec.getNbCpu() <= 0 )
78  			throw new IllegalArgumentException(VALIDATION_ERROR_MESSAGE_PREFIX +  "  invalid number of CPUs:" + spec.getNbCpu() + " specified for VM " + spec.getVmName());
79  		if ( spec.getVRAM() <= 0 )
80  			throw new IllegalArgumentException(VALIDATION_ERROR_MESSAGE_PREFIX +  "  invalid value for RAM:" + spec.getVRAM() + " specified for VM " + spec.getVmName());
81  	}
82  	
83  	public static boolean updateLineInCsvFile(String filename , MachineSpecification spec) {
84  		validateSpec(spec);
85  		try {
86  			List<String[]> entries = new CSVReader(new FileReader(filename),CSV_SEPARATOR).readAll();
87  			
88  			CSVWriter writer = new CSVWriter(new FileWriter(filename), CSV_SEPARATOR);
89  		    for ( String[] entry : entries ) {
90  		    	if ( spec.getVmName().equals(entry[1]) )
91  		    		entry = turnSpecIntoCsvLine(spec, entry);
92  			     writer.writeNext(entry);		    	
93  		    }	
94  			writer.close();
95  		return true;
96  		} catch (IOException e ) {
97  			throw new IllegalStateException(e);
98  		}
99  	}
100 	
101 	public static MachineSpecification turnLineIntoSpec(String line) {
102 		ColumnPositionMappingStrategy<MachineSpecification> strategy = new ColumnPositionMappingStrategy<MachineSpecification>();
103 		strategy.setType(MachineSpecification.class);
104 		String[] columns = new String[] {"env", "hostname", "role", "MAC", "ipAddress", "VLAN", "resourcePoolName", "datastoreName", "folder", "nbCpu", "vRAM", "diskSize"}; 
105 		strategy.setColumnMapping(columns);
106 
107 		CsvToBean<MachineSpecification> csv = new CsvToBean<MachineSpecification>();
108 		List<MachineSpecification> list = csv.parse(strategy, new CSVReader(new StringReader(line),CSV_SEPARATOR));
109 		if ( list == null || list.isEmpty() )
110 			throw new IllegalArgumentException("The following CSV line was successfully parsed but did not contained any data:" + line);
111 		return list.get(0);
112 	}
113 
114 }