Fixed sonar issues in CertificationModelFactory class
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / aaf / certservice / certification / CertificationModelFactory.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
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;
22
23 import org.onap.aaf.certservice.certification.configuration.Cmpv2ServerProvider;
24 import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server;
25 import org.onap.aaf.certservice.certification.exception.Cmpv2ClientAdapterException;
26 import org.onap.aaf.certservice.certification.exception.DecryptionException;
27 import org.onap.aaf.certservice.certification.model.CertificationModel;
28 import org.onap.aaf.certservice.certification.model.CsrModel;
29 import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Service;
34
35 @Service
36 public class CertificationModelFactory {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(CertificationModelFactory.class);
39
40     private final CsrModelFactory csrModelFactory;
41     private final Cmpv2ServerProvider cmpv2ServerProvider;
42     private final CertificationProvider certificationProvider;
43
44     @Autowired
45     CertificationModelFactory(
46             CsrModelFactory csrModelFactory,
47             Cmpv2ServerProvider cmpv2ServerProvider,
48             CertificationProvider certificationProvider
49     ) {
50         this.cmpv2ServerProvider = cmpv2ServerProvider;
51         this.csrModelFactory = csrModelFactory;
52         this.certificationProvider = certificationProvider;
53     }
54
55     public CertificationModel createCertificationModel(String encodedCsr, String encodedPrivateKey, String caName)
56             throws DecryptionException, CmpClientException, Cmpv2ClientAdapterException {
57         CsrModel csrModel = csrModelFactory.createCsrModel(
58                 new CsrModelFactory.StringBase64(encodedCsr),
59                 new CsrModelFactory.StringBase64(encodedPrivateKey)
60         );
61         LOGGER.debug("Received CSR meta data: \n{}", csrModel);
62
63         Cmpv2Server cmpv2Server = cmpv2ServerProvider.getCmpv2Server(caName);
64         LOGGER.debug("Found server for given CA name: \n{}", cmpv2Server);
65
66         LOGGER.info("Sending sign request for certification model for CA named: {}, and certificate signing request:\n{}",
67                 caName, csrModel);
68         return certificationProvider.signCsr(csrModel, cmpv2Server);
69     }
70
71 }