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) 1998-2001 by Sun Microsystems, Inc.
  24  * All rights reserved.
  25  */
  26 package com.sun.dhcpmgr.server;
  27 
  28 import com.sun.dhcpmgr.bridge.*;
  29 import com.sun.dhcpmgr.data.*;
  30 
  31 import java.net.InetAddress;
  32 import java.net.UnknownHostException;
  33 
  34 /**
  35  * This class provides the functionality to manage the DHCP network tables and
  36  * the hosts table.
  37  */
  38 public class DhcpNetMgrImpl implements DhcpNetMgr {
  39     private Bridge bridge;
  40 
  41     public DhcpNetMgrImpl(Bridge bridge) {
  42         this.bridge = bridge;
  43     }
  44 
  45     /**
  46      * Return the Network corresponding to the network string
  47      * @return a Network
  48      */
  49     public Network getNetwork(String network)
  50         throws BridgeException {
  51 
  52         return bridge.getNetwork(network);
  53     }
  54 
  55     /**
  56      * Return the list of networks currently known to DHCP
  57      * @return an array of Networks
  58      */
  59     public Network [] getNetworks() throws BridgeException {
  60         return getNetworks(null);
  61     }
  62 
  63     public Network [] getNetworks(DhcpDatastore datastore)
  64         throws BridgeException {
  65         return bridge.getNetworks(datastore);
  66     }
  67 
  68     /**
  69      * Return the list of addresses managed by DHCP on a given network
  70      * @param network the dotted-decimal representation of the network address
  71      * @return an array of records for the addresses defined on that network
  72      */
  73     public DhcpClientRecord [] loadNetwork(String network)
  74         throws BridgeException {
  75         return loadNetwork(network, null);
  76     }
  77 
  78     /**
  79      * Return the list of addresses managed by DHCP on a given network
  80      * @param network the dotted-decimal representation of the network address
  81      * @param datastore user-supplied datastore attributes
  82      * @return an array of records for the addresses defined on that network
  83      */
  84     public DhcpClientRecord [] loadNetwork(String network,
  85         DhcpDatastore datastore) throws BridgeException {
  86         return bridge.loadNetwork(network, datastore);
  87     }
  88 
  89     /**
  90      * Return the list of addresses managed by DHCP on a given network, with
  91      * the hostnames for each client looked up, too.
  92      * @param network the dotted-decimal representation of the network address
  93      * @return an array of records for the addresses defined on that network
  94      */
  95     public DhcpClientRecord [] loadNetworkCompletely(String network)
  96             throws BridgeException {
  97         DhcpClientRecord [] clients = loadNetwork(network);
  98         // Force loading of client name for each client
  99         for (int i = 0; i < clients.length; ++i) {
 100             clients[i].getClientName();
 101         }
 102         return clients;
 103     }
 104 
 105     /**
 106      * Modify an existing client record, and update the associated hosts
 107      * record if needed.
 108      * @param oldClient the existing record
 109      * @param newClient the new record
 110      * @param table the network on which the record is defined
 111      */
 112     public void modifyClient(DhcpClientRecord oldClient,
 113         DhcpClientRecord newClient, String table) throws BridgeException {
 114 
 115         modifyClient(oldClient, newClient, table, null);
 116     }
 117 
 118     /**
 119      * Modify an existing client record, and update the associated hosts
 120      * record if needed.
 121      * @param oldClient the existing record
 122      * @param newClient the new record
 123      * @param table the network on which the record is defined
 124      * @param datastore user-supplied datastore attributes
 125      */
 126     public void modifyClient(DhcpClientRecord oldClient,
 127         DhcpClientRecord newClient, String table, DhcpDatastore datastore)
 128         throws BridgeException {
 129 
 130         // Update the network table record
 131         bridge.modifyDhcpClientRecord(oldClient, newClient,
 132             table, datastore);
 133     }
 134 
 135     /**
 136      * Create a new record in the given table, and create a hosts record.
 137      * @param client the client to create
 138      * @param table the network on which to create the client
 139      */
 140     public void addClient(DhcpClientRecord client, String table)
 141         throws BridgeException {
 142 
 143         addClient(client, table, null);
 144     }
 145 
 146     /**
 147      * Create a new record in the given table, and create a hosts record.
 148      * @param client the client to create
 149      * @param table the network on which to create the client
 150      * @param datastore user-supplied datastore attributes
 151      */
 152     public void addClient(DhcpClientRecord client, String table,
 153         DhcpDatastore datastore) throws BridgeException {
 154 
 155         // Create the record in the per-network table
 156         bridge.createDhcpClientRecord(client, table, datastore);
 157 
 158     }
 159 
 160     /**
 161      * Delete a record from the given table, and delete the associated hosts
 162      * record if requested.
 163      * @param client the client to delete
 164      * @param table the network to delete the client from
 165      */
 166     public void deleteClient(DhcpClientRecord client, String table)
 167         throws BridgeException {
 168 
 169                 deleteClient(client, table, null);
 170         }
 171 
 172     /**
 173      * Delete a record from the given table, and delete the associated hosts
 174      * record if requested.
 175      * @param client the client to delete
 176      * @param table the network to delete the client from
 177      * @param datastore user-supplied datastore attributes
 178      */
 179     public void deleteClient(DhcpClientRecord client, String table,
 180         DhcpDatastore datastore)
 181         throws BridgeException {
 182 
 183         // Delete the client record from the per-network table
 184         bridge.deleteDhcpClientRecord(client, table, datastore);
 185     }
 186 
 187 
 188     /**
 189      * Retrieve a client record from the given table.
 190      * @param client the client to delete
 191      * @param table the network to delete the client from
 192      * @param datastore user-supplied datastore attributes
 193      */
 194     public DhcpClientRecord getClient(DhcpClientRecord client,
 195         String table, DhcpDatastore datastore) throws BridgeException {
 196 
 197         // Retrieve the client record from the per-network table
 198         DhcpClientRecord clientRecord =
 199             bridge.getDhcpClientRecord(client, table, datastore);
 200 
 201         return clientRecord;
 202     }
 203 
 204     /**
 205      * Create a new per-network table for the given network by converting the
 206      * one from the server's data store into a new data store.
 207      * @param network the network number in dotted-decimal form.
 208      * @param datastore user-supplied datastore attributes
 209      */
 210     public void cvtNetwork(String network,
 211         DhcpDatastore datastore) throws BridgeException {
 212         bridge.cvtNetwork(network, datastore);
 213     }
 214 
 215     /**
 216      * Create a new per-network table for the given network.
 217      * @param network the network number in dotted-decimal form.
 218      */
 219     public void createNetwork(String network)
 220         throws BridgeException {
 221 
 222         createNetwork(network, null);
 223     }
 224 
 225     /**
 226      * Create a new per-network table for the given network.
 227      * @param network the network number in dotted-decimal form.
 228      * @param datastore user-supplied datastore attributes
 229      */
 230     public void createNetwork(String network,
 231         DhcpDatastore datastore) throws BridgeException {
 232         bridge.createDhcpNetwork(network, datastore);
 233     }
 234 
 235     /**
 236      * Delete a per-network table, the macro associated with the network number,
 237      * and optionally deleting the associated hosts records.
 238      * @param network the network number in dotted-decimal form.
 239      * @param deleteMacro true if the network macro should be deleted
 240      */
 241     public void deleteNetwork(String network, boolean deleteMacro)
 242         throws BridgeException {
 243         deleteNetwork(network, deleteMacro, null);
 244     }
 245 
 246     /**
 247      * Delete a per-network table, the macro associated with the network number,
 248      * and optionally deleting the associated hosts records.
 249      * @param network the network number in dotted-decimal form.
 250      * @param deleteMacro true if the network macro should be deleted
 251      * @param datastore user-supplied datastore attributes
 252      */
 253     public void deleteNetwork(String network, boolean deleteMacro,
 254         DhcpDatastore datastore)
 255         throws BridgeException {
 256 
 257         // Delete network table, then the macro for the network
 258         bridge.deleteDhcpNetwork(network, datastore);
 259         try {
 260             if (deleteMacro) {
 261                 bridge.deleteDhcptabRecord(new Macro(network),
 262                     datastore);
 263             }
 264         } catch (Throwable e) {
 265             // All the errors here are ignorable
 266         }
 267     }
 268 }