blob: 90ea14c6c2bf072725e659d11a5849fd0c91e93e [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;
Jonathan Hart739c8352014-10-29 17:49:26 -070019import java.io.FileNotFoundException;
Jonathan Hartbac07a02014-10-13 21:29:54 -070020import java.io.IOException;
21import java.util.Collections;
22import java.util.Map;
23import java.util.concurrent.ConcurrentHashMap;
24
Jonathan Hartbac07a02014-10-13 21:29:54 -070025import org.onlab.packet.IpAddress;
26import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
Jonathan Hartd7bd9822014-10-20 18:18:02 -070029import com.fasterxml.jackson.databind.ObjectMapper;
30
Jonathan Hartbac07a02014-10-13 21:29:54 -070031/**
Jonathan Hart9965d772014-12-02 10:28:34 -080032 * Implementation of SdnIpConfigurationService which reads SDN-IP configuration
33 * from a file.
Jonathan Hartbac07a02014-10-13 21:29:54 -070034 */
Jonathan Hart9965d772014-12-02 10:28:34 -080035public class SdnIpConfigurationReader implements SdnIpConfigurationService {
Jonathan Hartbac07a02014-10-13 21:29:54 -070036
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080037 private final Logger log = LoggerFactory.getLogger(getClass());
Jonathan Hartbac07a02014-10-13 21:29:54 -070038
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080039 // 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 Hartbac07a02014-10-13 21:29:54 -070043 private String configFileName = DEFAULT_CONFIG_FILE;
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080044
Jonathan Hart739c8352014-10-29 17:49:26 -070045 private Map<String, BgpSpeaker> bgpSpeakers = new ConcurrentHashMap<>();
46 private Map<IpAddress, BgpPeer> bgpPeers = new ConcurrentHashMap<>();
Jonathan Hartbac07a02014-10-13 21:29:54 -070047
48 /**
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080049 * Reads SDN-IP related information contained in the configuration file.
Jonathan Hartbac07a02014-10-13 21:29:54 -070050 *
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080051 * @param configFilename the name of the configuration file for the SDN-IP
52 * application
Jonathan Hartbac07a02014-10-13 21:29:54 -070053 */
54 private void readConfiguration(String configFilename) {
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080055 File configFile = new File(CONFIG_DIR, configFilename);
Jonathan Hartbac07a02014-10-13 21:29:54 -070056 ObjectMapper mapper = new ObjectMapper();
57
58 try {
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080059 log.info("Loading config: {}", configFile.getAbsolutePath());
60 Configuration config = mapper.readValue(configFile,
61 Configuration.class);
Jonathan Hartbac07a02014-10-13 21:29:54 -070062 for (BgpSpeaker speaker : config.getBgpSpeakers()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070063 bgpSpeakers.put(speaker.name(), speaker);
Jonathan Hartbac07a02014-10-13 21:29:54 -070064 }
Jonathan Hartbac07a02014-10-13 21:29:54 -070065 for (BgpPeer peer : config.getPeers()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070066 bgpPeers.put(peer.ipAddress(), peer);
Jonathan Hartbac07a02014-10-13 21:29:54 -070067 }
Jonathan Hart739c8352014-10-29 17:49:26 -070068 } catch (FileNotFoundException e) {
69 log.warn("Configuration file not found: {}", configFileName);
Jonathan Hartbac07a02014-10-13 21:29:54 -070070 } catch (IOException e) {
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080071 log.error("Error loading configuration", e);
Jonathan Hartbac07a02014-10-13 21:29:54 -070072 }
Jonathan Hartbac07a02014-10-13 21:29:54 -070073 }
74
Jonathan Hart9965d772014-12-02 10:28:34 -080075 /**
76 * Instructs the configuration reader to read the configuration from the file.
77 */
78 public void readConfiguration() {
Jonathan Hartbac07a02014-10-13 21:29:54 -070079 readConfiguration(configFileName);
80 }
81
Jonathan Hartbac07a02014-10-13 21:29:54 -070082 @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 Hart9965d772014-12-02 10:28:34 -080092 /**
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 Hartbac07a02014-10-13 21:29:54 -070099 static String dpidToUri(String dpid) {
100 return "of:" + dpid.replace(":", "");
101 }
102}