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