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