blob: 2fcd1fe3012fcdd0d90a89515cf7a6f35bb7cc9d [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
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 Hart739c8352014-10-29 17:49:26 -070044 private Map<String, BgpSpeaker> bgpSpeakers = new ConcurrentHashMap<>();
45 private Map<IpAddress, BgpPeer> bgpPeers = new ConcurrentHashMap<>();
Jonathan Hartbac07a02014-10-13 21:29:54 -070046
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 Hartbac07a02014-10-13 21:29:54 -070058 for (BgpSpeaker speaker : config.getBgpSpeakers()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070059 bgpSpeakers.put(speaker.name(), speaker);
Jonathan Hartbac07a02014-10-13 21:29:54 -070060 }
Jonathan Hartbac07a02014-10-13 21:29:54 -070061 for (BgpPeer peer : config.getPeers()) {
Jonathan Hartdc711bd2014-10-15 11:24:23 -070062 bgpPeers.put(peer.ipAddress(), peer);
Jonathan Hartbac07a02014-10-13 21:29:54 -070063 }
Jonathan Hart739c8352014-10-29 17:49:26 -070064 } catch (FileNotFoundException e) {
65 log.warn("Configuration file not found: {}", configFileName);
Jonathan Hartbac07a02014-10-13 21:29:54 -070066 } catch (IOException e) {
67 log.error("Error reading JSON file", e);
Jonathan Hartbac07a02014-10-13 21:29:54 -070068 }
Jonathan Hartbac07a02014-10-13 21:29:54 -070069 }
70
Jonathan Hartbac07a02014-10-13 21:29:54 -070071 public void init() {
Jonathan Hartbac07a02014-10-13 21:29:54 -070072 log.debug("Config file set to {}", configFileName);
73
74 readConfiguration(configFileName);
75 }
76
Jonathan Hartbac07a02014-10-13 21:29:54 -070077 @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}