[OOM-CERT-SERVICE] Add logic for KUR/CR detection
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / oom / certservice / certification / model / CertificateData.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Cert Service
4  * ================================================================================
5  * Copyright (C) 2021 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.oom.certservice.certification.model;
22
23 import org.bouncycastle.asn1.x500.X500Name;
24 import org.bouncycastle.asn1.x509.GeneralName;
25
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.List;
30 import java.util.Objects;
31 import java.util.stream.Collectors;
32
33 public class CertificateData {
34
35     private final X500Name subject;
36     private final List<GeneralName> sortedSans;
37
38     public CertificateData(X500Name subject, GeneralName[] sans) {
39         this.subject = subject;
40         this.sortedSans = sans != null ? getSortedSansList(sans) : Collections.emptyList();
41     }
42
43     public X500Name getSubject() {
44         return subject;
45     }
46
47     public GeneralName[] getSortedSans() {
48         return sortedSans.toArray(new GeneralName[0]);
49     }
50
51     @Override
52     public boolean equals(Object o) {
53         if (this == o) return true;
54         if (o == null || getClass() != o.getClass()) return false;
55         CertificateData that = (CertificateData) o;
56         return Objects.equals(subject, that.subject) && Objects.equals(sortedSans, that.sortedSans);
57     }
58
59     @Override
60     public int hashCode() {
61         return Objects.hash(subject, sortedSans);
62     }
63
64     private List<GeneralName> getSortedSansList(GeneralName[] sans) {
65         return Arrays.stream(sans).sorted(Comparator.comparing(GeneralName::toString))
66             .collect(Collectors.toUnmodifiableList());
67     }
68
69 }