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