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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25 package com.sun.dhcpmgr.cli.dhcpconfig;
26
27 import com.sun.dhcpmgr.cli.common.DhcpCliFunction;
28 import com.sun.dhcpmgr.data.IPAddress;
29 import com.sun.dhcpmgr.data.Network;
30 import com.sun.dhcpmgr.data.ValidationException;
31 import com.sun.dhcpmgr.data.StandardOptions;
32
33 /**
34 * The main class for the "configure network" functionality of dhcpconfig.
35 */
36 public class ConfigureNetwork extends DhcpCfgFunction {
37
38 /**
39 * The valid options associated with configuring a network.
40 */
41 static final int supportedOptions[] = {
42 DhcpCfg.SUBNET_MASK,
43 DhcpCfg.POINT_TO_POINT,
44 DhcpCfg.ROUTER_ADDRESSES,
45 DhcpCfg.NIS_DOMAIN,
46 DhcpCfg.NIS_ADDRESSES,
47 DhcpCfg.SIGHUP
48 };
49
50 /**
51 * The address of the network to configure.
52 */
53 String address;
54
55 /**
56 * Constructs a ConfigureNetwork object.
57 * @param address the address of the network to configure
58 */
59 public ConfigureNetwork(String address) {
60
61 validOptions = supportedOptions;
62 this.address = address;
63
64 } // constructor
65
66 /**
67 * Returns the option flag for this function.
68 * @returns the option flag for this function.
69 */
70 public int getFunctionFlag() {
71 return (DhcpCfg.CONFIGURE_NETWORK);
72 }
73
74 /**
75 * Executes the "configure network" functionality.
76 * @return DhcpCfg.SUCCESS or DhcpCfg.FAILURE
77 */
78 public int execute() {
79
80 // Make sure that server is configured as a DHCP server.
81 //
82 if (!isServerConfigured()) {
83 return (DhcpCfg.FAILURE);
84 }
85
86 // Check the validity of the data store version.
87 //
88 if (!isVersionValid(false)) {
89 return (DhcpCfg.FAILURE);
90 }
91
92 // Validate the network address
93 //
94 Network network;
95 try {
96 network = getNetMgr().getNetwork(address);
97 } catch (Throwable e) {
98 Object [] arguments = new Object[1];
99 arguments[0] = address;
100 printErrMessage(getString("cfgnet_invalid_network_error"),
101 arguments);
102 return (DhcpCfg.FAILURE);
103 }
104
105 // Determine/validate the subnet mask.
106 //
107 IPAddress netmask = null;
108 String mask = options.valueOf(DhcpCfg.SUBNET_MASK);
109 if (mask != null) {
110 try {
111 netmask = new IPAddress(mask);
112 network.setMask(netmask);
113 } catch (ValidationException e) {
114 Object [] arguments = new Object[1];
115 arguments[0] = address;
116 printErrMessage(getString("cfgnet_invalid_ip_error"),
117 arguments);
118 return (DhcpCfg.FAILURE);
119 }
120 }
121
122 boolean isLan = !options.isSet(DhcpCfg.POINT_TO_POINT);
123
124 // Get the list of router addresses
125 //
126 IPAddressList routers = null;
127 if (options.isSet(DhcpCfg.ROUTER_ADDRESSES)) {
128 try {
129 String addrs = options.valueOf(DhcpCfg.ROUTER_ADDRESSES);
130 routers = new IPAddressList(addrs);
131 } catch (ValidationException e) {
132 printErrMessage(getMessage(e));
133 return (DhcpCfg.FAILURE);
134 }
135 }
136
137 // Get the NIS info.
138 //
139 String nisDomain = options.valueOf(DhcpCfg.NIS_DOMAIN);
140 String nisServers = options.valueOf(DhcpCfg.NIS_ADDRESSES);
141 if ((nisDomain == null) != (nisServers == null)) {
142 String msg = getString("cfgnet_nis_error");
143 throw new IllegalArgumentException(msg);
144 }
145
146 IPAddressList nisAddresses = null;
147 try {
148 if (nisDomain == null) {
149 nisDomain = getSvcMgr().getStringOption(
150 StandardOptions.CD_NIS_DOMAIN, "");
151 }
152 if (nisServers != null) {
153 nisAddresses = new IPAddressList(nisServers);
154 } else {
155 nisAddresses = new IPAddressList(
156 getSvcMgr().getIPOption(StandardOptions.CD_NIS_SERV, ""));
157 }
158 } catch (ValidationException e) {
159 Object [] arguments = new Object[1];
160 arguments[0] = getMessage(e);
161 printErrMessage(getString("cfgnet_nis_server_error"), arguments);
162 return (DhcpCfg.FAILURE);
163 } catch (Throwable e) {
164 // Ignore, NIS info will not be configured in the network macro.
165 }
166
167 // Create the network macro in the dhcptab
168 //
169 try {
170 IPAddress[] routersArray = null;
171 if (routers != null) {
172 routersArray = routers.toIPAddressArray();
173 }
174 getDhcptabMgr().createNetworkMacro(network, routersArray,
175 isLan, nisDomain, nisAddresses);
176 Object [] arguments = new Object[1];
177 arguments[0] = network.toString();
178 printMessage(getString("cfgnet_network_macro_progress"),
179 arguments);
180 } catch (Throwable e) {
181 Object [] arguments = new Object[1];
182 arguments[0] = getMessage(e);
183 printErrMessage(getString("cfgnet_network_macro_error"),
184 arguments);
185 return (DhcpCfg.FAILURE);
186 }
187
188
189 // Create the network table for this network
190 //
191 try {
192 getNetMgr().createNetwork(network.toString());
193 printMessage(getString("cfgnet_network_table_progress"));
194 } catch (Throwable e) {
195 Object [] arguments = new Object[1];
196 arguments[0] = getMessage(e);
197 printErrMessage(getString("cfgnet_network_table_error"),
198 arguments);
199 return (DhcpCfg.FAILURE);
200 }
201
202 // Signal the server if the user asked us to
203 try {
204 if (options.isSet(DhcpCfg.SIGHUP)) {
205 getSvcMgr().reload();
206 }
207 } catch (Throwable e) {
208 printErrMessage(getString("sighup_failed"));
209 return (DhcpCfg.FAILURE);
210 }
211
212 return (DhcpCfg.SUCCESS);
213
214 } // execute
215
216 } // ConfigureNetwork