Saurav Das | fb93c25 | 2014-08-18 20:40:13 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.configmanager; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | import java.util.Map.Entry; |
| 6 | import java.util.Set; |
| 7 | import java.util.concurrent.ConcurrentHashMap; |
| 8 | |
| 9 | import net.onrc.onos.core.configmanager.NetworkConfig.LinkConfig; |
| 10 | import net.onrc.onos.core.util.Dpid; |
| 11 | import net.onrc.onos.core.util.LinkTuple; |
| 12 | import net.onrc.onos.core.util.PortNumber; |
| 13 | |
| 14 | import org.codehaus.jackson.JsonNode; |
| 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| 18 | /** |
| 19 | * Configuration for a link between two packet-switches. |
| 20 | */ |
| 21 | public class PktLinkConfig extends LinkConfig { |
| 22 | protected static final Logger log = LoggerFactory |
| 23 | .getLogger(PktLinkConfig.class); |
| 24 | private int port1; |
| 25 | private int port2; |
| 26 | private String nodeName1; |
| 27 | private String nodeName2; |
| 28 | private List<LinkTuple> linkTupleList; |
| 29 | |
| 30 | public PktLinkConfig(LinkConfig lkc) { |
| 31 | nodeDpid1 = lkc.getNodeDpid1(); |
| 32 | nodeDpid2 = lkc.getNodeDpid2(); |
| 33 | dpid1 = lkc.getDpid1(); |
| 34 | dpid2 = lkc.getDpid2(); |
| 35 | type = lkc.getType(); |
| 36 | allowed = lkc.isAllowed(); |
| 37 | params = lkc.getParams(); |
| 38 | publishAttributes = new ConcurrentHashMap<String, String>(); |
| 39 | parseParams(); |
| 40 | validateParams(); |
| 41 | setPublishAttributes(); |
| 42 | } |
| 43 | |
| 44 | // ******************** |
| 45 | // Packet Link Configuration |
| 46 | // ******************** |
| 47 | |
| 48 | public int getPort1() { |
| 49 | return port1; |
| 50 | } |
| 51 | |
| 52 | public void setPort1(int port1) { |
| 53 | this.port1 = port1; |
| 54 | } |
| 55 | |
| 56 | public int getPort2() { |
| 57 | return port2; |
| 58 | } |
| 59 | |
| 60 | public void setPort2(int port2) { |
| 61 | this.port2 = port2; |
| 62 | } |
| 63 | |
| 64 | public String getNodeName1() { |
| 65 | return nodeName1; |
| 66 | } |
| 67 | |
| 68 | public void setNodeName1(String nodeName1) { |
| 69 | this.nodeName1 = nodeName1; |
| 70 | } |
| 71 | |
| 72 | public String getNodeName2() { |
| 73 | return nodeName2; |
| 74 | } |
| 75 | |
| 76 | public void setNodeName2(String nodeName2) { |
| 77 | this.nodeName2 = nodeName2; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the two unidirectional links corresponding to the packet-link |
| 82 | * configuration. It is possible that the ports in the LinkTuple have |
| 83 | * portnumber '0', implying that the configuration applies to all links |
| 84 | * between the two switches. |
| 85 | * |
| 86 | * @return a list of LinkTuple with exactly 2 unidirectional links |
| 87 | */ |
| 88 | public List<LinkTuple> getLinkTupleList() { |
| 89 | if (linkTupleList != null) { |
| 90 | return linkTupleList; |
| 91 | } else { |
| 92 | linkTupleList = new ArrayList<LinkTuple>(); |
| 93 | } |
| 94 | |
| 95 | // first one direction |
| 96 | linkTupleList.add(new LinkTuple( |
| 97 | new Dpid(nodeDpid1), PortNumber.uint32(port1), |
| 98 | new Dpid(nodeDpid2), PortNumber.uint32(port2))); |
| 99 | // then the reverse direction |
| 100 | linkTupleList.add(new LinkTuple( |
| 101 | new Dpid(nodeDpid2), PortNumber.uint32(port2), |
| 102 | new Dpid(nodeDpid1), PortNumber.uint32(port1))); |
| 103 | return linkTupleList; |
| 104 | } |
| 105 | |
| 106 | private void setPublishAttributes() { |
| 107 | |
| 108 | } |
| 109 | |
| 110 | private void parseParams() { |
| 111 | if (params == null) { |
| 112 | throw new PktLinkParamsNotSpecified(nodeDpid1, nodeDpid2); |
| 113 | } |
| 114 | Set<Entry<String, JsonNode>> m = params.entrySet(); |
| 115 | for (Entry<String, JsonNode> e : m) { |
| 116 | String key = e.getKey(); |
| 117 | JsonNode j = e.getValue(); |
| 118 | if (key.equals("nodeName1")) { |
| 119 | setNodeName1(j.asText()); |
| 120 | } else if (key.equals("nodeName2")) { |
| 121 | setNodeName2(j.asText()); |
| 122 | } else if (key.equals("port1")) { |
| 123 | setPort1(j.asInt()); |
| 124 | } else if (key.equals("port2")) { |
| 125 | setPort2(j.asInt()); |
| 126 | } else { |
| 127 | throw new UnknownPktLinkConfig(key, nodeDpid1, nodeDpid2); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private void validateParams() { |
| 133 | // TODO - wrong-names, duplicate links, |
| 134 | // duplicate use of port, is switch-allowed for which link is allowed? |
| 135 | } |
| 136 | |
| 137 | public static class PktLinkParamsNotSpecified extends RuntimeException { |
| 138 | private static final long serialVersionUID = 6247582323691265513L; |
| 139 | |
| 140 | public PktLinkParamsNotSpecified(String dpidA, String dpidB) { |
| 141 | super(); |
| 142 | log.error("Params required for packet link - not specified " |
| 143 | + "for link between switch1:{} and switch2:{}", |
| 144 | dpidA, dpidB); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | public static class UnknownPktLinkConfig extends RuntimeException { |
| 149 | private static final long serialVersionUID = -5750132094884129179L; |
| 150 | |
| 151 | public UnknownPktLinkConfig(String key, String dpidA, String dpidB) { |
| 152 | super(); |
| 153 | log.error("Unknown packet-link config {} for link between" |
| 154 | + " dpid1: {} and dpid2: {}", key, |
| 155 | dpidA, dpidB); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | } |