blob: b7ce1601afd3eb7938c3c0f68c9ec5d31105ba2a [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;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070039import org.onlab.packet.Ip4Prefix;
Jonathan Hart70da5122014-10-01 16:37:42 -070040import org.onlab.packet.MacAddress;
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070041import org.slf4j.Logger;
42
Jonathan Hartd7bd9822014-10-20 18:18:02 -070043import com.fasterxml.jackson.databind.ObjectMapper;
44
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070045/**
46 * Simple configuration module to read in supplementary network configuration
47 * from a file.
48 */
49@Component(immediate = true)
50public 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 Radoslavov76b0ae22014-10-27 15:33:19 -070075 Set<InterfaceIpAddress> interfaceIpAddresses = new HashSet<>();
Jonathan Hart70da5122014-10-01 16:37:42 -070076
77 for (String strIp : entry.getIpAddresses()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070078 // Get the IP address and the subnet mask length
Jonathan Hart70da5122014-10-01 16:37:42 -070079 try {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070080 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 Hart70da5122014-10-01 16:37:42 -070094 } 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 Hart74f9c3b2014-09-29 20:03:50 -0700109 PortAddresses addresses = new PortAddresses(cp,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700110 interfaceIpAddresses, macAddress);
Jonathan Hart74f9c3b2014-09-29 20:03:50 -0700111
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}