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 (c) 2001 by Sun Microsystems, Inc. 26 * All rights reserved. 27 */ 28 package com.sun.dhcpmgr.cli.pntadm; 29 30 import com.sun.dhcpmgr.cli.common.*; 31 32 import java.lang.IllegalArgumentException; 33 34 /** 35 * This class represents the entry point to the DHCP CLI network tables 36 * administration. 37 */ 38 public class PntAdm 39 extends DhcpCliProgram { 40 41 /** 42 * The program signature. 43 */ 44 public static final String SIGNATURE = "pntadm: "; 45 46 /** 47 * The valid options for all PntAdm administration. 48 */ 49 private static String optString = "LPCRyavxA:D:M:r:p:u:s:i:f:e:h:m:c:n:B;"; 50 51 public static final int ADD_CLIENT_ENTRY = 'A'; 52 public static final int MODIFY_CLIENT_ENTRY = 'M'; 53 public static final int DELETE_CLIENT_ENTRY = 'D'; 54 public static final int CREATE_NETWORK_TABLE = 'C'; 55 public static final int REMOVE_NETWORK_TABLE = 'R'; 56 public static final int DISPLAY_NETWORK_TABLE = 'P'; 57 public static final int LIST_NETWORK_TABLES = 'L'; 58 public static final int BATCH_EXECUTION = 'B'; 59 60 public static final int VERIFY_MACRO = 'y'; 61 public static final int DELETE_HOST = 'y'; 62 public static final int CONVERT_CLIENTID = 'a'; 63 public static final int RAW = 'x'; 64 public static final int VERBOSE = 'v'; 65 public static final int RESOURCE = 'r'; 66 public static final int RESOURCE_CONFIG = 'u'; 67 public static final int PATH = 'p'; 68 public static final int SERVER = 's'; 69 public static final int CLIENTID = 'i'; 70 public static final int FLAGS = 'f'; 71 public static final int LEASE_EXPIRATION = 'e'; 72 public static final int HOST_NAME = 'h'; 73 public static final int MACRO_NAME = 'm'; 74 public static final int COMMENT = 'c'; 75 public static final int NEW_IP = 'n'; 76 77 /** 78 * Constructs a pntadm command. 79 * @param args the options to the command. 80 */ PntAdm(String [] args)81 public PntAdm(String [] args) { 82 reset(args); 83 } // constructor 84 85 /** 86 * Resets a PntAdm for reuse. Used by DhcpBatch program. 87 * @param args the options to the command. 88 */ reset(String [] args)89 public void reset(String [] args) { 90 91 clearFunction(); 92 options = new DhcpCliOptions(); 93 this.args = args; 94 95 } 96 97 /** 98 * Returns the manpage signature for the program. 99 * @return the manpage signature for the program. 100 */ getManPage()101 public String getManPage() { 102 return "pntadm(1M)"; 103 } 104 105 /** 106 * Displays program usage. 107 */ usage()108 public void usage() { 109 110 DhcpCliPrint.printErrMessage(getString("usage")); 111 112 } // usage 113 114 /** 115 * Executes the program function. 116 * @return SUCCESS, EXISTS, ENOENT, WARNING, or CRITICAL 117 */ execute()118 public int execute() { 119 120 int returnCode = SUCCESS; 121 122 // Get the options and go exec the correct function. 123 // 124 GetOpt getopt = new GetOpt(args, optString); 125 try { 126 int option; 127 while ((option = getopt.getNextOption()) != -1) { 128 processArg(option, getopt.getOptionArg()); 129 } 130 131 int networkIndex = getopt.getNextOptionIndex(); 132 String network = null; 133 134 if (args.length == (networkIndex + 1)) { 135 network = args[networkIndex]; 136 } else if (args.length >= networkIndex + 1) { 137 throw new IllegalArgumentException( 138 ResourceStrings.getString("invalid_args")); 139 } 140 141 if (function == null) { 142 String msg = getString("no_function_error"); 143 throw new IllegalArgumentException(msg); 144 } 145 146 // Check the validity of the data store version. 147 // 148 if (!function.isVersionValid(false)) { 149 return (CRITICAL); 150 } 151 152 // Not all functions accept network arguments. 153 // 154 if (function instanceof ListNetworkTables || 155 function instanceof PntAdmBatch) { 156 if (network != null) { 157 String msg = getString("network_specified"); 158 throw new IllegalArgumentException(msg); 159 } 160 } else { 161 if (network == null) { 162 String msg = getString("no_network_specified"); 163 throw new IllegalArgumentException(msg); 164 } 165 } 166 167 // Create a DHCP datastore object with the user specified objects. 168 // 169 function.setDhcpDatastore(options.valueOf(RESOURCE), 170 options.valueOf(PATH), options.valueOf(RESOURCE_CONFIG)); 171 172 function.setOptions(options); 173 ((PntAdmFunction)function).setNetworkName(network); 174 returnCode = function.execute(); 175 176 } catch (IllegalArgumentException e) { 177 StringBuffer msg = new StringBuffer(SIGNATURE); 178 msg.append(DhcpCliFunction.getMessage(e)); 179 DhcpCliPrint.printErrMessage(msg.toString()); 180 DhcpCliPrint.printErrMessage(""); 181 usage(); 182 returnCode = CRITICAL; 183 } catch (Throwable e) { 184 StringBuffer msg = new StringBuffer(SIGNATURE); 185 msg.append(DhcpCliFunction.getMessage(e)); 186 DhcpCliPrint.printErrMessage(msg.toString()); 187 returnCode = CRITICAL; 188 } 189 190 return (returnCode); 191 192 } // execute 193 194 /** 195 * Processes one program argument. 196 * @param option the option flag 197 * @param value the option value(if any) 198 * @exception IllegalArgumentException if an invalid argument was entered 199 */ processArg(int option, String value)200 public void processArg(int option, String value) 201 throws IllegalArgumentException { 202 203 switch (option) { 204 case ADD_CLIENT_ENTRY: 205 setFunction(new AddClientEntry(value)); 206 break; 207 case MODIFY_CLIENT_ENTRY: 208 setFunction(new ModifyClientEntry(value)); 209 break; 210 case DELETE_CLIENT_ENTRY: 211 setFunction(new DeleteClientEntry(value)); 212 break; 213 case CREATE_NETWORK_TABLE: 214 setFunction(new CreateNetworkTable()); 215 break; 216 case REMOVE_NETWORK_TABLE: 217 setFunction(new RemoveNetworkTable()); 218 break; 219 case DISPLAY_NETWORK_TABLE: 220 setFunction(new DisplayNetworkTable()); 221 break; 222 case LIST_NETWORK_TABLES: 223 setFunction(new ListNetworkTables()); 224 break; 225 case BATCH_EXECUTION: 226 setFunction(new PntAdmBatch(value)); 227 break; 228 default: 229 options.setOption(option, value); 230 } 231 232 } // processArg 233 234 /** 235 * Returns a localized string for this function 236 * @param key the resource bundle string identifier 237 * @return string from resource bundle. 238 */ getString(String key)239 public String getString(String key) { 240 241 return ResourceStrings.getString(key); 242 243 } // getString 244 245 /** 246 * The entry point for the program. 247 * @param args the program arguments 248 */ main(String[] args)249 public static void main(String[] args) { 250 251 PntAdm pntadm = new PntAdm(args); 252 int returnCode = PntAdm.CRITICAL; 253 if (pntadm.isValidUser()) { 254 returnCode = pntadm.execute(); 255 } 256 System.exit(returnCode); 257 258 } // main 259 260 } // PntAdm 261