001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2014 ForgeRock AS.
025 */
026package org.forgerock.opendj.server.setup.model;
027
028import org.forgerock.i18n.LocalizableMessage;
029import org.forgerock.opendj.config.server.ConfigException;
030import org.forgerock.util.Reject;
031
032/**
033 * This class provides configuration's model for the OpenDJ3 setup.
034 */
035public abstract class Model {
036
037    /**
038     * This enumeration is used to know what kind of server we want to set up.
039     */
040    public enum Type {
041        /**
042         * Stand alone server.
043         */
044        STANDALONE,
045        /**
046         * First server in topology.
047         */
048        FIRST_IN_TOPOLOGY,
049        /**
050         * Replicate the new suffix with existing server.
051         */
052        IN_EXISTING_TOPOLOGY
053    }
054
055    private Type type;
056    private boolean startingServerAfterSetup;
057    private boolean isService;
058    private String instancePath;
059    private String installationPath;
060
061    private String license;
062    private ListenerSettings settings;
063    private RuntimeOptions serverRuntimeSettings;
064    private RuntimeOptions importLdifRuntimeSettings;
065    private DataConfiguration dataConfiguration;
066
067    private ReplicationConfiguration replicationConfiguration;
068
069    /**
070     * Returns the listener settings.
071     *
072     * @return The listener settings.
073     */
074    public ListenerSettings getListenerSettings() {
075        return settings;
076    }
077
078    /**
079     * Sets the listener settings.
080     *
081     * @param lSettings
082     *            The listener settings to set.
083     */
084    public void setListenerSettings(final ListenerSettings lSettings) {
085        settings = lSettings;
086    }
087
088    /**
089     * Returns {@code true} if this configuration has a non-empty license.
090     *
091     * @return {@code true} if this configuration has a license.
092     */
093    public boolean hasLicense() {
094        return license != null && !license.isEmpty();
095    }
096
097    /**
098     * Returns {@code true} if this configuration is stand alone data store.
099     *
100     * @return {@code true} if this configuration is stand alone data store.
101     */
102    boolean isStandAlone() {
103        return type == Type.STANDALONE;
104    }
105
106    /**
107     * Returns {@code true} if this configuration is a first server in a replication topology.
108     *
109     * @return {@code true} if this configuration is a first server in a replication topology.
110     */
111    boolean isFirstInTopology() {
112        return type == Type.FIRST_IN_TOPOLOGY;
113    }
114
115    /**
116     * Returns {@code true} if this configuration is part of a replication topology.
117     *
118     * @return {@code true} if this configuration is part of a replication topology.
119     */
120    boolean isPartOfReplicationTopology() {
121        return type == Type.IN_EXISTING_TOPOLOGY;
122    }
123
124    /**
125     * Returns {@code true} if this configuration has a certificate linked to it. That generally means SSL and/or SSL
126     * are activated.
127     *
128     * @return {@code true} if this configuration has a certificate linked to it.
129     */
130    boolean isSecure() {
131        return getListenerSettings() != null
132            && getListenerSettings().getCertificate() != null;
133    }
134
135    /**
136     * Sets this configuration as a stand alone data store.
137     */
138    void setStandAloneDS() {
139        setType(Type.STANDALONE);
140    }
141
142    /**
143     * Sets the type of this server as : replication activated and this is the first server in topology.
144     */
145    void setFirstInTopology() {
146        setType(Type.FIRST_IN_TOPOLOGY);
147    }
148
149    /**
150     * Sets the type of this server as : replication activated and this is a server in an existing topology.
151     */
152    void setInExistingTopology() {
153        setType(Type.IN_EXISTING_TOPOLOGY);
154    }
155
156    /**
157     * Returns the type of this configuration.
158     *
159     * @return The type of this configuration.
160     */
161    public Type getType() {
162        return type;
163    }
164
165    /**
166     * Sets the type of this configuration.
167     *
168     * @param mtype
169     *            The type of this configuration (standalone, etc...)
170     */
171    public void setType(Type mtype) {
172        type = mtype;
173    }
174
175    /**
176     * Returns {@code true} if the server must start after the installation.
177     *
178     * @return {@code true} if the server must start after the installation.
179     */
180    public boolean isStartingServerAfterSetup() {
181        return startingServerAfterSetup;
182    }
183
184    /**
185     * Sets if the server should start after its installation.
186     *
187     * @param startServerAfterSetup
188     *            {@code true} if the server must start after the installation.
189     */
190    public void setStartingServerAfterSetup(boolean startServerAfterSetup) {
191        startingServerAfterSetup = startServerAfterSetup;
192    }
193
194    /**
195     * Returns {@code true} if the directory server should start as a service.
196     *
197     * @return {@code true} if the directory server should start as a service, {@code false} otherwise.
198     */
199    public boolean isService() {
200        return isService;
201    }
202
203    /**
204     * Sets the directory server as a service.
205     *
206     * @param isAService
207     *            {@code true} if the directory server should start as a service, {@code false} otherwise.
208     */
209    public void setService(boolean isAService) {
210        isService = isAService;
211    }
212
213    /**
214     * Returns the instance path.
215     *
216     * @return The instance path where the binaries are installed.
217     */
218    public String getInstancePath() {
219        return instancePath;
220    }
221
222    /**
223     * Sets the current instance path location.
224     *
225     * @param iPath
226     *            The instance path.
227     */
228    public void setInstancePath(String iPath) {
229        instancePath = iPath;
230    }
231
232    /**
233     * Returns the license.
234     *
235     * @return The license.
236     */
237    public String getLicense() {
238        return license;
239    }
240
241    /**
242     * Sets the license linked to this installation.
243     *
244     * @param theLicense
245     *            The license to set.
246     */
247    public void setLicense(String theLicense) {
248        license = theLicense;
249    }
250
251    /**
252     * Returns the runtime options that apply to this installation.
253     *
254     * @return The runtime options that apply to this installation.
255     */
256    public RuntimeOptions getServerRuntimeSettings() {
257        return serverRuntimeSettings;
258    }
259
260    /**
261     * Sets the runtime options that apply to this installation.
262     *
263     * @param settings
264     *            The runtime options that apply to this installation.
265     */
266    public void setServerRuntimeOptions(RuntimeOptions settings) {
267        serverRuntimeSettings = settings;
268    }
269
270    /**
271     * Returns the runtime options that apply to the current import LDIF.
272     *
273     * @return The runtime options that apply to the current import LDIF.
274     */
275    public RuntimeOptions getImportLdifRuntimeOptions() {
276        return importLdifRuntimeSettings;
277    }
278
279    /**
280     * Sets the runtime options that apply to the current import LDIF.
281     *
282     * @param settings
283     *            The runtime options that apply to the current import LDIF.
284     */
285    public void setImportLdifRuntimeOptions(RuntimeOptions settings) {
286        importLdifRuntimeSettings = settings;
287    }
288
289    /**
290     * Returns the data configuration of this model.
291     *
292     * @return The data configuration of this model.
293     */
294    public DataConfiguration getDataConfiguration() {
295        return dataConfiguration;
296    }
297
298    /**
299     * Sets the data configuration of this model.
300     *
301     * @param dConfiguration
302     *            The data configuration to set for this model.
303     */
304    public void setDataConfiguration(DataConfiguration dConfiguration) {
305        dataConfiguration = dConfiguration;
306    }
307
308    /**
309     * Returns the replication configuration of this model.
310     *
311     * @return The replication configuration of this model.
312     */
313    public ReplicationConfiguration getReplicationConfiguration() {
314        return replicationConfiguration;
315    }
316
317    /**
318     * Sets the replication configuration of this model.
319     *
320     * @param replicationConfiguration
321     *            The replication configuration to set for this model.
322     */
323    public void setReplicationConfiguration(ReplicationConfiguration replicationConfiguration) {
324        this.replicationConfiguration = replicationConfiguration;
325    }
326
327    /**
328     * Returns the installation path of this model.
329     *
330     * @return The installation path of this model.
331     */
332    public String getInstallationPath() {
333        return installationPath;
334    }
335
336    /**
337     * Sets the installation path of this model.
338     *
339     * @param installationPath
340     *            The installation path of this model.
341     */
342    public void setInstallationPath(String installationPath) {
343        this.installationPath = installationPath;
344    }
345
346    /**
347     * Creates a basic data store model configuration for setup.
348     */
349    public static class DataStoreModel extends Model {
350        /**
351         * The default data store model.
352         */
353        public DataStoreModel() {
354            setStandAloneDS();
355            setDataConfiguration(new DataConfiguration());
356            setListenerSettings(new ListenerSettings());
357            setServerRuntimeOptions(RuntimeOptions.getDefault());
358            setImportLdifRuntimeOptions(RuntimeOptions.getDefault());
359            setStartingServerAfterSetup(true);
360        }
361    }
362
363    /**
364     * Checks the validity of the current setup configuration.
365     *
366     * @throws ConfigException
367     *             If this configuration is invalid.
368     */
369    void validate() throws ConfigException {
370        if (isFirstInTopology() || isPartOfReplicationTopology()) {
371            if (getReplicationConfiguration() == null) {
372                throw new ConfigException(LocalizableMessage.raw("No replication configuration found"));
373            }
374            if (isPartOfReplicationTopology()) {
375                Reject.ifNull(getReplicationConfiguration().getAdministrator(),
376                        "Administrator name should not be null");
377                Reject.ifNull(getReplicationConfiguration().getPassword(), "Admin password should not be null");
378                Reject.ifNull(getReplicationConfiguration().getGlobalAdministrator(),
379                        "Global administrator should not be null");
380                Reject.ifNull(getReplicationConfiguration().getGlobalAdministratorPassword(),
381                        "Global administrator should not be null");
382                if (getReplicationConfiguration().getSuffixes() == null
383                        || getReplicationConfiguration().getSuffixes().size() == 0) {
384                    throw new ConfigException(
385                            LocalizableMessage.raw("At least one base DN should be selected "
386                                    + "to replicate content with"));
387                }
388            }
389        }
390        final ListenerSettings settings = getListenerSettings();
391        final DataConfiguration dataConf = getDataConfiguration();
392        if (settings == null) {
393            throw new ConfigException(LocalizableMessage.raw("Invalid settings"));
394        }
395        if (dataConf == null) {
396            throw new ConfigException(LocalizableMessage.raw("Invalid data configuration"));
397        }
398        if (dataConf.isImportLDIF() && dataConf.getLdifImportDataPath() == null) {
399            throw new ConfigException(LocalizableMessage.raw("Invalid import ldif file."));
400        }
401        if (settings.getPasswordFile() == null && settings.getPassword() == null) {
402            throw new ConfigException(LocalizableMessage.raw("A password must be set for the root DN."));
403        }
404    }
405
406}