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 2001-2002 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 package com.sun.dhcpmgr.cli.pntadm;
  27 
  28 import com.sun.dhcpmgr.cli.common.Util;
  29 import com.sun.dhcpmgr.data.DhcpClientRecord;
  30 import com.sun.dhcpmgr.data.Macro;
  31 import com.sun.dhcpmgr.data.Network;
  32 import com.sun.dhcpmgr.bridge.BridgeException;
  33 import com.sun.dhcpmgr.bridge.ExistsException;
  34 
  35 import java.lang.IllegalArgumentException;
  36 
  37 /**
  38  * The main class for the "add client" functionality of pntadm.
  39  */
  40 public class AddClientEntry extends PntAdmFunction {
  41 
  42     /**
  43      * The valid options associated with adding a client entry.
  44      */
  45     static final int supportedOptions[] = {
  46         PntAdm.COMMENT,
  47         PntAdm.LEASE_EXPIRATION,
  48         PntAdm.FLAGS,
  49         PntAdm.CLIENTID,
  50         PntAdm.CONVERT_CLIENTID,
  51         PntAdm.MACRO_NAME,
  52         PntAdm.VERIFY_MACRO,
  53         PntAdm.SERVER,
  54         PntAdm.RESOURCE,
  55         PntAdm.RESOURCE_CONFIG,
  56         PntAdm.PATH
  57     };
  58 
  59     /**
  60      * The client entry to add.
  61      */
  62     String clientIP;
  63 
  64     /**
  65      * Constructs a AddClientEntry object for the client, clientIP.
  66      * @param clientIP the client name or IP address.
  67      */
  68     public AddClientEntry(String clientIP) {
  69 
  70         this.clientIP = clientIP;
  71         validOptions = supportedOptions;
  72 
  73     } // constructor
  74 
  75     /**
  76      * Returns the option flag for this function.
  77      * @returns the option flag for this function.
  78      */
  79     public int getFunctionFlag() {
  80         return (PntAdm.ADD_CLIENT_ENTRY);
  81     }
  82 
  83     /**
  84      * Executes the "add client" functionality.
  85      * @return PntAdm.SUCCESS, PntAdm.EXISTS, PntAdm.WARNING, or
  86      * PntAdm.CRITICAL
  87      */
  88     public int execute()
  89         throws IllegalArgumentException {
  90 
  91         int returnCode = PntAdm.SUCCESS;
  92 
  93         // Build up a DhcpClientRecord from the user specified options.
  94         //
  95         try {
  96             DhcpClientRecord dhcpClientRecord = new DhcpClientRecord(clientIP);
  97 
  98             String clientId = options.valueOf(PntAdm.CLIENTID);
  99             boolean convertClientId = options.isSet(PntAdm.CONVERT_CLIENTID);
 100             if (convertClientId) {
 101                 if (clientId == null) {
 102                     String msg = getString("no_clientid_specified");
 103                     throw new IllegalArgumentException(msg);
 104                 }
 105                 clientId = Util.asciiToHex(clientId);
 106             }
 107             if (clientId != null) {
 108                 dhcpClientRecord.setClientId(clientId);
 109             }
 110 
 111             String flags = options.valueOf(PntAdm.FLAGS);
 112             if (flags != null) {
 113                 dhcpClientRecord.setFlags(flags);
 114             }
 115 
 116             String serverIP = options.valueOf(PntAdm.SERVER);
 117             if (serverIP == null) {
 118                 serverIP = getSvcMgr().getServerName();
 119             }
 120             dhcpClientRecord.setServerIP(serverIP);
 121 
 122             String expiration = options.valueOf(PntAdm.LEASE_EXPIRATION);
 123             if (expiration != null) {
 124                 dhcpClientRecord.setExpiration(shortFormat, expiration);
 125             }
 126 
 127             boolean verifyMacro = options.isSet(PntAdm.VERIFY_MACRO);
 128             String macro = options.valueOf(PntAdm.MACRO_NAME);
 129             if (verifyMacro) {
 130                 if (macro == null) {
 131                     String msg = getString("no_macro_specified");
 132                     throw new IllegalArgumentException(msg);
 133                 }
 134 
 135                 // Create a Macro entry so that we can check to see if it
 136                 // exists in the dhcptab.
 137                 //
 138                 try {
 139                     Macro existingMacro = getDhcptabMgr().getMacro(macro);
 140                 }
 141                 catch (BridgeException e) {
 142                     printErrMessage(getString("macro_not_found"));
 143                     return (PntAdm.WARNING);
 144                 }
 145             }
 146             if (macro != null) {
 147                 dhcpClientRecord.setMacro(macro);
 148             }
 149 
 150             String comment =  options.valueOf(PntAdm.COMMENT);
 151             if (comment != null) {
 152                 dhcpClientRecord.setComment(comment);
 153             }
 154 
 155             // Create a Network object.
 156             //
 157             Network network = getNetMgr().getNetwork(networkName);
 158             if (network == null) {
 159                 printErrMessage(getString("network_name_error"));
 160                 return (PntAdm.WARNING);
 161             }
 162 
 163             // Add the client. The host will be added to the hosts table
 164             // if necessary.
 165             //
 166             getNetMgr().addClient(dhcpClientRecord, network.toString(),
 167                 getDhcpDatastore());
 168         } catch (IllegalArgumentException e) {
 169             throw e;
 170         } catch (ExistsException e) {
 171             printErrMessage(getMessage(e));
 172             returnCode = PntAdm.EXISTS;
 173         } catch (Throwable e) {
 174             printErrMessage(getMessage(e));
 175             returnCode = PntAdm.WARNING;
 176         }
 177 
 178         return (returnCode);
 179 
 180     } // execute
 181 
 182 } // AddClientEntry