1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * ident "%Z%%M% %I% %E% SMI" 24 * 25 * Copyright 2001-2002 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 package com.sun.dhcpmgr.cli.pntadm; 29 30 import com.sun.dhcpmgr.cli.common.Format; 31 import com.sun.dhcpmgr.data.DhcpClientRecord; 32 import com.sun.dhcpmgr.data.Network; 33 import com.sun.dhcpmgr.bridge.NoEntryException; 34 import com.sun.dhcpmgr.bridge.NoTableException; 35 36 import java.util.Date; 37 import java.lang.IllegalArgumentException; 38 39 /** 40 * The main class for the "display network table" functionality 41 * of pntadm. 42 */ 43 public class DisplayNetworkTable extends PntAdmFunction { 44 45 /** 46 * The valid options associated with displaying a network table. 47 */ 48 static final int supportedOptions[] = { 49 PntAdm.VERBOSE, 50 PntAdm.RAW, 51 PntAdm.RESOURCE, 52 PntAdm.RESOURCE_CONFIG, 53 PntAdm.PATH 54 }; 55 56 /** 57 * Constructs a DisplayNetworkTable object. 58 */ 59 public DisplayNetworkTable() { 60 61 validOptions = supportedOptions; 62 63 } // constructor 64 65 /** 66 * Returns the option flag for this function. 67 * @returns the option flag for this function. 68 */ 69 public int getFunctionFlag() { 70 return (PntAdm.DISPLAY_NETWORK_TABLE); 71 } 72 73 /** 74 * Executes the "display network table" functionality. 75 * @return PntAdm.SUCCESS, PntAdm.ENOENT, PntAdm.WARNING, or 76 * PntAdm.CRITICAL 77 */ 78 public int execute() 79 throws IllegalArgumentException { 80 81 int returnCode = PntAdm.SUCCESS; 82 83 // Is this a verbose display? 84 // 85 boolean verbose = false; 86 if (options.isSet(PntAdm.VERBOSE)) { 87 verbose = true; 88 } 89 90 // Is this a raw display? 91 // 92 boolean raw = false; 93 if (options.isSet(PntAdm.RAW)) { 94 raw = true; 95 } 96 97 if (verbose && raw) { 98 String msg = getString("display_mode_error"); 99 throw new IllegalArgumentException(msg); 100 } 101 102 // Display the network table. 103 DhcpClientRecord [] dhcpClientRecords = null; 104 try { 105 106 Network network = getNetMgr().getNetwork(networkName); 107 if (network == null) { 108 printErrMessage(getString("network_name_error")); 109 return (PntAdm.WARNING); 110 } 111 112 dhcpClientRecords = 113 getNetMgr().loadNetwork(network.toString(), 114 getDhcpDatastore()); 115 } catch (NoTableException e) { 116 printErrMessage(getMessage(e)); 117 return (PntAdm.ENOENT); 118 } catch (Throwable e) { 119 printErrMessage(getMessage(e)); 120 return (PntAdm.WARNING); 121 } 122 123 Format.print(System.out, "%-8s\t", getString("Client_ID")); 124 Format.print(System.out, "%-4s\t", getString("Flags")); 125 Format.print(System.out, "%-8s\t", getString("Client_IP")); 126 Format.print(System.out, "%-8s\t", getString("Server_IP")); 127 Format.print(System.out, "%-25s\t", getString("Lease_Expiration")); 128 Format.print(System.out, "%-8s\t", getString("Macro")); 129 Format.print(System.out, "%s\n\n", getString("Comment")); 130 131 DhcpClientRecord dhcpClientRecord; 132 for (int i = 0; 133 dhcpClientRecords != null && i < dhcpClientRecords.length; 134 i++) { 135 136 dhcpClientRecord = dhcpClientRecords[i]; 137 Format.print(System.out, "%-8s\t", dhcpClientRecord.getClientId()); 138 Format.print(System.out, "%-4s\t", 139 dhcpClientRecord.getFlagString(verbose)); 140 141 String client; 142 if (verbose) { 143 client = dhcpClientRecord.getClientIP().getHostName(); 144 } else { 145 client = dhcpClientRecord.getClientIP().toString(); 146 } 147 Format.print(System.out, "%-8s\t", client); 148 149 String server; 150 if (verbose) { 151 server = dhcpClientRecord.getServerIP().getHostName(); 152 } else { 153 server = dhcpClientRecord.getServerIP().toString(); 154 } 155 Format.print(System.out, "%-8s\t", server); 156 157 String lease; 158 Date expiration = dhcpClientRecord.getExpiration(); 159 if (raw) { 160 // Print date in seconds since the epoch 161 lease = Long.toString(expiration.getTime()/1000); 162 } else if (expiration == null || expiration.getTime() == 0) { 163 lease = getString("Zero"); 164 } else if (expiration.getTime() < 0) { 165 lease = getString("Forever"); 166 } else if (verbose) { 167 // Print date and time for lease in locale format 168 lease = verboseFormat.format(expiration); 169 } else { 170 // Print just the lease date in short format for locale 171 lease = shortFormat.format(expiration); 172 } 173 Format.print(System.out, "%-25s\t", lease); 174 175 Format.print(System.out, "%-8s\t", dhcpClientRecord.getMacro()); 176 Format.print(System.out, "%s\n", dhcpClientRecord.getComment()); 177 } 178 179 return (returnCode); 180 181 } // execute 182 183 } // DisplayNetworkTable