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
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>
159 * {"nodeProperties":[{"node":{"id":"00:00:00:00:00:00:00:02","type":"OF"},"properties":{"tables":{"value":"-1"},
160 * "description":{"value":"None"},"actions":{"value":"4095"},"macAddress":{"value":"00:00:00:00:00:02"},"capabilities"
161 * :{"value":"199"},"timeStamp":{"value":"1377291227877","name":"connectedSince"},"buffers":{"value":"256"}}}]}
165 @Path("/{containerName}/nodes")
167 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
168 @TypeHint(Nodes.class)
169 @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
170 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
171 @ResponseCode(code = 404, condition = "The containerName is not found"),
172 @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
173 public Nodes getNodes(@PathParam("containerName") String containerName) {
175 if (!isValidContainer(containerName)) {
176 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
179 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
180 throw new UnauthorizedException("User is not authorized to perform this operation on container "
184 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
185 if (switchManager == null) {
186 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
189 List<NodeProperties> res = new ArrayList<NodeProperties>();
190 Set<Node> nodes = switchManager.getNodes();
192 return new Nodes(res);
195 for (Node node : nodes) {
196 Map<String, Property> propMap = switchManager.getNodeProps(node);
197 if (propMap == null) {
200 Set<Property> props = new HashSet<Property>(propMap.values());
202 NodeProperties nodeProps = new NodeProperties(node, props);
206 return new Nodes(res);
210 * Add a Description, Tier and Forwarding mode property to a node. This
211 * method returns a non-successful response if a node by that name already
214 * @param containerName
215 * Name of the Container (Eg. 'default')
217 * Type of the node being programmed (Eg. 'OF')
219 * Node Identifier as specified by
220 * {@link org.opendaylight.controller.sal.core.Node} (Eg.
221 * '00:00:00:00:00:00:00:03')
222 * @param propertyName
223 * Name of the Property. Properties that can be configured are:
224 * description, forwarding(only for default container) and tier
225 * @param propertyValue
226 * Value of the Property. Description can be any string (Eg.
227 * 'Node1'), valid values for tier are non negative numbers, and
228 * valid values for forwarding are 0 for reactive and 1 for
229 * proactive forwarding.
230 * @return Response as dictated by the HTTP Response Status code
237 * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:03/property/description/Switch3
242 @Path("/{containerName}/node/{nodeType}/{nodeId}/property/{propertyName}/{propertyValue}")
244 @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
245 @TypeHint(Response.class)
247 @ResponseCode(code = 201, condition = "Operation successful"),
248 @ResponseCode(code = 400, condition = "The nodeId or configuration is invalid"),
249 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
250 @ResponseCode(code = 404, condition = "The Container Name or node or configuration name is not found"),
251 @ResponseCode(code = 406, condition = "The property cannot be configured in non-default container"),
252 @ResponseCode(code = 409, condition = "Unable to update configuration due to cluster conflict or conflicting description property"),
253 @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
254 public Response addNodeProperty(@Context UriInfo uriInfo, @PathParam("containerName") String containerName,
255 @PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
256 @PathParam("propertyName") String propertyName, @PathParam("propertyValue") String propertyValue) {
258 if (!isValidContainer(containerName)) {
259 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
261 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
262 throw new UnauthorizedException("User is not authorized to perform this operation on container "
265 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
266 if (switchManager == null) {
267 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
270 handleNodeAvailability(containerName, nodeType, nodeId);
271 Node node = Node.fromString(nodeType, nodeId);
272 Property prop = switchManager.createProperty(propertyName, propertyValue);
274 throw new ResourceNotFoundException("Property with name " + propertyName + " does not exist.");
276 SwitchConfig switchConfig = switchManager.getSwitchConfig(node.toString());
277 Map<String, Property> nodeProperties = (switchConfig == null) ? new HashMap<String, Property>()
278 : new HashMap<String, Property>(switchConfig.getNodeProperties());
279 nodeProperties.put(prop.getName(), prop);
280 SwitchConfig newSwitchConfig = new SwitchConfig(node.toString(), nodeProperties);
281 Status status = switchManager.updateNodeConfig(newSwitchConfig);
282 if (status.isSuccess()) {
283 NorthboundUtils.auditlog("Property " + propertyName, username, "updated",
284 "of Node " + NorthboundUtils.getNodeDesc(node, switchManager), containerName);
286 return Response.created(uriInfo.getRequestUri()).build();
288 return NorthboundUtils.getResponse(status);
292 * Delete a property of a node
294 * @param containerName
295 * Name of the Container (Eg. 'SliceRed')
297 * Type of the node being programmed (Eg. 'OF')
299 * Node Identifier as specified by
300 * {@link org.opendaylight.controller.sal.core.Node} (Eg.
301 * '00:00:00:00:00:03:01:02')
302 * @param propertyName
303 * Name of the Property. Properties that can be deleted are
304 * description, forwarding(only in default container) and tier.
305 * @return Response as dictated by the HTTP Response Status code
312 * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:03/property/forwarding
317 @Path("/{containerName}/node/{nodeType}/{nodeId}/property/{propertyName}")
319 @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
320 @StatusCodes({ @ResponseCode(code = 204, condition = "Property removed successfully"),
321 @ResponseCode(code = 400, condition = "The nodeId or configuration is invalid"),
322 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
323 @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
324 @ResponseCode(code = 409, condition = "Unable to delete property due to cluster conflict"),
325 @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
326 public Response deleteNodeProperty(@PathParam("containerName") String containerName,
327 @PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
328 @PathParam("propertyName") String propertyName) {
330 if (!isValidContainer(containerName)) {
331 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
333 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
334 throw new UnauthorizedException("User is not authorized to perform this operation on container "
337 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
338 if (switchManager == null) {
339 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
342 handleNodeAvailability(containerName, nodeType, nodeId);
343 Node node = Node.fromString(nodeType, nodeId);
345 SwitchConfig switchConfig = switchManager.getSwitchConfig(node.toString());
347 if (switchConfig == null) {
348 status = new Status(StatusCode.NOTFOUND, "Switch Configuration does not exist");
350 Map<String, Property> nodeProperties = new HashMap<String, Property>(switchConfig.getNodeProperties());
351 if (!nodeProperties.containsKey(propertyName.toLowerCase())) {
352 String msg = "Property " + propertyName + " does not exist or not configured for switch " + nodeId;
353 status = new Status(StatusCode.NOTFOUND, msg);
355 nodeProperties.remove(propertyName.toLowerCase());
356 SwitchConfig newSwitchConfig = new SwitchConfig(node.toString(), nodeProperties);
357 status = switchManager.updateNodeConfig(newSwitchConfig);
358 if (status.isSuccess()) {
359 NorthboundUtils.auditlog("Property " + propertyName, username, "removed", "of Node "
360 + NorthboundUtils.getNodeDesc(node, switchManager), containerName);
361 return Response.noContent().build();
365 return NorthboundUtils.getResponse(status);
370 * Retrieve a list of all the nodeconnectors and their properties in a given
373 * @param containerName
374 * The container for which we want to retrieve the list (Eg.
377 * Type of the node being programmed (Eg. 'OF')
379 * Node Identifier as specified by
380 * {@link org.opendaylight.controller.sal.core.Node} (Eg.
381 * '00:00:00:00:00:00:00:03')
382 * @return A List of Pair each pair represents a
383 * {@link org.opendaylight.controller.sal.core.NodeConnector} and
385 * {@link org.opendaylight.controller.sal.core.Property} attached to
393 * http://localhost:8080/controller/nb/v2/switchmanager/default/node/OF/00:00:00:00:00:00:00:01
396 * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
398 *    <nodeConnectorProperties>
399 *       <nodeconnector>
400 *          <node>
401 *             <id>00:00:00:00:00:00:00:01</id>
402 *             <type>OF</type>
403 *          </node>
404 *          <id>2</id>
405 *          <type>OF</type>
406 *       </nodeconnector>
407 *       <properties>
408 *          <state>
409 *             <value>1</value>
410 *          </state>
411 *          <config>
412 *             <value>1</value>
413 *          </config>
414 *          <name>
415 *             <value>L1_2-C2_1</value>
416 *          </name>
417 *       </properties>
418 *    </nodeConnectorProperties>
422 * {"nodeConnectorProperties":[{"nodeconnector":{"node":{"id":"00:00:00:00:00:00:00:01","type":"OF"},"id":"2","type":"OF"},
423 * "properties":{"state":{"value":"1"},"config":{"value":"1"},"name":{"value":"L1_2-C2_1"}}}]}
427 @Path("/{containerName}/node/{nodeType}/{nodeId}")
429 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
430 @TypeHint(NodeConnectors.class)
431 @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
432 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
433 @ResponseCode(code = 404, condition = "The containerName is not found"),
434 @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
435 public NodeConnectors getNodeConnectors(@PathParam("containerName") String containerName,
436 @PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId) {
438 if (!isValidContainer(containerName)) {
439 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
441 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
442 throw new UnauthorizedException("User is not authorized to perform this operation on container "
446 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
447 if (switchManager == null) {
448 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
451 handleNodeAvailability(containerName, nodeType, nodeId);
452 Node node = Node.fromString(nodeType, nodeId);
453 List<NodeConnectorProperties> res = new ArrayList<NodeConnectorProperties>();
454 Set<NodeConnector> ncs = switchManager.getNodeConnectors(node);
459 for (NodeConnector nc : ncs) {
460 Map<String, Property> propMap = switchManager.getNodeConnectorProps(nc);
461 if (propMap == null) {
464 Set<Property> props = new HashSet<Property>(propMap.values());
465 NodeConnectorProperties ncProps = new NodeConnectorProperties(nc, props);
469 return new NodeConnectors(res);
473 * Add node-connector property to a node connector. This method returns a
474 * non-successful response if a node connector by the given name already
477 * @param containerName
478 * Name of the Container (Eg. 'default')
480 * Type of the node being programmed (Eg. 'OF')
482 * Node Identifier as specified by
483 * {@link org.opendaylight.controller.sal.core.Node} (Eg.
484 * '00:00:00:00:00:00:00:03')
485 * @param nodeConnectorType
486 * Type of the node connector being programmed (Eg. 'OF')
487 * @param nodeConnectorId
488 * NodeConnector Identifier as specified by
489 * {@link org.opendaylight.controller.sal.core.NodeConnector}
491 * @param propertyName
492 * Name of the Property specified by
493 * {@link org.opendaylight.controller.sal.core.Property} and its
494 * extended classes Property that can be configured is bandwidth
495 * @param propertyValue
496 * Value of the Property specified by
497 * {@link org.opendaylight.controller.sal.core.Property} and its
499 * @return Response as dictated by the HTTP Response Status code
506 * http://localhost:8080/controller/nb/v2/switchmanager/default/nodeconnector/OF/00:00:00:00:00:00:00:01/OF/2/property/bandwidth/1
511 @Path("/{containerName}/nodeconnector/{nodeType}/{nodeId}/{nodeConnectorType}/{nodeConnectorId}/property/{propertyName}/{propertyValue}")
513 @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
514 @StatusCodes({ @ResponseCode(code = 201, condition = "Operation successful"),
515 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
516 @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
517 @ResponseCode(code = 409, condition = "Unable to add property due to cluster conflict"),
518 @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
519 public Response addNodeConnectorProperty(@Context UriInfo uriInfo,
520 @PathParam("containerName") String containerName, @PathParam("nodeType") String nodeType,
521 @PathParam("nodeId") String nodeId, @PathParam("nodeConnectorType") String nodeConnectorType,
522 @PathParam("nodeConnectorId") String nodeConnectorId, @PathParam("propertyName") String propertyName,
523 @PathParam("propertyValue") String propertyValue) {
525 if (!isValidContainer(containerName)) {
526 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
528 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
529 throw new UnauthorizedException("User is not authorized to perform this operation on container "
533 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
534 if (switchManager == null) {
535 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
538 handleNodeAvailability(containerName, nodeType, nodeId);
539 Node node = Node.fromString(nodeType, nodeId);
541 handleNodeConnectorAvailability(containerName, node, nodeConnectorType, nodeConnectorId);
542 NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType, nodeConnectorId, node);
544 Property prop = switchManager.createProperty(propertyName, propertyValue);
546 throw new ResourceNotFoundException(RestMessages.INVALIDDATA.toString());
549 Status ret = switchManager.addNodeConnectorProp(nc, prop);
550 if (ret.isSuccess()) {
551 NorthboundUtils.auditlog("Property " + propertyName, username, "updated", "of Node Connector "
552 + NorthboundUtils.getPortName(nc, switchManager), containerName);
553 return Response.created(uriInfo.getRequestUri()).build();
555 throw new InternalServerErrorException(ret.getDescription());
559 * Delete a property of a node connector
561 * @param containerName
562 * Name of the Container (Eg. 'default')
564 * Type of the node being programmed (Eg. 'OF')
566 * Node Identifier as specified by
567 * {@link org.opendaylight.controller.sal.core.Node} (Eg.
568 * '00:00:00:00:00:00:00:01')
569 * @param nodeConnectorType
570 * Type of the node connector being programmed (Eg. 'OF')
571 * @param nodeConnectorId
572 * NodeConnector Identifier as specified by
573 * {@link org.opendaylight.controller.sal.core.NodeConnector}
575 * @param propertyName
576 * Name of the Property specified by
577 * {@link org.opendaylight.controller.sal.core.Property} and its
578 * extended classes. Property that can be deleted is bandwidth
579 * @return Response as dictated by the HTTP Response Status code
586 * http://localhost:8080/controller/nb/v2/switchmanager/default/nodeconnector/OF/00:00:00:00:00:00:00:01/OF/2/property/bandwidth
591 @Path("/{containerName}/nodeconnector/{nodeType}/{nodeId}/{nodeConnectorType}/{nodeConnectorId}/property/{propertyName}")
593 @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
594 @StatusCodes({ @ResponseCode(code = 204, condition = "Property removed successfully"),
595 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
596 @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
597 @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
598 public Response deleteNodeConnectorProperty(@PathParam("containerName") String containerName,
599 @PathParam("nodeType") String nodeType, @PathParam("nodeId") String nodeId,
600 @PathParam("nodeConnectorType") String nodeConnectorType,
601 @PathParam("nodeConnectorId") String nodeConnectorId, @PathParam("propertyName") String propertyName) {
603 if (!isValidContainer(containerName)) {
604 throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
606 if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
607 throw new UnauthorizedException("User is not authorized to perform this operation on container "
611 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
612 if (switchManager == null) {
613 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
616 handleNodeAvailability(containerName, nodeType, nodeId);
617 Node node = Node.fromString(nodeType, nodeId);
619 handleNodeConnectorAvailability(containerName, node, nodeConnectorType, nodeConnectorId);
620 NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType, nodeConnectorId, node);
621 Status ret = switchManager.removeNodeConnectorProp(nc, propertyName);
622 if (ret.isSuccess()) {
623 NorthboundUtils.auditlog("Property " + propertyName, username, "removed", "of Node Connector "
624 + NorthboundUtils.getPortName(nc, switchManager), containerName);
625 return Response.noContent().build();
627 throw new ResourceNotFoundException(ret.getDescription());
631 * Save the current switch configurations
633 * @param containerName
634 * Name of the Container (Eg. 'default')
635 * @return Response as dictated by the HTTP Response Status code
642 * http://localhost:8080/controller/nb/v2/switchmanager/default/save
646 @Path("/{containerName}/save")
648 @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
650 @ResponseCode(code = 200, condition = "Operation successful"),
651 @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
652 @ResponseCode(code = 404, condition = "The containerName is not found"),
653 @ResponseCode(code = 500, condition = "Failed to save switch configuration. Failure Reason included in HTTP Error response"),
654 @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
655 public Response saveSwitchConfig(@PathParam("containerName") String containerName) {
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 "
664 ISwitchManager switchManager = getIfSwitchManagerService(containerName);
665 if (switchManager == null) {
666 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
669 Status ret = switchManager.saveSwitchConfig();
670 if (ret.isSuccess()) {
671 return Response.ok().build();
673 throw new InternalServerErrorException(ret.getDescription());
676 private Node handleNodeAvailability(String containerName, String nodeType, String nodeId) {
678 Node node = Node.fromString(nodeType, nodeId);
680 throw new ResourceNotFoundException(nodeId + " : " + RestMessages.NONODE.toString());
683 ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName, this);
686 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
689 if (!sm.getNodes().contains(node)) {
690 throw new ResourceNotFoundException(node.toString() + " : " + RestMessages.NONODE.toString());
695 private void handleNodeConnectorAvailability(String containerName, Node node, String nodeConnectorType,
696 String nodeConnectorId) {
698 NodeConnector nc = NodeConnector.fromStringNoNode(nodeConnectorType, nodeConnectorId, node);
700 throw new ResourceNotFoundException(nc + " : " + RestMessages.NORESOURCE.toString());
703 ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName, this);
706 throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
709 if (!sm.getNodeConnectors(node).contains(nc)) {
710 throw new ResourceNotFoundException(nc.toString() + " : " + RestMessages.NORESOURCE.toString());
714 private boolean isValidContainer(String containerName) {
715 if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
718 IContainerManager containerManager = (IContainerManager) ServiceHelper.getGlobalInstance(
719 IContainerManager.class, this);
720 if (containerManager == null) {
721 throw new InternalServerErrorException(RestMessages.INTERNALERROR.toString());
723 if (containerManager.getContainerNames().contains(containerName)) {