blob: f015e362c84500f7b13882110007dbf7b659fd11 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070016package org.onlab.onos.config;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
20import java.io.File;
21import java.io.FileNotFoundException;
22import java.io.IOException;
Jonathan Hart70da5122014-10-01 16:37:42 -070023import java.util.HashSet;
24import java.util.Set;
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070025
26import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070031import org.onlab.onos.net.ConnectPoint;
32import org.onlab.onos.net.DeviceId;
33import org.onlab.onos.net.PortNumber;
34import org.onlab.onos.net.host.HostAdminService;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070035import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070036import org.onlab.onos.net.host.PortAddresses;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070037import org.onlab.packet.IpAddress;
Jonathan Hart70da5122014-10-01 16:37:42 -070038import org.onlab.packet.IpPrefix;
39import org.onlab.packet.MacAddress;
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070040import org.slf4j.Logger;
41
Jonathan Hartd7bd9822014-10-20 18:18:02 -070042import com.fasterxml.jackson.databind.ObjectMapper;
43
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070044/**
45 * Simple configuration module to read in supplementary network configuration
46 * from a file.
47 */
48@Component(immediate = true)
49public class NetworkConfigReader {
50
51 private final Logger log = getLogger(getClass());
52
53 private static final String DEFAULT_CONFIG_FILE = "config/addresses.json";
54 private String configFileName = DEFAULT_CONFIG_FILE;
55
56 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected HostAdminService hostAdminService;
58
59 @Activate
60 protected void activate() {
61 log.info("Started network config reader");
62
63 log.info("Config file set to {}", configFileName);
64
65 AddressConfiguration config = readNetworkConfig();
66
67 if (config != null) {
68 for (AddressEntry entry : config.getAddresses()) {
69
70 ConnectPoint cp = new ConnectPoint(
71 DeviceId.deviceId(dpidToUri(entry.getDpid())),
72 PortNumber.portNumber(entry.getPortNumber()));
73
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070074 Set<InterfaceIpAddress> interfaceIpAddresses = new HashSet<>();
Jonathan Hart70da5122014-10-01 16:37:42 -070075
76 for (String strIp : entry.getIpAddresses()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070077 // Get the IP address and the subnet mask length
Jonathan Hart70da5122014-10-01 16:37:42 -070078 try {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070079 String[] splits = strIp.split("/");
80 if (splits.length != 2) {
81 throw new IllegalArgumentException("Invalid IP address and prefix length format");
82 }
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070083 // NOTE: IpPrefix will mask-out the bits after the prefix length.
84 IpPrefix subnet = IpPrefix.valueOf(strIp);
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070085 IpAddress addr = IpAddress.valueOf(splits[0]);
86 InterfaceIpAddress ia =
87 new InterfaceIpAddress(addr, subnet);
88 interfaceIpAddresses.add(ia);
Jonathan Hart70da5122014-10-01 16:37:42 -070089 } catch (IllegalArgumentException e) {
90 log.warn("Bad format for IP address in config: {}", strIp);
91 }
92 }
93
94 MacAddress macAddress = null;
95 if (entry.getMacAddress() != null) {
96 try {
97 macAddress = MacAddress.valueOf(entry.getMacAddress());
98 } catch (IllegalArgumentException e) {
99 log.warn("Bad format for MAC address in config: {}",
100 entry.getMacAddress());
101 }
102 }
103
Jonathan Hart74f9c3b2014-09-29 20:03:50 -0700104 PortAddresses addresses = new PortAddresses(cp,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700105 interfaceIpAddresses, macAddress);
Jonathan Hart74f9c3b2014-09-29 20:03:50 -0700106
107 hostAdminService.bindAddressesToPort(addresses);
108 }
109 }
110 }
111
112 @Deactivate
113 protected void deactivate() {
114 log.info("Stopped");
115 }
116
117 private AddressConfiguration readNetworkConfig() {
118 File configFile = new File(configFileName);
119
120 ObjectMapper mapper = new ObjectMapper();
121
122 try {
123 AddressConfiguration config =
124 mapper.readValue(configFile, AddressConfiguration.class);
125
126 return config;
127 } catch (FileNotFoundException e) {
128 log.warn("Configuration file not found: {}", configFileName);
129 } catch (IOException e) {
130 log.error("Unable to read config from file:", e);
131 }
132
133 return null;
134 }
135
136 private static String dpidToUri(String dpid) {
137 return "of:" + dpid.replace(":", "");
138 }
139}