Fix scale vnf parameters
[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
44  * <br/>
45  * <p>
46  * auth tokens interface is provided by platform
47  * not in nfvo for vnfm
48  * differences from other interface
49  * </p>
50  *
51  * @author
52  * @version VFC 1.0 Aug 24, 2016
53  */
54 @Path("/rest")
55 @Consumes(MediaType.APPLICATION_JSON)
56 @Produces(MediaType.APPLICATION_JSON)
57 public class AuthRoa {
58
59     private static final Logger LOG = LoggerFactory.getLogger(AuthRoa.class);
60
61     private AuthMgr authMgr;
62
63     public void setAuthMgr(AuthMgr authMgr) {
64         this.authMgr = authMgr;
65     }
66
67     /**
68      * Provide interface for add authInfo
69      * <br/>
70      *
71      * @param context
72      * @return
73      * @since VFC 1.0
74      */
75     @PUT
76     @Path("/plat/smapp/v1/oauth/token")
77     public String authToken(@Context HttpServletRequest context, @Context HttpServletResponse resp) {
78         LOG.warn("function=login, msg=enter to get token.");
79         JSONObject subJsonObject = VnfmJsonUtil.getJsonFromContexts(context);
80         LOG.warn("subJsonObject: {}", subJsonObject);
81
82         if(null == subJsonObject) {
83             LOG.error("function=login, msg=params are insufficient");
84             String resultStr = "Login params insufficient";
85             resp.setStatus(Constant.HTTP_BAD_REQUEST);
86
87             return resultStr;
88         }
89
90         JSONObject authResult = authMgr.authToken(subJsonObject);
91         LOG.warn("authResult: {}", authResult);
92         if(authResult.getInt(Constant.RETCODE) == Constant.REST_SUCCESS) {
93             JSONObject data = authResult.getJSONObject("data");
94             resp.setStatus(Constant.HTTP_OK);
95             return data.toString();
96         } else if(authResult.getInt(Constant.RETCODE) == Constant.HTTP_INNERERROR) {
97             Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(authResult.getString("data")).build();
98             return String.format(ParamConstants.GET_TOKEN_FAIL_RESP, authResult.getString("data"));
99         } else {
100             Response.status(Response.Status.UNAUTHORIZED).entity(authResult.getString("data")).build();
101             return String.format(ParamConstants.GET_TOKEN_FAIL_RESP, authResult.getString("data"));
102         }
103     }
104
105     /**
106      * Provide interface for delete authInfo
107      * <br/>
108      *
109      * @param userName
110      * @param roarand
111      * @return
112      * @since VFC 1.0
113      */
114     @DELETE
115     @Path("/plat/smapp/v1/auth/tokens/{userName}/{roarand}")
116     public String delAuthToken(@PathParam(Constant.USERNAME) String userName, @PathParam("roarand") String roarand,
117             @Context HttpServletResponse resp) {
118         LOG.warn("function=logout, msg=enter to logout");
119         JSONObject resultJson = new JSONObject();
120
121         resultJson.put("Information", "Operation success");
122         resp.setStatus(Constant.HTTP_NOCONTENT);
123         LOG.warn("function=logout, msg=end to logout");
124         return resultJson.toString();
125     }
126
127     /**
128      * Provide interface for handshake authInfo
129      * <br/>
130      *
131      * @param roattr
132      * @return
133      * @since VFC 1.0
134      */
135     @GET
136     @Path("/vnfmmed/v2/nfvo/shakehand")
137     public String shakehand(@QueryParam("roattr") String roattr, @Context HttpServletResponse resp) {
138         JSONObject resultJson = new JSONObject();
139         resultJson.put("status", "running");
140         resultJson.put("description", "Operation success");
141         resp.setStatus(Constant.HTTP_OK);
142
143         return resultJson.toString();
144     }
145 }