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