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; |
| 8 | |
| 9 | import org.apache.felix.scr.annotations.Activate; |
| 10 | import org.apache.felix.scr.annotations.Component; |
| 11 | import org.apache.felix.scr.annotations.Deactivate; |
| 12 | import org.apache.felix.scr.annotations.Reference; |
| 13 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 14 | import org.codehaus.jackson.map.ObjectMapper; |
| 15 | import org.onlab.onos.net.ConnectPoint; |
| 16 | import org.onlab.onos.net.DeviceId; |
| 17 | import org.onlab.onos.net.PortNumber; |
| 18 | import org.onlab.onos.net.host.HostAdminService; |
| 19 | import org.onlab.onos.net.host.PortAddresses; |
| 20 | import org.slf4j.Logger; |
| 21 | |
| 22 | import com.google.common.collect.Sets; |
| 23 | |
| 24 | /** |
| 25 | * Simple configuration module to read in supplementary network configuration |
| 26 | * from a file. |
| 27 | */ |
| 28 | @Component(immediate = true) |
| 29 | public class NetworkConfigReader { |
| 30 | |
| 31 | private final Logger log = getLogger(getClass()); |
| 32 | |
| 33 | private static final String DEFAULT_CONFIG_FILE = "config/addresses.json"; |
| 34 | private String configFileName = DEFAULT_CONFIG_FILE; |
| 35 | |
| 36 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 37 | protected HostAdminService hostAdminService; |
| 38 | |
| 39 | @Activate |
| 40 | protected void activate() { |
| 41 | log.info("Started network config reader"); |
| 42 | |
| 43 | log.info("Config file set to {}", configFileName); |
| 44 | |
| 45 | AddressConfiguration config = readNetworkConfig(); |
| 46 | |
| 47 | if (config != null) { |
| 48 | for (AddressEntry entry : config.getAddresses()) { |
| 49 | |
| 50 | ConnectPoint cp = new ConnectPoint( |
| 51 | DeviceId.deviceId(dpidToUri(entry.getDpid())), |
| 52 | PortNumber.portNumber(entry.getPortNumber())); |
| 53 | |
| 54 | PortAddresses addresses = new PortAddresses(cp, |
| 55 | Sets.newHashSet(entry.getIpAddresses()), |
| 56 | entry.getMacAddress()); |
| 57 | |
| 58 | hostAdminService.bindAddressesToPort(addresses); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Deactivate |
| 64 | protected void deactivate() { |
| 65 | log.info("Stopped"); |
| 66 | } |
| 67 | |
| 68 | private AddressConfiguration readNetworkConfig() { |
| 69 | File configFile = new File(configFileName); |
| 70 | |
| 71 | ObjectMapper mapper = new ObjectMapper(); |
| 72 | |
| 73 | try { |
| 74 | AddressConfiguration config = |
| 75 | mapper.readValue(configFile, AddressConfiguration.class); |
| 76 | |
| 77 | return config; |
| 78 | } catch (FileNotFoundException e) { |
| 79 | log.warn("Configuration file not found: {}", configFileName); |
| 80 | } catch (IOException e) { |
| 81 | log.error("Unable to read config from file:", e); |
| 82 | } |
| 83 | |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | private static String dpidToUri(String dpid) { |
| 88 | return "of:" + dpid.replace(":", ""); |
| 89 | } |
| 90 | } |