Update URL according to microservice name
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / openo / nfvo / vnfmadapter / service / csm / vnf / VnfMgrVnfm.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.openo.nfvo.vnfmadapter.service.csm.vnf;
18
19 import net.sf.json.JSONArray;
20 import org.openo.nfvo.vnfmadapter.common.ResultRequestUtil;
21 import org.openo.nfvo.vnfmadapter.service.constant.Constant;
22 import org.openo.nfvo.vnfmadapter.service.constant.ParamConstants;
23 import org.openo.nfvo.vnfmadapter.service.csm.inf.InterfaceVnfMgr;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import net.sf.json.JSONException;
28 import net.sf.json.JSONObject;
29
30 import javax.print.attribute.standard.ReferenceUriSchemesSupported;
31
32 /**
33  * create or terminate VNF to M
34  * <br/>
35  *
36  * @author
37  * @version NFVO 0.5 Aug 24, 2016
38  */
39 public class VnfMgrVnfm implements InterfaceVnfMgr {
40
41     private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
42
43     @Override
44     public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
45         LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
46         JSONObject restJson = new JSONObject();
47         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
48         String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
49
50         int scaleType = getScaleType(vnfObject.getString("type"));
51         //build request json object
52         JSONObject paramJson = new JSONObject();
53         JSONObject scaleInfo = new JSONObject();
54         JSONArray vduList = new JSONArray();
55         JSONObject vdu = new JSONObject();
56         vdu.put("vdu_type",this.getVduType(vnfmObject,vnfInstanceId));//TODO:set vdu_type
57         vdu.put("h_steps",vnfObject.get("numberOfSteps"));
58         vduList.add(vdu);
59         scaleInfo.put("vnf_id",vnfInstanceId);
60         scaleInfo.put("scale_type",0);
61         scaleInfo.put("scale_action",scaleType);
62         scaleInfo.put("vdu_list",vduList);
63         if(scaleType == 0){//scale_in
64             JSONArray vmList = new JSONArray();
65             try {
66                 JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
67                 vmList = additionalParam.getJSONArray("vm_list");
68             }catch (JSONException e) {
69               LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it",e);
70             }
71             scaleInfo.put("vm_list",vmList);
72         }
73         paramJson.put("scale_info",scaleInfo);
74         JSONObject queryResult = ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(),Constant.CERTIFICATE);
75         LOG.info("SCALE execute result:"+queryResult.toString());
76         try {
77             int statusCode = queryResult.getInt(Constant.RETCODE);
78
79             if(statusCode == Constant.HTTP_CREATED || statusCode == Constant.HTTP_OK) {
80                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
81                 JSONObject resultObj = new JSONObject();
82                 resultObj.put("jobId", vnfInstanceId + "_" + Constant.PUT);
83                 restJson.put("data", resultObj);
84             } else {
85                 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
86             }
87
88         } catch(JSONException e) {
89             LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
90         }
91
92         return restJson;
93     }
94
95
96     private String getVduType(JSONObject vnfmObject, String vnfInstanceId){
97         String vduType = "";
98         try {
99             JSONObject queryResult = ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId), Constant.GET, null,Constant.CERTIFICATE);
100             LOG.info("getVduType result="+queryResult);
101             vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
102         } catch (Exception e) {
103             LOG.error("get vdu_type failed.",e);
104         }
105         LOG.info("vdu_type="+vduType);
106         return vduType;
107     }
108     private int getScaleType(String type){
109         if("SCALE_OUT".equalsIgnoreCase(type)){
110             return 1;
111         }else if("SCALE_IN".equalsIgnoreCase(type)){
112             return 0;
113         }
114         return -1;
115     }
116     @Override
117     public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
118         LOG.info("function=createVnf, msg=enter to create a vnf");
119         LOG.info("createVnf csm request body :"+subJsonObject);
120         JSONObject restJson = new JSONObject();
121         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
122         String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
123
124         JSONObject queryResult = ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(),Constant.CERTIFICATE);
125         LOG.info("createVnf csm response content:"+queryResult);
126         try {
127             int statusCode = queryResult.getInt(Constant.RETCODE);
128
129             if(statusCode == Constant.HTTP_CREATED) {
130                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
131                 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
132                 JSONObject resultObj = new JSONObject();
133                 resultObj.put("vnfInstanceId", appInfo.getString("id"));
134                 resultObj.put("jobId", appInfo.getString("id") + "_" + Constant.POST);
135                 restJson.put("data", resultObj);
136             } else {
137                 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
138             }
139
140         } catch(JSONException e) {
141             LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
142         }
143
144         return restJson;
145     }
146
147     @Override
148     public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
149         LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
150         JSONObject restJson = new JSONObject();
151         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
152
153         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
154                 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,Constant.CERTIFICATE);
155
156         int statusCode = queryResult.getInt(Constant.RETCODE);
157
158         if(statusCode == Constant.HTTP_NOCONTENT) {
159             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
160             JSONObject resultObj = new JSONObject();
161             resultObj.put("jobId", vnfId + "_" + Constant.DELETE);
162             restJson.put("data", resultObj);
163         } else {
164             LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
165         }
166
167         return restJson;
168     }
169
170     @Override
171     public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
172         LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
173         JSONObject restJson = new JSONObject();
174         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
175
176         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
177                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
178                 null,Constant.CERTIFICATE);
179
180         int statusCode = queryResult.getInt("retCode");
181
182         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
183             if(null == (queryResult.get("data"))) {
184                 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
185                 return restJson;
186             }
187             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
188             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
189         } else {
190             LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
191         }
192
193         return restJson;
194     }
195
196     @Override
197     public JSONObject getJob(JSONObject vnfmObject, String jobId) {
198         LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
199         JSONObject restJson = new JSONObject();
200         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
201
202         String vnfId = jobId.split("_")[0];
203         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
204                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
205                 null,Constant.CERTIFICATE);
206
207         int statusCode = queryResult.getInt("retCode");
208
209         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
210
211             if((queryResult.get("data")) == null) {
212                 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
213                 return restJson;
214             }
215             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
216             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
217         } else {
218             LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
219         }
220
221         return restJson;
222     }
223 }