blob: a99fb53fa14b78b646bd11b04eaba042a1d3ef63 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
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 Hart74f9c3b2014-09-29 20:03:50 -070019package org.onlab.onos.config;
20
21import static org.slf4j.LoggerFactory.getLogger;
22
23import java.io.File;
24import java.io.FileNotFoundException;
25import java.io.IOException;
Jonathan Hart70da5122014-10-01 16:37:42 -070026import java.util.HashSet;
27import java.util.Set;
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070028
29import org.apache.felix.scr.annotations.Activate;
30import org.apache.felix.scr.annotations.Component;
31import org.apache.felix.scr.annotations.Deactivate;
32import org.apache.felix.scr.annotations.Reference;
33import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070034import org.onlab.onos.net.ConnectPoint;
35import org.onlab.onos.net.DeviceId;
36import org.onlab.onos.net.PortNumber;
37import org.onlab.onos.net.host.HostAdminService;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070038import org.onlab.onos.net.host.InterfaceIpAddress;
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070039import org.onlab.onos.net.host.PortAddresses;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070040import org.onlab.packet.IpAddress;
Jonathan Hart70da5122014-10-01 16:37:42 -070041import org.onlab.packet.IpPrefix;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070042import org.onlab.packet.Ip4Prefix;
Jonathan Hart70da5122014-10-01 16:37:42 -070043import org.onlab.packet.MacAddress;
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070044import org.slf4j.Logger;
45
Jonathan Hartd7bd9822014-10-20 18:18:02 -070046import com.fasterxml.jackson.databind.ObjectMapper;
47
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070048/**
49 * Simple configuration module to read in supplementary network configuration
50 * from a file.
51 */
52@Component(immediate = true)
53public class NetworkConfigReader {
54
55 private final Logger log = getLogger(getClass());
56
57 private static final String DEFAULT_CONFIG_FILE = "config/addresses.json";
58 private String configFileName = DEFAULT_CONFIG_FILE;
59
60 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
61 protected HostAdminService hostAdminService;
62
63 @Activate
64 protected void activate() {
65 log.info("Started network config reader");
66
67 log.info("Config file set to {}", configFileName);
68
69 AddressConfiguration config = readNetworkConfig();
70
71 if (config != null) {
72 for (AddressEntry entry : config.getAddresses()) {
73
74 ConnectPoint cp = new ConnectPoint(
75 DeviceId.deviceId(dpidToUri(entry.getDpid())),
76 PortNumber.portNumber(entry.getPortNumber()));
77
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070078 Set<InterfaceIpAddress> interfaceIpAddresses = new HashSet<>();
Jonathan Hart70da5122014-10-01 16:37:42 -070079
80 for (String strIp : entry.getIpAddresses()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070081 // Get the IP address and the subnet mask length
Jonathan Hart70da5122014-10-01 16:37:42 -070082 try {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -070083 String[] splits = strIp.split("/");
84 if (splits.length != 2) {
85 throw new IllegalArgumentException("Invalid IP address and prefix length format");
86 }
87 //
88 // TODO: For now we need Ip4Prefix to mask-out the
89 // subnet address.
90 //
91 Ip4Prefix subnet4 = new Ip4Prefix(strIp);
92 IpPrefix subnet = IpPrefix.valueOf(subnet4.toString());
93 IpAddress addr = IpAddress.valueOf(splits[0]);
94 InterfaceIpAddress ia =
95 new InterfaceIpAddress(addr, subnet);
96 interfaceIpAddresses.add(ia);
Jonathan Hart70da5122014-10-01 16:37:42 -070097 } catch (IllegalArgumentException e) {
98 log.warn("Bad format for IP address in config: {}", strIp);
99 }
100 }
101
102 MacAddress macAddress = null;
103 if (entry.getMacAddress() != null) {
104 try {
105 macAddress = MacAddress.valueOf(entry.getMacAddress());
106 } catch (IllegalArgumentException e) {
107 log.warn("Bad format for MAC address in config: {}",
108 entry.getMacAddress());
109 }
110 }
111
Jonathan Hart74f9c3b2014-09-29 20:03:50 -0700112 PortAddresses addresses = new PortAddresses(cp,
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700113 interfaceIpAddresses, macAddress);
Jonathan Hart74f9c3b2014-09-29 20:03:50 -0700114
115 hostAdminService.bindAddressesToPort(addresses);
116 }
117 }
118 }
119
120 @Deactivate
121 protected void deactivate() {
122 log.info("Stopped");
123 }
124
125 private AddressConfiguration readNetworkConfig() {
126 File configFile = new File(configFileName);
127
128 ObjectMapper mapper = new ObjectMapper();
129
130 try {
131 AddressConfiguration config =
132 mapper.readValue(configFile, AddressConfiguration.class);
133
134 return config;
135 } catch (FileNotFoundException e) {
136 log.warn("Configuration file not found: {}", configFileName);
137 } catch (IOException e) {
138 log.error("Unable to read config from file:", e);
139 }
140
141 return null;
142 }
143
144 private static String dpidToUri(String dpid) {
145 return "of:" + dpid.replace(":", "");
146 }
147}