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 | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame^] | 16 | package org.onosproject.routing.config; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.host.HostService; |
| 27 | import org.onosproject.routingapi.config.BgpPeer; |
| 28 | import org.onosproject.routingapi.config.BgpSpeaker; |
| 29 | import org.onosproject.routingapi.config.Interface; |
| 30 | import org.onosproject.routingapi.config.RoutingConfigurationService; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 33 | |
| 34 | import java.io.File; |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 35 | import java.io.FileNotFoundException; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 36 | import java.io.IOException; |
| 37 | import java.util.Collections; |
| 38 | import java.util.Map; |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame^] | 39 | import java.util.Set; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 40 | import java.util.concurrent.ConcurrentHashMap; |
| 41 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 42 | /** |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame^] | 43 | * Implementation of RoutingConfigurationService which reads routing |
| 44 | * configuration from a file. |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 45 | */ |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame^] | 46 | @Component(immediate = true) |
| 47 | @Service |
| 48 | public class RoutingConfigurationImpl implements RoutingConfigurationService { |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 49 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 50 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 51 | |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 52 | private static final String CONFIG_DIR = "../config"; |
| 53 | private static final String DEFAULT_CONFIG_FILE = "sdnip.json"; |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 54 | private String configFileName = DEFAULT_CONFIG_FILE; |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 55 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame^] | 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected HostService hostService; |
| 58 | |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 59 | private Map<String, BgpSpeaker> bgpSpeakers = new ConcurrentHashMap<>(); |
| 60 | private Map<IpAddress, BgpPeer> bgpPeers = new ConcurrentHashMap<>(); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 61 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame^] | 62 | private HostToInterfaceAdaptor hostAdaptor; |
| 63 | |
| 64 | @Activate |
| 65 | public void activate() { |
| 66 | readConfiguration(); |
| 67 | hostAdaptor = new HostToInterfaceAdaptor(hostService); |
| 68 | log.info("Routing configuration service started"); |
| 69 | } |
| 70 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 71 | /** |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 72 | * Reads SDN-IP related information contained in the configuration file. |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 73 | * |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 74 | * @param configFilename the name of the configuration file for the SDN-IP |
| 75 | * application |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 76 | */ |
| 77 | private void readConfiguration(String configFilename) { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 78 | File configFile = new File(CONFIG_DIR, configFilename); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 79 | ObjectMapper mapper = new ObjectMapper(); |
| 80 | |
| 81 | try { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 82 | log.info("Loading config: {}", configFile.getAbsolutePath()); |
| 83 | Configuration config = mapper.readValue(configFile, |
| 84 | Configuration.class); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 85 | for (BgpSpeaker speaker : config.getBgpSpeakers()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 86 | bgpSpeakers.put(speaker.name(), speaker); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 87 | } |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 88 | for (BgpPeer peer : config.getPeers()) { |
Jonathan Hart | dc711bd | 2014-10-15 11:24:23 -0700 | [diff] [blame] | 89 | bgpPeers.put(peer.ipAddress(), peer); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 90 | } |
Jonathan Hart | 739c835 | 2014-10-29 17:49:26 -0700 | [diff] [blame] | 91 | } catch (FileNotFoundException e) { |
| 92 | log.warn("Configuration file not found: {}", configFileName); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 93 | } catch (IOException e) { |
Pavlin Radoslavov | 0a297b1 | 2014-11-11 16:03:27 -0800 | [diff] [blame] | 94 | log.error("Error loading configuration", e); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 95 | } |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Jonathan Hart | 9965d77 | 2014-12-02 10:28:34 -0800 | [diff] [blame] | 98 | /** |
| 99 | * Instructs the configuration reader to read the configuration from the file. |
| 100 | */ |
| 101 | public void readConfiguration() { |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 102 | readConfiguration(configFileName); |
| 103 | } |
| 104 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 105 | @Override |
| 106 | public Map<String, BgpSpeaker> getBgpSpeakers() { |
| 107 | return Collections.unmodifiableMap(bgpSpeakers); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Map<IpAddress, BgpPeer> getBgpPeers() { |
| 112 | return Collections.unmodifiableMap(bgpPeers); |
| 113 | } |
| 114 | |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame^] | 115 | @Override |
| 116 | public Set<Interface> getInterfaces() { |
| 117 | return hostAdaptor.getInterfaces(); |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 118 | } |
Jonathan Hart | 90a02c2 | 2015-02-13 11:52:07 -0800 | [diff] [blame^] | 119 | |
| 120 | @Override |
| 121 | public Interface getInterface(ConnectPoint connectPoint) { |
| 122 | return hostAdaptor.getInterface(connectPoint); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public Interface getMatchingInterface(IpAddress ipAddress) { |
| 127 | return hostAdaptor.getMatchingInterface(ipAddress); |
| 128 | } |
| 129 | |
Jonathan Hart | bac07a0 | 2014-10-13 21:29:54 -0700 | [diff] [blame] | 130 | } |