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; |
| 26 | private List<AdjacencySid> adjacencySids; |
| 27 | private List<Subnet> subnets; |
| 28 | |
| 29 | public static final String ROUTER_IP = "routerIp"; |
| 30 | public static final String ROUTER_MAC = "routerMac"; |
| 31 | public static final String NODE_SID = "nodeSid"; |
| 32 | public static final String ADJACENCY_SIDS = "adjacencySids"; |
| 33 | public static final String SUBNETS = "subnets"; |
| 34 | |
| 35 | public SegmentRouterConfig(SwitchConfig swc) { |
| 36 | this.setName(swc.getName()); |
| 37 | this.setDpid(swc.getDpid()); |
| 38 | this.setType(swc.getType()); |
| 39 | this.setLatitude(swc.getLatitude()); |
| 40 | this.setLongitude(swc.getLongitude()); |
| 41 | this.setParams(swc.getParams()); |
| 42 | this.setAllowed(swc.isAllowed()); |
| 43 | publishAttributes = new ConcurrentHashMap<String, String>(); |
| 44 | adjacencySids = new ArrayList<AdjacencySid>(); |
| 45 | subnets = new ArrayList<Subnet>(); |
| 46 | parseParams(); |
| 47 | validateParams(); |
| 48 | setPublishAttributes(); |
| 49 | } |
| 50 | |
| 51 | // ******************** |
| 52 | // Segment Router Configuration |
| 53 | // ******************** |
| 54 | public String getRouterIp() { |
| 55 | return routerIp; |
| 56 | } |
| 57 | |
| 58 | public void setRouterIp(String routerIp) { |
| 59 | this.routerIp = routerIp; |
| 60 | } |
| 61 | |
| 62 | public String getRouterMac() { |
| 63 | return routerMac; |
| 64 | } |
| 65 | |
| 66 | public void setRouterMac(String routerMac) { |
| 67 | this.routerMac = routerMac; |
| 68 | } |
| 69 | |
| 70 | public int getNodeSid() { |
| 71 | return nodeSid; |
| 72 | } |
| 73 | |
| 74 | public void setNodeSid(int nodeSid) { |
| 75 | this.nodeSid = nodeSid; |
| 76 | } |
| 77 | |
| 78 | public static class AdjacencySid { |
| 79 | private int portNo; |
| 80 | private int adjSid; |
| 81 | |
| 82 | public AdjacencySid(int portNo, int adjSid) { |
| 83 | this.portNo = portNo; |
| 84 | this.adjSid = adjSid; |
| 85 | } |
| 86 | |
| 87 | public int getPortNo() { |
| 88 | return portNo; |
| 89 | } |
| 90 | |
| 91 | public void setPortNo(int portNo) { |
| 92 | this.portNo = portNo; |
| 93 | } |
| 94 | |
| 95 | public int getAdjSid() { |
| 96 | return adjSid; |
| 97 | } |
| 98 | |
| 99 | public void setAdjSid(int adjSid) { |
| 100 | this.adjSid = adjSid; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | public List<AdjacencySid> getAdjacencySids() { |
| 105 | return adjacencySids; |
| 106 | } |
| 107 | |
| 108 | public void setAdjacencySids(List<AdjacencySid> adjacencySids) { |
| 109 | this.adjacencySids = adjacencySids; |
| 110 | } |
| 111 | |
| 112 | public static class Subnet { |
| 113 | private int portNo; |
| 114 | private String subnetIp; |
| 115 | |
| 116 | public Subnet(int portNo, String subnetIp) { |
| 117 | this.portNo = portNo; |
| 118 | this.subnetIp = subnetIp; |
| 119 | } |
| 120 | |
| 121 | public int getPortNo() { |
| 122 | return portNo; |
| 123 | } |
| 124 | |
| 125 | public void setPortNo(int portNo) { |
| 126 | this.portNo = portNo; |
| 127 | } |
| 128 | |
| 129 | public String getSubnetIp() { |
| 130 | return subnetIp; |
| 131 | } |
| 132 | |
| 133 | public void setSubnetIp(String subnetIp) { |
| 134 | this.subnetIp = subnetIp; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public List<Subnet> getSubnets() { |
| 139 | return subnets; |
| 140 | } |
| 141 | |
| 142 | public void setSubnets(List<Subnet> subnets) { |
| 143 | this.subnets = subnets; |
| 144 | } |
| 145 | |
| 146 | // ******************** |
| 147 | // Helper methods |
| 148 | // ******************** |
| 149 | |
| 150 | private void parseParams() { |
| 151 | if (params == null) { |
| 152 | throw new NetworkConfigException.ParamsNotSpecified(name); |
| 153 | } |
| 154 | |
| 155 | Set<Entry<String, JsonNode>> m = params.entrySet(); |
| 156 | for (Entry<String, JsonNode> e : m) { |
| 157 | String key = e.getKey(); |
| 158 | JsonNode j = e.getValue(); |
| 159 | if (key.equals("routerIp")) { |
| 160 | setRouterIp(j.asText()); |
| 161 | } else if (key.equals("routerMac")) { |
| 162 | setRouterMac(j.asText()); |
| 163 | } else if (key.equals("nodeSid")) { |
| 164 | setNodeSid(j.asInt()); |
| 165 | } else if (key.equals("adjacencySids") || key.equals("subnets")) { |
| 166 | getInnerParams(j, key); |
| 167 | } else { |
| 168 | throw new UnknownSegmentRouterConfig(key, dpid); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | private void getInnerParams(JsonNode j, String innerParam) { |
| 174 | Iterator<JsonNode> innerList = j.getElements(); |
| 175 | while (innerList.hasNext()) { |
| 176 | Iterator<Entry<String, JsonNode>> f = innerList.next().getFields(); |
| 177 | int portNo = -1; |
| 178 | int adjSid = -1; |
| 179 | String subnetIp = null; |
| 180 | while (f.hasNext()) { |
| 181 | Entry<String, JsonNode> fe = f.next(); |
| 182 | if (fe.getKey().equals("portNo")) { |
| 183 | portNo = fe.getValue().asInt(); |
| 184 | } else if (fe.getKey().equals("adjSid")) { |
| 185 | adjSid = fe.getValue().asInt(); |
| 186 | } else if (fe.getKey().equals("subnetIp")) { |
| 187 | subnetIp = fe.getValue().asText(); |
| 188 | } else { |
| 189 | throw new UnknownSegmentRouterConfig(fe.getKey(), dpid); |
| 190 | } |
| 191 | } |
| 192 | if (innerParam.equals("adjacencySids")) { |
| 193 | AdjacencySid ads = new AdjacencySid(portNo, adjSid); |
| 194 | adjacencySids.add(ads); |
| 195 | } else { |
| 196 | Subnet sip = new Subnet(portNo, subnetIp); |
| 197 | subnets.add(sip); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | private void validateParams() { |
| 203 | if (routerIp == null) { |
| 204 | throw new IpNotSpecified(dpid); |
| 205 | } |
| 206 | |
| 207 | // TODO more validations |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Setting publishAttributes implies that this is the configuration that |
| 212 | * will be added to Topology.Switch object before it is published on the |
| 213 | * channel to other controller instances. |
| 214 | */ |
| 215 | private void setPublishAttributes() { |
| 216 | publishAttributes.put(ROUTER_IP, routerIp); |
| 217 | publishAttributes.put(ROUTER_MAC, routerMac); |
| 218 | publishAttributes.put(NODE_SID, String.valueOf(nodeSid)); |
| 219 | ObjectMapper mapper = new ObjectMapper(); |
| 220 | try { |
| 221 | publishAttributes.put(ADJACENCY_SIDS, |
| 222 | mapper.writeValueAsString(adjacencySids)); |
| 223 | publishAttributes.put(SUBNETS, |
| 224 | mapper.writeValueAsString(subnets)); |
| 225 | } catch (JsonProcessingException e) { |
| 226 | log.error("Error while writing SR config: {}", e.getCause()); |
| 227 | } catch (IOException e) { |
| 228 | log.error("Error while writing SR config: {}", e.getCause()); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // ******************** |
| 233 | // Exceptions |
| 234 | // ******************** |
| 235 | |
| 236 | public static class IpNotSpecified extends RuntimeException { |
| 237 | private static final long serialVersionUID = -3001502553646331686L; |
| 238 | |
| 239 | public IpNotSpecified(long dpid) { |
| 240 | super(); |
| 241 | log.error("Router IP address not specified for SR config dpid:{}", |
| 242 | HexString.toHexString(dpid)); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | public static class UnknownSegmentRouterConfig extends RuntimeException { |
| 247 | private static final long serialVersionUID = -5750132094884129179L; |
| 248 | |
| 249 | public UnknownSegmentRouterConfig(String key, long dpid) { |
| 250 | super(); |
| 251 | log.error("Unknown Segment Router config {} in dpid: {}", key, |
| 252 | HexString.toHexString(dpid)); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | |
| 257 | } |