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