2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.rest;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.servlet.http.HttpServletResponse;
21 import javax.ws.rs.Consumes;
22 import javax.ws.rs.DELETE;
23 import javax.ws.rs.GET;
24 import javax.ws.rs.PUT;
25 import javax.ws.rs.Path;
26 import javax.ws.rs.PathParam;
27 import javax.ws.rs.Produces;
28 import javax.ws.rs.QueryParam;
29 import javax.ws.rs.core.Context;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
33 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmJsonUtil;
34 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
35 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
36 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.process.AuthMgr;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 import net.sf.json.JSONObject;
43 * Provide interfaces for authInfo
49 * @version VFC 1.0 Aug 24, 2016
51 @Path("/rest/plat/smapp/v1")
52 @Consumes(MediaType.APPLICATION_JSON)
53 @Produces(MediaType.APPLICATION_JSON)
54 public class AuthRoa {
56 private static final Logger LOG = LoggerFactory.getLogger(AuthRoa.class);
58 private AuthMgr authMgr;
60 public void setAuthMgr(AuthMgr authMgr) {
61 this.authMgr = authMgr;
65 * Provide interface for add authInfo
74 public String authToken(@Context HttpServletRequest context, @Context HttpServletResponse resp) {
75 LOG.warn("function=login, msg=enter to get token.");
76 JSONObject subJsonObject = VnfmJsonUtil.getJsonFromContexts(context);
77 LOG.warn("subJsonObject: {}", subJsonObject);
79 if(null == subJsonObject) {
80 LOG.error("function=login, msg=params are insufficient");
81 String resultStr = "Login params insufficient";
82 resp.setStatus(Constant.HTTP_BAD_REQUEST);
87 JSONObject authResult = authMgr.authToken(subJsonObject);
88 LOG.warn("authResult: {}", authResult);
89 if(authResult.getInt(Constant.RETCODE) == Constant.REST_SUCCESS) {
90 JSONObject data = authResult.getJSONObject("data");
92 resp.setStatus(Constant.HTTP_OK);
93 return data.toString();
94 } else if(authResult.getInt(Constant.RETCODE) == Constant.HTTP_INNERERROR) {
95 Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(authResult.getString("data")).build();
97 return String.format(ParamConstants.GET_TOKEN_FAIL_RESP, authResult.getString("data"));
99 Response.status(Response.Status.UNAUTHORIZED).entity(authResult.getString("data")).build();
101 return String.format(ParamConstants.GET_TOKEN_FAIL_RESP, authResult.getString("data"));
106 * Provide interface for delete authInfo
116 @Path("/auth/tokens/{userName}/{roarand}")
117 public String delAuthToken(@PathParam(Constant.USERNAME) String userName, @PathParam("roarand") String roarand,
118 @Context HttpServletResponse resp) {
119 LOG.warn("function=logout, msg=enter to logout");
120 JSONObject resultJson = new JSONObject();
122 resultJson.put("Information", "Operation success");
123 resp.setStatus(Constant.HTTP_NOCONTENT);
124 LOG.warn("function=logout, msg=end to logout");
125 return resultJson.toString();
129 * Provide interface for handshake authInfo
138 @Path("/nfvo/shakehand")
139 public String shakehand(@QueryParam("roattr") String roattr, @Context HttpServletResponse resp) {
140 JSONObject resultJson = new JSONObject();
141 resultJson.put("status", "running");
142 resultJson.put("description", "Operation success");
143 resp.setStatus(Constant.HTTP_OK);
145 return resultJson.toString();