View Javadoc
1   /*
2    * Copyright 2019 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.bremersee.dccon.repository;
18  
19  import java.util.Collection;
20  import java.util.LinkedHashSet;
21  import java.util.List;
22  import java.util.Optional;
23  import java.util.Set;
24  import java.util.stream.Stream;
25  import javax.validation.constraints.NotNull;
26  import org.bremersee.dccon.model.DnsNode;
27  import org.bremersee.dccon.model.DnsPair;
28  import org.bremersee.dccon.model.DnsRecord;
29  import org.bremersee.dccon.model.UnknownFilter;
30  import org.springframework.lang.Nullable;
31  import org.springframework.validation.annotation.Validated;
32  
33  /**
34   * The dns node repository.
35   *
36   * @author Christian Bremer
37   */
38  @Validated
39  public interface DnsNodeRepository {
40  
41    /**
42     * Get unknown filter or default one.
43     *
44     * @param unknownFilter the unknown filter
45     * @return the unknown filter
46     */
47    @NotNull
48    default UnknownFilter unknownFilter(@Nullable UnknownFilter unknownFilter) {
49      return unknownFilter != null ? unknownFilter : UnknownFilter.NO_UNKNOWN;
50    }
51  
52    /**
53     * Find dns nodes by ips.
54     *
55     * @param ips the ips
56     * @param unknownFilter the unknown filter
57     * @return the dns nodes
58     */
59    List<DnsNode> findByIps(@NotNull Set<String> ips, UnknownFilter unknownFilter);
60  
61    /**
62     * Find dns node by host name.
63     *
64     * @param hostName the host name, can be a simple host name or a full qualified domain name
65     * @param unknownFilter the unknown filter
66     * @return the dns node
67     */
68    Optional<DnsNode> findByHostName(@NotNull String hostName, UnknownFilter unknownFilter);
69  
70    /**
71     * Find all.
72     *
73     * @param zoneName the zone name
74     * @param unknownFilter the unknown filter (default is {@link UnknownFilter#NO_UNKNOWN}
75     * @param query the query
76     * @return the dns nodes
77     */
78    Stream<DnsNode> findAll(
79        @NotNull String zoneName,
80        @Nullable UnknownFilter unknownFilter,
81        @Nullable String query);
82  
83    /**
84     * Check whether dns node exists or not.
85     *
86     * @param zoneName the zone name
87     * @param nodeName the node name
88     * @param unknownFilter the unknown filter (default is {@link UnknownFilter#NO_UNKNOWN}
89     * @return {@code true} if the dns node exists, otherwise {@code false}
90     */
91    boolean exists(
92        @NotNull String zoneName,
93        @NotNull String nodeName,
94        @Nullable UnknownFilter unknownFilter);
95  
96    /**
97     * Find dns node by zone name and node name.
98     *
99     * @param zoneName the zone name
100    * @param nodeName the node name
101    * @param unknownFilter the unknown filter (default is {@link UnknownFilter#NO_UNKNOWN}
102    * @return the dns node
103    */
104   Optional<DnsNode> findOne(
105       @NotNull String zoneName,
106       @NotNull String nodeName,
107       @Nullable UnknownFilter unknownFilter);
108 
109   /**
110    * Find correlated dns node optional.
111    *
112    * @param zoneName the zone name
113    * @param record the record
114    * @return the optional
115    */
116   Optional<DnsPair> findCorrelatedDnsNode(
117       @NotNull String zoneName,
118       @NotNull DnsRecord record);
119 
120   /**
121    * Save dns node.
122    *
123    * @param zoneName the zone name
124    * @param dnsNode the dns node
125    * @return the dns node (will be {@link Optional#empty()}, if the node has no records)
126    */
127   Optional<DnsNode> save(@NotNull String zoneName, @NotNull DnsNode dnsNode);
128 
129   /**
130    * Delete a dns node.
131    *
132    * @param zoneName the zone name
133    * @param nodeName the node name
134    * @return {@code true} if the dns node was removed; {@code false} if dns node didn't exist
135    */
136   default boolean delete(@NotNull String zoneName, @NotNull String nodeName) {
137     return findOne(zoneName, nodeName, UnknownFilter.ALL)
138         .map(node -> delete(zoneName, node))
139         .orElse(false);
140   }
141 
142   /**
143    * Delete dns node.
144    *
145    * @param zoneName the zone name
146    * @param node the node
147    * @return the boolean
148    */
149   boolean delete(@NotNull String zoneName, @NotNull DnsNode node);
150 
151   /**
152    * Delete all dns nodes of the specified dns zone.
153    *
154    * @param zoneName the zone name
155    */
156   default void deleteAll(@NotNull String zoneName) {
157     findAll(zoneName, UnknownFilter.ALL, null)
158         .forEach(dnsNode -> delete(zoneName, dnsNode));
159   }
160 
161   /**
162    * Delete all dns nodes with the specified names from the specified dns zone.
163    *
164    * @param zoneName the zone name
165    * @param nodeNames the node names
166    */
167   default void deleteAll(@NotNull String zoneName, @Nullable Collection<String> nodeNames) {
168     if (nodeNames != null && !nodeNames.isEmpty()) {
169       for (String nodeName : new LinkedHashSet<>(nodeNames)) {
170         findOne(zoneName, nodeName, UnknownFilter.ALL)
171             .ifPresent(dnsNode -> delete(zoneName, dnsNode));
172       }
173     }
174   }
175 
176 }