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