Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 1 | package org.onlab.onos.config; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.io.File; |
| 6 | import java.io.FileNotFoundException; |
| 7 | import java.io.IOException; |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 8 | import java.util.HashSet; |
| 9 | import java.util.Set; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 10 | |
| 11 | import org.apache.felix.scr.annotations.Activate; |
| 12 | import org.apache.felix.scr.annotations.Component; |
| 13 | import org.apache.felix.scr.annotations.Deactivate; |
| 14 | import org.apache.felix.scr.annotations.Reference; |
| 15 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 16 | import org.onlab.onos.net.ConnectPoint; |
| 17 | import org.onlab.onos.net.DeviceId; |
| 18 | import org.onlab.onos.net.PortNumber; |
| 19 | import org.onlab.onos.net.host.HostAdminService; |
| 20 | import org.onlab.onos.net.host.PortAddresses; |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onlab.packet.MacAddress; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 23 | import org.slf4j.Logger; |
| 24 | |
Jonathan Hart | d7bd982 | 2014-10-20 18:18:02 -0700 | [diff] [blame^] | 25 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Simple configuration module to read in supplementary network configuration |
| 29 | * from a file. |
| 30 | */ |
| 31 | @Component(immediate = true) |
| 32 | public class NetworkConfigReader { |
| 33 | |
| 34 | private final Logger log = getLogger(getClass()); |
| 35 | |
| 36 | private static final String DEFAULT_CONFIG_FILE = "config/addresses.json"; |
| 37 | private String configFileName = DEFAULT_CONFIG_FILE; |
| 38 | |
| 39 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 40 | protected HostAdminService hostAdminService; |
| 41 | |
| 42 | @Activate |
| 43 | protected void activate() { |
| 44 | log.info("Started network config reader"); |
| 45 | |
| 46 | log.info("Config file set to {}", configFileName); |
| 47 | |
| 48 | AddressConfiguration config = readNetworkConfig(); |
| 49 | |
| 50 | if (config != null) { |
| 51 | for (AddressEntry entry : config.getAddresses()) { |
| 52 | |
| 53 | ConnectPoint cp = new ConnectPoint( |
| 54 | DeviceId.deviceId(dpidToUri(entry.getDpid())), |
| 55 | PortNumber.portNumber(entry.getPortNumber())); |
| 56 | |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 57 | Set<IpPrefix> ipAddresses = new HashSet<IpPrefix>(); |
| 58 | |
| 59 | for (String strIp : entry.getIpAddresses()) { |
| 60 | try { |
| 61 | IpPrefix address = IpPrefix.valueOf(strIp); |
| 62 | ipAddresses.add(address); |
| 63 | } catch (IllegalArgumentException e) { |
| 64 | log.warn("Bad format for IP address in config: {}", strIp); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | MacAddress macAddress = null; |
| 69 | if (entry.getMacAddress() != null) { |
| 70 | try { |
| 71 | macAddress = MacAddress.valueOf(entry.getMacAddress()); |
| 72 | } catch (IllegalArgumentException e) { |
| 73 | log.warn("Bad format for MAC address in config: {}", |
| 74 | entry.getMacAddress()); |
| 75 | } |
| 76 | } |
| 77 | |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 78 | PortAddresses addresses = new PortAddresses(cp, |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 79 | ipAddresses, macAddress); |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 80 | |
| 81 | hostAdminService.bindAddressesToPort(addresses); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | protected void deactivate() { |
| 88 | log.info("Stopped"); |
| 89 | } |
| 90 | |
| 91 | private AddressConfiguration readNetworkConfig() { |
| 92 | File configFile = new File(configFileName); |
| 93 | |
| 94 | ObjectMapper mapper = new ObjectMapper(); |
| 95 | |
| 96 | try { |
| 97 | AddressConfiguration config = |
| 98 | mapper.readValue(configFile, AddressConfiguration.class); |
| 99 | |
| 100 | return config; |
| 101 | } catch (FileNotFoundException e) { |
| 102 | log.warn("Configuration file not found: {}", configFileName); |
| 103 | } catch (IOException e) { |
| 104 | log.error("Unable to read config from file:", e); |
| 105 | } |
| 106 | |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | private static String dpidToUri(String dpid) { |
| 111 | return "of:" + dpid.replace(":", ""); |
| 112 | } |
| 113 | } |