Merge "addvnf support emsUuid"
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / rest / AuthRoa.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 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.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;
39
40 import net.sf.json.JSONObject;
41
42 /**
43  * Provide interfaces for authInfo <br/>
44  * <p>
45  * auth tokens interface is provided by platform not in nfvo for vnfm
46  * differences from other interface
47  * </p>
48  *
49  * @author
50  * @version VFC 1.0 Aug 24, 2016
51  */
52 @Path("/rest")
53 @Consumes(MediaType.APPLICATION_JSON)
54 @Produces(MediaType.APPLICATION_JSON)
55 public class AuthRoa {
56
57         private static final Logger LOG = LoggerFactory.getLogger(AuthRoa.class);
58
59         private AuthMgr authMgr;
60
61         public void setAuthMgr(AuthMgr authMgr) {
62                 this.authMgr = authMgr;
63         }
64
65         /**
66          * Provide interface for add authInfo <br/>
67          *
68          * @param context
69          * @return
70          * @since VFC 1.0
71          */
72         @PUT
73         @Path("/plat/smapp/v1/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(Constant.RETCODE) == Constant.REST_SUCCESS) {
90                         JSONObject data = authResult.getJSONObject("data");
91                         resp.setStatus(Constant.HTTP_OK);
92                         return data.toString();
93                 } else if (authResult.getInt(Constant.RETCODE) == Constant.HTTP_INNERERROR) {
94                         Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(authResult.getString("data")).build();
95                         return String.format(ParamConstants.GET_TOKEN_FAIL_RESP, authResult.getString("data"));
96                 } else {
97                         Response.status(Response.Status.UNAUTHORIZED).entity(authResult.getString("data")).build();
98                         return String.format(ParamConstants.GET_TOKEN_FAIL_RESP, authResult.getString("data"));
99                 }
100         }
101
102         /**
103          * Provide interface for delete authInfo <br/>
104          *
105          * @param userName
106          * @param roarand
107          * @return
108          * @since VFC 1.0
109          */
110         @DELETE
111         @Path("/plat/smapp/v1/auth/tokens/{userName}/{roarand}")
112         public String delAuthToken(@PathParam(Constant.USERNAME) String userName, @PathParam("roarand") String roarand,
113                         @Context HttpServletResponse resp) {
114                 LOG.warn("function=logout, msg=enter to logout");
115                 JSONObject resultJson = new JSONObject();
116
117                 resultJson.put("Information", "Operation success");
118                 resp.setStatus(Constant.HTTP_NOCONTENT);
119                 LOG.warn("function=logout, msg=end to logout");
120                 return resultJson.toString();
121         }
122
123         /**
124          * Provide interface for handshake authInfo <br/>
125          *
126          * @param roattr
127          * @return
128          * @since VFC 1.0
129          */
130         @GET
131         @Path("/vnfmmed/v2/nfvo/shakehand")
132         public String shakehand(@QueryParam("roattr") String roattr, @Context HttpServletResponse resp) {
133                 JSONObject resultJson = new JSONObject();
134                 resultJson.put("status", "running");
135                 resultJson.put("description", "Operation success");
136                 resp.setStatus(Constant.HTTP_OK);
137
138                 return resultJson.toString();
139         }
140
141         /**
142          * Provide interface for handshake authInfo <br/>
143          *
144          * @param roattr
145          * @return
146          * @since VFC 1.0
147          */
148         @GET
149         @Path("/plat/smapp/v1/nfvo/shakehand")
150         public String shakehandOld(@QueryParam("roattr") String roattr, @Context HttpServletResponse resp) {
151                 JSONObject resultJson = new JSONObject();
152                 resultJson.put("status", "running");
153                 resultJson.put("description", "Operation success");
154                 resp.setStatus(Constant.HTTP_OK);
155
156                 return resultJson.toString();
157         }
158 }