blob: 846e83ce64bca716c157283e52de6356a8ef15d1 [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
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080053 // Current working dir seems to be /opt/onos/apache-karaf-3.0.2
54 // TODO: Set the path to /opt/onos/config
55 private static final String CONFIG_DIR = "../config";
56 private static final String DEFAULT_CONFIG_FILE = "addresses.json";
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070057 private String configFileName = DEFAULT_CONFIG_FILE;
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected HostAdminService hostAdminService;
61
62 @Activate
63 protected void activate() {
64 log.info("Started network config reader");
65
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070066 AddressConfiguration config = readNetworkConfig();
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070067 if (config != null) {
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080068 applyNetworkConfig(config);
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070069 }
70 }
71
72 @Deactivate
73 protected void deactivate() {
74 log.info("Stopped");
75 }
76
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080077 /**
78 * Reads the network configuration.
79 *
80 * @return the network configuration on success, otherwise null
81 */
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070082 private AddressConfiguration readNetworkConfig() {
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080083 File configFile = new File(CONFIG_DIR, configFileName);
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070084 ObjectMapper mapper = new ObjectMapper();
85
86 try {
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080087 log.info("Loading config: {}", configFile.getAbsolutePath());
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070088 AddressConfiguration config =
89 mapper.readValue(configFile, AddressConfiguration.class);
90
91 return config;
92 } catch (FileNotFoundException e) {
93 log.warn("Configuration file not found: {}", configFileName);
94 } catch (IOException e) {
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080095 log.error("Error loading configuration", e);
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070096 }
97
98 return null;
99 }
100
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -0800101 /**
102 * Applies the network configuration.
103 *
104 * @param config the network configuration to apply
105 */
106 private void applyNetworkConfig(AddressConfiguration config) {
107 for (AddressEntry entry : config.getAddresses()) {
108 ConnectPoint cp = new ConnectPoint(
109 DeviceId.deviceId(dpidToUri(entry.getDpid())),
110 PortNumber.portNumber(entry.getPortNumber()));
111
112 Set<InterfaceIpAddress> interfaceIpAddresses = new HashSet<>();
113 for (String strIp : entry.getIpAddresses()) {
114 // Get the IP address and the subnet mask length
115 try {
116 String[] splits = strIp.split("/");
117 if (splits.length != 2) {
118 throw new IllegalArgumentException("Invalid IP address and prefix length format");
119 }
120 // NOTE: IpPrefix will mask-out the bits after the prefix length.
121 IpPrefix subnet = IpPrefix.valueOf(strIp);
122 IpAddress addr = IpAddress.valueOf(splits[0]);
123 InterfaceIpAddress ia =
124 new InterfaceIpAddress(addr, subnet);
125 interfaceIpAddresses.add(ia);
126 } catch (IllegalArgumentException e) {
127 log.warn("Bad format for IP address in config: {}", strIp);
128 }
129 }
130
131 MacAddress macAddress = null;
132 if (entry.getMacAddress() != null) {
133 try {
134 macAddress = MacAddress.valueOf(entry.getMacAddress());
135 } catch (IllegalArgumentException e) {
136 log.warn("Bad format for MAC address in config: {}",
137 entry.getMacAddress());
138 }
139 }
140
141 PortAddresses addresses = new PortAddresses(cp,
142 interfaceIpAddresses, macAddress);
143 hostAdminService.bindAddressesToPort(addresses);
144 }
145 }
146
Jonathan Hart74f9c3b2014-09-29 20:03:50 -0700147 private static String dpidToUri(String dpid) {
148 return "of:" + dpid.replace(":", "");
149 }
150}