Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.routing.config; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.core.ApplicationId; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 23 | import org.onosproject.net.ConnectPoint; |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 24 | import org.onosproject.net.config.Config; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 25 | |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 26 | import java.util.Optional; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 27 | import java.util.Set; |
| 28 | |
| 29 | import static com.google.common.base.Preconditions.checkNotNull; |
| 30 | |
| 31 | /** |
| 32 | * Configuration object for BGP config. |
| 33 | */ |
| 34 | public class BgpConfig extends Config<ApplicationId> { |
| 35 | |
| 36 | public static final String SPEAKERS = "bgpSpeakers"; |
| 37 | public static final String CONNECT_POINT = "connectPoint"; |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 38 | public static final String NAME = "name"; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 39 | 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 Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 51 | JsonNode speakersNode = object.get(SPEAKERS); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 52 | speakersNode.forEach(jsonNode -> { |
| 53 | Set<IpAddress> listenAddresses = Sets.newHashSet(); |
| 54 | jsonNode.path(PEERS).forEach(addressNode -> |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 55 | listenAddresses.add(IpAddress.valueOf(addressNode.asText())) |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 56 | ); |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 57 | |
| 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 Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 66 | 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 Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 78 | private Optional<String> name; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 79 | private ConnectPoint connectPoint; |
| 80 | private Set<IpAddress> peers; |
| 81 | |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 82 | public BgpSpeakerConfig(Optional<String> name, ConnectPoint connectPoint, |
| 83 | Set<IpAddress> peers) { |
| 84 | this.name = checkNotNull(name); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 85 | this.connectPoint = checkNotNull(connectPoint); |
| 86 | this.peers = checkNotNull(peers); |
| 87 | } |
| 88 | |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 89 | public Optional<String> name() { |
| 90 | return name; |
| 91 | } |
| 92 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 93 | public ConnectPoint connectPoint() { |
| 94 | return connectPoint; |
| 95 | } |
| 96 | |
| 97 | public Set<IpAddress> peers() { |
| 98 | return peers; |
| 99 | } |
| 100 | } |
| 101 | } |