blob: 7262c425fafbc79e2f8ad4ae9145e9670a7cfff1 [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
Thomas Vachuska4b420772014-10-30 16:46:17 -070031// TODO: As a long term solution, a module providing general network configuration to ONOS nodes should be used.
32
Jonathan Hartbac07a02014-10-13 21:29:54 -070033/**
Thomas Vachuska4b420772014-10-30 16:46:17 -070034 * 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 Hartbac07a02014-10-13 21:29:54 -070036 * not running SDN-IP.
Jonathan Hartbac07a02014-10-13 21:29:54 -070037 */
38public class SdnIpConfigReader implements SdnIpConfigService {
39
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080040 private final Logger log = LoggerFactory.getLogger(getClass());
Jonathan Hartbac07a02014-10-13 21:29:54 -070041
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080042 // Current working dir seems to be /opt/onos/apache-karaf-3.0.2
43 // TODO: Set the path to /opt/onos/config
44 private static final String CONFIG_DIR = "../config";
45 private static final String DEFAULT_CONFIG_FILE = "sdnip.json";
Jonathan Hartbac07a02014-10-13 21:29:54 -070046 private String configFileName = DEFAULT_CONFIG_FILE;
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080047
Jonathan Hart739c8352014-10-29 17:49:26 -070048 private Map<String, BgpSpeaker> bgpSpeakers = new ConcurrentHashMap<>();
49 private Map<IpAddress, BgpPeer> bgpPeers = new ConcurrentHashMap<>();
Jonathan Hartbac07a02014-10-13 21:29:54 -070050
51 /**
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080052 * Reads SDN-IP related information contained in the configuration file.
Jonathan Hartbac07a02014-10-13 21:29:54 -070053 *
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080054 * @param configFilename the name of the configuration file for the SDN-IP
55 * application
Jonathan Hartbac07a02014-10-13 21:29:54 -070056 */
57 private void readConfiguration(String configFilename) {
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080058 File configFile = new File(CONFIG_DIR, configFilename);
Jonathan Hartbac07a02014-10-13 21:29:54 -070059 ObjectMapper mapper = new ObjectMapper();
60
61 try {
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080062 log.info("Loading config: {}", configFile.getAbsolutePath());
63 Configuration config = mapper.readValue(configFile,
64 Configuration.class);
Jonathan Hartbac07a02014-10-13 21:29:54 -070065 for (BgpSpeaker speaker : config.getBgpSpeakers()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070066 bgpSpeakers.put(speaker.name(), speaker);
Jonathan Hartbac07a02014-10-13 21:29:54 -070067 }
Jonathan Hartbac07a02014-10-13 21:29:54 -070068 for (BgpPeer peer : config.getPeers()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070069 bgpPeers.put(peer.ipAddress(), peer);
Jonathan Hartbac07a02014-10-13 21:29:54 -070070 }
Jonathan Hart739c8352014-10-29 17:49:26 -070071 } catch (FileNotFoundException e) {
72 log.warn("Configuration file not found: {}", configFileName);
Jonathan Hartbac07a02014-10-13 21:29:54 -070073 } catch (IOException e) {
Pavlin Radoslavov0a297b12014-11-11 16:03:27 -080074 log.error("Error loading configuration", e);
Jonathan Hartbac07a02014-10-13 21:29:54 -070075 }
Jonathan Hartbac07a02014-10-13 21:29:54 -070076 }
77
Jonathan Hartbac07a02014-10-13 21:29:54 -070078 public void init() {
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
92 static String dpidToUri(String dpid) {
93 return "of:" + dpid.replace(":", "");
94 }
95}