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 2002 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 package com.sun.dhcpmgr.cli.dhcpconfig;
  29 
  30 import com.sun.dhcpmgr.cli.common.DhcpCliFunction;
  31 import com.sun.dhcpmgr.data.DhcpdOptions;
  32 import com.sun.dhcpmgr.data.qualifier.*;
  33 import com.sun.dhcpmgr.server.*;
  34 import com.sun.dhcpmgr.bridge.BridgeException;
  35 
  36 /**
  37  * Functions for handling all dhcpconfig options that manage the DHCP server
  38  * parameters.
  39  */
  40 public class ConfigureService extends DhcpCfgFunction {
  41 
  42     /**
  43      * Return codes.
  44      *
  45      * These supplement the inherited SUCCESS and FAILURE return codes.
  46      * Note that these values are binary!!! The next value would be 4.
  47      */
  48     public static final int ENABLED     = 1;
  49     public static final int DISABLED    = 0;
  50     public static final int RUNNING     = 2;
  51     public static final int STOPPED     = 0;
  52 
  53     /**
  54      * Options that this DhcpCfgFunction will accept.
  55      */
  56     static final int supportedOptions[] = {
  57         DhcpCfg.SERVICE_ENABLE,
  58         DhcpCfg.SERVICE_DISABLE,
  59         DhcpCfg.SERVICE_REENABLE,
  60         DhcpCfg.SERVICE_QUERY,
  61     };
  62 
  63     public ConfigureService() {
  64         validOptions = supportedOptions;
  65     } // constructor
  66 
  67     /**
  68      * Returns the option flag for this function.
  69      * @returns
  70      *   The option flag for this function.
  71      */
  72     public int getFunctionFlag() {
  73         return DhcpCfg.CONFIGURE_SERVICE;
  74     } // getFunctionFlag
  75 
  76     /**
  77      * Parse and execute the options for this function.
  78      * @return
  79      *   DhcpCfg.SUCCESS for success and DhcpCfg.FAILURE for failure.
  80      */
  81     public int execute() throws IllegalArgumentException {
  82         Action action = null;
  83         boolean enableOptionSet;
  84         boolean disableOptionSet;
  85         boolean reenableOptionSet;
  86         boolean queryOptionSet;
  87         boolean[] possibleOptions =  {
  88             enableOptionSet = options.isSet(DhcpCfg.SERVICE_ENABLE),
  89             disableOptionSet = options.isSet(DhcpCfg.SERVICE_DISABLE),
  90             reenableOptionSet = options.isSet(DhcpCfg.SERVICE_REENABLE),
  91             queryOptionSet = options.isSet(DhcpCfg.SERVICE_QUERY)
  92         };
  93 
  94         int numOptionsSet = 0;
  95         for (int index = 0; index < possibleOptions.length; index++) {
  96             if (possibleOptions[index]) {
  97                 numOptionsSet++;
  98             }
  99         }
 100 
 101         if (numOptionsSet != 1) {
 102             String msg = getString("config_service_bad_action_error");
 103             throw new IllegalArgumentException(msg);
 104         }
 105 
 106         if (enableOptionSet) {
 107             action = new ActionEnable();
 108         } else if (disableOptionSet) {
 109             action = new ActionDisable();
 110         } else if (reenableOptionSet) {
 111             action = new ActionReenable();
 112         } else {
 113             action = new ActionQuery();
 114         }
 115 
 116         return action.execute();
 117     } // execute
 118 
 119     /**
 120      * All functions are carried out through a specific action sub-classed
 121      * from this class. 
 122      */
 123     private interface Action {
 124         /**
 125          * Execute the action.
 126          *
 127          * @return
 128          *   DhcpCfg.SUCCESS for success and DhcpCfg.FAILURE for failure.
 129          */
 130         public int execute();
 131     }
 132 
 133     /**
 134      * Shared super class for actions.
 135      */
 136     private abstract class ActionImpl implements Action {
 137         /**
 138          * Server parameters.
 139          */
 140         protected DhcpdOptions dhcpdOptions;
 141 
 142         /**
 143          * Service manager.
 144          */
 145         protected DhcpServiceMgr dhcpServiceMgr;
 146 
 147         /**
 148          * Validate and execute the action. A sub-classed action is passed
 149          * execution control via the doExecute() callback method.
 150          *
 151          * @return
 152          *   DhcpCfg.SUCCESS for success and DhcpCfg.FAILURE for failure.
 153          */
 154         public final int execute() {
 155             dhcpServiceMgr = getSvcMgr();
 156 
 157             try {
 158                 dhcpdOptions = dhcpServiceMgr.readDefaults();
 159             } catch (BridgeException be) {
 160                 printErrMessage(
 161                         getString("config_service_failed_read_params_error"));
 162                 return DhcpCfg.FAILURE;
 163             }
 164 
 165             dhcpdOptions.clearDirty();
 166 
 167             int result = doExecute();
 168 
 169             if (result != DhcpCfg.FAILURE) {
 170                 if (dhcpdOptionsWrite() == DhcpCfg.FAILURE) {
 171                     return DhcpCfg.FAILURE;
 172                 }
 173             }
 174 
 175             return result;
 176         } // execute
 177 
 178         /**
 179          * Sub-classed action callback method. Once validation has been
 180          * performed execution is continued in the action sub-class by calling
 181          * this method.
 182          *
 183          * @return
 184          *   DhcpCfg.SUCCESS for success, otherwise DhcpCfg.FAILURE for failure.
 185          */
 186         protected abstract int doExecute();
 187 
 188         /**
 189          * Ensures that changes to the parameter are written back to the data
 190          * store.
 191          *
 192          * @return
 193          *   DhcpCfg.SUCCESS for success, otherwise DhcpCfg.FAILURE for failure.
 194          */
 195         protected int dhcpdOptionsWrite() {
 196             if (dhcpdOptions.isDirty()) {
 197                 try {
 198                     dhcpServiceMgr.writeDefaults(dhcpdOptions);
 199                 } catch (BridgeException e) {
 200                     printErrMessage(getString(
 201                                 "config_service_failed_write_params_error"));
 202                     return DhcpCfg.FAILURE;
 203                 }
 204 
 205                 dhcpdOptions.clearDirty();
 206             }
 207 
 208             return DhcpCfg.SUCCESS;
 209         }
 210     }
 211 
 212     /**
 213      * Enable the DHCP service.
 214      */
 215     private class ActionEnable extends ActionImpl {
 216         /**
 217          * Enable the DHCP service.
 218          *
 219          * @return
 220          *   DhcpCfg.SUCCESS for success, otherwise DhcpCfg.FAILURE for failure.
 221          */
 222         protected int doExecute() {
 223             if (!dhcpdOptions.isDaemonEnabled()) {
 224                 dhcpdOptions.setDaemonEnabled(true);
 225                 if (dhcpdOptionsWrite() == DhcpCfg.FAILURE) {
 226                     return DhcpCfg.FAILURE;
 227                 }
 228                 printMessage(getString("config_service_state_enabled"));
 229             }
 230 
 231             try {
 232                 if (!dhcpServiceMgr.isServerRunning()) {
 233                     dhcpServiceMgr.startup();
 234                     printMessage(getString("config_service_state_startup"));
 235                 }
 236             } catch (BridgeException e) {
 237                 printErrMessage(
 238                         getString("config_service_failed_startup_error"));
 239                 return DhcpCfg.FAILURE;
 240             }
 241 
 242             return DhcpCfg.SUCCESS;
 243         } // doExecute
 244     }
 245 
 246     /**
 247      * Disable the DHCP service.
 248      */
 249     private class ActionDisable extends ActionImpl {
 250         /**
 251          * Disable the DHCP service.
 252          *
 253          * @return
 254          *   DhcpCfg.SUCCESS for success, otherwise DhcpCfg.FAILURE for failure.
 255          */
 256         protected int doExecute() {
 257             if (dhcpdOptions.isDaemonEnabled()) {
 258                 dhcpdOptions.setDaemonEnabled(false);
 259                 if (dhcpdOptionsWrite() == DhcpCfg.FAILURE) {
 260                     return DhcpCfg.FAILURE;
 261                 }
 262                 printMessage(getString("config_service_state_disabled"));
 263             }
 264 
 265             try {
 266                 if (dhcpServiceMgr.isServerRunning()) {
 267                     dhcpServiceMgr.shutdown();
 268                     printMessage(getString("config_service_state_shutdown"));
 269                 }
 270             } catch (BridgeException e) {
 271                 printErrMessage(
 272                         getString("config_service_failed_shutdown_error"));
 273                 return DhcpCfg.FAILURE;
 274             }
 275 
 276             return DhcpCfg.SUCCESS;
 277         } // doExecute
 278     }
 279 
 280     /**
 281      * Reenable the DHCP service.
 282      */
 283     private class ActionReenable extends ActionImpl {
 284         /**
 285          * Reenable the DHCP service.
 286          *
 287          * @return
 288          *   DhcpCfg.SUCCESS for success, otherwise DhcpCfg.FAILURE for failure.
 289          */
 290         protected int doExecute() {
 291             try {
 292                 if (dhcpServiceMgr.isServerRunning()) {
 293                     dhcpServiceMgr.shutdown();
 294                     printMessage(getString("config_service_state_shutdown"));
 295                 }
 296             } catch (BridgeException e) {
 297                 printErrMessage(
 298                         getString("config_service_failed_shutdown_error"));
 299                 return DhcpCfg.FAILURE;
 300             }
 301 
 302             if (!dhcpdOptions.isDaemonEnabled()) {
 303                 dhcpdOptions.setDaemonEnabled(true);
 304                 if (dhcpdOptionsWrite() == DhcpCfg.FAILURE) {
 305                     return DhcpCfg.FAILURE;
 306                 }
 307                 printMessage(getString("config_service_state_enabled"));
 308             }
 309 
 310             try {
 311                 dhcpServiceMgr.startup();
 312                 printMessage(getString("config_service_state_startup"));
 313             } catch (BridgeException e) {
 314                 printErrMessage(
 315                         getString("config_service_failed_startup_error"));
 316                 return DhcpCfg.FAILURE;
 317             }
 318 
 319             return DhcpCfg.SUCCESS;
 320         } // doExecute
 321     }
 322 
 323     /**
 324      * Query the DHCP service.
 325      */
 326     private class ActionQuery extends ActionImpl {
 327         /**
 328          * Query the DHCP service.
 329          *
 330          * @return
 331          *   DhcpCfg.SUCCESS for success, otherwise DhcpCfg.FAILURE for failure.
 332          */
 333         protected int doExecute() {
 334             int serviceState = DhcpCfg.SUCCESS;
 335 
 336             if (dhcpdOptions.isDaemonEnabled()) {
 337                 printMessage(getString("config_service_state_enabled"));
 338                 serviceState += ENABLED;
 339             } else {
 340                 printMessage(getString("config_service_state_disabled"));
 341                 serviceState += DISABLED;
 342             }
 343 
 344             try {
 345                 if (dhcpServiceMgr.isServerRunning()) {
 346                     printMessage(getString("config_service_state_running"));
 347                     serviceState += RUNNING;
 348                 } else {
 349                     printMessage(getString("config_service_state_stopped"));
 350                     serviceState += STOPPED;
 351                 }
 352             } catch (BridgeException e) {
 353                 printErrMessage(getString("config_service_failed_query_error"));
 354                 return DhcpCfg.FAILURE;
 355             }
 356 
 357             return serviceState;
 358         } // doExecute
 359     }
 360 
 361 
 362 } // ConfigureService