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