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 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 31 | /** |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame] | 32 | * Implementation of SdnIpConfigurationService which reads SDN-IP configuration |
| 33 | * from a file. |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 34 | */ |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame] | 35 | public class SdnIpConfigurationReader implements SdnIpConfigurationService { |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 36 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 37 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 38 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 39 | // Current working dir seems to be /opt/onos/apache-karaf-3.0.2 |
| 40 | // TODO: Set the path to /opt/onos/config |
| 41 | private static final String CONFIG_DIR = "../config"; |
| 42 | private static final String DEFAULT_CONFIG_FILE = "sdnip.json"; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 43 | private String configFileName = DEFAULT_CONFIG_FILE; |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 44 | |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 45 | private Map<String, BgpSpeaker> bgpSpeakers = new ConcurrentHashMap<>(); |
| 46 | private Map<IpAddress, BgpPeer> bgpPeers = new ConcurrentHashMap<>(); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 49 | * Reads SDN-IP related information contained in the configuration file. |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 50 | * |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 51 | * @param configFilename the name of the configuration file for the SDN-IP |
| 52 | * application |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 53 | */ |
| 54 | private void readConfiguration(String configFilename) { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 55 | File configFile = new File(CONFIG_DIR, configFilename); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 56 | ObjectMapper mapper = new ObjectMapper(); |
| 57 | |
| 58 | try { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 59 | log.info("Loading config: {}", configFile.getAbsolutePath()); |
| 60 | Configuration config = mapper.readValue(configFile, |
| 61 | Configuration.class); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 62 | for (BgpSpeaker speaker : config.getBgpSpeakers()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 63 | bgpSpeakers.put(speaker.name(), speaker); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 64 | } |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 65 | for (BgpPeer peer : config.getPeers()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 66 | bgpPeers.put(peer.ipAddress(), peer); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 67 | } |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 68 | } catch (FileNotFoundException e) { |
| 69 | log.warn("Configuration file not found: {}", configFileName); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 70 | } catch (IOException e) { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 71 | log.error("Error loading configuration", e); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 72 | } |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame] | 75 | /** |
| 76 | * Instructs the configuration reader to read the configuration from the file. |
| 77 | */ |
| 78 | public void readConfiguration() { |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 79 | readConfiguration(configFileName); |
| 80 | } |
| 81 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 82 | @Override |
| 83 | public Map<String, BgpSpeaker> getBgpSpeakers() { |
| 84 | return Collections.unmodifiableMap(bgpSpeakers); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public Map<IpAddress, BgpPeer> getBgpPeers() { |
| 89 | return Collections.unmodifiableMap(bgpPeers); |
| 90 | } |
| 91 | |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Converts DPIDs of the form xx:xx:xx:xx:xx:xx:xx to OpenFlow provider |
| 94 | * device URIs. |
| 95 | * |
| 96 | * @param dpid the DPID string to convert |
| 97 | * @return the URI string for this device |
| 98 | */ |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 99 | static String dpidToUri(String dpid) { |
| 100 | return "of:" + dpid.replace(":", ""); |
| 101 | } |
| 102 | } |