diff options
author | victor.gao <victor.gao@huawei.com> | 2018-03-28 14:55:02 +0800 |
---|---|---|
committer | victor.gao <victor.gao@huawei.com> | 2018-03-28 14:55:02 +0800 |
commit | a47a9c1f6a65deb36b5ed8918ddafcb003948230 (patch) | |
tree | a92fae693be4af1f3f22ec5b2f5336b531d31c8f /service/src | |
parent | 7c134b69ea323ddc30a507225ddffaa58565394d (diff) |
Add multivimproxy proxy code
Change-Id: I0c5c0d79721884fe61d4c5d9a678a10324b3844a
Issue-ID: VFC-644
Signed-off-by: victor.gao <victor.gao@huawei.com>
Diffstat (limited to 'service/src')
-rw-r--r-- | service/src/main/java/org/onap/vfc/nfvo/multivimproxy/service/rest/ProxyRoa.java | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/service/rest/ProxyRoa.java b/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/service/rest/ProxyRoa.java new file mode 100644 index 0000000..1e15740 --- /dev/null +++ b/service/src/main/java/org/onap/vfc/nfvo/multivimproxy/service/rest/ProxyRoa.java @@ -0,0 +1,195 @@ +/* + * Copyright 2016-2017 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.vfc.nfvo.multivimproxy.service.rest; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; + +import org.onap.vfc.nfvo.multivimproxy.common.util.RestfulUtil; +import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulParametes; +import org.onap.vfc.nfvo.multivimproxy.common.util.restclient.RestfulResponse; +import org.onap.vfc.nfvo.multivimproxy.common.constant.Constant; +import org.onap.vfc.nfvo.multivimproxy.common.constant.ParamConstant; +import org.onap.vfc.nfvo.multivimproxy.common.constant.UrlConstant; +import org.onap.vfc.nfvo.multivimproxy.common.conf.Config; +import org.onap.vfc.nfvo.multivimproxy.common.util.request.RequestUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + + +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class ProxyRoa { + + private static final Logger LOGGER = LoggerFactory.getLogger(ProxyRoa.class); + private static final String vimId = Config.getCloudRegionId() + "_" + Config.getCloudRegionId(); + + @POST + @Path("/v3/auth/tokens") + public String createTokens(@Context HttpServletRequest context, @Context HttpServletResponse rsp) { + JSONObject object = RequestUtil.getJsonRequestBody(context); + if (null == object) { + LOGGER.error("function=createTokens; msg=add error, because createTokens is null."); + String resultStr = "Login params insufficient"; + rsp.setStatus(Constant.HTTP_BAD_REQUEST); + + return resultStr; + } + + LOGGER.info("IdentityRoa::createTokens:{}", object.toString()); + + // transfer to MultiVim + RestfulParametes restfulParametes = new RestfulParametes(); + Map<String, String> headerMap = new HashMap<>(3); + headerMap.put("Content-Type", "application/json"); + restfulParametes.setHeaderMap(headerMap); + restfulParametes.setRawData(object.toString()); + String identityUrl = UrlConstant.MULTI_VIM_PREFIX + vimId + "/v3/auth/tokens"; + + RestfulResponse rest = RestfulUtil.getResponse(identityUrl, restfulParametes, null, ParamConstant.PARAM_POST); + + String result = rest.getResponseContent(); + if (null != result) { + JSONObject ret = JSONObject.fromObject(result); + JSONArray catalog = ret.getJSONArray("catalog"); + + for (int i = 0; i < catalog.size(); i++) { + JSONArray endpoints = catalog.getJSONObject(i).getJSONArray("endpoints"); + for (int j = 0; j < endpoints.size(); j++) { + String newUrl = ""; + JSONObject endpoint = endpoints.getJSONObject(j); + String url = endpoint.getString("url"); + for (int k = 6; k < url.split("/").length; k++) { + newUrl += "/" + url.split("/")[k]; + } + newUrl += Config.getOpenstackPrefix(); + endpoint.replace("url", newUrl); + } + } + + rsp.setStatus(rest.getStatus()); + + } else { + LOGGER.error("function=createTokens; msg=add error, because multivim return null."); + } + + return result; + } + + @POST + @Path("{var:.*}") + public String proxyPost(@Context HttpServletRequest context, @Context HttpServletResponse rsp) { + JSONObject object = RequestUtil.getJsonRequestBody(context); + if (null == object) { + LOGGER.error("function=proxyPost; msg=Post error, because proxyPost is null."); + String resultStr = "POST params insufficient"; + rsp.setStatus(Constant.HTTP_BAD_REQUEST); + + return resultStr; + } + // transfer to MultiVim + RestfulParametes restfulParametes = new RestfulParametes(); + Map<String, String> headerMap = new HashMap<>(3); + headerMap.put("Content-Type", "application/json"); + restfulParametes.setHeaderMap(headerMap); + restfulParametes.setRawData(object.toString()); + String Url = UrlConstant.MULTI_VIM_PREFIX + vimId + context.getRequestURI(); + + RestfulResponse rest = RestfulUtil.getResponse(Url, restfulParametes, null, ParamConstant.PARAM_POST); + String result = rest.getResponseContent(); + rsp.setStatus(rest.getStatus()); + return result; + } + + @GET + @Path("{var:.*}") + public String porxyGet(@Context HttpServletRequest context, @Context HttpServletResponse rsp) { + JSONObject object = RequestUtil.getJsonRequestBody(context); + + // transfer to MultiVim + RestfulParametes restfulParametes = new RestfulParametes(); + Map<String, String> headerMap = new HashMap<>(3); + headerMap.put("Content-Type", "application/json"); + restfulParametes.setHeaderMap(headerMap); + restfulParametes.setRawData(object.toString()); + String Url = UrlConstant.MULTI_VIM_PREFIX + vimId + context.getRequestURI(); + + RestfulResponse rest = RestfulUtil.getResponse(Url, restfulParametes, null, ParamConstant.PARAM_GET); + String result = rest.getResponseContent(); + rsp.setStatus(rest.getStatus()); + return result; + } + + @PUT + @Path("{var:.*}") + public String porxyPut(@Context HttpServletRequest context, @Context HttpServletResponse rsp) { + JSONObject object = RequestUtil.getJsonRequestBody(context); + if (null == object) { + LOGGER.error("function=proxyPost; msg=PUT error, because porxyPut is null."); + String resultStr = "update params insufficient"; + rsp.setStatus(Constant.HTTP_BAD_REQUEST); + + return resultStr; + } + // transfer to MultiVim + RestfulParametes restfulParametes = new RestfulParametes(); + Map<String, String> headerMap = new HashMap<>(3); + headerMap.put("Content-Type", "application/json"); + restfulParametes.setHeaderMap(headerMap); + restfulParametes.setRawData(object.toString()); + String Url = UrlConstant.MULTI_VIM_PREFIX + vimId + context.getRequestURI(); + RestfulResponse rest = RestfulUtil.getResponse(Url, restfulParametes, null, RestfulUtil.TYPE_PUT); + String result = rest.getResponseContent(); + rsp.setStatus(rest.getStatus()); + return result; + } + + @DELETE + @Path("{var:.*}") + public String porxyDelete(@Context HttpServletRequest context, @Context HttpServletResponse rsp) { + JSONObject object = RequestUtil.getJsonRequestBody(context); + + // transfer to MultiVim + RestfulParametes restfulParametes = new RestfulParametes(); + Map<String, String> headerMap = new HashMap<>(3); + headerMap.put("Content-Type", "application/json"); + restfulParametes.setHeaderMap(headerMap); + restfulParametes.setRawData(object.toString()); + String Url = UrlConstant.MULTI_VIM_PREFIX + vimId + context.getRequestURI(); + + RestfulResponse rest = RestfulUtil.getResponse(Url, restfulParametes, null, RestfulUtil.TYPE_DEL); + String result = rest.getResponseContent(); + rsp.setStatus(rest.getStatus()); + return result; + } + +} |