Bugfix: Exception when adding static host
[controller.git] / opendaylight / northbound / hosttracker / src / main / java / org / opendaylight / controller / hosttracker / northbound / HostTrackerNorthbound.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.hosttracker.northbound;
10
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16
17 import javax.ws.rs.Consumes;
18 import javax.ws.rs.DELETE;
19 import javax.ws.rs.GET;
20 import javax.ws.rs.PUT;
21 import javax.ws.rs.Path;
22 import javax.ws.rs.PathParam;
23 import javax.ws.rs.Produces;
24 import javax.ws.rs.core.Context;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.SecurityContext;
28 import javax.ws.rs.core.UriInfo;
29 import javax.xml.bind.JAXBElement;
30
31 import org.codehaus.enunciate.jaxrs.ResponseCode;
32 import org.codehaus.enunciate.jaxrs.StatusCodes;
33 import org.codehaus.enunciate.jaxrs.TypeHint;
34 import org.opendaylight.controller.containermanager.IContainerManager;
35 import org.opendaylight.controller.hosttracker.IfIptoHost;
36 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
37 import org.opendaylight.controller.northbound.commons.RestMessages;
38 import org.opendaylight.controller.northbound.commons.exception.BadRequestException;
39 import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException;
40 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
41 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
42 import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
43 import org.opendaylight.controller.northbound.commons.utils.NorthboundUtils;
44 import org.opendaylight.controller.sal.authorization.Privilege;
45 import org.opendaylight.controller.sal.core.Node;
46 import org.opendaylight.controller.sal.core.NodeConnector;
47 import org.opendaylight.controller.sal.utils.GlobalConstants;
48 import org.opendaylight.controller.sal.utils.ServiceHelper;
49 import org.opendaylight.controller.sal.utils.Status;
50 import org.opendaylight.controller.switchmanager.ISwitchManager;
51
52 /**
53  * Host Tracker Northbound REST APIs.<br>
54  * This class provides REST APIs to track host location in a network. Host
55  * Location is represented by Host node connector which is essentially a logical
56  * entity that represents a Switch/Port. A host is represented by it's
57  * IP-address and mac-address.
58  *
59  * <br>
60  * <br>
61  * Authentication scheme : <b>HTTP Basic</b><br>
62  * Authentication realm : <b>opendaylight</b><br>
63  * Transport : <b>HTTP and HTTPS</b><br>
64  * <br>
65  * HTTPS Authentication is disabled by default. Administrator can enable it in
66  * tomcat-server.xml after adding a proper keystore / SSL certificate from a
67  * trusted authority.<br>
68  * More info :
69  * http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
70  *
71  */
72
73 @Path("/")
74 public class HostTrackerNorthbound {
75
76     private String username;
77
78     @Context
79     public void setSecurityContext(SecurityContext context) {
80         if (context != null && context.getUserPrincipal() != null) username = context.getUserPrincipal().getName();
81     }
82
83     protected String getUserName() {
84         return username;
85     }
86
87     private IfIptoHost getIfIpToHostService(String containerName) {
88         IContainerManager containerManager = (IContainerManager) ServiceHelper.getGlobalInstance(
89                 IContainerManager.class, this);
90         if (containerManager == null) {
91             throw new ServiceUnavailableException("Container " + RestMessages.SERVICEUNAVAILABLE.toString());
92         }
93
94         boolean found = false;
95         List<String> containerNames = containerManager.getContainerNames();
96         for (String cName : containerNames) {
97             if (cName.trim().equalsIgnoreCase(containerName.trim())) {
98                 found = true;
99                 break;
100             }
101         }
102
103         if (!found) {
104             throw new ResourceNotFoundException(containerName + " " + RestMessages.NOCONTAINER.toString());
105         }
106
107         IfIptoHost hostTracker = (IfIptoHost) ServiceHelper.getInstance(IfIptoHost.class, containerName, this);
108         if (hostTracker == null) {
109             throw new ServiceUnavailableException("Host Tracker " + RestMessages.SERVICEUNAVAILABLE.toString());
110         }
111
112         return hostTracker;
113     }
114
115     private Hosts convertHosts(Set<HostNodeConnector> hostNodeConnectors) {
116         if(hostNodeConnectors == null) {
117             return null;
118         }
119         Set<HostConfig> hosts = new HashSet<HostConfig>();
120         for(HostNodeConnector hnc : hostNodeConnectors) {
121             hosts.add(HostConfig.convert(hnc));
122         }
123         return new Hosts(hosts);
124     }
125
126     /**
127      * Returns a list of all Hosts : both configured via PUT API and dynamically
128      * learnt on the network.
129      *
130      * @param containerName
131      *            Name of the Container. The Container name for the base
132      *            controller is "default".
133      * @return List of Active Hosts.
134      * <pre>
135      *
136      * Example:
137      *
138      * RequestURL:
139      *
140      * http://localhost:8080/controller/nb/v2/host/default
141      *
142      * Response in XML
143      *
144      * &lt;list&gt;
145      * &#x20;&lt;hostConfig&gt;
146      * &#x20;&#x20;&lt;dataLayerAddress&gt;00:00:00:00:01:01&lt;/dataLayerAddress&gt;
147      * &#x20;&#x20;&lt;networkAddress&gt;1.1.1.1&lt;/networkAddress&gt;
148      * &#x20;&#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
149      * &#x20;&#x20;&lt;nodeId&gt;00:00:00:00:00:00:00:01&lt;/nodeId&gt;
150      * &#x20;&#x20;&lt;nodeConnectorType&gt;OF&lt;/nodeConnectorType&gt;
151      * &#x20;&#x20;&lt;nodeConnectorId&gt;9&lt;/nodeConnectorId&gt;
152      * &#x20;&#x20;&lt;vlan&gt;0&lt;/vlan&gt;
153      * &#x20;&#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
154      * &#x20;&lt;/hostConfig&gt;
155      * &#x20;&lt;hostConfig&gt;
156      * &#x20;&#x20;&lt;dataLayerAddress&gt;00:00:00:00:02:02&lt;/dataLayerAddress&gt;
157      * &#x20;&#x20;&lt;networkAddress&gt;2.2.2.2&lt;/networkAddress&gt;
158      * &#x20;&#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
159      * &#x20;&#x20;&lt;nodeId&gt;00:00:00:00:00:00:00:02&lt;/nodeId&gt;
160      * &#x20;&#x20;&lt;nodeConnectorType&gt;OF&lt;/nodeConnectorType&gt;
161      * &#x20;&#x20;&lt;nodeConnectorId&gt;5&lt;/nodeConnectorId&gt;
162      * &#x20;&#x20;&lt;vlan&gt;0&lt;/vlan&gt;
163      * &#x20;&#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
164      * &#x20;&lt;/hostConfig&gt;
165      * &lt;/list&gt;
166      *
167      * Response in JSON:
168      *
169      * {
170      * &#x20;"hostConfig":[
171      * &#x20;&#x20;{
172      * &#x20;&#x20;&#x20;"dataLayerAddress":"00:00:00:00:01:01",
173      * &#x20;&#x20;&#x20;"nodeType":"OF",
174      * &#x20;&#x20;&#x20;"nodeId":"00:00:00:00:00:00:00:01",
175      * &#x20;&#x20;&#x20;"nodeConnectorType":"OF",
176      * &#x20;&#x20;&#x20;"nodeConnectorId":"9",
177      * &#x20;&#x20;&#x20;"vlan":"0",
178      * &#x20;&#x20;&#x20;"staticHost":"false",
179      * &#x20;&#x20;&#x20;"networkAddress":"1.1.1.1"
180      * &#x20;&#x20;},
181      * &#x20;&#x20;{
182      * &#x20;&#x20;&#x20;"dataLayerAddress":"00:00:00:00:02:02",
183      * &#x20;&#x20;&#x20;"nodeType":"OF",
184      * &#x20;&#x20;&#x20;"nodeId":"00:00:00:00:00:00:00:02",
185      * &#x20;&#x20;&#x20;"nodeConnectorType":"OF",
186      * &#x20;&#x20;&#x20;"nodeConnectorId":"5",
187      * &#x20;&#x20;&#x20;"vlan":"0",
188      * &#x20;&#x20;&#x20;"staticHost":"false",
189      * &#x20;&#x20;&#x20;"networkAddress":"2.2.2.2"
190      * &#x20;&#x20;}
191      * &#x20;]
192      * }
193      * </pre>
194      */
195     @Path("/{containerName}")
196     @GET
197     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
198     @TypeHint(Hosts.class)
199     @StatusCodes({
200             @ResponseCode(code = 200, condition = "Operation successful"),
201             @ResponseCode(code = 404, condition = "The containerName is not found"),
202             @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
203     public Hosts getActiveHosts(@PathParam("containerName") String containerName) {
204
205         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
206             throw new UnauthorizedException("User is not authorized to perform this operation on container "
207                     + containerName);
208         }
209         IfIptoHost hostTracker = getIfIpToHostService(containerName);
210         return convertHosts(hostTracker.getAllHosts());
211     }
212
213     /**
214      * Returns a list of Hosts that are statically configured and are connected
215      * to a NodeConnector that is down.
216      *
217      * @param containerName
218      *            Name of the Container. The Container name for the base
219      *            controller is "default".
220      * @return List of inactive Hosts.
221      * <pre>
222      *
223      * Example:
224      *
225      * RequestURL:
226      *
227      * http://localhost:8080/controller/nb/v2/host/default/inactive
228      *
229      * Response in XML
230      *
231      * &lt;list&gt;
232      * &#x20;&lt;hostConfig&gt;
233      * &#x20;&#x20;&lt;dataLayerAddress&gt;00:00:00:00:01:01&lt;/dataLayerAddress&gt;
234      * &#x20;&#x20;&lt;networkAddress&gt;1.1.1.1&lt;/networkAddress&gt;
235      * &#x20;&#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
236      * &#x20;&#x20;&lt;nodeId&gt;00:00:00:00:00:00:00:01&lt;/nodeId&gt;
237      * &#x20;&#x20;&lt;nodeConnectorType&gt;OF&lt;/nodeConnectorType&gt;
238      * &#x20;&#x20;&lt;nodeConnectorId&gt;9&lt;/nodeConnectorId&gt;
239      * &#x20;&#x20;&lt;vlan&gt;0&lt;/vlan&gt;
240      * &#x20;&#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
241      * &#x20;&lt;/hostConfig&gt;
242      * &#x20;&lt;hostConfig&gt;
243      * &#x20;&#x20;&lt;dataLayerAddress&gt;00:00:00:00:02:02&lt;/dataLayerAddress&gt;
244      * &#x20;&#x20;&lt;networkAddress&gt;2.2.2.2&lt;/networkAddress&gt;
245      * &#x20;&#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
246      * &#x20;&#x20;&lt;nodeId&gt;00:00:00:00:00:00:00:02&lt;/nodeId&gt;
247      * &#x20;&#x20;&lt;nodeConnectorType&gt;OF&lt;/nodeConnectorType&gt;
248      * &#x20;&#x20;&lt;nodeConnectorId&gt;5&lt;/nodeConnectorId&gt;
249      * &#x20;&#x20;&lt;vlan&gt;0&lt;/vlan&gt;
250      * &#x20;&#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
251      * &#x20;&lt;/hostConfig&gt;
252      * &lt;/list&gt;
253      *
254      * Response in JSON:
255      *
256      * {
257      * &#x20;"hostConfig":[
258      * &#x20;&#x20;{
259      * &#x20;&#x20;&#x20;"dataLayerAddress":"00:00:00:00:01:01",
260      * &#x20;&#x20;&#x20;"nodeType":"OF",
261      * &#x20;&#x20;&#x20;"nodeId":"00:00:00:00:00:00:00:01",
262      * &#x20;&#x20;&#x20;"nodeConnectorType":"OF",
263      * &#x20;&#x20;&#x20;"nodeConnectorId":"9",
264      * &#x20;&#x20;&#x20;"vlan":"0",
265      * &#x20;&#x20;&#x20;"staticHost":"false",
266      * &#x20;&#x20;&#x20;"networkAddress":"1.1.1.1"
267      * &#x20;&#x20;},
268      * &#x20;&#x20;{
269      * &#x20;&#x20;&#x20;"dataLayerAddress":"00:00:00:00:02:02",
270      * &#x20;&#x20;&#x20;"nodeType":"OF",
271      * &#x20;&#x20;&#x20;"nodeId":"00:00:00:00:00:00:00:02",
272      * &#x20;&#x20;&#x20;"nodeConnectorType":"OF",
273      * &#x20;&#x20;&#x20;"nodeConnectorId":"5",
274      * &#x20;&#x20;&#x20;"vlan":"0",
275      * &#x20;&#x20;&#x20;"staticHost":"false",
276      * &#x20;&#x20;&#x20;"networkAddress":"2.2.2.2"
277      * &#x20;&#x20;}
278      * &#x20;]
279      * }
280      * </pre>
281      */
282     @Path("/{containerName}/inactive")
283     @GET
284     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
285     @TypeHint(Hosts.class)
286     @StatusCodes({
287             @ResponseCode(code = 200, condition = "Operation successful"),
288             @ResponseCode(code = 404, condition = "The containerName is not found"),
289             @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
290     public Hosts getInactiveHosts(
291             @PathParam("containerName") String containerName) {
292         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
293             throw new UnauthorizedException("User is not authorized to perform this operation on container "
294                     + containerName);
295         }
296         IfIptoHost hostTracker = getIfIpToHostService(containerName);
297         return convertHosts(hostTracker.getInactiveStaticHosts());
298     }
299
300     /**
301      * Returns a host that matches the IP Address value passed as parameter.
302      *
303      * @param containerName
304      *            Name of the Container. The Container name for the base
305      *            controller is "default".
306      * @param networkAddress
307      *            IP Address being looked up
308      * @return host that matches the IP Address
309      * <pre>
310      *
311      * Example:
312      *
313      * RequestURL:
314      *
315      * http://localhost:8080/controller/nb/v2/host/default/1.1.1.1
316      *
317      * Response in XML
318      *
319      * &lt;hostConfig&gt;
320      * &#x20;&lt;dataLayerAddress&gt;00:00:00:00:01:01&lt;/dataLayerAddress&gt;
321      * &#x20;&lt;networkAddress&gt;1.1.1.1&lt;/networkAddress&gt;
322      * &#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
323      * &#x20;&lt;nodeId&gt;00:00:00:00:00:00:00:01&lt;/nodeId&gt;
324      * &#x20;&lt;nodeConnectorType&gt;OF&lt;/nodeConnectorType&gt;
325      * &#x20;&lt;nodeConnectorId&gt;9&lt;/nodeConnectorId&gt;
326      * &#x20;&lt;vlan&gt;0&lt;/vlan&gt;
327      * &#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
328      * &lt;/hostConfig&gt;
329      *
330      * Response in JSON:
331      *
332      * {
333      * &#x20;"dataLayerAddress":"00:00:00:00:01:01",
334      * &#x20;"nodeType":"OF",
335      * &#x20;"nodeId":"00:00:00:00:00:00:00:01",
336      * &#x20;"nodeConnectorType":"OF",
337      * &#x20;"nodeConnectorId":"9",
338      * &#x20;"vlan":"0",
339      * &#x20;"staticHost":"false",
340      * &#x20;"networkAddress":"1.1.1.1"
341      * }
342      * </pre>
343      */
344     @Path("/{containerName}/{networkAddress}")
345     @GET
346     @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
347     @TypeHint(HostConfig.class)
348     @StatusCodes({
349             @ResponseCode(code = 200, condition = "Operation successful"),
350             @ResponseCode(code = 400, condition = "Invalid IP specified in networkAddress parameter"),
351             @ResponseCode(code = 404, condition = "The containerName is not found"),
352             @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
353     public HostConfig getHostDetails(
354             @PathParam("containerName") String containerName,
355             @PathParam("networkAddress") String networkAddress) {
356         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
357             throw new UnauthorizedException("User is not authorized to perform this operation on container "
358                     + containerName);
359         }
360         IfIptoHost hostTracker = getIfIpToHostService(containerName);
361
362         InetAddress ip;
363         try {
364             ip = InetAddress.getByName(networkAddress);
365         } catch (UnknownHostException e) {
366             throw new BadRequestException(RestMessages.INVALIDADDRESS.toString() + " " + networkAddress);
367         }
368         for (HostNodeConnector host : hostTracker.getAllHosts()) {
369             if (host.getNetworkAddress().equals(ip)) {
370                 return HostConfig.convert(host);
371             }
372         }
373         throw new ResourceNotFoundException(RestMessages.NOHOST.toString());
374     }
375
376     /**
377      * Add a Static Host configuration
378      *
379      * @param containerName
380      *            Name of the Container. The Container name for the base
381      *            controller is "default".
382      * @param networkAddress
383      *            Host IP Address
384      * @param hostConfig
385      *            Host Config Details
386      * @return Response as dictated by the HTTP Response Status code
387      *
388      * <pre>
389      *
390      * Example:
391      *
392      * RequestURL:
393      *
394      * http://localhost:8080/controller/nb/v2/host/default/1.1.1.1
395      *
396      * Request in XML
397      *
398      * &lt;hostConfig&gt;
399      * &#x20;&lt;dataLayerAddress&gt;00:00:00:00:01:01&lt;/dataLayerAddress&gt;
400      * &#x20;&lt;networkAddress&gt;1.1.1.1&lt;/networkAddress&gt;
401      * &#x20;&lt;nodeType&gt;OF&lt;/nodeType&gt;
402      * &#x20;&lt;nodeId&gt;00:00:00:00:00:00:00:01&lt;/nodeId&gt;
403      * &#x20;&lt;nodeConnectorType&gt;OF&lt;/nodeConnectorType&gt;
404      * &#x20;&lt;nodeConnectorId&gt;9&lt;/nodeConnectorId&gt;
405      * &#x20;&lt;vlan&gt;0&lt;/vlan&gt;
406      * &#x20;&lt;staticHost&gt;false&lt;/staticHost&gt;
407      * &lt;/hostConfig&gt;
408      *
409      * Request in JSON:
410      *
411      * {
412      * &#x20;"dataLayerAddress":"00:00:00:00:01:01",
413      * &#x20;"nodeType":"OF",
414      * &#x20;"nodeId":"00:00:00:00:00:00:00:01",
415      * &#x20;"nodeConnectorType":"OF",
416      * &#x20;"nodeConnectorId":"9",
417      * &#x20;"vlan":"0",
418      * &#x20;"staticHost":"false",
419      * &#x20;"networkAddress":"1.1.1.1"
420      * }
421      * </pre>
422      */
423
424     @Path("/{containerName}/{networkAddress}")
425     @PUT
426     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
427     @StatusCodes({
428             @ResponseCode(code = 201, condition = "Static host created successfully"),
429             @ResponseCode(code = 400, condition = "Invalid parameters specified, see response body for details"),
430             @ResponseCode(code = 404, condition = "The container or resource is not found"),
431             @ResponseCode(code = 409, condition = "Resource conflict, see response body for details"),
432             @ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
433     public Response addHost(@Context UriInfo uriInfo, @PathParam("containerName") String containerName,
434             @PathParam("networkAddress") String networkAddress,
435             @TypeHint(HostConfig.class) JAXBElement<HostConfig> hostConfig) {
436
437         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
438             return Response.status(Response.Status.UNAUTHORIZED)
439                     .entity("User is not authorized to perform this operation on container " + containerName)
440                     .build();
441         }
442         handleDefaultDisabled(containerName);
443
444         IfIptoHost hostTracker = getIfIpToHostService(containerName);
445
446         HostConfig hc = hostConfig.getValue();
447         if (!networkAddress.equals(hc.getNetworkAddress())) {
448             return Response.status(Response.Status.CONFLICT)
449                     .entity("Resource name in config object doesn't match URI")
450                     .build();
451         }
452         if (!hc.isStaticHost()) {
453             return Response.status(Response.Status.BAD_REQUEST)
454                     .entity("Can only add static host.")
455                     .build();
456         }
457         Node node = handleNodeAvailability(containerName, hc.getNodeType(), hc.getNodeId());
458         NodeConnector nc = NodeConnector.fromStringNoNode(hc.getNodeConnectorType(), hc.getNodeConnectorId(), node);
459
460         Status status = hostTracker.addStaticHost(networkAddress, hc.getDataLayerAddress(), nc, hc.getVlan());
461         if (status.isSuccess()) {
462             NorthboundUtils.auditlog("Static Host", username, "added", networkAddress, containerName);
463             return Response.created(uriInfo.getRequestUri()).build();
464         }
465
466         return NorthboundUtils.getResponse(status);
467     }
468
469     /**
470      * Delete a Static Host configuration
471      *
472      * @param containerName
473      *            Name of the Container. The Container name for the base
474      *            controller is "default".
475      * @param networkAddress
476      *            IP Address
477      * @return Response as dictated by the HTTP Response code.
478      */
479
480     @Path("/{containerName}/{networkAddress}")
481     @DELETE
482     @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
483     @StatusCodes({
484             @ResponseCode(code = 204, condition = "Static host deleted successfully"),
485             @ResponseCode(code = 404, condition = "The container or a specified resource was not found"),
486             @ResponseCode(code = 406, condition = "Cannot operate on Default Container when other Containers are active"),
487             @ResponseCode(code = 503, condition = "One or more of Controller service is unavailable") })
488     public Response deleteHost(
489             @PathParam(value = "containerName") String containerName,
490             @PathParam(value = "networkAddress") String networkAddress) {
491
492         if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.WRITE, this)) {
493             return Response.status(Response.Status.UNAUTHORIZED)
494                     .entity("User is not authorized to perform this operation on container " + containerName)
495                     .build();
496         }
497         handleDefaultDisabled(containerName);
498         IfIptoHost hostTracker = getIfIpToHostService(containerName);
499
500         Status status = hostTracker.removeStaticHost(networkAddress);
501         if (status.isSuccess()) {
502             NorthboundUtils.auditlog("Static Host", username, "removed", networkAddress, containerName);
503             return Response.noContent().build();
504         }
505         return NorthboundUtils.getResponse(status);
506
507     }
508
509     private void handleDefaultDisabled(String containerName) {
510         IContainerManager containerManager = (IContainerManager) ServiceHelper
511                 .getGlobalInstance(IContainerManager.class, this);
512         if (containerManager == null) {
513             throw new ServiceUnavailableException(
514                     RestMessages.SERVICEUNAVAILABLE.toString());
515         }
516         if (containerName.equals(GlobalConstants.DEFAULT.toString())
517                 && containerManager.hasNonDefaultContainer()) {
518             throw new ResourceConflictException(
519                     RestMessages.DEFAULTDISABLED.toString());
520         }
521     }
522
523     private Node handleNodeAvailability(String containerName, String nodeType, String nodeId) {
524
525         Node node = Node.fromString(nodeType, nodeId);
526         if (node == null) {
527             throw new ResourceNotFoundException(nodeId + " : "
528                     + RestMessages.NONODE.toString());
529         }
530
531         ISwitchManager sm = (ISwitchManager) ServiceHelper.getInstance(
532                 ISwitchManager.class, containerName, this);
533
534         if (sm == null) {
535             throw new ServiceUnavailableException("Switch Manager "
536                     + RestMessages.SERVICEUNAVAILABLE.toString());
537         }
538
539         if (!sm.getNodes().contains(node)) {
540             throw new ResourceNotFoundException(node.toString() + " : "
541                     + RestMessages.NONODE.toString());
542         }
543         return node;
544     }
545
546 }