blob: 2f1ede79782c9fd65e9499267e38133efd21d3a8 [file] [log] [blame]
Jonathan Hart4cb39882015-08-12 23:50:55 -04001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
7 *
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.
15 */
16
17package org.onosproject.routing.config;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.google.common.collect.Sets;
21import org.onlab.packet.IpAddress;
22import org.onosproject.core.ApplicationId;
Jonathan Hart4cb39882015-08-12 23:50:55 -040023import org.onosproject.net.ConnectPoint;
Jonathan Harta8625482015-09-08 16:14:56 -070024import org.onosproject.net.config.Config;
Jonathan Hart4cb39882015-08-12 23:50:55 -040025
Jonathan Harta8625482015-09-08 16:14:56 -070026import java.util.Optional;
Jonathan Hart4cb39882015-08-12 23:50:55 -040027import java.util.Set;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Configuration object for BGP config.
33 */
34public class BgpConfig extends Config<ApplicationId> {
35
36 public static final String SPEAKERS = "bgpSpeakers";
37 public static final String CONNECT_POINT = "connectPoint";
Jonathan Harta8625482015-09-08 16:14:56 -070038 public static final String NAME = "name";
Jonathan Hart4cb39882015-08-12 23:50:55 -040039 public static final String PEERS = "peers";
40
41 // TODO add methods for updating config
42
43 /**
44 * Gets the set of configured BGP speakers.
45 *
46 * @return BGP speakers
47 */
48 public Set<BgpSpeakerConfig> bgpSpeakers() {
49 Set<BgpSpeakerConfig> speakers = Sets.newHashSet();
50
Thomas Vachuska0a400ea2015-09-04 11:25:03 -070051 JsonNode speakersNode = object.get(SPEAKERS);
Jonathan Hart4cb39882015-08-12 23:50:55 -040052 speakersNode.forEach(jsonNode -> {
53 Set<IpAddress> listenAddresses = Sets.newHashSet();
54 jsonNode.path(PEERS).forEach(addressNode ->
Jonathan Harta8625482015-09-08 16:14:56 -070055 listenAddresses.add(IpAddress.valueOf(addressNode.asText()))
Jonathan Hart4cb39882015-08-12 23:50:55 -040056 );
Jonathan Harta8625482015-09-08 16:14:56 -070057
58 Optional<String> name;
59 if (jsonNode.get(NAME) == null) {
60 name = Optional.empty();
61 } else {
62 name = Optional.of(jsonNode.get(NAME).asText());
63 }
64
65 speakers.add(new BgpSpeakerConfig(name,
Jonathan Hart4cb39882015-08-12 23:50:55 -040066 ConnectPoint.deviceConnectPoint(jsonNode.path(CONNECT_POINT).asText()),
67 listenAddresses));
68 });
69
70 return speakers;
71 }
72
73 /**
74 * Configuration for a BGP speaker.
75 */
76 public static class BgpSpeakerConfig {
77
Jonathan Harta8625482015-09-08 16:14:56 -070078 private Optional<String> name;
Jonathan Hart4cb39882015-08-12 23:50:55 -040079 private ConnectPoint connectPoint;
80 private Set<IpAddress> peers;
81
Jonathan Harta8625482015-09-08 16:14:56 -070082 public BgpSpeakerConfig(Optional<String> name, ConnectPoint connectPoint,
83 Set<IpAddress> peers) {
84 this.name = checkNotNull(name);
Jonathan Hart4cb39882015-08-12 23:50:55 -040085 this.connectPoint = checkNotNull(connectPoint);
86 this.peers = checkNotNull(peers);
87 }
88
Jonathan Harta8625482015-09-08 16:14:56 -070089 public Optional<String> name() {
90 return name;
91 }
92
Jonathan Hart4cb39882015-08-12 23:50:55 -040093 public ConnectPoint connectPoint() {
94 return connectPoint;
95 }
96
97 public Set<IpAddress> peers() {
98 return peers;
99 }
100 }
101}