Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.configmanager; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.ArrayList; |
| 5 | import java.util.Iterator; |
| 6 | import java.util.List; |
| 7 | import java.util.Map.Entry; |
| 8 | import java.util.Set; |
| 9 | import java.util.concurrent.ConcurrentHashMap; |
| 10 | |
| 11 | import net.onrc.onos.core.configmanager.NetworkConfig.SwitchConfig; |
| 12 | |
| 13 | import org.codehaus.jackson.JsonNode; |
| 14 | import org.codehaus.jackson.JsonProcessingException; |
| 15 | import org.codehaus.jackson.map.ObjectMapper; |
| 16 | import org.projectfloodlight.openflow.util.HexString; |
| 17 | import org.slf4j.Logger; |
| 18 | import org.slf4j.LoggerFactory; |
| 19 | |
| 20 | public class SegmentRouterConfig extends SwitchConfig { |
| 21 | protected static final Logger log = LoggerFactory |
| 22 | .getLogger(SegmentRouterConfig.class); |
| 23 | private String routerIp; |
| 24 | private String routerMac; |
| 25 | private int nodeSid; |
Sangho Shin | dafae19 | 2014-10-07 17:06:48 -0700 | [diff] [blame] | 26 | private boolean isEdgeRouter; |
Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 27 | private List<AdjacencySid> adjacencySids; |
| 28 | private List<Subnet> subnets; |
| 29 | |
| 30 | public static final String ROUTER_IP = "routerIp"; |
| 31 | public static final String ROUTER_MAC = "routerMac"; |
| 32 | public static final String NODE_SID = "nodeSid"; |
| 33 | public static final String ADJACENCY_SIDS = "adjacencySids"; |
| 34 | public static final String SUBNETS = "subnets"; |
Sangho Shin | dafae19 | 2014-10-07 17:06:48 -0700 | [diff] [blame] | 35 | public static final String ISEDGE = "isEdgeRouter"; |
Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 36 | |
| 37 | public SegmentRouterConfig(SwitchConfig swc) { |
| 38 | this.setName(swc.getName()); |
| 39 | this.setDpid(swc.getDpid()); |
| 40 | this.setType(swc.getType()); |
| 41 | this.setLatitude(swc.getLatitude()); |
| 42 | this.setLongitude(swc.getLongitude()); |
| 43 | this.setParams(swc.getParams()); |
| 44 | this.setAllowed(swc.isAllowed()); |
| 45 | publishAttributes = new ConcurrentHashMap<String, String>(); |
| 46 | adjacencySids = new ArrayList<AdjacencySid>(); |
| 47 | subnets = new ArrayList<Subnet>(); |
| 48 | parseParams(); |
| 49 | validateParams(); |
| 50 | setPublishAttributes(); |
| 51 | } |
| 52 | |
| 53 | // ******************** |
| 54 | // Segment Router Configuration |
| 55 | // ******************** |
| 56 | public String getRouterIp() { |
| 57 | return routerIp; |
| 58 | } |
| 59 | |
| 60 | public void setRouterIp(String routerIp) { |
| 61 | this.routerIp = routerIp; |
| 62 | } |
| 63 | |
| 64 | public String getRouterMac() { |
| 65 | return routerMac; |
| 66 | } |
| 67 | |
| 68 | public void setRouterMac(String routerMac) { |
| 69 | this.routerMac = routerMac; |
| 70 | } |
| 71 | |
| 72 | public int getNodeSid() { |
| 73 | return nodeSid; |
| 74 | } |
| 75 | |
| 76 | public void setNodeSid(int nodeSid) { |
| 77 | this.nodeSid = nodeSid; |
| 78 | } |
| 79 | |
Sangho Shin | dafae19 | 2014-10-07 17:06:48 -0700 | [diff] [blame] | 80 | public boolean isEdgeRouter() { |
| 81 | return isEdgeRouter; |
| 82 | } |
| 83 | |
| 84 | public void setIsEdgeRouter(boolean isEdge) { |
| 85 | this.isEdgeRouter = isEdge; |
| 86 | } |
| 87 | |
Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 88 | public static class AdjacencySid { |
| 89 | private int portNo; |
| 90 | private int adjSid; |
| 91 | |
| 92 | public AdjacencySid(int portNo, int adjSid) { |
| 93 | this.portNo = portNo; |
| 94 | this.adjSid = adjSid; |
| 95 | } |
| 96 | |
| 97 | public int getPortNo() { |
| 98 | return portNo; |
| 99 | } |
| 100 | |
| 101 | public void setPortNo(int portNo) { |
| 102 | this.portNo = portNo; |
| 103 | } |
| 104 | |
| 105 | public int getAdjSid() { |
| 106 | return adjSid; |
| 107 | } |
| 108 | |
| 109 | public void setAdjSid(int adjSid) { |
| 110 | this.adjSid = adjSid; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | public List<AdjacencySid> getAdjacencySids() { |
| 115 | return adjacencySids; |
| 116 | } |
| 117 | |
| 118 | public void setAdjacencySids(List<AdjacencySid> adjacencySids) { |
| 119 | this.adjacencySids = adjacencySids; |
| 120 | } |
| 121 | |
| 122 | public static class Subnet { |
| 123 | private int portNo; |
| 124 | private String subnetIp; |
| 125 | |
| 126 | public Subnet(int portNo, String subnetIp) { |
| 127 | this.portNo = portNo; |
| 128 | this.subnetIp = subnetIp; |
| 129 | } |
| 130 | |
| 131 | public int getPortNo() { |
| 132 | return portNo; |
| 133 | } |
| 134 | |
| 135 | public void setPortNo(int portNo) { |
| 136 | this.portNo = portNo; |
| 137 | } |
| 138 | |
| 139 | public String getSubnetIp() { |
| 140 | return subnetIp; |
| 141 | } |
| 142 | |
| 143 | public void setSubnetIp(String subnetIp) { |
| 144 | this.subnetIp = subnetIp; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | public List<Subnet> getSubnets() { |
| 149 | return subnets; |
| 150 | } |
| 151 | |
| 152 | public void setSubnets(List<Subnet> subnets) { |
| 153 | this.subnets = subnets; |
| 154 | } |
| 155 | |
| 156 | // ******************** |
| 157 | // Helper methods |
| 158 | // ******************** |
| 159 | |
| 160 | private void parseParams() { |
| 161 | if (params == null) { |
| 162 | throw new NetworkConfigException.ParamsNotSpecified(name); |
| 163 | } |
| 164 | |
| 165 | Set<Entry<String, JsonNode>> m = params.entrySet(); |
| 166 | for (Entry<String, JsonNode> e : m) { |
| 167 | String key = e.getKey(); |
| 168 | JsonNode j = e.getValue(); |
| 169 | if (key.equals("routerIp")) { |
| 170 | setRouterIp(j.asText()); |
| 171 | } else if (key.equals("routerMac")) { |
| 172 | setRouterMac(j.asText()); |
| 173 | } else if (key.equals("nodeSid")) { |
| 174 | setNodeSid(j.asInt()); |
Sangho Shin | dafae19 | 2014-10-07 17:06:48 -0700 | [diff] [blame] | 175 | } else if (key.equals("isEdgeRouter")) { |
| 176 | setIsEdgeRouter(j.asBoolean()); |
| 177 | } |
| 178 | else if (key.equals("adjacencySids") || key.equals("subnets")) { |
Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 179 | getInnerParams(j, key); |
| 180 | } else { |
| 181 | throw new UnknownSegmentRouterConfig(key, dpid); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private void getInnerParams(JsonNode j, String innerParam) { |
| 187 | Iterator<JsonNode> innerList = j.getElements(); |
| 188 | while (innerList.hasNext()) { |
| 189 | Iterator<Entry<String, JsonNode>> f = innerList.next().getFields(); |
| 190 | int portNo = -1; |
| 191 | int adjSid = -1; |
| 192 | String subnetIp = null; |
| 193 | while (f.hasNext()) { |
| 194 | Entry<String, JsonNode> fe = f.next(); |
| 195 | if (fe.getKey().equals("portNo")) { |
| 196 | portNo = fe.getValue().asInt(); |
| 197 | } else if (fe.getKey().equals("adjSid")) { |
| 198 | adjSid = fe.getValue().asInt(); |
| 199 | } else if (fe.getKey().equals("subnetIp")) { |
| 200 | subnetIp = fe.getValue().asText(); |
| 201 | } else { |
| 202 | throw new UnknownSegmentRouterConfig(fe.getKey(), dpid); |
| 203 | } |
| 204 | } |
| 205 | if (innerParam.equals("adjacencySids")) { |
| 206 | AdjacencySid ads = new AdjacencySid(portNo, adjSid); |
| 207 | adjacencySids.add(ads); |
| 208 | } else { |
| 209 | Subnet sip = new Subnet(portNo, subnetIp); |
| 210 | subnets.add(sip); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private void validateParams() { |
| 216 | if (routerIp == null) { |
| 217 | throw new IpNotSpecified(dpid); |
| 218 | } |
Sangho Shin | dafae19 | 2014-10-07 17:06:48 -0700 | [diff] [blame] | 219 | if (isEdgeRouter && subnets.isEmpty()) { |
| 220 | throw new SubnetNotSpecifiedInEdgeRouter(dpid); |
| 221 | } |
| 222 | if (!isEdgeRouter && !subnets.isEmpty()) { |
| 223 | throw new SubnetSpecifiedInBackboneRouter(dpid); |
| 224 | } |
Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 225 | |
| 226 | // TODO more validations |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Setting publishAttributes implies that this is the configuration that |
| 231 | * will be added to Topology.Switch object before it is published on the |
| 232 | * channel to other controller instances. |
| 233 | */ |
| 234 | private void setPublishAttributes() { |
| 235 | publishAttributes.put(ROUTER_IP, routerIp); |
| 236 | publishAttributes.put(ROUTER_MAC, routerMac); |
| 237 | publishAttributes.put(NODE_SID, String.valueOf(nodeSid)); |
Sangho Shin | dafae19 | 2014-10-07 17:06:48 -0700 | [diff] [blame] | 238 | publishAttributes.put(ISEDGE, String.valueOf(isEdgeRouter)); |
Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 239 | ObjectMapper mapper = new ObjectMapper(); |
| 240 | try { |
| 241 | publishAttributes.put(ADJACENCY_SIDS, |
| 242 | mapper.writeValueAsString(adjacencySids)); |
| 243 | publishAttributes.put(SUBNETS, |
| 244 | mapper.writeValueAsString(subnets)); |
| 245 | } catch (JsonProcessingException e) { |
| 246 | log.error("Error while writing SR config: {}", e.getCause()); |
| 247 | } catch (IOException e) { |
| 248 | log.error("Error while writing SR config: {}", e.getCause()); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // ******************** |
| 253 | // Exceptions |
| 254 | // ******************** |
| 255 | |
| 256 | public static class IpNotSpecified extends RuntimeException { |
| 257 | private static final long serialVersionUID = -3001502553646331686L; |
| 258 | |
| 259 | public IpNotSpecified(long dpid) { |
| 260 | super(); |
| 261 | log.error("Router IP address not specified for SR config dpid:{}", |
| 262 | HexString.toHexString(dpid)); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | public static class UnknownSegmentRouterConfig extends RuntimeException { |
| 267 | private static final long serialVersionUID = -5750132094884129179L; |
| 268 | |
| 269 | public UnknownSegmentRouterConfig(String key, long dpid) { |
| 270 | super(); |
| 271 | log.error("Unknown Segment Router config {} in dpid: {}", key, |
| 272 | HexString.toHexString(dpid)); |
| 273 | } |
| 274 | } |
| 275 | |
Sangho Shin | dafae19 | 2014-10-07 17:06:48 -0700 | [diff] [blame] | 276 | public static class SubnetNotSpecifiedInEdgeRouter extends RuntimeException { |
| 277 | private static final long serialVersionUID = -5855458472668581268L; |
| 278 | |
| 279 | public SubnetNotSpecifiedInEdgeRouter(long dpid) { |
| 280 | super(); |
| 281 | log.error("Subnet was not specified for edge router in dpid: {}", |
| 282 | HexString.toHexString(dpid)); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | public static class SubnetSpecifiedInBackboneRouter extends RuntimeException { |
| 287 | private static final long serialVersionUID = -5855458472668581268L; |
| 288 | |
| 289 | public SubnetSpecifiedInBackboneRouter(long dpid) { |
| 290 | super(); |
| 291 | log.error("Subnet was specified in backbone router in dpid: {}", |
| 292 | HexString.toHexString(dpid)); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | |
Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 297 | |
| 298 | } |