Change: add OPEN-O seed code for VF-C
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / openo / nfvo / vnfmadapter / service / rest / AuthRoa.java
1 /*
2  * Copyright 2016 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.openo.nfvo.vnfmadapter.service.rest;
18
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;
32
33 import org.openo.nfvo.vnfmadapter.common.VnfmJsonUtil;
34 import org.openo.nfvo.vnfmadapter.service.constant.Constant;
35 import org.openo.nfvo.vnfmadapter.service.constant.ParamConstants;
36 import org.openo.nfvo.vnfmadapter.service.process.AuthMgr;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import net.sf.json.JSONObject;
41
42 /**
43  * Provide interfaces for authInfo
44  * <br/>
45  * <p>
46  * </p>
47  *
48  * @author
49  * @version NFVO 0.5 Aug 24, 2016
50  */
51 @Path("/rest/plat/smapp/v1")
52 @Consumes(MediaType.APPLICATION_JSON)
53 @Produces(MediaType.APPLICATION_JSON)
54 public class AuthRoa {
55
56     private static final Logger LOG = LoggerFactory.getLogger(AuthRoa.class);
57
58     private AuthMgr authMgr;
59
60     public void setAuthMgr(AuthMgr authMgr) {
61         this.authMgr = authMgr;
62     }
63
64     /**
65      * Provide interface for add authInfo
66      * <br/>
67      *
68      * @param context
69      * @return
70      * @since NFVO 0.5
71      */
72     @PUT
73     @Path("/oauth/token")
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);
78
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);
83
84             return resultStr;
85         }
86
87         JSONObject authResult = authMgr.authToken(subJsonObject);
88         LOG.warn("authResult: {}", authResult);
89         if(authResult.getInt("retCode") == Constant.REST_SUCCESS) {
90             JSONObject data = authResult.getJSONObject("data");
91
92             resp.setStatus(Constant.HTTP_OK);
93             // resp.setHeader("accessSession", data.getString("accessSession"));
94             return data.toString();// String.format(ParamConstants.GET_TOKEN_SUC_RESP,
95             // data.getString("userName"),
96             // data.getString("userName"), data.getString("roaRand"));
97         } else if(authResult.getInt("retCode") == Constant.HTTP_INNERERROR) {
98             Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(authResult.getString("data")).build();
99
100             return String.format(ParamConstants.GET_TOKEN_FAIL_RESP, authResult.getString("data"));
101         } else {
102             Response.status(Response.Status.UNAUTHORIZED).entity(authResult.getString("data")).build();
103
104             return String.format(ParamConstants.GET_TOKEN_FAIL_RESP, authResult.getString("data"));
105         }
106     }
107
108     /**
109      * Provide interface for delete authInfo
110      * <br/>
111      *
112      * @param context
113      * @param userName
114      * @param roarand
115      * @return
116      * @since NFVO 0.5
117      */
118     @DELETE
119     @Path("/auth/tokens/{userName}/{roarand}")
120     public String delAuthToken(@Context HttpServletRequest context, @PathParam("userName") String userName,
121                                @PathParam("roarand") String roarand, @Context HttpServletResponse resp) {
122         LOG.warn("function=logout, msg=enter to logout");
123         JSONObject resultJson = new JSONObject();
124
125         resultJson.put("Information", "Operation success");
126         resp.setStatus(Constant.HTTP_NOCONTENT);
127         LOG.warn("function=logout, msg=end to logout");
128         return resultJson.toString();
129     }
130
131     /**
132      * Provide interface for handshake authInfo
133      * <br/>
134      *
135      * @param context
136      * @param roattr
137      * @return
138      * @since NFVO 0.5
139      */
140     @GET
141     @Path("/nfvo/shakehand")
142     public String shakehand(@Context HttpServletRequest context, @QueryParam("roattr") String roattr,
143                             @Context HttpServletResponse resp) {
144         JSONObject resultJson = new JSONObject();
145         resultJson.put("status", "running");
146         resultJson.put("description", "Operation success");
147         resp.setStatus(Constant.HTTP_OK);
148
149         return resultJson.toString();
150     }
151 }