add some new function
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / rest / VnfResourceRoa.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.rest;
18
19 import java.io.IOException;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.ws.rs.Consumes;
23 import javax.ws.rs.PUT;
24 import javax.ws.rs.Path;
25 import javax.ws.rs.PathParam;
26 import javax.ws.rs.Produces;
27 import javax.ws.rs.core.Context;
28 import javax.ws.rs.core.MediaType;
29
30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmJsonUtil;
31 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
32 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VnfmRestfulUtil;
33 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.impl.AdapterResourceManager;
34 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
35 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.process.VnfResourceMgr;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import net.sf.json.JSONArray;
40 import net.sf.json.JSONObject;
41
42 /**
43  * Provide interfaces of resource for VNFM.
44  * <br/>
45  *
46  * @author
47  * @version VFC 1.0 Aug 24, 2016
48  */
49 @Path("/rest/vnfmmed/csm/v2/vapps")
50 @Consumes(MediaType.APPLICATION_JSON)
51 @Produces(MediaType.APPLICATION_JSON)
52 public class VnfResourceRoa {
53
54     private static final Logger LOG = LoggerFactory.getLogger(VnfResourceRoa.class);
55
56     private VnfResourceMgr vnfResourceMgr;
57
58     public void setVnfResourceMgr(VnfResourceMgr vnfResourceMgr) {
59         this.vnfResourceMgr = vnfResourceMgr;
60     }
61
62     /**
63      * Provide function of grant resource.
64      * <br/>
65      *
66      * @param context
67      * @param vnfId
68      * @return
69      * @since VFC 1.0
70      */
71     @PUT
72     @Path("/instances/{vnfId}/grant")
73     public String grantVnfRes(@Context HttpServletRequest context, @PathParam("vnfId") String vnfId) {
74         LOG.info("function=grantVnfRes, msg=enter to grant vnf resource.");
75         JSONObject restJson = new JSONObject();
76         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
77
78         JSONObject dataObject = VnfmJsonUtil.getJsonFromContexts(context);
79         LOG.info("function=grantVnfRes, dataObject: {}", dataObject);
80         if(null == dataObject) {
81             LOG.error("function=grantVnfRes, msg=param error");
82             restJson.put("data", "Params error");
83             return restJson.toString();
84         }
85
86         JSONObject grantObj = dataObject.getJSONObject("grant");
87
88         if(null == grantObj) {
89             LOG.error("function=grantVnfRes, msg=param error");
90             restJson.put("data", "Grant param error");
91             return restJson.toString();
92         }
93
94         String vnfmId = grantObj.getString("project_id");
95
96         JSONObject resultObj = vnfResourceMgr.grantVnfResource(grantObj, vnfId, vnfmId);
97         LOG.info("grantVnfResource resultObj:", resultObj);
98         JSONObject res = new JSONObject();
99         res.put("msg", "grant success");
100         return res.toString();
101     }
102
103     @PUT
104     @Path("/lifecycle_changes_notification")
105     public String notify(@Context HttpServletRequest context) throws IOException {
106         LOG.info("function=notify, msg=enter to notify vnf resource");
107         JSONObject dataObject = VnfmJsonUtil.getJsonFromContexts(context);
108         LOG.info("function=notify, dataObject: {}", dataObject);
109         callLcmNotify(dataObject);
110         JSONObject restJson = new JSONObject();
111         restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
112         return restJson.toString();
113     }
114
115     private void callLcmNotify(JSONObject dataObject) throws IOException {
116         String vnfPkgInfo = AdapterResourceManager.readVfnPkgInfoFromJson();
117         JSONObject vnfpkgJson = JSONObject.fromObject(vnfPkgInfo);
118         String vnfmId = vnfpkgJson.getString("vnfmid");
119         String vimId = vnfpkgJson.getString("vimid");
120         JSONArray affectedVnfc = new JSONArray();
121         JSONArray vmList = dataObject.getJSONArray("vm_list");
122         String changeType = "";
123         String operation = "";
124         if(1 == dataObject.getInt("event_type")) {
125             changeType = "added";
126             operation = "Instantiate";
127         } else {
128             changeType = "removed";
129             operation = "Terminal";
130         }
131         String vnfInstanceId = dataObject.getString("vnf_id");
132         for(int i = 0; i < vmList.size(); i++) {
133             JSONObject vm = vmList.getJSONObject(i);
134             LOG.info("function=callLcmNotify, vm: {}", vm);
135             JSONObject affectedVm = new JSONObject();
136             String vimVimId = vm.getString("vim_vm_id");
137             affectedVm.put("vnfcInstanceId", vimVimId);
138             affectedVm.put("changeType", changeType);
139             affectedVm.put("vimid", vimId);
140             affectedVm.put("vmid", vimVimId);
141             affectedVm.put("vmname", vm.getString("vm_name"));
142             affectedVm.put("vduid", vimVimId);
143             LOG.info("function=callLcmNotify, affectedVm: {}", affectedVm);
144             affectedVnfc.add(affectedVm);
145         }
146         JSONObject notification = new JSONObject();
147         notification.put("status", dataObject.getString("vnf_status"));
148         notification.put("vnfInstanceId", vnfInstanceId);
149         notification.put("operation", operation);
150         notification.put("affectedVnfc", affectedVnfc);
151         LOG.info("function=callLcmNotify, notification: {}", notification);
152         String url = "/api/nslcm/v1/ns/" + vnfmId + "/vnfs/" + vnfInstanceId + "/Notify";
153         LOG.info("function=callLcmNotify, url: {}", url);
154         RestfulResponse rsp =
155                 VnfmRestfulUtil.getRemoteResponse(url, VnfmRestfulUtil.TYPE_POST, notification.toString());
156         if(rsp != null) {
157             LOG.info("function=callLcmNotify, status: {}, content: {}", rsp.getStatus(), rsp.getResponseContent());
158         }
159     }
160 }