Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 19 | package org.onlab.onos.config; |
| 20 | |
| 21 | import static org.slf4j.LoggerFactory.getLogger; |
| 22 | |
| 23 | import java.io.File; |
| 24 | import java.io.FileNotFoundException; |
| 25 | import java.io.IOException; |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 26 | import java.util.HashSet; |
| 27 | import java.util.Set; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 28 | |
| 29 | import org.apache.felix.scr.annotations.Activate; |
| 30 | import org.apache.felix.scr.annotations.Component; |
| 31 | import org.apache.felix.scr.annotations.Deactivate; |
| 32 | import org.apache.felix.scr.annotations.Reference; |
| 33 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 34 | import org.onlab.onos.net.ConnectPoint; |
| 35 | import org.onlab.onos.net.DeviceId; |
| 36 | import org.onlab.onos.net.PortNumber; |
| 37 | import org.onlab.onos.net.host.HostAdminService; |
| 38 | import org.onlab.onos.net.host.PortAddresses; |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 39 | import org.onlab.packet.IpPrefix; |
| 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 | |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 75 | Set<IpPrefix> ipAddresses = new HashSet<IpPrefix>(); |
| 76 | |
| 77 | for (String strIp : entry.getIpAddresses()) { |
| 78 | try { |
| 79 | IpPrefix address = IpPrefix.valueOf(strIp); |
| 80 | ipAddresses.add(address); |
| 81 | } catch (IllegalArgumentException e) { |
| 82 | log.warn("Bad format for IP address in config: {}", strIp); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | MacAddress macAddress = null; |
| 87 | if (entry.getMacAddress() != null) { |
| 88 | try { |
| 89 | macAddress = MacAddress.valueOf(entry.getMacAddress()); |
| 90 | } catch (IllegalArgumentException e) { |
| 91 | log.warn("Bad format for MAC address in config: {}", |
| 92 | entry.getMacAddress()); |
| 93 | } |
| 94 | } |
| 95 | |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 96 | PortAddresses addresses = new PortAddresses(cp, |
Jonathan Hart | 70da512 | 2014-10-01 16:37:42 -0700 | [diff] [blame] | 97 | ipAddresses, macAddress); |
Jonathan Hart | 74f9c3b | 2014-09-29 20:03:50 -0700 | [diff] [blame] | 98 | |
| 99 | hostAdminService.bindAddressesToPort(addresses); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | @Deactivate |
| 105 | protected void deactivate() { |
| 106 | log.info("Stopped"); |
| 107 | } |
| 108 | |
| 109 | private AddressConfiguration readNetworkConfig() { |
| 110 | File configFile = new File(configFileName); |
| 111 | |
| 112 | ObjectMapper mapper = new ObjectMapper(); |
| 113 | |
| 114 | try { |
| 115 | AddressConfiguration config = |
| 116 | mapper.readValue(configFile, AddressConfiguration.class); |
| 117 | |
| 118 | return config; |
| 119 | } catch (FileNotFoundException e) { |
| 120 | log.warn("Configuration file not found: {}", configFileName); |
| 121 | } catch (IOException e) { |
| 122 | log.error("Unable to read config from file:", e); |
| 123 | } |
| 124 | |
| 125 | return null; |
| 126 | } |
| 127 | |
| 128 | private static String dpidToUri(String dpid) { |
| 129 | return "of:" + dpid.replace(":", ""); |
| 130 | } |
| 131 | } |