blob: cde65c62bdf1ad5b41d1e8dfd822e6f1d6d20969 [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 Hartbac07a02014-10-13 21:29:54 -070016package org.onlab.onos.sdnip.config;
17
18import java.io.File;
19import java.io.IOException;
20import java.util.Collections;
21import java.util.Map;
22import java.util.concurrent.ConcurrentHashMap;
23
Jonathan Hartbac07a02014-10-13 21:29:54 -070024import org.onlab.packet.IpAddress;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27
Jonathan Hartd7bd9822014-10-20 18:18:02 -070028import com.fasterxml.jackson.databind.ObjectMapper;
29
Thomas Vachuska4b420772014-10-30 16:46:17 -070030// TODO: As a long term solution, a module providing general network configuration to ONOS nodes should be used.
31
Jonathan Hartbac07a02014-10-13 21:29:54 -070032/**
Thomas Vachuska4b420772014-10-30 16:46:17 -070033 * SDN-IP Config Reader provides IConfigInfoService by reading from an
34 * SDN-IP configuration file. It must be enabled on the nodes within the cluster
Jonathan Hartbac07a02014-10-13 21:29:54 -070035 * not running SDN-IP.
Jonathan Hartbac07a02014-10-13 21:29:54 -070036 */
37public class SdnIpConfigReader implements SdnIpConfigService {
38
39 private static final Logger log = LoggerFactory.getLogger(SdnIpConfigReader.class);
40
41 private static final String DEFAULT_CONFIG_FILE = "config/sdnip.json";
42 private String configFileName = DEFAULT_CONFIG_FILE;
43 //private Map<String, Interface> interfaces;
44 // We call the BGP routers in our SDN network the BGP speakers, and call
45 // the BGP routers outside our SDN network the BGP peers.
46 private Map<String, BgpSpeaker> bgpSpeakers;
47 private Map<IpAddress, BgpPeer> bgpPeers;
48 //private InvertedRadixTree<Interface> interfaceRoutes;
49
50 /**
51 * Reads the info contained in the configuration file.
52 *
53 * @param configFilename The name of configuration file for SDN-IP application.
54 */
55 private void readConfiguration(String configFilename) {
56 File gatewaysFile = new File(configFilename);
57 ObjectMapper mapper = new ObjectMapper();
58
59 try {
60 Configuration config = mapper.readValue(gatewaysFile, Configuration.class);
61 /*interfaces = new ConcurrentHashMap<>();
62 for (Interface intf : config.getInterfaces()) {
63 interfaces.put(intf.getName(), intf);
64 }*/
65 bgpSpeakers = new ConcurrentHashMap<>();
66 for (BgpSpeaker speaker : config.getBgpSpeakers()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070067 bgpSpeakers.put(speaker.name(), speaker);
Jonathan Hartbac07a02014-10-13 21:29:54 -070068 }
69 bgpPeers = new ConcurrentHashMap<>();
70 for (BgpPeer peer : config.getPeers()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070071 bgpPeers.put(peer.ipAddress(), peer);
Jonathan Hartbac07a02014-10-13 21:29:54 -070072 }
73 } catch (IOException e) {
74 log.error("Error reading JSON file", e);
75 //throw new ConfigurationRuntimeException("Error in JSON file", e);
76 }
77
78 // Populate the interface InvertedRadixTree
79 /*for (Interface intf : interfaces.values()) {
80 Ip4Prefix prefix = intf.getIp4Prefix();
81 String binaryString = RouteEntry.createBinaryString(prefix);
82 interfaceRoutes.put(binaryString, intf);
83 }*/
84 }
85
Thomas Vachuska4b420772014-10-30 16:46:17 -070086 /*
Jonathan Hartbac07a02014-10-13 21:29:54 -070087 * To find the Interface which has longest matchable IP prefix (sub-network
88 * prefix) to next hop IP address.
89 *
90 * @param address the IP address of next hop router
91 * @return the Interface which has longest matchable IP prefix
92 */
93 /*private Interface longestInterfacePrefixMatch(IpAddress address) {
94 Ip4Prefix prefixToSearchFor =
95 new Ip4Prefix(address, (short) Ip4Address.BIT_LENGTH);
96 String binaryString = RouteEntry.createBinaryString(prefixToSearchFor);
97
98 Iterator<Interface> it =
99 interfaceRoutes.getValuesForKeysPrefixing(binaryString).iterator();
100 Interface intf = null;
101 // Find the last prefix, which will be the longest prefix
102 while (it.hasNext()) {
103 intf = it.next();
104 }
105
106 return intf;
107 }*/
108
109 /*@Override
110 public Interface getOutgoingInterface(IpAddress dstIpAddress) {
111 return longestInterfacePrefixMatch(dstIpAddress);
112 }*/
113
114 public void init() {
115 //interfaceRoutes = new ConcurrentInvertedRadixTree<>(
116 //new DefaultByteArrayNodeFactory());
117
118 // Reading config values
119 /*String configFilenameParameter = context.getConfigParams(this).get("configfile");
120 if (configFilenameParameter != null) {
121 currentConfigFilename = configFilenameParameter;
122 }*/
123 log.debug("Config file set to {}", configFileName);
124
125 readConfiguration(configFileName);
126 }
127
128 /*@Override
129 public Map<String, Interface> getInterfaces() {
130 return Collections.unmodifiableMap(interfaces);
131 }*/
132
133 @Override
134 public Map<String, BgpSpeaker> getBgpSpeakers() {
135 return Collections.unmodifiableMap(bgpSpeakers);
136 }
137
138 @Override
139 public Map<IpAddress, BgpPeer> getBgpPeers() {
140 return Collections.unmodifiableMap(bgpPeers);
141 }
142
143 static String dpidToUri(String dpid) {
144 return "of:" + dpid.replace(":", "");
145 }
146}