blob: ae788ce09a9a35bc266134f6bcc0022a0531b879 [file] [log] [blame]
Jonathan Hartbac07a02014-10-13 21:29:54 -07001package org.onlab.onos.sdnip.config;
2
3import java.io.File;
4import java.io.IOException;
5import java.util.Collections;
6import java.util.Map;
7import java.util.concurrent.ConcurrentHashMap;
8
Jonathan Hartbac07a02014-10-13 21:29:54 -07009import org.onlab.packet.IpAddress;
10import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
Jonathan Hartd7bd9822014-10-20 18:18:02 -070013import com.fasterxml.jackson.databind.ObjectMapper;
14
Jonathan Hartbac07a02014-10-13 21:29:54 -070015/**
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 */
24public 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 Hartdc711bd2014-10-15 11:24:23 -070054 bgpSpeakers.put(speaker.name(), speaker);
Jonathan Hartbac07a02014-10-13 21:29:54 -070055 }
56 bgpPeers = new ConcurrentHashMap<>();
57 for (BgpPeer peer : config.getPeers()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070058 bgpPeers.put(peer.ipAddress(), peer);
Jonathan Hartbac07a02014-10-13 21:29:54 -070059 }
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}