Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 16 | package org.onlab.onos.sdnip.config; |
| 17 | |
| 18 | import java.io.File; |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 19 | import java.io.FileNotFoundException; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 20 | import java.io.IOException; |
| 21 | import java.util.Collections; |
| 22 | import java.util.Map; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
Jonathan Hart | d7bd982 | 2014-10-20 18:18:02 -0700 | [diff] [blame] | 29 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 30 | |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 31 | // TODO: As a long term solution, a module providing general network configuration to ONOS nodes should be used. |
| 32 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 33 | /** |
Thomas Vachuska | 4b42077 | 2014-10-30 16:46:17 -0700 | [diff] [blame] | 34 | * SDN-IP Config Reader provides IConfigInfoService by reading from an |
| 35 | * SDN-IP configuration file. It must be enabled on the nodes within the cluster |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 36 | * not running SDN-IP. |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 37 | */ |
| 38 | public class SdnIpConfigReader implements SdnIpConfigService { |
| 39 | |
| 40 | private static final Logger log = LoggerFactory.getLogger(SdnIpConfigReader.class); |
| 41 | |
| 42 | private static final String DEFAULT_CONFIG_FILE = "config/sdnip.json"; |
| 43 | private String configFileName = DEFAULT_CONFIG_FILE; |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 44 | private Map<String, BgpSpeaker> bgpSpeakers = new ConcurrentHashMap<>(); |
| 45 | private Map<IpAddress, BgpPeer> bgpPeers = new ConcurrentHashMap<>(); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Reads the info contained in the configuration file. |
| 49 | * |
| 50 | * @param configFilename The name of configuration file for SDN-IP application. |
| 51 | */ |
| 52 | private void readConfiguration(String configFilename) { |
| 53 | File gatewaysFile = new File(configFilename); |
| 54 | ObjectMapper mapper = new ObjectMapper(); |
| 55 | |
| 56 | try { |
| 57 | Configuration config = mapper.readValue(gatewaysFile, Configuration.class); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 58 | for (BgpSpeaker speaker : config.getBgpSpeakers()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 59 | bgpSpeakers.put(speaker.name(), speaker); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 60 | } |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 61 | for (BgpPeer peer : config.getPeers()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 62 | bgpPeers.put(peer.ipAddress(), peer); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 63 | } |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 64 | } catch (FileNotFoundException e) { |
| 65 | log.warn("Configuration file not found: {}", configFileName); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 66 | } catch (IOException e) { |
| 67 | log.error("Error reading JSON file", e); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 68 | } |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 71 | public void init() { |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 72 | log.debug("Config file set to {}", configFileName); |
| 73 | |
| 74 | readConfiguration(configFileName); |
| 75 | } |
| 76 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 77 | @Override |
| 78 | public Map<String, BgpSpeaker> getBgpSpeakers() { |
| 79 | return Collections.unmodifiableMap(bgpSpeakers); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public Map<IpAddress, BgpPeer> getBgpPeers() { |
| 84 | return Collections.unmodifiableMap(bgpPeers); |
| 85 | } |
| 86 | |
| 87 | static String dpidToUri(String dpid) { |
| 88 | return "of:" + dpid.replace(":", ""); |
| 89 | } |
| 90 | } |