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