blob: dd89aed0db5d73f0e65bde8d50998cabffafebd1 [file] [log] [blame]
Andreas Papazoisa9964ea2016-01-08 15:58:22 +02001/*
2 * Copyright 2014-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.sdxl3.config;
18
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.google.common.collect.Sets;
21import org.onlab.packet.IpAddress;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.net.config.Config;
24
25import java.util.Objects;
26import java.util.Optional;
27import java.util.Set;
28
29import static com.google.common.base.Preconditions.checkNotNull;
30
31public class BgpPeersConfig extends Config<ApplicationId> {
32 public static final String NAME = "name";
33 public static final String IP = "ip";
34 public static final String INTERFACE = "interface";
35
36 /**
37 * Gets the set of configured BGP peers.
38 *
39 * @return BGP peers
40 */
41 public Set<PeerConfig> bgpPeers() {
42 Set<PeerConfig> peers = Sets.newHashSet();
43
44 ArrayNode peersNode = array;
45 peersNode.forEach(jsonNode -> {
46 Optional<String> name;
47 if (jsonNode.get(NAME) == null) {
48 name = Optional.empty();
49 } else {
50 name = Optional.of(jsonNode.get(NAME).asText());
51 }
52
53 peers.add(new PeerConfig(name,
54 IpAddress.valueOf(jsonNode.path(IP).asText()),
55 jsonNode.path(INTERFACE).asText()));
56 });
57
58 return peers;
59 }
60
61 /**
62 * Gets the interface name configured for a given BGP peer.
63 *
64 * @param peerAddress IP address of the peer
65 * @return interface name
66 */
67
68 public String getInterfaceNameForPeer(IpAddress peerAddress) {
69 Optional<PeerConfig> match = bgpPeers()
70 .stream()
71 .filter(p -> p.ip().equals(peerAddress))
72 .findAny();
73
74 if (match.isPresent()) {
75 return match.get().interfaceName();
76 } else {
77 return null;
78 }
79 }
80
81
82 /**
83 * Configuration for a BGP peer.
84 */
85 public static class PeerConfig {
86
87 private Optional<String> name;
88 private IpAddress ip;
89 private String interfaceName;
90
91 public PeerConfig(Optional<String> name, IpAddress ip, String interfaceName) {
92 this.name = checkNotNull(name);
93 this.ip = checkNotNull(ip);
94 this.interfaceName = checkNotNull(interfaceName);
95 }
96
97 public Optional<String> name() {
98 return name;
99 }
100
101 public IpAddress ip() {
102 return ip;
103 }
104
105 public String interfaceName() {
106 return interfaceName;
107 }
108
109 @Override
110 public boolean equals(Object obj) {
111 if (this == obj) {
112 return true;
113 }
114 if (obj instanceof PeerConfig) {
115 final PeerConfig that = (PeerConfig) obj;
116 return Objects.equals(this.name, that.name) &&
117 Objects.equals(this.ip, that.ip) &&
118 Objects.equals(this.interfaceName, that.interfaceName);
119 }
120 return false;
121 }
122
123 @Override
124 public int hashCode() {
125 return Objects.hash(name, ip, interfaceName);
126 }
127 }
128}