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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.config; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 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; |
Pavlin Radoslavov | 492cc3a | 2015-01-22 18:46:26 -0800 | [diff] [blame] | 31 | import org.apache.felix.scr.annotations.Service; |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 32 | import org.onlab.packet.IpAddress; |
| 33 | import org.onlab.packet.IpPrefix; |
| 34 | import org.onlab.packet.MacAddress; |
| 35 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.ConnectPoint; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.host.HostAdminService; |
| 40 | import org.onosproject.net.host.InterfaceIpAddress; |
| 41 | import org.onosproject.net.host.PortAddresses; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | |
Jonathan Hart | d7bd982 | 2014-10-20 18:18:02 -0700 | [diff] [blame] | 44 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 45 | |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Simple configuration module to read in supplementary network configuration |
| 48 | * from a file. |
| 49 | */ |
| 50 | @Component(immediate = true) |
Pavlin Radoslavov | 492cc3a | 2015-01-22 18:46:26 -0800 | [diff] [blame] | 51 | @Service |
| 52 | public class NetworkConfigReader implements NetworkConfigService { |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 56 | // Current working dir seems to be /opt/onos/apache-karaf-3.0.2 |
| 57 | // TODO: Set the path to /opt/onos/config |
| 58 | private static final String CONFIG_DIR = "../config"; |
| 59 | private static final String DEFAULT_CONFIG_FILE = "addresses.json"; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 60 | private String configFileName = DEFAULT_CONFIG_FILE; |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected HostAdminService hostAdminService; |
| 64 | |
| 65 | @Activate |
| 66 | protected void activate() { |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 67 | AddressConfiguration config = readNetworkConfig(); |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 68 | if (config != null) { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 69 | applyNetworkConfig(config); |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 70 | } |
Saurav Das | fbe25c5 | 2015-03-04 11:12:00 -0800 | [diff] [blame] | 71 | log.info("Started network config reader"); |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Deactivate |
| 75 | protected void deactivate() { |
| 76 | log.info("Stopped"); |
| 77 | } |
| 78 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 79 | /** |
| 80 | * Reads the network configuration. |
| 81 | * |
| 82 | * @return the network configuration on success, otherwise null |
| 83 | */ |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 84 | private AddressConfiguration readNetworkConfig() { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 85 | File configFile = new File(CONFIG_DIR, configFileName); |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 86 | ObjectMapper mapper = new ObjectMapper(); |
| 87 | |
| 88 | try { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 89 | log.info("Loading config: {}", configFile.getAbsolutePath()); |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 90 | AddressConfiguration config = |
| 91 | mapper.readValue(configFile, AddressConfiguration.class); |
| 92 | |
| 93 | return config; |
| 94 | } catch (FileNotFoundException e) { |
| 95 | log.warn("Configuration file not found: {}", configFileName); |
| 96 | } catch (IOException e) { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 97 | log.error("Error loading configuration", e); |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return null; |
| 101 | } |
| 102 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 103 | /** |
| 104 | * Applies the network configuration. |
| 105 | * |
| 106 | * @param config the network configuration to apply |
| 107 | */ |
| 108 | private void applyNetworkConfig(AddressConfiguration config) { |
| 109 | for (AddressEntry entry : config.getAddresses()) { |
| 110 | ConnectPoint cp = new ConnectPoint( |
| 111 | DeviceId.deviceId(dpidToUri(entry.getDpid())), |
| 112 | PortNumber.portNumber(entry.getPortNumber())); |
| 113 | |
| 114 | Set<InterfaceIpAddress> interfaceIpAddresses = new HashSet<>(); |
| 115 | for (String strIp : entry.getIpAddresses()) { |
| 116 | // Get the IP address and the subnet mask length |
| 117 | try { |
| 118 | String[] splits = strIp.split("/"); |
| 119 | if (splits.length != 2) { |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 120 | throw new IllegalArgumentException( |
| 121 | "Invalid IP address and prefix length format"); |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 122 | } |
| 123 | // NOTE: IpPrefix will mask-out the bits after the prefix length. |
| 124 | IpPrefix subnet = IpPrefix.valueOf(strIp); |
| 125 | IpAddress addr = IpAddress.valueOf(splits[0]); |
| 126 | InterfaceIpAddress ia = |
| 127 | new InterfaceIpAddress(addr, subnet); |
| 128 | interfaceIpAddresses.add(ia); |
| 129 | } catch (IllegalArgumentException e) { |
| 130 | log.warn("Bad format for IP address in config: {}", strIp); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | MacAddress macAddress = null; |
| 135 | if (entry.getMacAddress() != null) { |
| 136 | try { |
| 137 | macAddress = MacAddress.valueOf(entry.getMacAddress()); |
| 138 | } catch (IllegalArgumentException e) { |
| 139 | log.warn("Bad format for MAC address in config: {}", |
| 140 | entry.getMacAddress()); |
| 141 | } |
| 142 | } |
| 143 | |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 144 | VlanId vlan = null; |
| 145 | if (entry.getVlan() == null) { |
| 146 | vlan = VlanId.NONE; |
| 147 | } else { |
| 148 | try { |
| 149 | vlan = VlanId.vlanId(entry.getVlan()); |
| 150 | } catch (IllegalArgumentException e) { |
| 151 | log.warn("Bad format for VLAN id in config: {}", |
| 152 | entry.getVlan()); |
| 153 | vlan = VlanId.NONE; |
| 154 | } |
| 155 | } |
| 156 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 157 | PortAddresses addresses = new PortAddresses(cp, |
Jonathan Hart | 6cd2f35 | 2015-01-13 17:44:45 -0800 | [diff] [blame] | 158 | interfaceIpAddresses, macAddress, vlan); |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 159 | hostAdminService.bindAddressesToPort(addresses); |
| 160 | } |
| 161 | } |
| 162 | |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 163 | private static String dpidToUri(String dpid) { |
| 164 | return "of:" + dpid.replace(":", ""); |
| 165 | } |
| 166 | } |