Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 16 | package org.onlab.onos.config; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.io.File; |
| 21 | import java.io.FileNotFoundException; |
| 22 | import java.io.IOException; |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 23 | import java.util.HashSet; |
| 24 | import java.util.Set; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 25 | |
| 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 31 | import org.onlab.onos.net.ConnectPoint; |
| 32 | import org.onlab.onos.net.DeviceId; |
| 33 | import org.onlab.onos.net.PortNumber; |
| 34 | import org.onlab.onos.net.host.HostAdminService; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 35 | import org.onlab.onos.net.host.InterfaceIpAddress; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 36 | import org.onlab.onos.net.host.PortAddresses; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 37 | import org.onlab.packet.IpAddress; |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 38 | import org.onlab.packet.IpPrefix; |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 39 | import org.onlab.packet.Ip4Prefix; |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 40 | import org.onlab.packet.MacAddress; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 41 | import org.slf4j.Logger; |
| 42 | |
Jonathan Hart | d7bd982 | 2014-10-20 18:18:02 -0700 | [diff] [blame] | 43 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 44 | |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Simple configuration module to read in supplementary network configuration |
| 47 | * from a file. |
| 48 | */ |
| 49 | @Component(immediate = true) |
| 50 | public class NetworkConfigReader { |
| 51 | |
| 52 | private final Logger log = getLogger(getClass()); |
| 53 | |
| 54 | private static final String DEFAULT_CONFIG_FILE = "config/addresses.json"; |
| 55 | private String configFileName = DEFAULT_CONFIG_FILE; |
| 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected HostAdminService hostAdminService; |
| 59 | |
| 60 | @Activate |
| 61 | protected void activate() { |
| 62 | log.info("Started network config reader"); |
| 63 | |
| 64 | log.info("Config file set to {}", configFileName); |
| 65 | |
| 66 | AddressConfiguration config = readNetworkConfig(); |
| 67 | |
| 68 | if (config != null) { |
| 69 | for (AddressEntry entry : config.getAddresses()) { |
| 70 | |
| 71 | ConnectPoint cp = new ConnectPoint( |
| 72 | DeviceId.deviceId(dpidToUri(entry.getDpid())), |
| 73 | PortNumber.portNumber(entry.getPortNumber())); |
| 74 | |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 75 | Set<InterfaceIpAddress> interfaceIpAddresses = new HashSet<>(); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 76 | |
| 77 | for (String strIp : entry.getIpAddresses()) { |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 78 | // Get the IP address and the subnet mask length |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 79 | try { |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 80 | String[] splits = strIp.split("/"); |
| 81 | if (splits.length != 2) { |
| 82 | throw new IllegalArgumentException("Invalid IP address and prefix length format"); |
| 83 | } |
| 84 | // |
| 85 | // TODO: For now we need Ip4Prefix to mask-out the |
| 86 | // subnet address. |
| 87 | // |
| 88 | Ip4Prefix subnet4 = new Ip4Prefix(strIp); |
| 89 | IpPrefix subnet = IpPrefix.valueOf(subnet4.toString()); |
| 90 | IpAddress addr = IpAddress.valueOf(splits[0]); |
| 91 | InterfaceIpAddress ia = |
| 92 | new InterfaceIpAddress(addr, subnet); |
| 93 | interfaceIpAddresses.add(ia); |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 94 | } catch (IllegalArgumentException e) { |
| 95 | log.warn("Bad format for IP address in config: {}", strIp); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | MacAddress macAddress = null; |
| 100 | if (entry.getMacAddress() != null) { |
| 101 | try { |
| 102 | macAddress = MacAddress.valueOf(entry.getMacAddress()); |
| 103 | } catch (IllegalArgumentException e) { |
| 104 | log.warn("Bad format for MAC address in config: {}", |
| 105 | entry.getMacAddress()); |
| 106 | } |
| 107 | } |
| 108 | |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 109 | PortAddresses addresses = new PortAddresses(cp, |
Pavlin Radoslavov | 76b0ae2 | 2014-10-27 15:33:19 -0700 | [diff] [blame] | 110 | interfaceIpAddresses, macAddress); |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 111 | |
| 112 | hostAdminService.bindAddressesToPort(addresses); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Deactivate |
| 118 | protected void deactivate() { |
| 119 | log.info("Stopped"); |
| 120 | } |
| 121 | |
| 122 | private AddressConfiguration readNetworkConfig() { |
| 123 | File configFile = new File(configFileName); |
| 124 | |
| 125 | ObjectMapper mapper = new ObjectMapper(); |
| 126 | |
| 127 | try { |
| 128 | AddressConfiguration config = |
| 129 | mapper.readValue(configFile, AddressConfiguration.class); |
| 130 | |
| 131 | return config; |
| 132 | } catch (FileNotFoundException e) { |
| 133 | log.warn("Configuration file not found: {}", configFileName); |
| 134 | } catch (IOException e) { |
| 135 | log.error("Unable to read config from file:", e); |
| 136 | } |
| 137 | |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | private static String dpidToUri(String dpid) { |
| 142 | return "of:" + dpid.replace(":", ""); |
| 143 | } |
| 144 | } |