BUG-2218: Keep existing link augmentations during discovery process
[controller.git] / opendaylight / northbound / commons / src / main / java / org / opendaylight / controller / northbound / commons / utils / NorthboundUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
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 package org.opendaylight.controller.northbound.commons.utils;
9
10 import java.util.HashMap;
11 import java.util.Map;
12
13 import javax.ws.rs.core.Response;
14
15 import org.opendaylight.controller.containermanager.IContainerAuthorization;
16 import org.opendaylight.controller.sal.authorization.Privilege;
17 import org.opendaylight.controller.sal.authorization.UserLevel;
18 import org.opendaylight.controller.sal.core.Description;
19 import org.opendaylight.controller.sal.core.Name;
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.core.NodeConnector;
22 import org.opendaylight.controller.sal.utils.GlobalConstants;
23 import org.opendaylight.controller.sal.utils.ServiceHelper;
24 import org.opendaylight.controller.sal.utils.Status;
25 import org.opendaylight.controller.sal.utils.StatusCode;
26 import org.opendaylight.controller.switchmanager.ISwitchManager;
27 import org.opendaylight.controller.usermanager.IUserManager;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class NorthboundUtils {
32
33     private static final Map<StatusCode, Response.Status> ResponseStatusMapping = new HashMap<StatusCode, Response.Status>() {
34         private static final long serialVersionUID = 1L;
35         {
36             put(StatusCode.SUCCESS, Response.Status.OK);
37             put(StatusCode.BADREQUEST, Response.Status.BAD_REQUEST);
38             put(StatusCode.UNAUTHORIZED, Response.Status.UNAUTHORIZED);
39             put(StatusCode.FORBIDDEN, Response.Status.FORBIDDEN);
40             put(StatusCode.NOTFOUND, Response.Status.NOT_FOUND);
41             put(StatusCode.NOTALLOWED, Response.Status.FORBIDDEN);
42             put(StatusCode.NOTACCEPTABLE, Response.Status.NOT_ACCEPTABLE);
43             put(StatusCode.TIMEOUT, Response.Status.GONE);
44             put(StatusCode.CONFLICT, Response.Status.CONFLICT);
45             put(StatusCode.GONE, Response.Status.GONE);
46             put(StatusCode.UNSUPPORTED, Response.Status.BAD_REQUEST);
47             put(StatusCode.INTERNALERROR, Response.Status.INTERNAL_SERVER_ERROR);
48             put(StatusCode.NOTIMPLEMENTED, Response.Status.NOT_ACCEPTABLE);
49             put(StatusCode.NOSERVICE, Response.Status.SERVICE_UNAVAILABLE);
50             put(StatusCode.UNDEFINED, Response.Status.BAD_REQUEST);
51         }
52     };
53
54     private static final String AUDIT = "audit";
55
56     private static final Logger logger = LoggerFactory.getLogger(AUDIT);
57
58     // Suppress default constructor for noninstantiability
59     private NorthboundUtils() {
60     }
61
62     /**
63      * Returns Response.Status for a given status. If the status is null or if
64      * the corresponding StatusCode is not present in the ResponseStatusMapping,
65      * it returns null.
66      *
67      * @param status
68      *            The Status
69      * @return The Response.Status for a given status
70      */
71     public static Response.Status getResponseStatus(Status status) {
72         return ResponseStatusMapping.get(status.getCode());
73     }
74
75     /**
76      * Returns Response for a given status. If the status provided is null or if
77      * the corresponding StatusCode is not present in the ResponseStatusMapping,
78      * it returns Response with StatusType as INTERNAL_SERVER_ERROR.
79      *
80      * @param status
81      *            The Status
82      * @return The Response for a given status.
83      */
84     public static Response getResponse(Status status) {
85         if ((status == null) || (!ResponseStatusMapping.containsKey(status.getCode()))) {
86             return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Action Result Unknown").build();
87         }
88         return Response.status(ResponseStatusMapping.get(status.getCode())).entity(status.getDescription()).build();
89     }
90
91     /**
92      * Returns whether the current user has the required privilege on the
93      * specified container
94      *
95      * @param userName
96      *            The user name
97      * @param containerName
98      *            The container name
99      * @param required
100      *            Operation to be performed - READ/WRITE
101      * @param bundle
102      *            Class from where the function is invoked
103      * @return The Status of the request, either Success or Unauthorized
104      */
105     public static boolean isAuthorized(String userName, String containerName, Privilege required, Object bundle) {
106
107         if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
108             IUserManager auth = (IUserManager) ServiceHelper.getGlobalInstance(IUserManager.class, bundle);
109
110             switch (required) {
111             case WRITE:
112                 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKADMIN.ordinal());
113             case READ:
114                 return (auth.getUserLevel(userName).ordinal() <= UserLevel.NETWORKOPERATOR.ordinal());
115             default:
116                 return false;
117             }
118
119         } else {
120             IContainerAuthorization auth = (IContainerAuthorization) ServiceHelper.getGlobalInstance(
121                     IContainerAuthorization.class, bundle);
122
123             if (auth == null) {
124                 return false;
125             }
126
127             Privilege current = auth.getResourcePrivilege(userName, containerName);
128             if (required.ordinal() > current.ordinal()) {
129                 return false;
130             }
131         }
132         return true;
133     }
134
135     public static void auditlog(String moduleName, String user, String action, String resource,
136             String containerName) {
137         String auditMsg = "";
138         String mode = "REST";
139         if (containerName != null) {
140             auditMsg = "Mode: " + mode + " User " + user + " "  + action + " " + moduleName + " " + resource + " in container "
141                     + containerName;
142         } else {
143             auditMsg = "Mode: " + mode + " User " + user + " "  + action + " " + moduleName + " " + resource;
144         }
145         logger.trace(auditMsg);
146     }
147
148     public static void auditlog(String moduleName, String user, String action, String resource) {
149         auditlog(moduleName, user, action, resource, null);
150     }
151
152     public static String getNodeDesc(Node node, ISwitchManager switchManager) {
153         Description desc = (Description) switchManager.getNodeProp(node,
154                 Description.propertyName);
155         String description = (desc == null) ? "" : desc.getValue();
156         return (description.isEmpty() || description.equalsIgnoreCase("none")) ? node
157                 .toString() : description;
158     }
159
160     public static String getNodeDesc(Node node, String containerName,
161             Object bundle) {
162         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
163                 .getInstance(ISwitchManager.class, containerName, bundle);
164         if (switchManager == null) {
165             return null;
166         }
167
168         return getNodeDesc(node, switchManager);
169     }
170
171     public static String getNodeDesc(Node node, Object bundle) {
172         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
173                 .getInstance(ISwitchManager.class,
174                         GlobalConstants.DEFAULT.toString(), bundle);
175         if (switchManager == null) {
176             return null;
177         }
178
179         return getNodeDesc(node, switchManager);
180     }
181
182     public static String getPortName(NodeConnector nodeConnector,
183             String container, Object bundle) {
184         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
185                 .getInstance(ISwitchManager.class, container, bundle);
186         return getPortName(nodeConnector, switchManager);
187     }
188
189     public static String getPortName(NodeConnector nodeConnector, Object bundle) {
190         return getPortName(nodeConnector, GlobalConstants.DEFAULT.toString(), bundle);
191     }
192
193     public static String getPortName(NodeConnector nodeConnector,
194             ISwitchManager switchManager) {
195         Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector,
196                 Name.NamePropName));
197         String nodeConnectorName = (ncName != null) ? ncName.getValue() : nodeConnector.getNodeConnectorIdAsString();
198         nodeConnectorName = nodeConnectorName + "@"
199                 + getNodeDesc(nodeConnector.getNode(), switchManager);
200         return nodeConnectorName.substring(0, nodeConnectorName.length());
201     }
202 }