7d2466ea91221ba2148ac1986fdae8ea1ba25fb0
[vfc/nfvo/resmanagement.git] /
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.resmanagement.service.adapter.impl;
18
19 import java.util.Map;
20
21 import org.onap.vfc.nfvo.resmanagement.common.constant.Constant;
22 import org.onap.vfc.nfvo.resmanagement.common.constant.HttpConstant;
23 import org.onap.vfc.nfvo.resmanagement.common.util.RestfulUtil;
24 import org.onap.vfc.nfvo.resmanagement.service.adapter.inf.IResmgrAdapter2MSBManager;
25 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulResponse;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import net.sf.json.JSONObject;
30
31 /**
32  * <br>
33  * <p>
34  * </p>
35  * 
36  * @author
37  * @version VFC 1.0 Sep 22, 2016
38  */
39 public class ResmgrAdapter2MSBManager implements IResmgrAdapter2MSBManager {
40
41     private static final Logger LOG = LoggerFactory.getLogger(ResmgrAdapter2MSBManager.class);
42
43     @Override
44     public JSONObject registerResmgr(Map<String, String> paramsMap, JSONObject driverInfo) {
45         JSONObject resultObj = new JSONObject();
46
47         RestfulResponse rsp = RestfulUtil.getRemoteResponse(paramsMap, driverInfo.toString());
48         if(null == rsp) {
49             LOG.error("function=registerResmgr,  RestfulResponse is null");
50             resultObj.put("reason", "RestfulResponse is null.");
51             resultObj.put("retCode", Constant.ERROR_CODE);
52             return resultObj;
53         }
54         LOG.warn("function=registerResmgr, status={}, content={}.", rsp.getStatus(), rsp.getResponseContent());
55         String resultCreate = rsp.getResponseContent();
56
57         if(rsp.getStatus() == HttpConstant.HTTP_CREATED) {
58             LOG.warn("function=registerResmgr, msg= status={}, result={}.", rsp.getStatus(), resultCreate);
59             resultObj = JSONObject.fromObject(resultCreate);
60             resultObj.put("retCode", HttpConstant.HTTP_CREATED);
61             return resultObj;
62         } else if(rsp.getStatus() == HttpConstant.HTTP_INVALID_PARAMETERS) {
63             LOG.error("function=registerResmgr, msg=MSB return fail,invalid parameters,status={}, result={}.",
64                     rsp.getStatus(), resultCreate);
65             resultObj.put("reason", "MSB return fail,invalid parameters.");
66         } else if(rsp.getStatus() == HttpConstant.HTTP_INNERERROR_CODE) {
67             LOG.error("function=registerResmgr, msg=MSB return fail,internal system error,status={}, result={}.",
68                     rsp.getStatus(), resultCreate);
69             resultObj.put("reason", "MSB return fail,internal system error.");
70         }
71         resultObj.put("retCode", Constant.ERROR_CODE);
72         return resultObj;
73     }
74
75     @Override
76     public JSONObject unregisterResmgr(Map<String, String> paramsMap) {
77         JSONObject resultObj = new JSONObject();
78
79         RestfulResponse rsp = RestfulUtil.getRemoteResponse(paramsMap, "");
80         if(null == rsp) {
81             LOG.error("function=unregisterResmgr,  RestfulResponse is null");
82             resultObj.put("reason", "RestfulResponse is null.");
83             resultObj.put("retCode", Constant.ERROR_CODE);
84             return resultObj;
85         }
86         String resultCreate = rsp.getResponseContent();
87
88         if(rsp.getStatus() == HttpConstant.HTTP_NOCONTENT) {
89             LOG.warn("function=unregisterResmgr, msg= status={}, result={}.", rsp.getStatus(), resultCreate);
90             resultObj = JSONObject.fromObject(resultCreate);
91             resultObj.put("retCode", HttpConstant.HTTP_NOCONTENT);
92             return resultObj;
93         } else if(rsp.getStatus() == HttpConstant.HTTP_NOTFOUND_CODE) {
94             LOG.error(
95                     "function=unregisterResmgr, msg=MSB return fail,can't find the service instance.status={}, result={}.",
96                     rsp.getStatus(), resultCreate);
97             resultObj.put("reason", "MSB return fail,can't find the service instance.");
98         } else if(rsp.getStatus() == HttpConstant.HTTP_INVALID_PARAMETERS) {
99             LOG.error("function=unregisterResmgr, msg=MSB return fail,invalid parameters,status={}, result={}.",
100                     rsp.getStatus(), resultCreate);
101             resultObj.put("reason", "MSB return fail,invalid parameters.");
102         } else if(rsp.getStatus() == HttpConstant.HTTP_INNERERROR_CODE) {
103             LOG.error("function=unregisterResmgr, msg=MSB return fail,internal system error,status={}, result={}.",
104                     rsp.getStatus(), resultCreate);
105             resultObj.put("reason", "MSB return fail,internal system error.");
106         }
107         resultObj.put("retCode", Constant.ERROR_CODE);
108         return resultObj;
109     }
110
111 }