b9fa5e39c22e22cbb85f7e7233b857a1e0cd12b7
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / csm / connect / SslProtocolSocketFactory.java
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.connect;
18
19 import java.util.Map;
20 import java.util.concurrent.ConcurrentHashMap;
21
22 import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
23 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException;
24 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * SSL Socket Factory.
30  * .</br>
31  *
32  * @author
33  * @version     VFC 1.0  Sep 14, 2016
34  */
35 public class SslProtocolSocketFactory {
36
37     private static final Logger LOG = LoggerFactory.getLogger(SslProtocolSocketFactory.class);
38
39     private static final Map<String, ProtocolSocketFactory> SOCKMAP =
40             new ConcurrentHashMap<String, ProtocolSocketFactory>(2);
41
42     private static SslProtocolSocketFactory singleinstance = null;
43
44     /**
45      * Generate instance of SslProtocolSocketFactory
46      * <br>
47      *
48      * @return
49      * @since  VFC 1.0
50      */
51     public static synchronized SslProtocolSocketFactory getInstance() {
52         if(singleinstance == null) {
53             singleinstance = new SslProtocolSocketFactory();
54         }
55         return singleinstance;
56     }
57
58     /**
59      *
60      * <br>
61      *
62      * @param authenticateMode
63      * @return
64      * @throws VnfmException
65      * @since  VFC 1.0
66      */
67     public synchronized ProtocolSocketFactory get(String authenticateMode) throws VnfmException {
68         if(SOCKMAP.get(authenticateMode) == null) {
69             if(Constant.ANONYMOUS.equals(authenticateMode)) {
70                 SslAnonymousSocket anonymous = new SslAnonymousSocket();
71                 anonymous.init();
72                 SOCKMAP.put(Constant.ANONYMOUS, anonymous);
73             }else if (Constant.CERTIFICATE.equals(authenticateMode)){
74                 SslCertificateSocket certificateSocket = new SslCertificateSocket();
75                 certificateSocket.init();
76                 SOCKMAP.put(Constant.CERTIFICATE, certificateSocket);
77             } else {
78                 LOG.error("funtion=get, msg=ProtocolSocketFactory Unknown AuthenticateMode={}", authenticateMode);
79                 throw new VnfmException(String.format("Illegal Auth mode", authenticateMode));
80             }
81         }
82
83         return SOCKMAP.get(authenticateMode);
84     }
85
86     /**
87      * Refresh local socket map
88      * <br>
89      *
90      * @param autherMode
91      * @throws VnfmException
92      * @since  VFC 1.0
93      */
94     public synchronized void refresh(String autherMode) throws VnfmException {
95         if(Constant.ANONYMOUS.equals(autherMode)) {
96             SslAnonymousSocket anonymous = new SslAnonymousSocket();
97             anonymous.init();
98             SOCKMAP.put(Constant.ANONYMOUS, anonymous);
99         }
100     }
101 }