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.dhcpconfig;
  27 
  28 import com.sun.dhcpmgr.cli.common.DhcpCliFunction;
  29 import com.sun.dhcpmgr.cli.common.Console;
  30 import com.sun.dhcpmgr.data.DhcpdOptions;
  31 import com.sun.dhcpmgr.data.DhcpDatastore;
  32 import com.sun.dhcpmgr.data.Network;
  33 import com.sun.dhcpmgr.bridge.ExistsException;
  34 
  35 public class ConvertDataStore extends DhcpCfgFunction {
  36 
  37     static final int supportedOptions[] = {
  38         DhcpCfg.FORCE,
  39         DhcpCfg.KEEP_TABLES,
  40         DhcpCfg.RESOURCE,
  41         DhcpCfg.RESOURCE_CONFIG,
  42         DhcpCfg.PATH
  43     };
  44 
  45     public ConvertDataStore() {
  46 
  47         validOptions = supportedOptions;
  48 
  49     } // constructor
  50 
  51     /**
  52      * Returns the option flag for this function.
  53      * @returns the option flag for this function.
  54      */
  55     public int getFunctionFlag() {
  56         return (DhcpCfg.CONVERT_DATA_STORE);
  57     }
  58 
  59     public int execute() throws IllegalArgumentException {
  60 
  61         // User must define both resource and path.
  62         //
  63         if (options.valueOf(DhcpCfg.RESOURCE) == null ||
  64             options.valueOf(DhcpCfg.PATH) == null) {
  65             String msg = getString("convert_null_datastore_error");
  66             throw new IllegalArgumentException(msg);
  67         }
  68 
  69         try {
  70             setDhcpDatastore(getSvcMgr().getDataStore(
  71                 options.valueOf(DhcpCfg.RESOURCE)));
  72             getDhcpDatastore().setLocation(options.valueOf(DhcpCfg.PATH));
  73             getDhcpDatastore().setConfig(
  74                 options.valueOf(DhcpCfg.RESOURCE_CONFIG));
  75         } catch (Throwable e) {
  76             // resource will not be valid
  77         }
  78 
  79 
  80         if (getDhcpDatastore() == null || !getDhcpDatastore().isEnabled()) {
  81             Object [] arguments = new Object[1];
  82             arguments[0] = getDhcpDatastore().getResource();
  83             printErrMessage(getString("convert_invalid_resource_error"),
  84                 arguments);
  85             return (DhcpCfg.FAILURE);
  86         }
  87 
  88         // Should we delete the dhcptab and the network tables after
  89         // they have been converted?
  90         //
  91         boolean deleteTables = !options.isSet(DhcpCfg.KEEP_TABLES);
  92 
  93         // Get the old configuration values.
  94         //
  95         DhcpdOptions dhcpdOptions = null;
  96         try {
  97             dhcpdOptions = getSvcMgr().readDefaults();
  98         } catch (Throwable e) {
  99             Object [] arguments = new Object[1];
 100             arguments[0] = getMessage(e);
 101             printErrMessage(getString("convert_conf_read_error"), arguments);
 102             return (DhcpCfg.FAILURE);
 103         }
 104 
 105         // If the source, destination, and version are all the same
 106         // then there is nothing to do ... report it as an error.
 107         //
 108         DhcpDatastore oldDatastore = dhcpdOptions.getDhcpDatastore();
 109         if (getDhcpDatastore().equals(oldDatastore)) {
 110             printErrMessage(getString("convert_same_datastore_error"));
 111             return (DhcpCfg.FAILURE);
 112         }
 113 
 114         // Confirm?
 115         //
 116         if (!options.isSet(DhcpCfg.FORCE)) {
 117             printMessage(getString("convert_explanation"));
 118             String confirmationMsg = getString("convert_confirmation");
 119             String affirmative = getString("affirmative");
 120             String negative = getString("negative");
 121             boolean doit = Console.promptUser(confirmationMsg, affirmative,
 122                 negative, true);
 123             if (!doit) {
 124                 return (DhcpCfg.FAILURE);
 125             }
 126         }
 127 
 128         // Create the location if it does not exist.
 129         //
 130         try {
 131             getSvcMgr().makeLocation(getDhcpDatastore());
 132         } catch (ExistsException e) {
 133             // this is o.k.
 134         } catch (Throwable e) {
 135             Object [] arguments = new Object[1];
 136             arguments[0] = getDhcpDatastore().getLocation();
 137             printErrMessage(getString("convert_make_location_error"),
 138                 arguments);
 139             return (DhcpCfg.FAILURE);
 140         }
 141 
 142         // Shut down the server if it is running
 143         //
 144         try {
 145             if (getSvcMgr().isServerRunning()) {
 146                 getSvcMgr().shutdown();
 147                 printMessage(getString("convert_shutdown_progress"));
 148             } else {
 149                 printMessage(getString("convert_no_shutdown_progress"));
 150             }
 151         } catch (Throwable e) {
 152             Object [] arguments = new Object[1];
 153             arguments[0] = getMessage(e);
 154             printErrMessage(getString("convert_shutdown_error"), arguments);
 155         }
 156 
 157         // Convert the dhcptab.
 158         //
 159         try {
 160             getDhcptabMgr().cvtDhcptab(getDhcpDatastore());
 161             printMessage(getString("convert_dhcptab_progress"));
 162         } catch (Throwable e) {
 163             Object [] arguments = new Object[1];
 164             arguments[0] = getMessage(e);
 165             printErrMessage(getString("convert_dhcptab_error"), arguments);
 166             deleteTables = false;
 167         }
 168 
 169         // Go get a list of the network tables to convert.
 170         //
 171         Network[] networks = new Network[0];
 172         try {
 173             networks = getNetMgr().getNetworks(oldDatastore);
 174             if (networks == null) {
 175                 networks = new Network[0];
 176             }
 177         } catch (Throwable e) {
 178             Object [] arguments = new Object[1];
 179             arguments[0] = getMessage(e);
 180             printErrMessage(getString("convert_get_nets_error"), arguments);
 181             deleteTables = false;
 182         }
 183 
 184         // Convert the network tables
 185         //
 186         for (int i = 0; i < networks.length; ++i) {
 187             String netString = networks[i].toString();
 188             try {
 189                 getNetMgr().cvtNetwork(netString, getDhcpDatastore());
 190                 Object [] arguments = new Object[1];
 191                 arguments[0] = netString;
 192                 printMessage(getString("convert_network_progress"), arguments);
 193             } catch (Throwable e) {
 194                 Object [] arguments = new Object[2];
 195                 arguments[0] = netString;
 196                 arguments[1] = getMessage(e);
 197                 printErrMessage(getString("convert_network_error"),
 198                     arguments);
 199             }
 200         }
 201 
 202         dhcpdOptions.setDhcpDatastore(getDhcpDatastore());
 203         try {
 204             getSvcMgr().writeDefaults(dhcpdOptions);
 205             printMessage(getString("convert_conf_update_progress"));
 206         } catch (Throwable e) {
 207             Object [] arguments = new Object[1];
 208             arguments[0] = getMessage(e);
 209             printErrMessage(getString("convert_conf_write_error"), arguments);
 210             deleteTables = false;
 211         }
 212 
 213         if (deleteTables) {
 214             // Delete the network tables
 215             //
 216             for (int i = 0; i < networks.length; ++i) {
 217                 String netString = networks[i].toString();
 218                 try {
 219                     getNetMgr().deleteNetwork(netString, false, oldDatastore);
 220                     Object [] arguments = new Object[1];
 221                     arguments[0] = netString;
 222                     printMessage(getString("convert_delete_network_progress"),
 223                         arguments);
 224                 } catch (Throwable e) {
 225                     Object [] arguments = new Object[2];
 226                     arguments[0] = netString;
 227                     arguments[1] = getMessage(e);
 228                     printErrMessage(getString("convert_delete_network_error"),
 229                         arguments);
 230                 }
 231             }
 232 
 233             // Delete the dhcptab
 234             //
 235             try {
 236                 getDhcptabMgr().deleteDhcptab(oldDatastore);
 237                 printMessage(getString("convert_delete_dhcptab_progress"));
 238             } catch (Throwable e) {
 239                 Object [] arguments = new Object[1];
 240                 arguments[0] = getMessage(e);
 241                 printErrMessage(getString("convert_delete_dhcptab_error"),
 242                     arguments);
 243             }
 244         }
 245 
 246         // Start it up.
 247         //
 248         if (dhcpdOptions.isDaemonEnabled()) {
 249             try {
 250                 getSvcMgr().startup();
 251                 printMessage(getString("convert_startup_progress"));
 252             } catch (Throwable e) {
 253                 Object [] arguments = new Object[1];
 254                 arguments[0] = getMessage(e);
 255                 printErrMessage(getString("convert_startup_error"), arguments);
 256             }
 257         }
 258 
 259         return (DhcpCfg.SUCCESS);
 260 
 261     } // execute
 262 
 263 } // ConvertDataStore