/*
* Copyright 1999-2010 University of Chicago
*
* 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.
*/
package org.globus.workspace.network.defaults;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.globus.workspace.network.Association;
import org.globus.workspace.network.AssociationEntry;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
public class FileUtil {
private static final Log logger =
LogFactory.getLog(FileUtil.class.getName());
private static final DateFormat localFormat = DateFormat.getDateTimeInstance();
public static void writeIpMacPairs(File file, Map<String, Association> associations)
throws IOException {
if (file == null) {
throw new IllegalArgumentException("file may not be null");
}
if (associations == null) {
throw new IllegalArgumentException("associations may not be null");
}
final List<String> lines = new ArrayList<String>();
lines.add("# This file contains IP -> MAC pairs for each network pool entry");
lines.add(getGeneratedLine());
lines.add("");
for (Map.Entry<String, Association> assoc : associations.entrySet()) {
lines.add("# network pool: " + assoc.getKey());
for (Object entryObject : assoc.getValue().getEntries()) {
final AssociationEntry entry = (AssociationEntry) entryObject;
lines.add(String.format("%s %s", entry.getIpAddress(), entry.getMac()));
}
lines.add("###############################################");
}
logger.debug("Writing IP -> MAC pairs file to "+ file.getAbsolutePath());
writeFile(file, lines);
}
public static void writeDhcpdEntries(File file, Map<String, Association> associations)
throws IOException {
if (file == null) {
throw new IllegalArgumentException("file may not be null");
}
if (associations == null) {
throw new IllegalArgumentException("associations may not be null");
}
final List<String> lines = new ArrayList<String>();
lines.add("# This file contains dhcpd.conf blocks for each network pool entry");
lines.add("# It is generated by the service at boot. When using a site DHCP server,");
lines.add("# this file can be included in your dhcpd.conf:");
lines.add("# include \""+file.getAbsolutePath()+"\";");
lines.add("# For details, check the Nimbus admin documentation.");
lines.add("");
lines.add(getGeneratedLine());
lines.add("");
for (Association association : associations.values()) {
for (Object entry : association.getEntries()) {
lines.add(getDhcpdEntry((AssociationEntry) entry,
association.getDns()));
}
}
logger.debug("Writing dhcpd entries file to "+ file.getAbsolutePath());
writeFile(file, lines);
}
public static String getDhcpdEntry(AssociationEntry entry, String dns) {
final StringBuilder sb = new StringBuilder();
sb.append("host ").append(entry.getHostname()).append(" {\n");
sb.append(" hardware ethernet ").append(entry.getMac()).append(";\n");
sb.append(" fixed-address ").append(entry.getIpAddress()).append(";\n");
sb.append(" option host-name \"").append(entry.getHostname()).append("\";\n");
final String mask = entry.getSubnetMask();
if (mask != null) {
sb.append(" option subnet-mask ").append(mask).append(";\n");
}
final String gateway = entry.getGateway();
if (gateway != null) {
sb.append(" option routers ").append(gateway).append(";\n");
}
final String broadcast = entry.getBroadcast();
if (broadcast != null) {
sb.append(" option broadcast-address ").append(broadcast).append(";\n");
}
if (dns != null) {
sb.append(" option domain-name-servers ").append(dns).append(";\n");
}
sb.append('}');
return sb.toString();
}
public static void writeNetSample(File file, AssociationEntry entry,
String netname, String dns) throws IOException {
if (entry == null) {
throw new IllegalArgumentException("entry may not be null");
}
if (file == null) {
throw new IllegalArgumentException("file may not be null");
}
if (netname == null || netname.length() == 0) {
throw new IllegalArgumentException("netname may not be null or empty");
}
final List<String> lines = new ArrayList<String>();
lines.add("# This file contains a sample network configuration to be used");
lines.add("# for testing new VMM nodes. Refer to the admin documentation.");
lines.add(getGeneratedLine());
lines.add("");
lines.add("[netsample]");
lines.add("netname: "+ netname);
lines.add("mac: " + entry.getMac());
lines.add("ip: " + entry.getIpAddress());
lines.add("hostname: " + entry.getHostname());
// some values can be none, leave them out of file
if (entry.getGateway() != null) {
lines.add("gateway: " + entry.getGateway());
}
if (entry.getBroadcast() != null) {
lines.add("broadcast: " + entry.getBroadcast());
}
if (entry.getSubnetMask() != null) {
lines.add("netmask: " + entry.getSubnetMask());
}
if (dns != null) {
lines.add("dns: " + dns);
}
logger.debug("Writing netsample file to "+ file.getAbsolutePath());
writeFile(file, lines);
}
private static String getGeneratedLine() {
return "# Generated at " +
localFormat.format(Calendar.getInstance().getTime());
}
private static void writeFile(File file, Iterable<String> lines)
throws IOException {
final PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(file)));
try {
for (String line : lines) {
out.println(line);
}
} finally {
out.close();
}
}
}