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);
401 * Retrieve a list of all the nodeconnectors and their properties in a given
404 * @param containerName
405 * The container for which we want to retrieve the list (Eg.
408 * Type of the node being programmed (Eg. 'OF')
410 * Node Identifier as specified by
411 * {@link org.opendaylight.controller.sal.core.Node} (Eg.
412 * '00:00:00:00:00:00:00:03')
413 * @return A List of Pair each pair represents a
414 * {@link org.opendaylight.controller.sal.core.NodeConnector} and
416 * {@link org.opendaylight.controller.sal.core.Property} attached to
424 * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:01
426 * Response body in XML:
427 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
429 *    <nodeConnectorProperties>
430 *       <nodeconnector>
431 *          <node>
432 *             <id>00:00:00:00:00:00:00:01</id>
433 *             <type>OF</type>
434 *          </node>
435 *          <id>2</id>
436 *          <type>OF</type>
437 *       </nodeconnector>
438 *       <properties>
439 *          <state>
440 *             <value>1</value>
441 *          </state>
442 *          <config>
443 *             <value>1</value>
444 *          </config>
445 *          <name>
446 *             <value>L1_2-C2_1</value>
447 *          </name>
448 *       </properties>
449 *    </nodeConnectorProperties>
452 * Response body in JSON:
454 * "nodeConnectorProperties":[
458 * "id":"00:00:00:00:00:00:00:01",
472 * "value":"L1_2-C2_1"
481 @Path("/{containerName}/node/{nodeType}/{nodeId}")
483 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
484 @TypeHint(NodeConnectors.class)
485 @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
486 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
487 @ResponseCode(code = 404, condition = "The containerName is not found"),
488 @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
489 public NodeConnectors getNodeConnectors(@PathParam("containerName") String containerName,
490 @PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId) {
492 if (!isValidContainer(containerName)) {
493 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
495 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
496 throw new UnauthorizedException("User is not authorized to perform this operation on container "
500 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
501 if (switchManager == null) {
502 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
505 handleNodeAvailability(containerName, nodeType, nodeId);
506 Node node = Node.fromString(nodeType, nodeId);
507 List<NodeConnectorProperties> res = new ArrayList<NodeConnectorProperties>();
508 Set<NodeConnector> ncs = switchManager.getNodeConnectors(node);
513 for (NodeConnector nc : ncs) {
514 Map<String, Property> propMap = switchManager.getNodeConnectorProps(nc);
515 if (propMap == null) {
518 Set<Property> props = new HashSet<Property>(propMap.values());
519 NodeConnectorProperties ncProps = new NodeConnectorProperties(nc, props);
523 return new NodeConnectors(res);
527 * Add node-connector property to a node connector. This method returns a
528 * non-successful response if a node connector by the given name already
531 * @param containerName
532 * Name of the Container (Eg. 'default')
534 * Type of the node being programmed (Eg. 'OF')
536 * Node Identifier as specified by
537 * {@link org.opendaylight.controller.sal.core.Node} (Eg.
538 * '00:00:00:00:00:00:00:03')
539 * @param nodeConnectorType
540 * Type of the node connector being programmed (Eg. 'OF')
541 * @param nodeConnectorId
542 * NodeConnector Identifier as specified by
543 * {@link org.opendaylight.controller.sal.core.NodeConnector}
545 * @param propertyName
546 * Name of the Property specified by
547 * {@link org.opendaylight.controller.sal.core.Property} and its
548 * extended classes Property that can be configured is bandwidth
549 * @param propertyValue
550 * Value of the Property specified by
551 * {@link org.opendaylight.controller.sal.core.Property} and its
553 * @return Response as dictated by the HTTP Response Status code
560 * http://localhost:8080/controller/nb/v2/switchmanager/default/nodeconnector/OF/00:00:00:00:00:00:00:01/OF/2/property/bandwidth/1
565 @Path("/{containerName}/nodeconnector/{nodeType}/{nodeId}/{nodeConnectorType}/{nodeConnectorId}/property/{propertyName}/{propertyValue}")
567 @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
568 @StatusCodes({ @ResponseCode(code = 201, condition = "Operation successful"),
569 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
570 @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
571 @ResponseCode(code = 409, condition = "Unable to add property due to cluster conflict"),
572 @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
573 public Response addNodeConnectorProperty(@Context UriInfo uriInfo,
574 @PathParam("containerName") String containerName, @PathParam("nodeType") String nodeType,
575 @PathParam("nodeId") String nodeId, @PathParam("nodeConnectorType") String nodeConnectorType,
576 @PathParam("nodeConnectorId") String nodeConnectorId, @PathParam("propertyName") String propertyName,
577 @PathParam("propertyValue") String propertyValue) {
579 if (!isValidContainer(containerName)) {
580 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
582 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
583 throw new UnauthorizedException("User is not authorized to perform this operation on container "
587 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
588 if (switchManager == null) {
589 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
592 handleNodeAvailability(containerName, nodeType, nodeId);
593 Node node = Node.fromString(nodeType, nodeId);
595 handleNodeConnectorAvailability(containerName, node, nodeConnectorType, nodeConnectorId);
596 NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType, nodeConnectorId, node);
598 Property prop = switchManager.createProperty(propertyName, propertyValue);
600 throw new ResourceNotFoundException(RestMessages.INVALIDDATA.toString());
603 Status ret = switchManager.addNodeConnectorProp(nc, prop);
604 if (ret.isSuccess()) {
605 NorthboundUtils.auditlog("Property " + propertyName, username, "updated", "of Node Connector "
606 + NorthboundUtils.getPortName(nc, switchManager), containerName);
607 return Response.created(uriInfo.getRequestUri()).build();
609 throw new InternalServerErrorException(ret.getDescription());
613 * Delete a property of a node connector
615 * @param containerName
616 * Name of the Container (Eg. 'default')
618 * Type of the node being programmed (Eg. 'OF')
620 * Node Identifier as specified by
621 * {@link org.opendaylight.controller.sal.core.Node} (Eg.
622 * '00:00:00:00:00:00:00:01')
623 * @param nodeConnectorType
624 * Type of the node connector being programmed (Eg. 'OF')
625 * @param nodeConnectorId
626 * NodeConnector Identifier as specified by
627 * {@link org.opendaylight.controller.sal.core.NodeConnector}
629 * @param propertyName
630 * Name of the Property specified by
631 * {@link org.opendaylight.controller.sal.core.Property} and its
632 * extended classes. Property that can be deleted is bandwidth
633 * @return Response as dictated by the HTTP Response Status code
640 * http://localhost:8080/controller/nb/v2/switchmanager/default/nodeconnector/OF/00:00:00:00:00:00:00:01/OF/2/property/bandwidth
645 @Path("/{containerName}/nodeconnector/{nodeType}/{nodeId}/{nodeConnectorType}/{nodeConnectorId}/property/{propertyName}")
647 @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
648 @StatusCodes({ @ResponseCode(code = 204, condition = "Property removed successfully"),
649 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
650 @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
651 @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
652 public Response deleteNodeConnectorProperty(@PathParam("containerName") String containerName,
653 @PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
654 @PathParam("nodeConnectorType") String nodeConnectorType,
655 @PathParam("nodeConnectorId") String nodeConnectorId, @PathParam("propertyName") String propertyName) {
657 if (!isValidContainer(containerName)) {
658 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
660 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
661 throw new UnauthorizedException("User is not authorized to perform this operation on container "
665 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
666 if (switchManager == null) {
667 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
670 handleNodeAvailability(containerName, nodeType, nodeId);
671 Node node = Node.fromString(nodeType, nodeId);
673 handleNodeConnectorAvailability(containerName, node, nodeConnectorType, nodeConnectorId);
674 NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType, nodeConnectorId, node);
675 Status ret = switchManager.removeNodeConnectorProp(nc, propertyName);
676 if (ret.isSuccess()) {
677 NorthboundUtils.auditlog("Property " + propertyName, username, "removed", "of Node Connector "
678 + NorthboundUtils.getPortName(nc, switchManager), containerName);
679 return Response.noContent().build();
681 throw new ResourceNotFoundException(ret.getDescription());
685 * Save the current switch configurations
687 * @param containerName
688 * Name of the Container (Eg. 'default')
689 * @return Response as dictated by the HTTP Response Status code
696 * http://localhost:8080/controller/nb/v2/switchmanager/default/save
700 @Path("/{containerName}/save")
702 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
704 @ResponseCode(code = 200, condition = "Operation successful"),
705 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
706 @ResponseCode(code = 404, condition = "The containerName is not found"),
707 @ResponseCode(code = 500, condition = "Failed to save switch configuration. Failure Reason included in HTTP Error response"),
708 @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
709 public Response saveSwitchConfig(@PathParam("containerName") String containerName) {
711 if (!isValidContainer(containerName)) {
712 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
714 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
715 throw new UnauthorizedException("User is not authorized to perform this operation on container "
718 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
719 if (switchManager == null) {
720 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
723 Status ret = switchManager.saveSwitchConfig();
724 if (ret.isSuccess()) {
725 return Response.ok().build();
727 throw new InternalServerErrorException(ret.getDescription());
730 private Node handleNodeAvailability(String containerName, String nodeType, String nodeId) {
732 Node node = Node.fromString(nodeType, nodeId);
734 throw new ResourceNotFoundException(nodeId + " : " + RestMessages.NONODE.toString());
737 ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName, this);
740 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
743 if (!sm.getNodes().contains(node)) {
744 throw new ResourceNotFoundException(node.toString() + " : " + RestMessages.NONODE.toString());
749 private void handleNodeConnectorAvailability(String containerName, Node node, String nodeConnectorType,
750 String nodeConnectorId) {
752 NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType, nodeConnectorId, node);
754 throw new ResourceNotFoundException(nc + " : " + RestMessages.NORESOURCE.toString());
757 ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName, this);
760 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
763 if (!sm.getNodeConnectors(node).contains(nc)) {
764 throw new ResourceNotFoundException(nc.toString() + " : " + RestMessages.NORESOURCE.toString());
768 private boolean isValidContainer(String containerName) {
769 if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
772 IContainerManager containerManager = (IContainerManager) ServiceHelper.getGlobalInstance(
773 IContainerManager.class, this);
774 if (containerManager == null) {
775 throw new InternalServerErrorException(RestMessages.INTERNALERROR.toString());
777 if (containerManager.getContainerNames().contains(containerName)) {