001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2015 ForgeRock AS.
015 */
016package org.opends.server.tools;
017
018import java.io.File;
019import java.util.Collection;
020import java.util.LinkedList;
021import java.util.List;
022
023import org.forgerock.opendj.config.LDAPProfile;
024import org.forgerock.opendj.config.ManagedObjectDefinition;
025import org.forgerock.opendj.config.client.ManagementContext;
026import org.forgerock.opendj.config.client.ldap.LDAPManagementContext;
027import org.forgerock.opendj.ldap.DN;
028import org.forgerock.opendj.server.config.client.BackendCfgClient;
029import org.forgerock.opendj.server.config.client.BackendIndexCfgClient;
030import org.forgerock.opendj.server.config.client.PluggableBackendCfgClient;
031import org.forgerock.opendj.server.config.client.RootCfgClient;
032import org.forgerock.opendj.server.config.meta.BackendCfgDefn.WritabilityMode;
033import org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn;
034import org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType;
035import org.forgerock.opendj.server.config.server.BackendCfg;
036import org.opends.guitools.controlpanel.util.Utilities;
037import org.opends.quicksetup.Installation;
038
039/** Utility class which can be used by tools to create a new backend with default indexes. */
040public class BackendCreationHelper
041{
042  /** Describes an attribute index which should be created during installation. */
043  public static final class DefaultIndex
044  {
045    static DefaultIndex withEqualityAndSubstring(final String name)
046    {
047      return new DefaultIndex(name, true);
048    }
049
050    static DefaultIndex withEquality(final String name)
051    {
052      return new DefaultIndex(name, false);
053    }
054
055    private final String name;
056    private final boolean shouldCreateSubstringIndex;
057
058    private DefaultIndex(final String name, final boolean substringIndex)
059    {
060      this.name = name;
061      this.shouldCreateSubstringIndex = substringIndex;
062    }
063
064    /**
065     * Return the name of this default index.
066     *
067     * @return The name of this default index
068     */
069    public String getName()
070    {
071      return name;
072    }
073
074    /**
075     * Return {@code true} if the substring index type should be enabled for
076     * this index.
077     *
078     * @return {@code true} if the substring index type should be enabled for
079     *         this index.
080     */
081    public boolean shouldCreateSubstringIndex()
082    {
083      return shouldCreateSubstringIndex;
084    }
085  }
086
087  /** Default indexes to add in a new backend. */
088  public static final DefaultIndex[] DEFAULT_INDEXES = {
089    DefaultIndex.withEqualityAndSubstring("cn"),
090    DefaultIndex.withEqualityAndSubstring("givenName"),
091    DefaultIndex.withEqualityAndSubstring("mail"),
092    DefaultIndex.withEqualityAndSubstring("sn"),
093    DefaultIndex.withEqualityAndSubstring("telephoneNumber"),
094    DefaultIndex.withEquality("member"),
095    DefaultIndex.withEquality("uid"),
096    DefaultIndex.withEquality("uniqueMember")
097  };
098
099  /**
100   * Add a new backend with the provided name in the config.ldif file.
101   *
102   * @param backendName
103   *          The new backend name
104   * @param baseDNs
105   *          The base dns to add in the new backend.
106   * @param backendType
107   *          The backend type
108   * @throws Exception
109   *           If any problems occurred
110   */
111  public static void createBackendOffline(String backendName, Collection<DN> baseDNs,
112      ManagedObjectDefinition<? extends BackendCfgClient, ? extends BackendCfg> backendType) throws Exception
113  {
114    Utilities.initializeConfigurationFramework();
115    final File configFile = Installation.getLocal().getCurrentConfigurationFile();
116    final LDAPProfile ldapProfile = LDAPProfile.getInstance();
117    try (ManagementContext context = LDAPManagementContext.newLDIFManagementContext(configFile, ldapProfile))
118    {
119      createBackend(context.getRootConfiguration(), backendName, baseDNs, backendType);
120    }
121  }
122
123  /**
124   * Create a backend with the provided name using the provided
125   * {@code RootCfgClient}.
126   *
127   * @param rootConfiguration
128   *          The root configuration to use to create the new backend
129   * @param backendName
130   *          The new backend name
131   * @param baseDNs
132   *          The base dns to add in the new backend.
133   * @param backendType
134   *          The backend type
135   * @throws Exception
136   *           If any problems occurred
137   */
138  public static void createBackend(RootCfgClient rootConfiguration, String backendName, Collection<DN> baseDNs,
139      ManagedObjectDefinition<? extends BackendCfgClient, ? extends BackendCfg> backendType) throws Exception
140  {
141      final BackendCfgClient backendCfgClient = rootConfiguration.createBackend(backendType, backendName, null);
142      backendCfgClient.setEnabled(true);
143      backendCfgClient.setBaseDN(baseDNs);
144      backendCfgClient.setWritabilityMode(WritabilityMode.ENABLED);
145      backendCfgClient.commit();
146
147      addBackendDefaultIndexes((PluggableBackendCfgClient) backendCfgClient);
148  }
149
150  private static void addBackendDefaultIndexes(PluggableBackendCfgClient backendCfgClient) throws Exception
151  {
152    for (DefaultIndex defaultIndex : DEFAULT_INDEXES)
153    {
154      final BackendIndexCfgClient index =
155          backendCfgClient.createBackendIndex(BackendIndexCfgDefn.getInstance(), defaultIndex.name, null);
156
157      final List<IndexType> indexTypes = new LinkedList<>();
158      indexTypes.add(IndexType.EQUALITY);
159      if (defaultIndex.shouldCreateSubstringIndex)
160      {
161        indexTypes.add(IndexType.SUBSTRING);
162      }
163      index.setIndexType(indexTypes);
164
165      index.commit();
166    }
167  }
168}