Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame^] | 1 | package org.onlab.onos.sdnip.config; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.util.Collections; |
| 6 | import java.util.Map; |
| 7 | import java.util.concurrent.ConcurrentHashMap; |
| 8 | |
| 9 | import org.codehaus.jackson.map.ObjectMapper; |
| 10 | import org.onlab.packet.IpAddress; |
| 11 | import org.slf4j.Logger; |
| 12 | import org.slf4j.LoggerFactory; |
| 13 | |
| 14 | /** |
| 15 | * SDN-IP Config Reader provides IConfigInfoService |
| 16 | * by reading from an SDN-IP configuration file. |
| 17 | * It must be enabled on the nodes within the cluster |
| 18 | * not running SDN-IP. |
| 19 | * <p/> |
| 20 | * TODO: As a long term solution, a module providing |
| 21 | * general network configuration to ONOS nodes should be used. |
| 22 | */ |
| 23 | public class SdnIpConfigReader implements SdnIpConfigService { |
| 24 | |
| 25 | private static final Logger log = LoggerFactory.getLogger(SdnIpConfigReader.class); |
| 26 | |
| 27 | private static final String DEFAULT_CONFIG_FILE = "config/sdnip.json"; |
| 28 | private String configFileName = DEFAULT_CONFIG_FILE; |
| 29 | //private Map<String, Interface> interfaces; |
| 30 | // We call the BGP routers in our SDN network the BGP speakers, and call |
| 31 | // the BGP routers outside our SDN network the BGP peers. |
| 32 | private Map<String, BgpSpeaker> bgpSpeakers; |
| 33 | private Map<IpAddress, BgpPeer> bgpPeers; |
| 34 | //private InvertedRadixTree<Interface> interfaceRoutes; |
| 35 | |
| 36 | /** |
| 37 | * Reads the info contained in the configuration file. |
| 38 | * |
| 39 | * @param configFilename The name of configuration file for SDN-IP application. |
| 40 | */ |
| 41 | private void readConfiguration(String configFilename) { |
| 42 | File gatewaysFile = new File(configFilename); |
| 43 | ObjectMapper mapper = new ObjectMapper(); |
| 44 | |
| 45 | try { |
| 46 | Configuration config = mapper.readValue(gatewaysFile, Configuration.class); |
| 47 | /*interfaces = new ConcurrentHashMap<>(); |
| 48 | for (Interface intf : config.getInterfaces()) { |
| 49 | interfaces.put(intf.getName(), intf); |
| 50 | }*/ |
| 51 | bgpSpeakers = new ConcurrentHashMap<>(); |
| 52 | for (BgpSpeaker speaker : config.getBgpSpeakers()) { |
| 53 | bgpSpeakers.put(speaker.getSpeakerName(), speaker); |
| 54 | } |
| 55 | bgpPeers = new ConcurrentHashMap<>(); |
| 56 | for (BgpPeer peer : config.getPeers()) { |
| 57 | bgpPeers.put(peer.getIpAddress(), peer); |
| 58 | } |
| 59 | } catch (IOException e) { |
| 60 | log.error("Error reading JSON file", e); |
| 61 | //throw new ConfigurationRuntimeException("Error in JSON file", e); |
| 62 | } |
| 63 | |
| 64 | // Populate the interface InvertedRadixTree |
| 65 | /*for (Interface intf : interfaces.values()) { |
| 66 | Ip4Prefix prefix = intf.getIp4Prefix(); |
| 67 | String binaryString = RouteEntry.createBinaryString(prefix); |
| 68 | interfaceRoutes.put(binaryString, intf); |
| 69 | }*/ |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * To find the Interface which has longest matchable IP prefix (sub-network |
| 74 | * prefix) to next hop IP address. |
| 75 | * |
| 76 | * @param address the IP address of next hop router |
| 77 | * @return the Interface which has longest matchable IP prefix |
| 78 | */ |
| 79 | /*private Interface longestInterfacePrefixMatch(IpAddress address) { |
| 80 | Ip4Prefix prefixToSearchFor = |
| 81 | new Ip4Prefix(address, (short) Ip4Address.BIT_LENGTH); |
| 82 | String binaryString = RouteEntry.createBinaryString(prefixToSearchFor); |
| 83 | |
| 84 | Iterator<Interface> it = |
| 85 | interfaceRoutes.getValuesForKeysPrefixing(binaryString).iterator(); |
| 86 | Interface intf = null; |
| 87 | // Find the last prefix, which will be the longest prefix |
| 88 | while (it.hasNext()) { |
| 89 | intf = it.next(); |
| 90 | } |
| 91 | |
| 92 | return intf; |
| 93 | }*/ |
| 94 | |
| 95 | /*@Override |
| 96 | public Interface getOutgoingInterface(IpAddress dstIpAddress) { |
| 97 | return longestInterfacePrefixMatch(dstIpAddress); |
| 98 | }*/ |
| 99 | |
| 100 | public void init() { |
| 101 | //interfaceRoutes = new ConcurrentInvertedRadixTree<>( |
| 102 | //new DefaultByteArrayNodeFactory()); |
| 103 | |
| 104 | // Reading config values |
| 105 | /*String configFilenameParameter = context.getConfigParams(this).get("configfile"); |
| 106 | if (configFilenameParameter != null) { |
| 107 | currentConfigFilename = configFilenameParameter; |
| 108 | }*/ |
| 109 | log.debug("Config file set to {}", configFileName); |
| 110 | |
| 111 | readConfiguration(configFileName); |
| 112 | } |
| 113 | |
| 114 | /*@Override |
| 115 | public Map<String, Interface> getInterfaces() { |
| 116 | return Collections.unmodifiableMap(interfaces); |
| 117 | }*/ |
| 118 | |
| 119 | @Override |
| 120 | public Map<String, BgpSpeaker> getBgpSpeakers() { |
| 121 | return Collections.unmodifiableMap(bgpSpeakers); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Map<IpAddress, BgpPeer> getBgpPeers() { |
| 126 | return Collections.unmodifiableMap(bgpPeers); |
| 127 | } |
| 128 | |
| 129 | static String dpidToUri(String dpid) { |
| 130 | return "of:" + dpid.replace(":", ""); |
| 131 | } |
| 132 | } |