Merge "Fix VNF instantiate problem"
[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      * <br>
60      *
61      * @param authenticateMode
62      * @return
63      * @throws VnfmException
64      * @since VFC 1.0
65      */
66     public synchronized ProtocolSocketFactory get(String authenticateMode) throws VnfmException {
67         if(SOCKMAP.get(authenticateMode) == null) {
68             if(Constant.ANONYMOUS.equals(authenticateMode)) {
69                 SslAnonymousSocket anonymous = new SslAnonymousSocket();
70                 anonymous.init();
71                 SOCKMAP.put(Constant.ANONYMOUS, anonymous);
72             } else if(Constant.CERTIFICATE.equals(authenticateMode)) {
73                 SslCertificateSocket certificateSocket = new SslCertificateSocket();
74                 certificateSocket.init();
75                 SOCKMAP.put(Constant.CERTIFICATE, certificateSocket);
76             } else {
77                 LOG.error("funtion=get, msg=ProtocolSocketFactory Unknown AuthenticateMode={}", authenticateMode);
78                 throw new VnfmException("Illegal Auth mode: " + authenticateMode);
79             }
80         }
81
82         return SOCKMAP.get(authenticateMode);
83     }
84
85     /**
86      * Refresh local socket map
87      * <br>
88      *
89      * @param autherMode
90      * @throws VnfmException
91      * @since VFC 1.0
92      */
93     public synchronized void refresh(String autherMode) throws VnfmException {
94         if(Constant.ANONYMOUS.equals(autherMode)) {
95             SslAnonymousSocket anonymous = new SslAnonymousSocket();
96             anonymous.init();
97             SOCKMAP.put(Constant.ANONYMOUS, anonymous);
98         }
99     }
100 }