web.xml
<servlet>
<servlet-name>RESTEntrance</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.certusnet.nfv.mano.vim.RestApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
//这里定义好路径组成格式
<servlet-mapping>
<servlet-name>RESTEntrance</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
客户端:
public SummeryOfResourcesVO getSummeryOfResource() {
logger.info("查询一组资源使用情况(云主机、虚拟内核、虚拟内存、安全组)信息");
final SummeryOfResourcesVO vo = new SummeryOfResourcesVO();
final JSONObject[] provider = new JSONObject[1];
final String[] reslt= new String[1];
/**取资源使用情况*/
HttpClientRequest request = new HttpClientGetRequest(ConfigFileLoad.getConfContent("API_IP") + "/rest/resource_used");
try {
request.process(new HttpClientResponseHandler() {
@Override
public void handle(String response, String retCode, String msg) throws HttpClientException {
logger.debug("查询一组资源使用情况信息,返回状态:" + retCode + ",返回信息:" + msg);
JSONObject obj = JSONObject.parseObject(response);
if(obj.get("result")!=null){
System.out.println(obj.get("result").toString());
JSONObject jsonObj = JSONObject.parseObject(obj.get("result").toString());
if (jsonObj == null){
//&&!reslt[0].equals("0")
return;
}
// 一 、请求并计算 云主机信息
Integer vms = jsonObj.getInteger(Constants.OverView.VMS);
Integer used_vms = jsonObj.getInteger(Constants.OverView.USED_VMS);
Integer unused_vms = 0;
if (vms == null || vms.equals("")) {
vms = 0;
} else if (used_vms == null || used_vms.equals("")) {
used_vms = 0;
} else {
unused_vms = vms - used_vms;
}
vo.setCloudPlatformAll(String.valueOf(vms));
vo.setCloudPlatformUsed(String.valueOf(used_vms));
vo.setCloudPlatformUnUsed(String.valueOf(unused_vms));
// 二 、请求 虚拟内核
Integer cpus = jsonObj.getInteger(Constants.OverView.CPUS);
Integer used_cpus = jsonObj.getInteger(Constants.OverView.USED_CPUS);
Integer unused_cpus = 0;
if (cpus == null || cpus.equals("")) {
cpus = 0;
} else if (used_cpus == null || used_cpus.equals("")) {
used_cpus = 0;
} else {
unused_cpus = cpus - used_cpus;
}
vo.setVirtualKernelAll(String.valueOf(cpus));
vo.setVirtualKernelUsed(String.valueOf(used_cpus));
vo.setVirtualKernelUnUsed(String.valueOf(unused_cpus));
// 三、请求 虚拟内存
Integer mems = jsonObj.getInteger(Constants.OverView.MEMS);
Integer used_mem = jsonObj.getInteger(Constants.OverView.USED_MEM);
Integer unused_mem = 0;
if (mems == null || mems.equals("")) {
mems = 0;
} else if (used_mem == null || used_mem.equals("")) {
used_mem = 0;
} else {
unused_mem = mems - used_mem;
}
vo.setVirtualMemoryAll(String.valueOf(mems));
vo.setVirtualMemoryUsed(String.valueOf(used_mem));
vo.setVirtualMemoryUnUsed(String.valueOf(unused_mem));
// 四、请求 安全组
Integer security_group = jsonObj.getInteger(Constants.OverView.SECURITY_GROUP);
Integer used_security_group = jsonObj.getInteger(Constants.OverView.USED_SECURITY_GROUP);
Integer unused_security_group = 0;
if (security_group == null || security_group.equals("")) {
security_group = 0;
} else if (used_security_group == null || used_security_group.equals("")) {
used_security_group = 0;
} else {
unused_security_group = security_group - used_security_group;
}
vo.setSecurityGroupAll(String.valueOf(security_group));
vo.setSecurityGroupUsed(String.valueOf(used_security_group));
vo.setSecurityGroupUnUsed(String.valueOf(unused_security_group));
}else{
/**provider连接数*/
HttpClientRequest requestProvider = new HttpClientGetRequest(ConfigFileLoad.getConfContent("API_IP") + "/rest/provider_network_cn");
try {
requestProvider.process(new HttpClientResponseHandler() {
@Override
public void handle(String response, String retCode, String msg) throws HttpClientException {
logger.debug("查询一组资源使用情况信息,返回状态:" + retCode + ",返回信息:" + msg);
provider[0] = JSONObject.parseObject(response);
System.out.println(provider[0].get("result").toString()+"rrrrrrrrrrrrr");
String jsonObj = provider[0].get("result").toString();
reslt[0] =jsonObj;
if (jsonObj == null)
return;
}
});
} catch (HttpClientException e) {
e.printStackTrace();
}
if(reslt[0].equals("0")){
throw new ManoException("0","没建立与openstack的连接,请检查openstack资源中openstack提供者是否已添加");
}else{
throw new ManoException("1","没有数据");
}
//throw new HttpClientException("没建立连接");
}
}});
} catch (HttpClientException e) {
throw new ManoException("1","没建立连接");
}
logger.info("SummeryOfResourcesVO is " + vo.toString());
return vo;
}
服务端:
package com.certusnet.nfv.mano.vim.providernet.rest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.certusnet.nfv.mano.exception.ManoException;
import com.certusnet.nfv.mano.rest.ContentType;
import com.certusnet.nfv.mano.vim.ExceptionCode;
import com.certusnet.nfv.mano.vim.HttpsUtil;
import com.certusnet.nfv.mano.vim.host.rsclient.IHostRsClient;
import com.certusnet.nfv.mano.vim.host.rsclient.impl.HostRsClient;
import com.certusnet.nfv.mano.vim.providernet.rsclient.IProvidernetRsClient;
import com.certusnet.nfv.mano.vim.providernet.rsclient.impl.ProvidernetRsClient;
import com.certusnet.nfv.mano.vim.vm.rest.VmRest;
import com.certusnet.nfv.mano.vim.vm.rsclient.IVmRsClient;
@Component
@Path("/")
public class ProvidernetRest {
private static Logger logger = Logger.getLogger(ProvidernetRest.class);
@Autowired
private IProvidernetRsClient providernetRsClient;
// private IProvidernetRsClient providernetRsClient = new ProvidernetRsClient();
@Autowired
private IHostRsClient hostRsClient;
// private IHostRsClient hostRsClient = new HostRsClient();
@GET
@Path("provider_networks")
@Produces(ContentType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.TEXT_PLAIN)
public Object findProvidernetList(@HeaderParam("X-Auth-Token") String token)
{
logger.info("X-Auth-Token=" + token);
try {
String returnStr= providernetRsClient.findProvidernetList(token);
logger.debug("providernetwork-string:"+returnStr);
returnStr=returnStr.replace("null", "\"\"");
JSONObject jsonObj = JSONObject.fromObject(returnStr);
if (!"200".equalsIgnoreCase(jsonObj.getString("retCode")))
throw new ManoException(
ExceptionCode.FIND_PROVIDER_NETWORK_LIST_FAILED);
JSONArray networkJsonObj = jsonObj.getJSONArray("providernets");
logger.info("provider_networkJsonObj_list" + networkJsonObj.toString());
return networkJsonObj;
} catch (ManoException ex) {
throw ex;
} catch (Exception ex) {
throw new ManoException(ExceptionCode.FIND_PROVIDER_NETWORK_LIST_FAILED, ex);
}
}
@GET
@Path("provider_networks/show/{providernet_id}")
@Produces(ContentType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.TEXT_PLAIN)
public Object findProvidernetById(@PathParam("providernet_id") String providernet_id,@HeaderParam("X-Auth-Token") String token)
{
logger.info("X-Auth-Token=" + token);
logger.info("providernet_id=" + providernet_id);
if ((null == providernet_id) || (providernet_id.trim().equalsIgnoreCase(""))) {
throw new ManoException(ExceptionCode.PARAM_NEEDED_IS_NULL);
}
try {
String returnStr= providernetRsClient.findProvidernetById(providernet_id,token);
logger.debug("providernetwork-detail-string:"+returnStr);
returnStr=returnStr.replace("null", "\"\"");
JSONObject jsonObj = JSONObject.fromObject(returnStr);
if (!"200".equalsIgnoreCase(jsonObj.getString("retCode")))
throw new ManoException(
ExceptionCode. FIND_PROVIDER_NETWORK_DETAIL_FAILED);
JSONObject networkJsonObj = jsonObj.getJSONObject("providernet");
logger.info("provider_networkJsonObj_detail" + networkJsonObj.toString());
return networkJsonObj;
} catch (ManoException ex) {
throw ex;
} catch (Exception ex) {
throw new ManoException(ExceptionCode. FIND_PROVIDER_NETWORK_DETAIL_FAILED, ex);
}
}
@GET
@Path("provider_networks/{provider_network_uuid}/tenant_networks")
@Produces(ContentType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.TEXT_PLAIN)
public Object findTenantNetworkListByProvidernetId(@PathParam("provider_network_uuid") String providernet_id,@HeaderParam("X-Auth-Token") String token)
{
logger.info("X-Auth-Token=" + token);
logger.info("providernet_id=" + providernet_id);
if ((null == providernet_id) || (providernet_id.trim().equalsIgnoreCase(""))) {
throw new ManoException(ExceptionCode.PARAM_NEEDED_IS_NULL);
}
try {
String returnStr= providernetRsClient.findTenantNetworkList( providernet_id,token) ;
logger.debug("providernetwork-TenantNetworkList-string:"+returnStr);
returnStr=returnStr.replace("null", "\"\"");
JSONObject jsonObj = JSONObject.fromObject(returnStr);
logger.debug("providernetwork-TenantNetworkList-jsonOjb:"+jsonObj);
if (!"200".equalsIgnoreCase(jsonObj.getString("retCode")))
throw new ManoException(
ExceptionCode.FIND_TENANT_NETWORK_LIST_FAILED);
JSONArray networkJsonObj = jsonObj.getJSONArray("providernet_network");
logger.info("provider_networkJsonObj_list" + networkJsonObj.toString());
return networkJsonObj;
} catch (ManoException ex) {
throw ex;
} catch (Exception ex) {
throw new ManoException(ExceptionCode.FIND_TENANT_NETWORK_LIST_FAILED, ex);
}
}
@GET
@Path("provider_networks/{provider_network_name}/interfaces")
@Produces(ContentType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.TEXT_PLAIN)
public Object findInterfaceListByProvidernetId(@PathParam("provider_network_name") String providernet_name,@HeaderParam("X-Auth-Token") String token)
{
logger.info("X-Auth-Token=" + token);
logger.info("providernet_name=" + providernet_name);
if ((null == providernet_name) || (providernet_name.trim().equalsIgnoreCase(""))) {
throw new ManoException(ExceptionCode.PARAM_NEEDED_IS_NULL);
}
try {
String hostReturnStr= hostRsClient.findHostList(token);
logger.debug("hostReturnStr:"+hostReturnStr);
JSONObject hostJsonObj = JSONObject.fromObject(hostReturnStr);
logger.debug("hostJsonObj-jsonObj:"+hostJsonObj);
if (!"200".equalsIgnoreCase(hostJsonObj.getString("retCode")))
throw new ManoException(
ExceptionCode.FIND_HOST_LIST_FAILED);
JSONArray ihostArray = hostJsonObj.getJSONArray("ihost");
logger.debug("ihostArray-jsonObj:"+ihostArray);
JSONArray returnJsonArray = new JSONArray();
int ihostArray_length = ihostArray.size();
for(int i=0; i < ihostArray_length; i++){
String tempIhostId= "" + ihostArray.getJSONObject(i).getString("uuid");
logger.info("tempIhostId="+tempIhostId);
String interfaceReturnStr= providernetRsClient.findInterfaceListByHostId(tempIhostId,token) ;
logger.debug("interfaceReturnStr="+interfaceReturnStr);
interfaceReturnStr= interfaceReturnStr.replace("null", "\"\"");
JSONObject interfaceJsonObj = JSONObject.fromObject(interfaceReturnStr);
logger.debug("interfaceJsonObj="+interfaceJsonObj);
JSONArray interfaceArray = interfaceJsonObj.getJSONArray("iinterfaces");
int interfaceArray_length = interfaceArray.size();
for(int j=0; j < interfaceArray_length; j++){
JSONObject tmpJSONObject = interfaceArray.getJSONObject(j);
String tempProvidernetNameList = tmpJSONObject.getString("providernetworks");
if (tempProvidernetNameList.indexOf(providernet_name) > -1){
returnJsonArray.add(tmpJSONObject);
}
}
}
logger.info("returnJsonArray=" + returnJsonArray);
return returnJsonArray;
} catch (ManoException ex) {
throw ex;
} catch (Exception ex) {
throw new ManoException(ExceptionCode.FIND_TENANT_NETWORK_LIST_FAILED, ex);
}
}
@GET
@Path("logical_topology")
@Produces(ContentType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.TEXT_PLAIN)
public Object findProvidernetListDetail(@HeaderParam("X-Auth-Token") String token)
{
logger.info("X-Auth-Token=" + token);
try {
String returnStr= providernetRsClient.findProvidernetListDetail(token);
returnStr=returnStr.replace("nil", "\"\"");
JSONObject jsonObj = JSONObject.fromObject(returnStr);
if (!"200".equalsIgnoreCase(jsonObj.getString("retCode")))
throw new ManoException(
ExceptionCode.FIND_PROVIDER_NETWORK_LIST_FAILED);
JSONArray networkJsonObj = jsonObj.getJSONArray("providernet_list_detail");
logger.info("provider_networkJsonObj_list" + networkJsonObj.toString());
return networkJsonObj;
} catch (ManoException ex) {
throw ex;
} catch (Exception ex) {
throw new ManoException(ExceptionCode.FIND_PROVIDER_NETWORK_LIST_FAILED, ex);
}
}
@GET
@Path("provider_networks_without_tenant_network")
@Produces(ContentType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.TEXT_PLAIN)
public Object findProvidernetListWithoutNetwork(@HeaderParam("X-Auth-Token") String token)
{
logger.info("X-Auth-Token=" + token);
try {
String returnStr= providernetRsClient.findProvidernetListWithoutNetwork(token);
logger.debug("providernetwork-string:"+returnStr);
returnStr=returnStr.replace("null", "\"\"");
JSONObject jsonObj = JSONObject.fromObject(returnStr);
if (!"200".equalsIgnoreCase(jsonObj.getString("retCode")))
throw new ManoException(
ExceptionCode.FIND_PROVIDER_NETWORK_LIST_FAILED);
JSONArray networkJsonObj = jsonObj.getJSONArray("providernets");
logger.info("provider_networkJsonObj_list" + networkJsonObj.toString());
return networkJsonObj;
} catch (ManoException ex) {
throw ex;
} catch (Exception ex) {
throw new ManoException(ExceptionCode.FIND_PROVIDER_NETWORK_LIST_FAILED, ex);
}
}
/**
* 获取provider的连接数
* @author admin
* @param token
* @return
*/
@GET
@Path("provider_network_cn")
@Produces(ContentType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.TEXT_PLAIN)
public Object findProvidercnCount(@HeaderParam("X-Auth-Token") String token)
{
logger.info("X-Auth-Token=" + token);
try {
String returnStr= providernetRsClient.findProvidercnCount(token);
logger.debug("providernetwork-string:"+returnStr);
returnStr=returnStr.replace("null", "\"\"");
JSONObject jsonObj = JSONObject.fromObject(returnStr);
if (!"200".equalsIgnoreCase(jsonObj.getString("retCode")))
throw new ManoException(
ExceptionCode.FIND_PROVIDER_NETWORK_LIST_FAILED);
String networkJsonObj = jsonObj.getString("ems_num");
logger.info("provider_networkJsonObj_list" + networkJsonObj);
return networkJsonObj;
} catch (ManoException ex) {
throw ex;
} catch (Exception ex) {
throw new ManoException(ExceptionCode.FIND_PROVIDER_NETWORK_LIST_FAILED, ex);
}
}
public static void main(String[] args) {
ProvidernetRest providernet = new ProvidernetRest();
// providernet.findProvidernetList();a
// providernet.findProvidernetById("e59cf271-34fb-4c77-8cf1-bb9792c48e3c");
// providernet.findTenantNetworkListByProvidernetId("6f0ca883-e581-4dc5-a14f-3e9a5a4651a7");
// providernet.findInterfaceListByProvidernetId("provider-data-15");
// providernet.findProvidernetListDetail();
}
}
服务端接口注册:
package com.certusnet.nfv.mano.vim;
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.web.filter.RequestContextFilter;
import com.certusnet.nfv.mano.rest.ManoExceptionMapper;
import com.certusnet.nfv.mano.rest.RestResponseFilter;
import com.certusnet.nfv.mano.vim.host.rest.HostRest;
import com.certusnet.nfv.mano.vim.alarm.rest.AlarmRest;
import com.certusnet.nfv.mano.vim.cpu_utilization_history.rest.Cpu_utilization_historyRest;
import com.certusnet.nfv.mano.vim.network.rest.NetworkRest;
import com.certusnet.nfv.mano.vim.port.rest.PortRest;
import com.certusnet.nfv.mano.vim.providernet.rest.ProvidernetRest;
import com.certusnet.nfv.mano.vim.usage_history.rest.Usage_historyRest;
import com.certusnet.nfv.mano.vim.vm.rest.VmRest;
import com.certusnet.nfv.mano.vim.vnf.rest.HelloNfvRest;
public class RestApplication extends ResourceConfig {
/**
* Register JAX-RS application components.
*/
public RestApplication () {
register(RequestContextFilter.class);
register(HelloNfvRest.class);
register(VmRest.class);
register(Usage_historyRest.class);
register(Cpu_utilization_historyRest.class);
register(ProvidernetRest.class);
register(ManoExceptionMapper.class);
register(RestResponseFilter.class);
register(NetworkRest.class);
register(HostRest.class);
register(AlarmRest.class);
register(PortRest.class);
}
}
rest 应用demo 2
猜你喜欢
转载自yuhuiblog6338999322098842.iteye.com/blog/2207624
今日推荐
周排行