b328f779644d02566bb0c3cc3c1c6000c16c2f61
[controller.git] / opendaylight / northbound / networkconfiguration / bridgedomain / src / main / java / org / opendaylight / controller / networkconfig / bridgedomain / northbound / BridgeDomainNorthbound.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.networkconfig.bridgedomain.northbound;
11
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import javax.ws.rs.POST;
16 import javax.ws.rs.Path;
17 import javax.ws.rs.PathParam;
18 import javax.ws.rs.core.Context;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.core.SecurityContext;
21
22 import org.codehaus.enunciate.jaxrs.ResponseCode;
23 import org.codehaus.enunciate.jaxrs.StatusCodes;
24 import org.opendaylight.controller.connectionmanager.IConnectionManager;
25 import org.opendaylight.controller.northbound.commons.exception.NotAcceptableException;
26 import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
27 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
28 import org.opendaylight.controller.sal.core.Node;
29 import org.opendaylight.controller.sal.networkconfig.bridgedomain.ConfigConstants;
30 import org.opendaylight.controller.sal.networkconfig.bridgedomain.IBridgeDomainConfigService;
31 import org.opendaylight.controller.sal.utils.ServiceHelper;
32 import org.opendaylight.controller.sal.utils.Status;
33 import org.opendaylight.controller.sal.utils.StatusCode;
34
35 /**
36  * BridgeDomain Configuration Northbound APIs
37  *
38  * <br><br>
39  * Authentication scheme : <b>HTTP Basic</b><br>
40  * Authentication realm : <b>opendaylight</b><br>
41  * Transport : <b>HTTP and HTTPS</b><br>
42  * <br>
43  * HTTPS Authentication is disabled by default. Administrator can enable it in tomcat-server.xml after adding
44  * a proper keystore / SSL certificate from a trusted authority.<br>
45  * More info : http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html#Configuration
46  */
47 @Path("/")
48 public class BridgeDomainNorthbound {
49     private String username;
50
51     @Context
52     public void setSecurityContext(SecurityContext context) {
53         if (context != null && context.getUserPrincipal() != null) username = context.getUserPrincipal().getName();
54     }
55     protected String getUserName() {
56         return username;
57     }
58
59     private IBridgeDomainConfigService getConfigurationService() {
60         return (IBridgeDomainConfigService) ServiceHelper
61                 .getGlobalInstance(IBridgeDomainConfigService.class, this);
62     }
63
64     private IConnectionManager getConnectionManager() {
65         return (IConnectionManager) ServiceHelper
66                 .getGlobalInstance(IConnectionManager.class, this);
67     }
68
69     /**
70      * Create a Bridge.
71      * <pre>
72      *
73      * Example :
74      *
75      * Request :
76      * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/bridge/STUB/mgmt1/bridge1
77      *
78      *</pre>
79      * @param nodeType Node Type of the node with the management session.
80      * @param nodeId Node Identifier of the node with the management session.
81      * @param bridgeName Name / Identifier for a bridge to be created.
82      */
83
84    @Path("/bridge/{nodeType}/{nodeId}/{bridgeName}")
85    @POST
86    @StatusCodes( { @ResponseCode(code = 201, condition = "Bridge created successfully"),
87        @ResponseCode(code = 404, condition = "Could not create Bridge"),
88        @ResponseCode(code = 412, condition = "Failed to create Bridge due to an exception"),
89        @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
90
91    public Response createBridge(
92            @PathParam(value = "nodeType") String nodeType,
93            @PathParam(value = "nodeId") String nodeId,
94            @PathParam(value = "bridgeName") String name) {
95
96        IBridgeDomainConfigService configurationService = getConfigurationService();
97        if (configurationService == null) {
98            throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
99        }
100
101        Node node = Node.fromString(nodeType, nodeId);
102        Status status = null;
103        try {
104            status = configurationService.createBridgeDomain(node, name, null);
105            if (status.getCode().equals(StatusCode.SUCCESS)) {
106                return Response.status(Response.Status.CREATED).build();
107            }
108        } catch (Throwable t) {
109            return Response.status(Response.Status.PRECONDITION_FAILED).build();
110        }
111        throw new ResourceNotFoundException(status.getDescription());
112    }
113
114    /**
115     * Add a Port to a Bridge
116     * <pre>
117     *
118     * Example :
119     *
120     * Request :
121     * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/port/STUB/mgmt1/bridge1/port1
122     *
123     *</pre>
124     * @param nodeType Node Type of the node with the management session.
125     * @param nodeId Node Identifier of the node with the management session.
126     * @param bridgeName Name / Identifier of the bridge to which a Port is being added.
127     * @param portName Name / Identifier of a Port that is being added to a bridge.
128     */
129
130    @Path("/port/{nodeType}/{nodeId}/{bridgeName}/{portName}")
131    @POST
132    @StatusCodes( { @ResponseCode(code = 201, condition = "Port added successfully"),
133        @ResponseCode(code = 404, condition = "Could not add Port to the Bridge"),
134        @ResponseCode(code = 412, condition = "Failed to add Port due to an exception"),
135        @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
136
137    public Response addPort(
138            @PathParam(value = "nodeType") String nodeType,
139            @PathParam(value = "nodeId") String nodeId,
140            @PathParam(value = "bridgeName") String bridge,
141            @PathParam(value = "portName") String port) {
142
143        IBridgeDomainConfigService configurationService = getConfigurationService();
144        if (configurationService == null) {
145            throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
146        }
147
148        Node node = Node.fromString(nodeType, nodeId);
149        Status status = null;
150        try {
151            status = configurationService.addPort(node, bridge, port, null);
152            if (status.getCode().equals(StatusCode.SUCCESS)) {
153                return Response.status(Response.Status.CREATED).build();
154            }
155        } catch (Throwable t) {
156            return Response.status(Response.Status.PRECONDITION_FAILED).build();
157        }
158        throw new ResourceNotFoundException(status.getDescription());
159    }
160
161    /**
162     * Add a Port,Vlan to a Bridge
163     * <pre>
164     *
165     * Example :
166     * Request :
167     * http://localhost:8080/controller/nb/v2/networkconfig/bridgedomain/port/STUB/mgmt1/bridge1/port2/200
168     *
169     * </pre>
170     * @param nodeType Node Type of the node with the management session.
171     * @param nodeId Node Identifier of the node with the management session.
172     * @param bridgeName Name / Identifier of the bridge to which a Port is being added.
173     * @param portName Name / Identifier of a Port that is being added to a bridge.
174     * @param vlan Vlan Id.
175     */
176
177    @Path("/port/{nodeType}/{nodeId}/{bridgeName}/{portName}/{vlan}")
178    @POST
179    @StatusCodes( { @ResponseCode(code = 201, condition = "Created Port with Vlan tag successfully"),
180        @ResponseCode(code = 404, condition = "Could not add Port,Vlan to the Bridge"),
181        @ResponseCode(code = 406, condition = "Invalid Vlan parameter passed."),
182        @ResponseCode(code = 412, condition = "Failed to add Port,Vlan due to an exception"),
183        @ResponseCode(code = 503, condition = "Bridge Domain Configuration Service not available")} )
184
185    public Response addPort(
186            @PathParam(value = "nodeType") String nodeType,
187            @PathParam(value = "nodeId") String nodeId,
188            @PathParam(value = "bridgeName") String bridge,
189            @PathParam(value = "portName") String port,
190            @PathParam(value = "vlan") String vlan) {
191
192        IBridgeDomainConfigService configurationService = getConfigurationService();
193        if (configurationService == null) {
194            throw new ServiceUnavailableException("IBridgeDomainConfigService not available.");
195        }
196        try {
197            Integer.parseInt(vlan);
198        } catch (Exception e) {
199            throw new NotAcceptableException("Incorrect Vlan :"+vlan);
200        }
201
202        Node node = Node.fromString(nodeType, nodeId);
203        Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
204        configs.put(ConfigConstants.TYPE, ConfigConstants.VLAN.name());
205        configs.put(ConfigConstants.VLAN, vlan);
206
207        Status status = null;
208        try {
209        status = configurationService.addPort(node, bridge, port, configs);
210        if (status.getCode().equals(StatusCode.SUCCESS)) {
211            return Response.status(Response.Status.CREATED).build();
212        }
213        } catch (Exception e) {
214            return Response.status(Response.Status.PRECONDITION_FAILED).build();
215        }
216        throw new ResourceNotFoundException(status.getDescription());
217    }
218 }