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