Merge "Fix for Sonar bugs Signed-off-by: Bindya Narayan <bindya.narayan@intel.com>"
[ovsdb.git] / northbound / src / main / java / org / opendaylight / ovsdb / northbound / OvsdbNorthboundV3.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc. and others
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Authors : Madhu Venugopal
9  */
10 package org.opendaylight.ovsdb.northbound;
11
12 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
13 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
14 import org.opendaylight.controller.sal.authorization.Privilege;
15
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 import javax.ws.rs.Path;
20 import javax.ws.rs.core.Context;
21 import javax.ws.rs.core.SecurityContext;
22 import javax.ws.rs.core.UriInfo;
23
24 /**
25 * OVSDB Northbound V3 REST API
26 */
27
28 @Path("/v3/")
29 public class OvsdbNorthboundV3 {
30     protected static final Logger LOGGER = LoggerFactory.getLogger(OvsdbNorthboundV3.class);
31
32     @Context
33     private String username;
34
35     @Context
36     public void setSecurityContext(SecurityContext context) {
37         if (context != null && context.getUserPrincipal() != null) {
38             username = context.getUserPrincipal().getName();
39         }
40     }
41
42     protected String getUserName() {
43         return username;
44     }
45
46     @Path("node")
47     public NodeResource getNode(){
48         if (!NorthboundUtils.isAuthorized(getUserName(), "default", Privilege.WRITE, this)) {
49             throw new UnauthorizedException("User is not authorized to perform this operation");
50         }
51         return new NodeResource();
52     }
53
54 }