Merge "Removed unused parameters when creating certificate"
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / aaf / certservice / certification / adapter / Cmpv2ClientAdapter.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Cert Service
4  * ================================================================================
5  * Copyright (C) 2020 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.aaf.certservice.certification.adapter;
22
23 import java.io.IOException;
24 import java.io.StringWriter;
25 import java.security.cert.X509Certificate;
26 import java.util.List;
27 import java.util.stream.Collectors;
28
29 import org.bouncycastle.openssl.jcajce.JcaMiscPEMGenerator;
30 import org.bouncycastle.util.io.pem.PemObjectGenerator;
31 import org.bouncycastle.util.io.pem.PemWriter;
32 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
33 import org.onap.aaf.certservice.certification.model.CertificationModel;
34 import org.onap.aaf.certservice.certification.model.CsrModel;
35 import org.onap.aaf.certservice.cmpv2client.api.CmpClient;
36 import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41
42 @Component
43 public class Cmpv2ClientAdapter {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(Cmpv2ClientAdapter.class);
46
47     private final CmpClient cmpClient;
48
49     @Autowired
50     public Cmpv2ClientAdapter(CmpClient cmpClient) {
51         this.cmpClient = cmpClient;
52     }
53
54     /**
55      * Uses CmpClient to call to Cmp Server and gather certificates data
56      *
57      * @param csrModel Certificate Signing Request from Service external  API
58      * @param server   Cmp Server configuration from cmpServers.json
59      * @return container for returned certificates
60      * @throws CmpClientException          Exceptions which comes from Cmp Client
61      */
62     public CertificationModel callCmpClient(CsrModel csrModel, Cmpv2Server server)
63             throws CmpClientException {
64         List<List<X509Certificate>> certificates = cmpClient.createCertificate(csrModel, server);
65         return new CertificationModel(convertFromX509CertificateListToPemList(certificates.get(0)),
66                 convertFromX509CertificateListToPemList(certificates.get(1)));
67     }
68
69     private String convertFromX509CertificateToPem(X509Certificate certificate) {
70         StringWriter sw = new StringWriter();
71         try (PemWriter pw = new PemWriter(sw)) {
72             PemObjectGenerator gen = new JcaMiscPEMGenerator(certificate);
73             pw.writeObject(gen);
74         } catch (IOException e) {
75             LOGGER.error("Exception occurred during convert of X509 certificate", e);
76         }
77         return sw.toString();
78     }
79
80     private List<String> convertFromX509CertificateListToPemList(List<X509Certificate> certificates) {
81         return certificates.stream().map(this::convertFromX509CertificateToPem).filter(cert -> !cert.isEmpty())
82                 .collect(Collectors.toList());
83     }
84
85 }