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.service;
18  
19  import java.util.regex.Pattern;
20  import lombok.extern.slf4j.Slf4j;
21  import org.bremersee.dccon.model.DnsZone;
22  import org.bremersee.dccon.repository.DnsZoneRepository;
23  
24  /**
25   * The dns zone comparator.
26   *
27   * @author Christian Bremer
28   */
29  @Slf4j
30  public class DnsZoneComparator extends AbstractDnsComparator<DnsZone> {
31  
32    private DnsZoneRepository dnsZoneRepository;
33  
34    /**
35     * Instantiates a new dns zone comparator.
36     *
37     * @param dnsZoneRepository the dns zone repository
38     */
39    DnsZoneComparator(DnsZoneRepository dnsZoneRepository) {
40      super(true);
41      this.dnsZoneRepository = dnsZoneRepository;
42    }
43  
44    @Override
45    public int compare(DnsZone o1, DnsZone o2) {
46      if (o1 != null && Boolean.TRUE.equals(o1.getDefaultZone())) {
47        return -1;
48      } else if (o2 != null && Boolean.TRUE.equals(o2.getDefaultZone())) {
49        return 1;
50      }
51      final String s1 = o1 != null && o1.getName() != null ? o1.getName() : "";
52      final String s2 = o2 != null && o2.getName() != null ? o2.getName() : "";
53      log.debug("msg=[Comparing zones.] zone1=[{}] zone2=[{}]", s1, s2);
54      if (dnsZoneRepository.isDnsReverseZone(s1) && dnsZoneRepository.isDnsReverseZone(s2)) {
55        final String[] sa1 = s1.split(Pattern.quote("."));
56        final String[] sa2 = s2.split(Pattern.quote("."));
57        int c = sa2.length - sa1.length;
58        if (c == 0) {
59          c = compare(sa1, sa2);
60        }
61        log.debug("msg=[Both zones are dns reverse zones.] result=[{}]", c);
62        return isAsc() ? c : -1 * c;
63      } else if (!dnsZoneRepository.isDnsReverseZone(s1) && dnsZoneRepository.isDnsReverseZone(s2)) {
64        log.debug("msg=[First is non reverse zone, second is reverse zone.] "
65            + "first=[{}] second=[{}] result=[-1]", s1, s2);
66        return isAsc() ? -1 : 1;
67      } else if (dnsZoneRepository.isDnsReverseZone(s1) && !dnsZoneRepository.isDnsReverseZone(s2)) {
68        log.debug("msg=[First is reverse zone, second is non reverse zone.] "
69            + "first=[{}] second=[{}] result=[1]", s1, s2);
70        return isAsc() ? 1 : -1;
71      }
72      final int result = s1.compareToIgnoreCase(s2);
73      log.debug("msg=[Both zones are non dns reverse zones.] result=[{}]", result);
74      return isAsc() ? result : -1 * result;
75    }
76  
77    private int compare(String[] sa1, String[] sa2) {
78      final String[] sav1 = sa1 != null ? sa1 : new String[0];
79      final String[] sav2 = sa2 != null ? sa2 : new String[0];
80      final int len = Math.min(sav1.length, sav2.length);
81      for (int i = len - 1; i >= 0; i--) {
82        int c = compare(sav1[i], sav2[i]);
83        if (c != 0) {
84          return c;
85        }
86      }
87      return 0;
88    }
89  
90    private int compare(String s1, String s2) {
91      return format(s1, 3).compareToIgnoreCase(format(s2, 3));
92    }
93  
94  }