blob: 8b1e1ce59602240781c17e59d181c913125383d6 [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;
38import org.onlab.onos.net.host.PortAddresses;
Jonathan Hart70da5122014-10-01 16:37:42 -070039import org.onlab.packet.IpPrefix;
40import 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
Jonathan Hart70da5122014-10-01 16:37:42 -070075 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 Hart74f9c3b2014-09-29 20:03:50 -070096 PortAddresses addresses = new PortAddresses(cp,
Jonathan Hart70da5122014-10-01 16:37:42 -070097 ipAddresses, macAddress);
Jonathan Hart74f9c3b2014-09-29 20:03:50 -070098
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}