Pavlin Radoslavov

Cleanup in the SDN-IP related configuration files:

 * The (deployed) Interface addresses configuration file "addresses.json"
   from the apps/config application is expected to be found in the
   /opt/onos/config directory

 * The (deployed) SDN-IP configuration file "sdnip.json" is expected
   to be found in the /opt/onos/config directory

 * All configuration files (i.e., addresses.json and sdnip.json) should
   be stored in the tools/package/config directory before deployment
   (i.e., before running onos-config)

 * Removed the apps/config/src/main/resources/config.json sample configuration
   file, because it was incorrect, and replaced it with "addresses.json"
   file in the same directory

 * Updated the text in files tools/package/config/README and
   apps/sdnip/src/main/resources/config-examples/README

 * Minor code cleanup in SdnIpConfigReader.java and NetworkConfigReader.java

Change-Id: I8af75e62a94e4fb701e2c6a09cde93cd8461e255
...@@ -50,7 +50,10 @@ public class NetworkConfigReader { ...@@ -50,7 +50,10 @@ public class NetworkConfigReader {
50 50
51 private final Logger log = getLogger(getClass()); 51 private final Logger log = getLogger(getClass());
52 52
53 - private static final String DEFAULT_CONFIG_FILE = "config/addresses.json"; 53 + // Current working dir seems to be /opt/onos/apache-karaf-3.0.2
54 + // TODO: Set the path to /opt/onos/config
55 + private static final String CONFIG_DIR = "../config";
56 + private static final String DEFAULT_CONFIG_FILE = "addresses.json";
54 private String configFileName = DEFAULT_CONFIG_FILE; 57 private String configFileName = DEFAULT_CONFIG_FILE;
55 58
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
...@@ -60,52 +63,9 @@ public class NetworkConfigReader { ...@@ -60,52 +63,9 @@ public class NetworkConfigReader {
60 protected void activate() { 63 protected void activate() {
61 log.info("Started network config reader"); 64 log.info("Started network config reader");
62 65
63 - log.info("Config file set to {}", configFileName);
64 -
65 AddressConfiguration config = readNetworkConfig(); 66 AddressConfiguration config = readNetworkConfig();
66 -
67 if (config != null) { 67 if (config != null) {
68 - for (AddressEntry entry : config.getAddresses()) { 68 + applyNetworkConfig(config);
69 -
70 - ConnectPoint cp = new ConnectPoint(
71 - DeviceId.deviceId(dpidToUri(entry.getDpid())),
72 - PortNumber.portNumber(entry.getPortNumber()));
73 -
74 - Set<InterfaceIpAddress> interfaceIpAddresses = new HashSet<>();
75 -
76 - for (String strIp : entry.getIpAddresses()) {
77 - // Get the IP address and the subnet mask length
78 - try {
79 - String[] splits = strIp.split("/");
80 - if (splits.length != 2) {
81 - throw new IllegalArgumentException("Invalid IP address and prefix length format");
82 - }
83 - // NOTE: IpPrefix will mask-out the bits after the prefix length.
84 - IpPrefix subnet = IpPrefix.valueOf(strIp);
85 - IpAddress addr = IpAddress.valueOf(splits[0]);
86 - InterfaceIpAddress ia =
87 - new InterfaceIpAddress(addr, subnet);
88 - interfaceIpAddresses.add(ia);
89 - } catch (IllegalArgumentException e) {
90 - log.warn("Bad format for IP address in config: {}", strIp);
91 - }
92 - }
93 -
94 - MacAddress macAddress = null;
95 - if (entry.getMacAddress() != null) {
96 - try {
97 - macAddress = MacAddress.valueOf(entry.getMacAddress());
98 - } catch (IllegalArgumentException e) {
99 - log.warn("Bad format for MAC address in config: {}",
100 - entry.getMacAddress());
101 - }
102 - }
103 -
104 - PortAddresses addresses = new PortAddresses(cp,
105 - interfaceIpAddresses, macAddress);
106 -
107 - hostAdminService.bindAddressesToPort(addresses);
108 - }
109 } 69 }
110 } 70 }
111 71
...@@ -114,12 +74,17 @@ public class NetworkConfigReader { ...@@ -114,12 +74,17 @@ public class NetworkConfigReader {
114 log.info("Stopped"); 74 log.info("Stopped");
115 } 75 }
116 76
77 + /**
78 + * Reads the network configuration.
79 + *
80 + * @return the network configuration on success, otherwise null
81 + */
117 private AddressConfiguration readNetworkConfig() { 82 private AddressConfiguration readNetworkConfig() {
118 - File configFile = new File(configFileName); 83 + File configFile = new File(CONFIG_DIR, configFileName);
119 -
120 ObjectMapper mapper = new ObjectMapper(); 84 ObjectMapper mapper = new ObjectMapper();
121 85
122 try { 86 try {
87 + log.info("Loading config: {}", configFile.getAbsolutePath());
123 AddressConfiguration config = 88 AddressConfiguration config =
124 mapper.readValue(configFile, AddressConfiguration.class); 89 mapper.readValue(configFile, AddressConfiguration.class);
125 90
...@@ -127,12 +92,58 @@ public class NetworkConfigReader { ...@@ -127,12 +92,58 @@ public class NetworkConfigReader {
127 } catch (FileNotFoundException e) { 92 } catch (FileNotFoundException e) {
128 log.warn("Configuration file not found: {}", configFileName); 93 log.warn("Configuration file not found: {}", configFileName);
129 } catch (IOException e) { 94 } catch (IOException e) {
130 - log.error("Unable to read config from file:", e); 95 + log.error("Error loading configuration", e);
131 } 96 }
132 97
133 return null; 98 return null;
134 } 99 }
135 100
101 + /**
102 + * Applies the network configuration.
103 + *
104 + * @param config the network configuration to apply
105 + */
106 + private void applyNetworkConfig(AddressConfiguration config) {
107 + for (AddressEntry entry : config.getAddresses()) {
108 + ConnectPoint cp = new ConnectPoint(
109 + DeviceId.deviceId(dpidToUri(entry.getDpid())),
110 + PortNumber.portNumber(entry.getPortNumber()));
111 +
112 + Set<InterfaceIpAddress> interfaceIpAddresses = new HashSet<>();
113 + for (String strIp : entry.getIpAddresses()) {
114 + // Get the IP address and the subnet mask length
115 + try {
116 + String[] splits = strIp.split("/");
117 + if (splits.length != 2) {
118 + throw new IllegalArgumentException("Invalid IP address and prefix length format");
119 + }
120 + // NOTE: IpPrefix will mask-out the bits after the prefix length.
121 + IpPrefix subnet = IpPrefix.valueOf(strIp);
122 + IpAddress addr = IpAddress.valueOf(splits[0]);
123 + InterfaceIpAddress ia =
124 + new InterfaceIpAddress(addr, subnet);
125 + interfaceIpAddresses.add(ia);
126 + } catch (IllegalArgumentException e) {
127 + log.warn("Bad format for IP address in config: {}", strIp);
128 + }
129 + }
130 +
131 + MacAddress macAddress = null;
132 + if (entry.getMacAddress() != null) {
133 + try {
134 + macAddress = MacAddress.valueOf(entry.getMacAddress());
135 + } catch (IllegalArgumentException e) {
136 + log.warn("Bad format for MAC address in config: {}",
137 + entry.getMacAddress());
138 + }
139 + }
140 +
141 + PortAddresses addresses = new PortAddresses(cp,
142 + interfaceIpAddresses, macAddress);
143 + hostAdminService.bindAddressesToPort(addresses);
144 + }
145 + }
146 +
136 private static String dpidToUri(String dpid) { 147 private static String dpidToUri(String dpid) {
137 return "of:" + dpid.replace(":", ""); 148 return "of:" + dpid.replace(":", "");
138 } 149 }
......
1 -{
2 - "interfaces" : [
3 - {
4 - "dpid" : "00:00:00:00:00:00:01",
5 - "port" : "1",
6 - "ips" : ["192.168.10.101/24"],
7 - "mac" : "00:00:00:11:22:33"
8 - },
9 - {
10 - "dpid" : "00:00:00:00:00:00:02",
11 - "port" : "1",
12 - "ips" : ["192.168.20.101/24", "192.168.30.101/24"]
13 - },
14 - {
15 - "dpid" : "00:00:00:00:00:00:03",
16 - "port" : "1",
17 - "ips" : ["10.1.0.1/16"],
18 - "mac" : "00:00:00:00:00:01"
19 - }
20 - ]
21 -}
...@@ -37,24 +37,31 @@ import com.fasterxml.jackson.databind.ObjectMapper; ...@@ -37,24 +37,31 @@ import com.fasterxml.jackson.databind.ObjectMapper;
37 */ 37 */
38 public class SdnIpConfigReader implements SdnIpConfigService { 38 public class SdnIpConfigReader implements SdnIpConfigService {
39 39
40 - private static final Logger log = LoggerFactory.getLogger(SdnIpConfigReader.class); 40 + private final Logger log = LoggerFactory.getLogger(getClass());
41 41
42 - private static final String DEFAULT_CONFIG_FILE = "config/sdnip.json"; 42 + // Current working dir seems to be /opt/onos/apache-karaf-3.0.2
43 + // TODO: Set the path to /opt/onos/config
44 + private static final String CONFIG_DIR = "../config";
45 + private static final String DEFAULT_CONFIG_FILE = "sdnip.json";
43 private String configFileName = DEFAULT_CONFIG_FILE; 46 private String configFileName = DEFAULT_CONFIG_FILE;
47 +
44 private Map<String, BgpSpeaker> bgpSpeakers = new ConcurrentHashMap<>(); 48 private Map<String, BgpSpeaker> bgpSpeakers = new ConcurrentHashMap<>();
45 private Map<IpAddress, BgpPeer> bgpPeers = new ConcurrentHashMap<>(); 49 private Map<IpAddress, BgpPeer> bgpPeers = new ConcurrentHashMap<>();
46 50
47 /** 51 /**
48 - * Reads the info contained in the configuration file. 52 + * Reads SDN-IP related information contained in the configuration file.
49 * 53 *
50 - * @param configFilename The name of configuration file for SDN-IP application. 54 + * @param configFilename the name of the configuration file for the SDN-IP
55 + * application
51 */ 56 */
52 private void readConfiguration(String configFilename) { 57 private void readConfiguration(String configFilename) {
53 - File gatewaysFile = new File(configFilename); 58 + File configFile = new File(CONFIG_DIR, configFilename);
54 ObjectMapper mapper = new ObjectMapper(); 59 ObjectMapper mapper = new ObjectMapper();
55 60
56 try { 61 try {
57 - Configuration config = mapper.readValue(gatewaysFile, Configuration.class); 62 + log.info("Loading config: {}", configFile.getAbsolutePath());
63 + Configuration config = mapper.readValue(configFile,
64 + Configuration.class);
58 for (BgpSpeaker speaker : config.getBgpSpeakers()) { 65 for (BgpSpeaker speaker : config.getBgpSpeakers()) {
59 bgpSpeakers.put(speaker.name(), speaker); 66 bgpSpeakers.put(speaker.name(), speaker);
60 } 67 }
...@@ -64,13 +71,11 @@ public class SdnIpConfigReader implements SdnIpConfigService { ...@@ -64,13 +71,11 @@ public class SdnIpConfigReader implements SdnIpConfigService {
64 } catch (FileNotFoundException e) { 71 } catch (FileNotFoundException e) {
65 log.warn("Configuration file not found: {}", configFileName); 72 log.warn("Configuration file not found: {}", configFileName);
66 } catch (IOException e) { 73 } catch (IOException e) {
67 - log.error("Error reading JSON file", e); 74 + log.error("Error loading configuration", e);
68 } 75 }
69 } 76 }
70 77
71 public void init() { 78 public void init() {
72 - log.debug("Config file set to {}", configFileName);
73 -
74 readConfiguration(configFileName); 79 readConfiguration(configFileName);
75 } 80 }
76 81
......
1 -ONOS looks for these config files by default in $KARAF_HOME/config/
...\ No newline at end of file ...\ No newline at end of file
1 +The SDN-IP configuration files should be copied to directory
2 + $ONOS_HOME/tools/package/config
3 +
4 +After deployment and starting up the ONOS cluster, ONOS looks for these
5 +configuration files in /opt/onos/config on each cluster member.
......
1 -onos-config command will copy files contained in this directory to ONOS instances according to cell definition 1 +The onos-config command will copy files contained in this directory to ONOS
2 - 2 +instances according to cell definition.
......