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 (c) 2001 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 package com.sun.dhcpmgr.cli.common;
29 
30 import com.sun.dhcpmgr.data.DhcpDatastore;
31 import com.sun.dhcpmgr.data.Option;
32 import com.sun.dhcpmgr.data.StandardOptions;
33 import com.sun.dhcpmgr.server.DhcpMgr;
34 import com.sun.dhcpmgr.server.DhcpMgrImpl;
35 import com.sun.dhcpmgr.server.DhcpServiceMgr;
36 import com.sun.dhcpmgr.server.DhcpNetMgr;
37 import com.sun.dhcpmgr.server.DhcptabMgr;
38 import com.sun.dhcpmgr.bridge.*;
39 
40 import java.lang.IllegalArgumentException;
41 
42 /**
43  * This class is the base class extended by the DHCP CLI subcommand classes.
44  */
45 public abstract class DhcpCliFunction {
46 
47     /**
48      * The options for the function.
49      */
50     protected DhcpCliOptions options = null;
51     protected int validOptions[];
52 
53     /**
54      * Handles to the managers.
55      */
56     static private DhcpMgr dhcpMgr = null;
57     static private DhcpNetMgr netMgr = null;
58     static private DhcptabMgr dhcptabMgr = null;
59     static private DhcpServiceMgr svcMgr = null;
60 
61     /**
62      * The DhcpDatastore to be used for this function. A value of 'null'
63      * means to just use DHCP defaults.
64      */
65     private DhcpDatastore datastore = null;
66 
67     /**
68      * Constructor.
69      */
DhcpCliFunction()70     public DhcpCliFunction() {
71 	dhcpMgr = new DhcpMgrImpl();
72     } // constructor
73 
74     /**
75      * Get a handle to the DhcpNetMgr
76      * @return an instance of DhcpNetMgr
77      */
getDhcpMgr()78     public static DhcpMgr getDhcpMgr() {
79 	return dhcpMgr;
80     } // getDhcpMgr
81 
82     /**
83      * Get a handle to the DhcpNetMgr
84      * @return an instance of DhcpNetMgr
85      */
getNetMgr()86     public static DhcpNetMgr getNetMgr() {
87 	if (netMgr == null) {
88 	    netMgr = dhcpMgr.getNetMgr();
89 	}
90 	return netMgr;
91     } // getNetMgr
92 
93     /**
94      * Get a handle to the DhcptabMgr
95      * @return an instance of DhcptabMgr
96      */
getDhcptabMgr()97     public static DhcptabMgr getDhcptabMgr() {
98 	if (dhcptabMgr == null) {
99 	    dhcptabMgr = dhcpMgr.getDhcptabMgr();
100 	}
101 	return dhcptabMgr;
102     } // getDhcptabMgr
103 
104     /**
105      * Get a handle to the DhcpServiceMgr
106      * @return an instance of DhcpServiceMgr
107      */
getSvcMgr()108     public static DhcpServiceMgr getSvcMgr() {
109 	if (svcMgr == null) {
110 	    svcMgr = dhcpMgr.getDhcpServiceMgr();
111 	}
112 	return svcMgr;
113     } // getSvcMgr
114 
115     /**
116      * Used to execute the subcommand functionality.
117      */
execute()118     public abstract int execute()
119 	throws IllegalArgumentException;
120 
121     /**
122      * Returns the option flag for this function.
123      * @returns the option flag for this function.
124      */
getFunctionFlag()125     public abstract int getFunctionFlag();
126 
127     /**
128      * Returns the DhcpDatastore associated with this function.
129      * @returns the DhcpDatastore associated with this function.
130      */
getDhcpDatastore()131     public DhcpDatastore getDhcpDatastore() {
132 	return datastore;
133     } // getDhcpDatastore
134 
135     /**
136      * Sets the DhcpDatastore associated with this function.
137      * @param ds the data store
138      */
setDhcpDatastore(DhcpDatastore ds)139     public void setDhcpDatastore(DhcpDatastore ds) {
140 	datastore = ds;
141     } // setDhcpDatastore
142 
143     /**
144      * Sets the DhcpDatastore associated with this function.
145      * @param r the data store resource
146      * @param l the data store location
147      * @param a the data store config
148      */
setDhcpDatastore(String r, String l, String a)149     public void setDhcpDatastore(String r, String l, String a) {
150 	datastore = createDhcpDatastore(r, l, a);
151     } // setDhcpDatastore
152 
153     /**
154      * Create a DhcpDatastore with the given argument values as attributes.
155      * @param r the data store resource
156      * @param l the data store location
157      * @param a the data store config
158      * @returns a DhcpDatastore with the given argument values as attributes
159      * or null if all arguments are null.
160      */
createDhcpDatastore(String r, String l, String a)161     public DhcpDatastore createDhcpDatastore(String r, String l, String a) {
162 	return this.createDhcpDatastore(r, l, a, -1);
163     } // createDhcpDatastore
164 
165     /**
166      * Create a DhcpDatastore with the given argument values as attributes.
167      * @param r the data store resource
168      * @param l the data store location
169      * @param a the data store config
170      * @param v the data store resource version
171      * @returns a DhcpDatastore with the given argument values as attributes
172      * or null if all arguments are null.
173      */
createDhcpDatastore(String r, String l, String a, int v)174     public DhcpDatastore createDhcpDatastore(String r, String l, String a,
175 	int v) {
176 
177 	DhcpDatastore datastore = null;
178 
179 	if (r != null || l != null || a != null || v != -1) {
180 	    datastore = new DhcpDatastore(r, l, a, v);
181 	}
182 
183 	return datastore;
184     } // createDhcpDatastore
185 
186     /**
187      * Used to determine whether or not the data store version is valid.
188      */
isVersionValid(boolean ignoreAbsentDefaults)189     public boolean isVersionValid(boolean ignoreAbsentDefaults) {
190 
191 	boolean isValid = false;
192 	try {
193 	    isValid = getSvcMgr().isVersionCurrent();
194 
195 	    if (!isValid) {
196 		printCmnErrMessage("need_to_convert_datastore");
197 	    }
198 	} catch (NoDefaultsException e) {
199 	    if (!ignoreAbsentDefaults) {
200 		printCmnErrMessage("no_conf_warning");
201 	    }
202 	    isValid = true;
203 	} catch (Throwable e) {
204 	    printCmnErrMessage(e.getMessage());
205 	}
206 
207 	return isValid;
208 
209     } // isVersionValid
210 
211     /**
212      * Used to set the read the STANDARD options from the DHCP inittab and
213      * to initialize the StandardOptions table with these options.
214      */
setStandardOptions()215     public void setStandardOptions() {
216 	try {
217 	   StandardOptions.setAllOptions(getSvcMgr().getInittabOptions(
218 		Option.ctxts[Option.STANDARD].getCode()));
219 	} catch (Throwable e) {
220 	    printCmnErrMessage(e.getMessage());
221 	}
222     } // setStandardOptions
223 
224     /**
225      * Used to set the options for this function. Validates that the options
226      * received were only options that are legitimate for this subcommand.
227      * @param options the options object built as a result of parsing
228      * command line input.
229      */
setOptions(DhcpCliOptions options)230     public void setOptions(DhcpCliOptions options)
231 	throws IllegalArgumentException {
232 
233 	options.validate(validOptions);
234 	this.options = options;
235 
236     } // setOptions
237 
238     /**
239      * Uses the resourceKey to retrieve a string from a ResourceBundle (if
240      * one exists) and prints the string to the console.
241      * @param resourceKey the ResourceBundle key value.
242      */
printCmnMessage(String resourceKey)243     public void printCmnMessage(String resourceKey) {
244 	DhcpCliPrint.printMessage(ResourceStrings.getString(resourceKey));
245     } // printMessage
246 
247     /**
248      * Uses the resourceKey to retrieve a string from a ResourceBundle (if
249      * one exists) and prints the string to the console error stream.
250      * @param resourceKey the ResourceBundle key value.
251      */
printCmnErrMessage(String resourceKey)252     public void printCmnErrMessage(String resourceKey) {
253 	DhcpCliPrint.printErrMessage(ResourceStrings.getString(resourceKey));
254     } // printErrMessage
255 
256     /**
257      * Given a Throwable object, returns a message for it.
258      * @param e the Throwable object.
259      * @returns a message.
260      */
getMessage(Throwable e)261     public static String getMessage(Throwable e) {
262 	String message = e.getMessage();
263 	if (message == null) {
264 	    message = e.toString();
265 	}
266 	return message;
267     } // getMessage
268 
269 } // DhcpCliFunction
270