OpenDaylight Controller functional modules.
[controller.git] / opendaylight / web / flows / src / main / java / org / opendaylight / controller / flows / web / Flows.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.flows.web;
11
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17
18 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
19 import org.opendaylight.controller.forwardingrulesmanager.IForwardingRulesManager;
20 import org.opendaylight.controller.sal.authorization.UserLevel;
21 import org.opendaylight.controller.sal.core.Name;
22 import org.opendaylight.controller.sal.core.Node;
23 import org.opendaylight.controller.sal.core.NodeConnector;
24 import org.opendaylight.controller.sal.utils.ServiceHelper;
25 import org.opendaylight.controller.sal.utils.Status;
26 import org.opendaylight.controller.sal.utils.StatusCode;
27 import org.opendaylight.controller.switchmanager.ISwitchManager;
28 import org.opendaylight.controller.switchmanager.Switch;
29 import org.opendaylight.controller.switchmanager.SwitchConfig;
30 import org.opendaylight.controller.usermanager.IUserManager;
31 import org.opendaylight.controller.web.IOneWeb;
32 import org.springframework.security.core.context.SecurityContextHolder;
33 import org.springframework.stereotype.Controller;
34 import org.springframework.web.bind.annotation.PathVariable;
35 import org.springframework.web.bind.annotation.RequestMapping;
36 import org.springframework.web.bind.annotation.RequestMethod;
37 import org.springframework.web.bind.annotation.RequestParam;
38 import org.springframework.web.bind.annotation.ResponseBody;
39
40 import com.google.gson.Gson;
41
42 @Controller
43 @RequestMapping("/")
44 public class Flows implements IOneWeb {
45         private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
46     private final String WEB_NAME = "Flows";
47     private final String WEB_ID = "flows";
48     private final short WEB_ORDER = 2;
49
50     public Flows() {
51         ServiceHelper.registerGlobalService(IOneWeb.class, this, null);
52     }
53
54     @Override
55     public String getWebName() {
56         return WEB_NAME;
57     }
58
59     @Override
60     public String getWebId() {
61         return WEB_ID;
62     }
63
64     @Override
65     public short getWebOrder() {
66         return WEB_ORDER;
67     }
68
69         @Override
70         public boolean isAuthorized(UserLevel userLevel) {
71                 return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
72         }
73         
74     @RequestMapping(value = "/main")
75     @ResponseBody
76     public Set<Map<String, Object>> getFlows() {
77         // fetch frm
78         IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
79                 .getInstance(IForwardingRulesManager.class, "default", this);
80         if (frm == null)
81             return null;
82
83         // fetch sm
84         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
85                 .getInstance(ISwitchManager.class, "default", this);
86         if (switchManager == null)
87             return null;
88         
89         // get static flow list
90         List<FlowConfig> staticFlowList = frm.getStaticFlows();
91         Set<Map<String, Object>> output = new HashSet<Map<String, Object>>();
92         for (FlowConfig flowConfig : staticFlowList) {
93                 Map<String, Object> entry = new HashMap<String, Object>();
94                 entry.put("flow", flowConfig);
95                 entry.put("name", flowConfig.getName());
96                 
97                 Node node = flowConfig.getNode(); 
98                 SwitchConfig switchConfig = switchManager.getSwitchConfig(node.getNodeIDString());
99                 String nodeName = node.toString();
100                 if (switchConfig != null) { nodeName = switchConfig.getNodeName(); }
101                 entry.put("node", nodeName);
102                 entry.put("nodeId", node.toString());
103                 output.add(entry);
104         }
105         
106         return output;
107     }
108
109     @RequestMapping(value = "/node-ports")
110     @ResponseBody
111     public Map<String, Object> getNodePorts() {
112         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
113                 .getInstance(ISwitchManager.class, "default", this);
114         if (switchManager == null)
115             return null;
116
117         Map<String, Object> nodes = new HashMap<String, Object>();
118         Map<Short, String> port;
119
120         for (Switch node : switchManager.getNetworkDevices()) {
121             port = new HashMap<Short, String>(); // new port
122             Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
123
124             if (nodeConnectorSet != null)
125                 for (NodeConnector nodeConnector : nodeConnectorSet) {
126                     String nodeConnectorName = ((Name) switchManager
127                             .getNodeConnectorProp(nodeConnector,
128                                     Name.NamePropName)).getValue();
129                     port.put((Short) nodeConnector.getID(),
130                              nodeConnectorName + "("
131                              + nodeConnector.getNodeConnectorIDString() + ")");
132                 }
133             
134             // add ports
135             Map<String, Object> entry = new HashMap<String, Object>();
136             entry.put("ports", port);
137             
138             // add name
139             String nodeName = node.getNode().toString();
140             SwitchConfig config = switchManager.getSwitchConfig(node.getNode().getNodeIDString());
141             if (config != null) {
142                 nodeName = config.getNodeName();
143             }
144             entry.put("name", nodeName);
145             
146             // add to the node
147             nodes.put(node.getNode().toString(), entry);
148         }
149
150         return nodes;
151     }
152     
153     @RequestMapping(value = "/node-flows")
154     @ResponseBody
155     public Map<String, Object> getNodeFlows() {
156         ISwitchManager switchManager = (ISwitchManager) ServiceHelper
157                 .getInstance(ISwitchManager.class, "default", this);
158         if (switchManager == null) { return null; }
159         IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
160                 .getInstance(IForwardingRulesManager.class, "default", this);
161         if (frm == null) { return null; }
162
163         Map<String, Object> nodes = new HashMap<String, Object>();
164
165         for (Switch sw : switchManager.getNetworkDevices()) {
166             Node node = sw.getNode();
167             
168             List<FlowConfig> flows = frm.getStaticFlows(node);
169             
170             String nodeName = node.toString();
171             SwitchConfig config = switchManager.getSwitchConfig(node.getNodeIDString());
172             if (config != null) {
173                 nodeName = config.getNodeName();
174             }
175             
176             nodes.put(nodeName, flows.size());
177         }
178
179         return nodes;
180     }
181
182     @RequestMapping(value = "/flow", method = RequestMethod.POST)
183     @ResponseBody
184     public String actionFlow(@RequestParam(required = true) String action,
185             @RequestParam(required = false) String body, @RequestParam(required = true) String nodeId) {
186         if (!authorize(UserLevel.NETWORKADMIN)) {
187                 return "Operation not authorized";
188         }
189         
190         IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
191                 .getInstance(IForwardingRulesManager.class, "default", this);
192         if (frm == null) { return null; }
193
194         Gson gson = new Gson();
195         FlowConfig flow = gson.fromJson(body, FlowConfig.class);
196         Node node = Node.fromString(nodeId);
197         flow.setNode(node);
198         Status result = null;
199         if (action.equals("add")) {
200             result = frm.addStaticFlow(flow, false);
201         }
202
203         return result.getDescription();
204     }
205     
206     @RequestMapping(value = "/flow/{nodeId}/{name}", method = RequestMethod.POST)
207     @ResponseBody
208     public String removeFlow(@PathVariable("nodeId") String nodeId, @PathVariable("name") String name,
209                 @RequestParam(required = true) String action) {
210         if (!authorize(UserLevel.NETWORKADMIN)) { return "Operation not authorized"; }
211         
212         IForwardingRulesManager frm = (IForwardingRulesManager) ServiceHelper
213                 .getInstance(IForwardingRulesManager.class, "default", this);
214         if (frm == null) { return null; }
215         
216         Status result = null;
217         Node node = Node.fromString(nodeId);
218         if (node == null) {
219             return null;
220         }
221         if (action.equals("remove")) {
222                 result = frm.removeStaticFlow(name, node);
223         } else if (action.equals("toggle")) {
224                 FlowConfig config = frm.getStaticFlow(name, node);
225                 result = frm.toggleStaticFlowStatus(config);
226         } else {
227                 result = new Status(StatusCode.BADREQUEST, "Unknown action");
228         }
229         
230         return result.getDescription();
231     }
232     
233     /**
234      * Is the operation permitted for the given level
235      * 
236      * @param level
237      */
238     private boolean authorize(UserLevel level) {
239         IUserManager userManager = (IUserManager) ServiceHelper
240                 .getGlobalInstance(IUserManager.class, this);
241         if (userManager == null) {
242                 return false;
243         }
244         
245         String username = SecurityContextHolder.getContext().getAuthentication().getName();
246         UserLevel userLevel = userManager.getUserLevel(username);
247         if (userLevel.toNumber() <= level.toNumber()) {
248                 return true;
249         }
250         return false;
251     }
252
253 }