26430799cf2e5a8d8741b7cd2b8c1eaa8f0c95b8
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / csm / connect / ConnectMgrVnfm.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.io.IOException;
20
21 import org.apache.commons.httpclient.Header;
22 import org.apache.commons.httpclient.HttpMethod;
23 import org.apache.commons.httpclient.HttpStatus;
24 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException;
25 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
26 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.api.ConnectInfo;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import net.sf.json.JSONException;
32 import net.sf.json.JSONObject;
33
34 /**
35  * VNFM connection manager
36  * .</br>
37  *
38  * @author
39  * @version     NFVO 0.5  Sep 14, 2016
40  */
41 public class ConnectMgrVnfm {
42
43     private static final Logger LOG = LoggerFactory.getLogger(ConnectMgrVnfm.class);
44
45     private String accessSession;
46
47     private String roaRand;
48
49     public String getAccessSession() {
50         return accessSession;
51     }
52
53     public void setAccessSession(String accessSession) {
54         this.accessSession = accessSession;
55     }
56
57     public String getRoaRand() {
58         return roaRand;
59     }
60
61     public void setRoaRand(String roaRand) {
62         this.roaRand = roaRand;
63     }
64
65
66     /**
67      * Make connection
68      * <br>
69      *
70      * @param vnfmObj
71      * @return
72      * @since  NFVO 0.5
73      */
74     public int connect(JSONObject vnfmObj,String authModel) {
75         LOG.info("function=connect, msg=enter connect function.");
76
77         ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString("userName"),
78                 vnfmObj.getString("password"), authModel);
79         HttpMethod httpMethod = null;
80         int statusCode = Constant.INTERNAL_EXCEPTION;
81
82         try {
83             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
84                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
85                     .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
86                     .post().execute();
87             statusCode = httpMethod.getStatusCode();
88
89             String result = httpMethod.getResponseBodyAsString();
90             LOG.info("connect result:"+result);
91             if(statusCode == HttpStatus.SC_CREATED) {
92                 JSONObject accessObj = JSONObject.fromObject(result);
93                 JSONObject tokenObj = accessObj.getJSONObject("token");
94                 Header header = httpMethod.getResponseHeader("accessSession");
95                 setAccessSession(header.getValue());
96                 setRoaRand(tokenObj.getString("roa_rand"));
97                 statusCode = HttpStatus.SC_OK;
98             } else {
99                 LOG.error("connect fail, code:" + statusCode + " re:" + result);
100             }
101
102         } catch(JSONException e) {
103             LOG.error("function=connect, msg=connect JSONException e={}.", e);
104         } catch(VnfmException e) {
105             LOG.error("function=connect, msg=connect VnfmException e={}.", e);
106         } catch(IOException e) {
107             LOG.error("function=connect, msg=connect IOException e={}.", e);
108         } finally {
109             clearCSMPwd(info);
110             if(httpMethod != null) {
111                 httpMethod.releaseConnection();
112             }
113         }
114         return statusCode;
115
116     }
117     /**
118      * Make connection
119      * <br>
120      *
121      * @param vnfmObj
122      * @return
123      * @since  NFVO 0.5
124      */
125     public int connect(JSONObject vnfmObj) {
126         LOG.info("function=connect, msg=enter connect function.");
127
128         ConnectInfo info = new ConnectInfo(vnfmObj.getString("url"), vnfmObj.getString("userName"),
129                 vnfmObj.getString("password"), Constant.ANONYMOUS);
130         HttpMethod httpMethod = null;
131         int statusCode = Constant.INTERNAL_EXCEPTION;
132
133         try {
134             httpMethod = new HttpRequests.Builder(info.getAuthenticateMode())
135                     .setUrl(info.getUrl(), ParamConstants.CSM_AUTH_CONNECT)
136                     .setParams(String.format(ParamConstants.GET_TOKENS_V2, info.getUserName(), info.getUserPwd()))
137                     .post().execute();
138             statusCode = httpMethod.getStatusCode();
139
140             String result = httpMethod.getResponseBodyAsString();
141
142             if(statusCode == HttpStatus.SC_CREATED) {
143                 JSONObject accessObj = JSONObject.fromObject(result);
144                 JSONObject tokenObj = accessObj.getJSONObject("token");
145                 Header header = httpMethod.getResponseHeader("accessSession");
146                 setAccessSession(header.getValue());
147                 setRoaRand(tokenObj.getString("roa_rand"));
148                 statusCode = HttpStatus.SC_OK;
149             } else {
150                 LOG.error("connect fail, code:" + statusCode + " re:" + result);
151             }
152
153         } catch(JSONException e) {
154             LOG.error("function=connect, msg=connect JSONException e={}.", e);
155         } catch(VnfmException e) {
156             LOG.error("function=connect, msg=connect VnfmException e={}.", e);
157         } catch(IOException e) {
158             LOG.error("function=connect, msg=connect IOException e={}.", e);
159         } finally {
160             clearCSMPwd(info);
161             if(httpMethod != null) {
162                 httpMethod.releaseConnection();
163             }
164         }
165         return statusCode;
166
167     }
168
169     private void clearCSMPwd(ConnectInfo connectInfo) {
170         if(null != connectInfo) {
171             connectInfo.setUserPwd("");
172         }
173     }
174 }