REST API to fetch Node Property
[controller.git] / opendaylight / northbound / switchmanager / src / main / java / org / opendaylight / controller / switchmanager / northbound / SwitchNorthbound.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.switchmanager.northbound;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17
18 import javax.ws.rs.Consumes;
19 import javax.ws.rs.DELETE;
20 import javax.ws.rs.GET;
21 import javax.ws.rs.POST;
22 import javax.ws.rs.PUT;
23 import javax.ws.rs.Path;
24 import javax.ws.rs.PathParam;
25 import javax.ws.rs.Produces;
26 import javax.ws.rs.core.Context;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.SecurityContext;
30 import javax.ws.rs.core.UriInfo;
31
32 import org.codehaus.enunciate.jaxrs.ResponseCode;
33 import org.codehaus.enunciate.jaxrs.StatusCodes;
34 import org.codehaus.enunciate.jaxrs.TypeHint;
35 import org.opendaylight.controller.containermanager.IContainerManager;
36 import org.opendaylight.controller.northbound.commons.RestMessages;
37 import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
38 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
39 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
40 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
41 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
42 import org.opendaylight.controller.sal.authorization.Privilege;
43 import org.opendaylight.controller.sal.core.Node;
44 import org.opendaylight.controller.sal.core.NodeConnector;
45 import org.opendaylight.controller.sal.core.Property;
46 import org.opendaylight.controller.sal.utils.GlobalConstants;
47 import org.opendaylight.controller.sal.utils.ServiceHelper;
48 import org.opendaylight.controller.sal.utils.Status;
49 import org.opendaylight.controller.sal.utils.StatusCode;
50 import org.opendaylight.controller.switchmanager.ISwitchManager;
51 import org.opendaylight.controller.switchmanager.SwitchConfig;
52
53 /**
54  * The class provides Northbound REST APIs to access the nodes, node connectors
55  * and their properties.
56  *
57  */
58
59 @Path("/")
60 public class SwitchNorthbound {
61
62     private String username;
63
64     @Context
65     public void setSecurityContext(SecurityContext context) {
66         if (context != null && context.getUserPrincipal() != null) {
67             username = context.getUserPrincipal().getName();
68         }
69     }
70
71     protected String getUserName() {
72         return username;
73     }
74
75     private ISwitchManager getIfSwitchManagerService(String containerName) {
76         IContainerManager containerManager = (IContainerManager) ServiceHelper.getGlobalInstance(
77                 IContainerManager.class, this);
78         if (containerManager == null) {
79             throw new ServiceUnavailableException("Container " + RestMessages.SERVICEUNAVAILABLE.toString());
80         }
81
82         boolean found = false;
83         List<String> containerNames = containerManager.getContainerNames();
84         for (String cName : containerNames) {
85             if (cName.trim().equalsIgnoreCase(containerName.trim())) {
86                 found = true;
87                 break;
88             }
89         }
90
91         if (found == false) {
92             throw new ResourceNotFoundException(containerName + " " + RestMessages.NOCONTAINER.toString());
93         }
94
95         ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
96                 this);
97
98         if (switchManager == null) {
99             throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
100         }
101
102         return switchManager;
103     }
104
105     /**
106      *
107      * Retrieve a list of all the nodes and their properties in the network
108      *
109      * @param containerName
110      *            Name of the Container (Eg. 'default')
111      * @return A list of Pair each pair represents a
112      *         {@link org.opendaylight.controller.sal.core.Node} and Set of
113      *         {@link org.opendaylight.controller.sal.core.Property} attached to
114      *         it.
115      *
116      *         <pre>
117      *
118      * Example:
119      *
120      * Request URL:
121      * http://localhost:8080/controller/nb/v2/switchmanager/default/nodes
122      *
123      * Response body in XML:
124      * &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
125      * &lt;list&gt;
126      *     &#x20;&#x20;&#x20;&lt;nodeProperties&gt;
127      *         &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;node&gt;
128      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;id&gt;00:00:00:00:00:00:00:02&lt;/id&gt;
129      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;type&gt;OF&lt;/type&gt;
130      *         &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/node&gt;
131      *         &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;properties&gt;
132      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;tables&gt;
133      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;value&gt;-1&lt;/value&gt;
134      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/tables&gt;
135      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;description&gt;
136      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;value&gt;Switch2&lt;/value&gt;
137      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/description&gt;
138      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;actions&gt;
139      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;value&gt;4095&lt;/value&gt;
140      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/actions&gt;
141      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;macAddress&gt;
142      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;value&gt;00:00:00:00:00:02&lt;/value&gt;
143      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/macAddress&gt;
144      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;capabilities&gt;
145      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;value&gt;199&lt;/value&gt;
146      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/capabilities&gt;
147      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;timeStamp&gt;
148      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;value&gt;1377291227877&lt;/value&gt;
149      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;name&gt;connectedSince&lt;/name&gt;
150      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/timeStamp&gt;
151      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;buffers&gt;
152      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;value&gt;256&lt;/value&gt;
153      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/buffers&gt;
154      *         &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/properties&gt;
155      *     &#x20;&#x20;&#x20;&lt;/nodeProperties&gt;
156      * &lt;/list&gt;
157      *
158      * Response body in JSON:
159      * {
160      *    "nodeProperties":[
161      *       {
162      *          "node":{
163      *             "id":"00:00:00:00:00:00:00:02",
164      *             "type":"OF"
165      *          },
166      *          "properties":{
167      *             "tables":{
168      *                "value":"-1"
169      *             },
170      *             "description":{
171      *                "value":"None"
172      *             },
173      *             "actions":{
174      *                "value":"4095"
175      *             },
176      *             "macAddress":{
177      *                "value":"00:00:00:00:00:02"
178      *             },
179      *             "capabilities":{
180      *                "value":"199"
181      *             },
182      *             "timeStamp":{
183      *                "value":"1377291227877",
184      *                "name":"connectedSince"
185      *             },
186      *             "buffers":{
187      *                "value":"256"
188      *             }
189      *          }
190      *       }
191      *    ]
192      * }
193      *
194      * </pre>
195      */
196     @Path("/{containerName}/nodes")
197     @GET
198     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
199     @TypeHint(Nodes.class)
200     @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
201         @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
202         @ResponseCode(code = 404, condition = "The containerName is not found"),
203         @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
204     public Nodes getNodes(@PathParam("containerName") String containerName) {
205
206         if (!isValidContainer(containerName)) {
207             throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
208         }
209
210         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
211             throw new UnauthorizedException("User is not authorized to perform this operation on container "
212                     + containerName);
213         }
214
215         ISwitchManager switchManager = getIfSwitchManagerService(containerName);
216         if (switchManager == null) {
217             throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
218         }
219
220         List<NodeProperties> res = new ArrayList<NodeProperties>();
221         Set<Node> nodes = switchManager.getNodes();
222         if (nodes == null) {
223             return new Nodes(res);
224         }
225
226         for (Node node : nodes) {
227             Map<String, Property> propMap = switchManager.getNodeProps(node);
228             if (propMap == null) {
229                 continue;
230             }
231             Set<Property> props = new HashSet<Property>(propMap.values());
232
233             NodeProperties nodeProps = new NodeProperties(node, props);
234             res.add(nodeProps);
235         }
236
237         return new Nodes(res);
238     }
239
240     /**
241      * Add a Description, Tier and Forwarding mode property to a node. This
242      * method returns a non-successful response if a node by that name already
243      * exists.
244      *
245      * @param containerName
246      *            Name of the Container (Eg. 'default')
247      * @param nodeType
248      *            Type of the node being programmed (Eg. 'OF')
249      * @param nodeId
250      *            Node Identifier as specified by
251      *            {@link org.opendaylight.controller.sal.core.Node} (Eg.
252      *            '00:00:00:00:00:00:00:03')
253      * @param propertyName
254      *            Name of the Property. Properties that can be configured are:
255      *            description, forwarding(only for default container) and tier
256      * @param propertyValue
257      *            Value of the Property. Description can be any string (Eg.
258      *            'Node1'), valid values for tier are non negative numbers, and
259      *            valid values for forwarding are 0 for reactive and 1 for
260      *            proactive forwarding.
261      * @return Response as dictated by the HTTP Response Status code
262      *
263      *         <pre>
264      *
265      * Example:
266      *
267      * Request URL:
268      * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:03/property/description/Switch3
269      *
270      * </pre>
271      */
272
273     @Path("/{containerName}/node/{nodeType}/{nodeId}/property/{propertyName}/{propertyValue}")
274     @PUT
275     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
276     @TypeHint(Response.class)
277     @StatusCodes({
278         @ResponseCode(code = 201, condition = "Operation successful"),
279         @ResponseCode(code = 400, condition = "The nodeId or configuration is invalid"),
280         @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
281         @ResponseCode(code = 404, condition = "The Container Name or node or configuration name is not found"),
282         @ResponseCode(code = 406, condition = "The property cannot be configured in non-default container"),
283         @ResponseCode(code = 409, condition = "Unable to update configuration due to cluster conflict or conflicting description property"),
284         @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
285     public Response addNodeProperty(@Context UriInfo uriInfo, @PathParam("containerName") String containerName,
286             @PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
287             @PathParam("propertyName") String propertyName, @PathParam("propertyValue") String propertyValue) {
288
289         if (!isValidContainer(containerName)) {
290             throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
291         }
292         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
293             throw new UnauthorizedException("User is not authorized to perform this operation on container "
294                     + containerName);
295         }
296         ISwitchManager switchManager = getIfSwitchManagerService(containerName);
297         if (switchManager == null) {
298             throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
299         }
300
301         handleNodeAvailability(containerName, nodeType, nodeId);
302         Node node = Node.fromString(nodeType, nodeId);
303         Property prop = switchManager.createProperty(propertyName, propertyValue);
304         if (prop == null) {
305             throw new ResourceNotFoundException("Property with name " + propertyName + " does not exist.");
306         }
307         SwitchConfig switchConfig = switchManager.getSwitchConfig(node.toString());
308         Map<String, Property> nodeProperties = (switchConfig == null) ? new HashMap<String, Property>()
309                 : new HashMap<String, Property>(switchConfig.getNodeProperties());
310         nodeProperties.put(prop.getName(), prop);
311         SwitchConfig newSwitchConfig = new SwitchConfig(node.toString(), nodeProperties);
312         Status status = switchManager.updateNodeConfig(newSwitchConfig);
313         if (status.isSuccess()) {
314             NorthboundUtils.auditlog("Property " + propertyName, username, "updated",
315                     "of Node " + NorthboundUtils.getNodeDesc(node, switchManager), containerName);
316
317             return Response.created(uriInfo.getRequestUri()).build();
318         }
319         return NorthboundUtils.getResponse(status);
320     }
321
322     /**
323      * Delete a property of a node
324      *
325      * @param containerName
326      *            Name of the Container (Eg. 'SliceRed')
327      * @param nodeType
328      *            Type of the node being programmed (Eg. 'OF')
329      * @param nodeId
330      *            Node Identifier as specified by
331      *            {@link org.opendaylight.controller.sal.core.Node} (Eg.
332      *            '00:00:00:00:00:03:01:02')
333      * @param propertyName
334      *            Name of the Property. Properties that can be deleted are
335      *            description, forwarding(only in default container) and tier.
336      * @return Response as dictated by the HTTP Response Status code
337      *
338      *         <pre>
339      *
340      * Example:
341      *
342      * Request URL:
343      * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:03/property/forwarding
344      *
345      * </pre>
346      */
347
348     @Path("/{containerName}/node/{nodeType}/{nodeId}/property/{propertyName}")
349     @DELETE
350     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
351     @StatusCodes({ @ResponseCode(code = 204, condition = "Property removed successfully"),
352         @ResponseCode(code = 400, condition = "The nodeId or configuration is invalid"),
353         @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
354         @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
355         @ResponseCode(code = 409, condition = "Unable to delete property due to cluster conflict"),
356         @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
357     public Response deleteNodeProperty(@PathParam("containerName") String containerName,
358             @PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
359             @PathParam("propertyName") String propertyName) {
360
361         if (!isValidContainer(containerName)) {
362             throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
363         }
364         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
365             throw new UnauthorizedException("User is not authorized to perform this operation on container "
366                     + containerName);
367         }
368         ISwitchManager switchManager = getIfSwitchManagerService(containerName);
369         if (switchManager == null) {
370             throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
371         }
372
373         handleNodeAvailability(containerName, nodeType, nodeId);
374         Node node = Node.fromString(nodeType, nodeId);
375
376         SwitchConfig switchConfig = switchManager.getSwitchConfig(node.toString());
377         Status status;
378         if (switchConfig == null) {
379             status = new Status(StatusCode.NOTFOUND, "Switch Configuration does not exist");
380         } else {
381             Map<String, Property> nodeProperties = new HashMap<String, Property>(switchConfig.getNodeProperties());
382             if (!nodeProperties.containsKey(propertyName.toLowerCase())) {
383                 String msg = "Property " + propertyName + " does not exist or not configured for switch " + nodeId;
384                 status = new Status(StatusCode.NOTFOUND, msg);
385             } else {
386                 nodeProperties.remove(propertyName.toLowerCase());
387                 SwitchConfig newSwitchConfig = new SwitchConfig(node.toString(), nodeProperties);
388                 status = switchManager.updateNodeConfig(newSwitchConfig);
389                 if (status.isSuccess()) {
390                     NorthboundUtils.auditlog("Property " + propertyName, username, "removed", "of Node "
391                             + NorthboundUtils.getNodeDesc(node, switchManager), containerName);
392                     return Response.noContent().build();
393                 }
394             }
395         }
396         return NorthboundUtils.getResponse(status);
397     }
398
399     /**
400      * Get a property of a node
401      *
402      * @param containerName
403      *            Name of the Container (Eg. 'SliceRed')
404      * @param nodeType
405      *            Type of the node being programmed (Eg. 'OF')
406      * @param nodeId
407      *            Node Identifier as specified by
408      *            {@link org.opendaylight.controller.sal.core.Node} (Eg.
409      *            '00:00:00:00:00:03:01:02')
410      * @param propertyName
411      *            Name of the Property. Properties that can be deleted are
412      *            description, forwarding(only in default container) and tier.
413      * @return Property value of the property
414      *
415      *         <pre>
416      *
417      * Example:
418      *
419      * Request URL:
420      * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:01/property/description
421      *
422      * Response body in XML
423      * &lt;description&gt;
424      *     &#x20;&#x20;&lt;value&gt;switch1&lt;/value&gt;
425      * &lt;/description&gt;
426      *
427      * Response body in JSON
428      * {
429      *     &#x20;&#x20;"value": "switch1"
430      * }
431      * </pre>
432      */
433
434     @Path("/{containerName}/node/{nodeType}/{nodeId}/property/{propertyName}")
435     @GET
436     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
437     @TypeHint(String.class)
438     @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
439         @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
440         @ResponseCode(code = 404, condition = "The containerName is not found"),
441         @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
442     public Property getNodeProperty(@PathParam("containerName") String containerName,
443             @PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
444             @PathParam("propertyName") String propertyName) {
445
446         if (!isValidContainer(containerName)) {
447             throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
448         }
449         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
450             throw new UnauthorizedException("User is not authorized to perform this operation on container "
451                     + containerName);
452         }
453         ISwitchManager switchManager = getIfSwitchManagerService(containerName);
454         if (switchManager == null) {
455             throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
456         }
457
458         handleNodeAvailability(containerName, nodeType, nodeId);
459         Node node = Node.fromString(nodeType, nodeId);
460         if (node == null) {
461             throw new ResourceNotFoundException(nodeId + " : " + RestMessages.NONODE.toString());
462         }
463         SwitchConfig switchConfig = switchManager.getSwitchConfig(node.toString());
464         if (switchConfig == null) {
465             throw new ResourceNotFoundException(nodeId + " : " + "Config Not Found" );
466         } else {
467             Map<String, Property> nodeProperties = new HashMap<String, Property>(switchConfig.getNodeProperties());
468             if (!nodeProperties.containsKey(propertyName.toLowerCase())) {
469                 String msg = "Property " + propertyName + " does not exist or not "
470                         + "configured for switch " + nodeId;
471                 throw new ResourceNotFoundException(msg);
472             } else {
473                 return nodeProperties.get(propertyName.toLowerCase());
474             }
475         }
476     }
477
478     /**
479      *
480      * Retrieve a list of all the nodeconnectors and their properties in a given
481      * node
482      *
483      * @param containerName
484      *            The container for which we want to retrieve the list (Eg.
485      *            'default')
486      * @param nodeType
487      *            Type of the node being programmed (Eg. 'OF')
488      * @param nodeId
489      *            Node Identifier as specified by
490      *            {@link org.opendaylight.controller.sal.core.Node} (Eg.
491      *            '00:00:00:00:00:00:00:03')
492      * @return A List of Pair each pair represents a
493      *         {@link org.opendaylight.controller.sal.core.NodeConnector} and
494      *         its corresponding
495      *         {@link org.opendaylight.controller.sal.core.Property} attached to
496      *         it.
497      *
498      * <pre>
499      *
500      * Example:
501      *
502      * Request URL:
503      * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:01
504      *
505      * Response body in XML:
506      * &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
507      * &lt;list&gt;
508      *     &#x20;&#x20;&#x20;&lt;nodeConnectorProperties&gt;
509      *         &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;nodeconnector&gt;
510      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;node&gt;
511      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;id&gt;00:00:00:00:00:00:00:01&lt;/id&gt;
512      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;type&gt;OF&lt;/type&gt;
513      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/node&gt;
514      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;id&gt;2&lt;/id&gt;
515      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;type&gt;OF&lt;/type&gt;
516      *         &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/nodeconnector&gt;
517      *         &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;properties&gt;
518      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;state&gt;
519      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;value&gt;1&lt;/value&gt;
520      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/state&gt;
521      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;config&gt;
522      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;value&gt;1&lt;/value&gt;
523      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/config&gt;
524      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;name&gt;
525      *                 &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;value&gt;L1_2-C2_1&lt;/value&gt;
526      *             &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/name&gt;
527      *         &#x20;&#x20;&#x20;&#x20;&#x20;&#x20;&lt;/properties&gt;
528      *     &#x20;&#x20;&#x20;&lt;/nodeConnectorProperties&gt;
529      * &lt;/list&gt;
530      *
531      * Response body in JSON:
532      * {
533      *    "nodeConnectorProperties":[
534      *       {
535      *          "nodeconnector":{
536      *             "node":{
537      *                "id":"00:00:00:00:00:00:00:01",
538      *                "type":"OF"
539      *             },
540      *             "id":"2",
541      *             "type":"OF"
542      *          },
543      *          "properties":{
544      *             "state":{
545      *                "value":"1"
546      *             },
547      *             "config":{
548      *                "value":"1"
549      *             },
550      *             "name":{
551      *                "value":"L1_2-C2_1"
552      *             }
553      *          }
554      *       }
555      *    ]
556      * }
557      *
558      * </pre>
559      */
560     @Path("/{containerName}/node/{nodeType}/{nodeId}")
561     @GET
562     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
563     @TypeHint(NodeConnectors.class)
564     @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
565         @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
566         @ResponseCode(code = 404, condition = "The containerName is not found"),
567         @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
568     public NodeConnectors getNodeConnectors(@PathParam("containerName") String containerName,
569             @PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId) {
570
571         if (!isValidContainer(containerName)) {
572             throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
573         }
574         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
575             throw new UnauthorizedException("User is not authorized to perform this operation on container "
576                     + containerName);
577         }
578
579         ISwitchManager switchManager = getIfSwitchManagerService(containerName);
580         if (switchManager == null) {
581             throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
582         }
583
584         handleNodeAvailability(containerName, nodeType, nodeId);
585         Node node = Node.fromString(nodeType, nodeId);
586         List<NodeConnectorProperties> res = new ArrayList<NodeConnectorProperties>();
587         Set<NodeConnector> ncs = switchManager.getNodeConnectors(node);
588         if (ncs == null) {
589             return null;
590         }
591
592         for (NodeConnector nc : ncs) {
593             Map<String, Property> propMap = switchManager.getNodeConnectorProps(nc);
594             if (propMap == null) {
595                 continue;
596             }
597             Set<Property> props = new HashSet<Property>(propMap.values());
598             NodeConnectorProperties ncProps = new NodeConnectorProperties(nc, props);
599             res.add(ncProps);
600         }
601
602         return new NodeConnectors(res);
603     }
604
605     /**
606      * Add node-connector property to a node connector. This method returns a
607      * non-successful response if a node connector by the given name already
608      * exists.
609      *
610      * @param containerName
611      *            Name of the Container (Eg. 'default')
612      * @param nodeType
613      *            Type of the node being programmed (Eg. 'OF')
614      * @param nodeId
615      *            Node Identifier as specified by
616      *            {@link org.opendaylight.controller.sal.core.Node} (Eg.
617      *            '00:00:00:00:00:00:00:03')
618      * @param nodeConnectorType
619      *            Type of the node connector being programmed (Eg. 'OF')
620      * @param nodeConnectorId
621      *            NodeConnector Identifier as specified by
622      *            {@link org.opendaylight.controller.sal.core.NodeConnector}.
623      *            (Eg. '2'). If nodeConnecterId contains forward slash(/),
624      *            replace forward slash with underscore(_) in the URL. (Eg. for
625      *            Ethernet1/2, use Ethernet1_2)
626      * @param propertyName
627      *            Name of the Property specified by
628      *            {@link org.opendaylight.controller.sal.core.Property} and its
629      *            extended classes Property that can be configured is bandwidth
630      * @param propertyValue
631      *            Value of the Property specified by
632      *            {@link org.opendaylight.controller.sal.core.Property} and its
633      *            extended classes
634      * @return Response as dictated by the HTTP Response Status code
635      *
636      * <pre>
637      *
638      * Example:
639      *
640      * Request URL:
641      * http://localhost:8080/controller/nb/v2/switchmanager/default/nodeconnector/OF/00:00:00:00:00:00:00:01/OF/2/property/bandwidth/1
642      *
643      * </pre>
644      */
645
646     @Path("/{containerName}/nodeconnector/{nodeType}/{nodeId}/{nodeConnectorType}/{nodeConnectorId}/property/{propertyName}/{propertyValue}")
647     @PUT
648     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
649     @StatusCodes({ @ResponseCode(code = 201, condition = "Operation successful"),
650         @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
651         @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
652         @ResponseCode(code = 409, condition = "Unable to add property due to cluster conflict"),
653         @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
654     public Response addNodeConnectorProperty(@Context UriInfo uriInfo,
655             @PathParam("containerName") String containerName, @PathParam("nodeType") String nodeType,
656             @PathParam("nodeId") String nodeId, @PathParam("nodeConnectorType") String nodeConnectorType,
657             @PathParam("nodeConnectorId") String nodeConnectorId, @PathParam("propertyName") String propertyName,
658             @PathParam("propertyValue") String propertyValue) {
659
660         if (!isValidContainer(containerName)) {
661             throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
662         }
663         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
664             throw new UnauthorizedException("User is not authorized to perform this operation on container "
665                     + containerName);
666         }
667
668         ISwitchManager switchManager = getIfSwitchManagerService(containerName);
669         if (switchManager == null) {
670             throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
671         }
672
673         handleNodeAvailability(containerName, nodeType, nodeId);
674         Node node = Node.fromString(nodeType, nodeId);
675
676         if (nodeConnectorId.contains("_")) {
677             nodeConnectorId = nodeConnectorId.replace("_", "/");
678         }
679
680         handleNodeConnectorAvailability(containerName, node, nodeConnectorType, nodeConnectorId);
681         NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType, nodeConnectorId, node);
682
683         Property prop = switchManager.createProperty(propertyName, propertyValue);
684         if (prop == null) {
685             throw new ResourceNotFoundException(RestMessages.INVALIDDATA.toString());
686         }
687
688         Status ret = switchManager.addNodeConnectorProp(nc, prop);
689         if (ret.isSuccess()) {
690             NorthboundUtils.auditlog("Property " + propertyName, username, "updated", "of Node Connector "
691                     + NorthboundUtils.getPortName(nc, switchManager), containerName);
692             return Response.created(uriInfo.getRequestUri()).build();
693         }
694         throw new InternalServerErrorException(ret.getDescription());
695     }
696
697     /**
698      * Delete a property of a node connector
699      *
700      * @param containerName
701      *            Name of the Container (Eg. 'default')
702      * @param nodeType
703      *            Type of the node being programmed (Eg. 'OF')
704      * @param nodeId
705      *            Node Identifier as specified by
706      *            {@link org.opendaylight.controller.sal.core.Node} (Eg.
707      *            '00:00:00:00:00:00:00:01')
708      * @param nodeConnectorType
709      *            Type of the node connector being programmed (Eg. 'OF')
710      * @param nodeConnectorId
711      *            NodeConnector Identifier as specified by
712      *            {@link org.opendaylight.controller.sal.core.NodeConnector}
713      *            (Eg. '1'). If nodeConnecterId contains forward slash(/),
714      *            replace forward slash with underscore(_) in the URL. (Eg. for
715      *            Ethernet1/2, use Ethernet1_2)
716      * @param propertyName
717      *            Name of the Property specified by
718      *            {@link org.opendaylight.controller.sal.core.Property} and its
719      *            extended classes. Property that can be deleted is bandwidth
720      * @return Response as dictated by the HTTP Response Status code
721      *
722      * <pre>
723      *
724      * Example:
725      *
726      * Request URL:
727      * http://localhost:8080/controller/nb/v2/switchmanager/default/nodeconnector/OF/00:00:00:00:00:00:00:01/OF/2/property/bandwidth
728      *
729      * </pre>
730      */
731
732     @Path("/{containerName}/nodeconnector/{nodeType}/{nodeId}/{nodeConnectorType}/{nodeConnectorId}/property/{propertyName}")
733     @DELETE
734     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
735     @StatusCodes({ @ResponseCode(code = 204, condition = "Property removed successfully"),
736         @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
737         @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
738         @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
739     public Response deleteNodeConnectorProperty(@PathParam("containerName") String containerName,
740             @PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
741             @PathParam("nodeConnectorType") String nodeConnectorType,
742             @PathParam("nodeConnectorId") String nodeConnectorId, @PathParam("propertyName") String propertyName) {
743
744         if (!isValidContainer(containerName)) {
745             throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
746         }
747         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
748             throw new UnauthorizedException("User is not authorized to perform this operation on container "
749                     + containerName);
750         }
751
752         ISwitchManager switchManager = getIfSwitchManagerService(containerName);
753         if (switchManager == null) {
754             throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
755         }
756
757         handleNodeAvailability(containerName, nodeType, nodeId);
758         Node node = Node.fromString(nodeType, nodeId);
759
760         if (nodeConnectorId.contains("_")) {
761             nodeConnectorId = nodeConnectorId.replace("_", "/");
762         }
763
764         handleNodeConnectorAvailability(containerName, node, nodeConnectorType, nodeConnectorId);
765         NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType, nodeConnectorId, node);
766         Status ret = switchManager.removeNodeConnectorProp(nc, propertyName);
767         if (ret.isSuccess()) {
768             NorthboundUtils.auditlog("Property " + propertyName, username, "removed", "of Node Connector "
769                     + NorthboundUtils.getPortName(nc, switchManager), containerName);
770             return Response.noContent().build();
771         }
772         throw new ResourceNotFoundException(ret.getDescription());
773     }
774
775     /**
776      * Save the current switch configurations
777      *
778      * @param containerName
779      *            Name of the Container (Eg. 'default')
780      * @return Response as dictated by the HTTP Response Status code
781      *
782      *         <pre>
783      *
784      * Example:
785      *
786      * Request URL:
787      * http://localhost:8080/controller/nb/v2/switchmanager/default/save
788      *
789      * </pre>
790      */
791     @Path("/{containerName}/save")
792     @POST
793     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
794     @StatusCodes({
795         @ResponseCode(code = 200, condition = "Operation successful"),
796         @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
797         @ResponseCode(code = 404, condition = "The containerName is not found"),
798         @ResponseCode(code = 500, condition = "Failed to save switch configuration. Failure Reason included in HTTP Error response"),
799         @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
800     public Response saveSwitchConfig(@PathParam("containerName") String containerName) {
801
802         if (!isValidContainer(containerName)) {
803             throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
804         }
805         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
806             throw new UnauthorizedException("User is not authorized to perform this operation on container "
807                     + containerName);
808         }
809         ISwitchManager switchManager = getIfSwitchManagerService(containerName);
810         if (switchManager == null) {
811             throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
812         }
813
814         Status ret = switchManager.saveSwitchConfig();
815         if (ret.isSuccess()) {
816             return Response.ok().build();
817         }
818         throw new InternalServerErrorException(ret.getDescription());
819     }
820
821     private Node handleNodeAvailability(String containerName, String nodeType, String nodeId) {
822
823         Node node = Node.fromString(nodeType, nodeId);
824         if (node == null) {
825             throw new ResourceNotFoundException(nodeId + " : " + RestMessages.NONODE.toString());
826         }
827
828         ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName, this);
829
830         if (sm == null) {
831             throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
832         }
833
834         if (!sm.getNodes().contains(node)) {
835             throw new ResourceNotFoundException(node.toString() + " : " + RestMessages.NONODE.toString());
836         }
837         return node;
838     }
839
840     private void handleNodeConnectorAvailability(String containerName, Node node, String nodeConnectorType,
841             String nodeConnectorId) {
842
843         NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType, nodeConnectorId, node);
844         if (nc == null) {
845             throw new ResourceNotFoundException(nc + " : " + RestMessages.NORESOURCE.toString());
846         }
847
848         ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName, this);
849
850         if (sm == null) {
851             throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
852         }
853
854         if (!sm.getNodeConnectors(node).contains(nc)) {
855             throw new ResourceNotFoundException(nc.toString() + " : " + RestMessages.NORESOURCE.toString());
856         }
857     }
858
859     private boolean isValidContainer(String containerName) {
860         if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
861             return true;
862         }
863         IContainerManager containerManager = (IContainerManager) ServiceHelper.getGlobalInstance(
864                 IContainerManager.class, this);
865         if (containerManager == null) {
866             throw new InternalServerErrorException(RestMessages.INTERNALERROR.toString());
867         }
868         if (containerManager.getContainerNames().contains(containerName)) {
869             return true;
870         }
871         return false;
872     }
873
874 }