2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.switchmanager.northbound;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
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;
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;
54 * The class provides Northbound REST APIs to access the nodes, node connectors
55 * and their properties.
60 public class SwitchNorthbound {
62 private String username;
65 public void setSecurityContext(SecurityContext context) {
66 if (context != null && context.getUserPrincipal() != null) {
67 username = context.getUserPrincipal().getName();
71 protected String getUserName() {
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());
82 boolean found = false;
83 List<String> containerNames = containerManager.getContainerNames();
84 for (String cName : containerNames) {
85 if (cName.trim().equalsIgnoreCase(containerName.trim())) {
92 throw new ResourceNotFoundException(containerName + " " + RestMessages.NOCONTAINER.toString());
95 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
98 if (switchManager == null) {
99 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
102 return switchManager;
107 * Retrieve a list of all the nodes and their properties in the network
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
121 * http://localhost:8080/controller/nb/v2/switchmanager/default/nodes
123 * Response body in XML:
124 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
126 *    <nodeProperties>
127 *       <node>
128 *          <id>00:00:00:00:00:00:00:02</id>
129 *          <type>OF</type>
130 *       </node>
131 *       <properties>
132 *          <tables>
133 *             <value>-1</value>
134 *          </tables>
135 *          <description>
136 *             <value>Switch2</value>
137 *          </description>
138 *          <actions>
139 *             <value>4095</value>
140 *          </actions>
141 *          <macAddress>
142 *             <value>00:00:00:00:00:02</value>
143 *          </macAddress>
144 *          <capabilities>
145 *             <value>199</value>
146 *          </capabilities>
147 *          <timeStamp>
148 *             <value>1377291227877</value>
149 *             <name>connectedSince</name>
150 *          </timeStamp>
151 *          <buffers>
152 *             <value>256</value>
153 *          </buffers>
154 *       </properties>
155 *    </nodeProperties>
158 * Response body in JSON:
163 * "id":"00:00:00:00:00:00:00:02",
177 * "value":"00:00:00:00:00:02"
183 * "value":"1377291227877",
184 * "name":"connectedSince"
196 @Path("/{containerName}/nodes")
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) {
206 if (!isValidContainer(containerName)) {
207 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
210 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
211 throw new UnauthorizedException("User is not authorized to perform this operation on container "
215 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
216 if (switchManager == null) {
217 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
220 List<NodeProperties> res = new ArrayList<NodeProperties>();
221 Set<Node> nodes = switchManager.getNodes();
223 return new Nodes(res);
226 for (Node node : nodes) {
227 Map<String, Property> propMap = switchManager.getNodeProps(node);
228 if (propMap == null) {
231 Set<Property> props = new HashSet<Property>(propMap.values());
233 NodeProperties nodeProps = new NodeProperties(node, props);
237 return new Nodes(res);
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
245 * @param containerName
246 * Name of the Container (Eg. 'default')
248 * Type of the node being programmed (Eg. 'OF')
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
268 * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:03/property/description/Switch3
273 @Path("/{containerName}/node/{nodeType}/{nodeId}/property/{propertyName}/{propertyValue}")
275 @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
276 @TypeHint(Response.class)
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) {
289 if (!isValidContainer(containerName)) {
290 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
292 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
293 throw new UnauthorizedException("User is not authorized to perform this operation on container "
296 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
297 if (switchManager == null) {
298 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
301 handleNodeAvailability(containerName, nodeType, nodeId);
302 Node node = Node.fromString(nodeType, nodeId);
303 Property prop = switchManager.createProperty(propertyName, propertyValue);
305 throw new ResourceNotFoundException("Property with name " + propertyName + " does not exist.");
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);
317 return Response.created(uriInfo.getRequestUri()).build();
319 return NorthboundUtils.getResponse(status);
323 * Delete a property of a node
325 * @param containerName
326 * Name of the Container (Eg. 'SliceRed')
328 * Type of the node being programmed (Eg. 'OF')
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
343 * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:03/property/forwarding
348 @Path("/{containerName}/node/{nodeType}/{nodeId}/property/{propertyName}")
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) {
361 if (!isValidContainer(containerName)) {
362 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
364 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
365 throw new UnauthorizedException("User is not authorized to perform this operation on container "
368 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
369 if (switchManager == null) {
370 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
373 handleNodeAvailability(containerName, nodeType, nodeId);
374 Node node = Node.fromString(nodeType, nodeId);
376 SwitchConfig switchConfig = switchManager.getSwitchConfig(node.toString());
378 if (switchConfig == null) {
379 status = new Status(StatusCode.NOTFOUND, "Switch Configuration does not exist");
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);
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();
396 return NorthboundUtils.getResponse(status);
400 * Get a property of a node
402 * @param containerName
403 * Name of the Container (Eg. 'SliceRed')
405 * Type of the node being programmed (Eg. 'OF')
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
420 * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:01/property/description
422 * Response body in XML
423 * <description>
424 *   <value>switch1</value>
425 * </description>
427 * Response body in JSON
429 *   "value": "switch1"
434 @Path("/{containerName}/node/{nodeType}/{nodeId}/property/{propertyName}")
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) {
446 if (!isValidContainer(containerName)) {
447 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
449 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
450 throw new UnauthorizedException("User is not authorized to perform this operation on container "
453 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
454 if (switchManager == null) {
455 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
458 handleNodeAvailability(containerName, nodeType, nodeId);
459 Node node = Node.fromString(nodeType, nodeId);
461 throw new ResourceNotFoundException(nodeId + " : " + RestMessages.NONODE.toString());
463 SwitchConfig switchConfig = switchManager.getSwitchConfig(node.toString());
464 if (switchConfig == null) {
465 throw new ResourceNotFoundException(nodeId + " : " + "Config Not Found" );
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);
473 return nodeProperties.get(propertyName.toLowerCase());
480 * Retrieve a list of all the nodeconnectors and their properties in a given
483 * @param containerName
484 * The container for which we want to retrieve the list (Eg.
487 * Type of the node being programmed (Eg. 'OF')
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
495 * {@link org.opendaylight.controller.sal.core.Property} attached to
503 * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:01
505 * Response body in XML:
506 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
508 *    <nodeConnectorProperties>
509 *       <nodeconnector>
510 *          <node>
511 *             <id>00:00:00:00:00:00:00:01</id>
512 *             <type>OF</type>
513 *          </node>
514 *          <id>2</id>
515 *          <type>OF</type>
516 *       </nodeconnector>
517 *       <properties>
518 *          <state>
519 *             <value>1</value>
520 *          </state>
521 *          <config>
522 *             <value>1</value>
523 *          </config>
524 *          <name>
525 *             <value>L1_2-C2_1</value>
526 *          </name>
527 *       </properties>
528 *    </nodeConnectorProperties>
531 * Response body in JSON:
533 * "nodeConnectorProperties":[
537 * "id":"00:00:00:00:00:00:00:01",
551 * "value":"L1_2-C2_1"
560 @Path("/{containerName}/node/{nodeType}/{nodeId}")
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) {
571 if (!isValidContainer(containerName)) {
572 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
574 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
575 throw new UnauthorizedException("User is not authorized to perform this operation on container "
579 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
580 if (switchManager == null) {
581 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
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);
592 for (NodeConnector nc : ncs) {
593 Map<String, Property> propMap = switchManager.getNodeConnectorProps(nc);
594 if (propMap == null) {
597 Set<Property> props = new HashSet<Property>(propMap.values());
598 NodeConnectorProperties ncProps = new NodeConnectorProperties(nc, props);
602 return new NodeConnectors(res);
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
610 * @param containerName
611 * Name of the Container (Eg. 'default')
613 * Type of the node being programmed (Eg. 'OF')
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
634 * @return Response as dictated by the HTTP Response Status code
641 * http://localhost:8080/controller/nb/v2/switchmanager/default/nodeconnector/OF/00:00:00:00:00:00:00:01/OF/2/property/bandwidth/1
646 @Path("/{containerName}/nodeconnector/{nodeType}/{nodeId}/{nodeConnectorType}/{nodeConnectorId}/property/{propertyName}/{propertyValue}")
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) {
660 if (!isValidContainer(containerName)) {
661 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
663 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
664 throw new UnauthorizedException("User is not authorized to perform this operation on container "
668 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
669 if (switchManager == null) {
670 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
673 handleNodeAvailability(containerName, nodeType, nodeId);
674 Node node = Node.fromString(nodeType, nodeId);
676 if (nodeConnectorId.contains("_")) {
677 nodeConnectorId = nodeConnectorId.replace("_", "/");
680 handleNodeConnectorAvailability(containerName, node, nodeConnectorType, nodeConnectorId);
681 NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType, nodeConnectorId, node);
683 Property prop = switchManager.createProperty(propertyName, propertyValue);
685 throw new ResourceNotFoundException(RestMessages.INVALIDDATA.toString());
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();
694 throw new InternalServerErrorException(ret.getDescription());
698 * Delete a property of a node connector
700 * @param containerName
701 * Name of the Container (Eg. 'default')
703 * Type of the node being programmed (Eg. 'OF')
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
727 * http://localhost:8080/controller/nb/v2/switchmanager/default/nodeconnector/OF/00:00:00:00:00:00:00:01/OF/2/property/bandwidth
732 @Path("/{containerName}/nodeconnector/{nodeType}/{nodeId}/{nodeConnectorType}/{nodeConnectorId}/property/{propertyName}")
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) {
744 if (!isValidContainer(containerName)) {
745 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
747 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
748 throw new UnauthorizedException("User is not authorized to perform this operation on container "
752 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
753 if (switchManager == null) {
754 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
757 handleNodeAvailability(containerName, nodeType, nodeId);
758 Node node = Node.fromString(nodeType, nodeId);
760 if (nodeConnectorId.contains("_")) {
761 nodeConnectorId = nodeConnectorId.replace("_", "/");
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();
772 throw new ResourceNotFoundException(ret.getDescription());
776 * Save the current switch configurations
778 * @param containerName
779 * Name of the Container (Eg. 'default')
780 * @return Response as dictated by the HTTP Response Status code
787 * http://localhost:8080/controller/nb/v2/switchmanager/default/save
791 @Path("/{containerName}/save")
793 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
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) {
802 if (!isValidContainer(containerName)) {
803 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
805 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
806 throw new UnauthorizedException("User is not authorized to perform this operation on container "
809 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
810 if (switchManager == null) {
811 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
814 Status ret = switchManager.saveSwitchConfig();
815 if (ret.isSuccess()) {
816 return Response.ok().build();
818 throw new InternalServerErrorException(ret.getDescription());
821 private Node handleNodeAvailability(String containerName, String nodeType, String nodeId) {
823 Node node = Node.fromString(nodeType, nodeId);
825 throw new ResourceNotFoundException(nodeId + " : " + RestMessages.NONODE.toString());
828 ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName, this);
831 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
834 if (!sm.getNodes().contains(node)) {
835 throw new ResourceNotFoundException(node.toString() + " : " + RestMessages.NONODE.toString());
840 private void handleNodeConnectorAvailability(String containerName, Node node, String nodeConnectorType,
841 String nodeConnectorId) {
843 NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType, nodeConnectorId, node);
845 throw new ResourceNotFoundException(nc + " : " + RestMessages.NORESOURCE.toString());
848 ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName, this);
851 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
854 if (!sm.getNodeConnectors(node).contains(nc)) {
855 throw new ResourceNotFoundException(nc.toString() + " : " + RestMessages.NORESOURCE.toString());
859 private boolean isValidContainer(String containerName) {
860 if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
863 IContainerManager containerManager = (IContainerManager) ServiceHelper.getGlobalInstance(
864 IContainerManager.class, this);
865 if (containerManager == null) {
866 throw new InternalServerErrorException(RestMessages.INTERNALERROR.toString());
868 if (containerManager.getContainerNames().contains(containerName)) {