aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java
blob: 6085c60a71272272dc3ee75dea142d47828b058c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onosproject.routing.config;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Sets;
import org.onlab.packet.IpAddress;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.config.Config;

import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Configuration object for BGP config.
 */
public class BgpConfig extends Config<ApplicationId> {

    public static final String SPEAKERS = "bgpSpeakers";
    public static final String CONNECT_POINT = "connectPoint";
    public static final String NAME = "name";
    public static final String PEERS = "peers";

    // TODO add methods for updating config

    /**
     * Gets the set of configured BGP speakers.
     *
     * @return BGP speakers
     */
    public Set<BgpSpeakerConfig> bgpSpeakers() {
        Set<BgpSpeakerConfig> speakers = Sets.newHashSet();

        JsonNode speakersNode = object.get(SPEAKERS);

        if (speakersNode == null) {
            return speakers;
        }

        speakersNode.forEach(jsonNode -> {
            Set<IpAddress> listenAddresses = Sets.newHashSet();
            jsonNode.path(PEERS).forEach(addressNode ->
                            listenAddresses.add(IpAddress.valueOf(addressNode.asText()))
            );

            Optional<String> name;
            if (jsonNode.get(NAME) == null) {
                name = Optional.empty();
            } else {
                name = Optional.of(jsonNode.get(NAME).asText());
            }

            speakers.add(new BgpSpeakerConfig(name,
                    ConnectPoint.deviceConnectPoint(jsonNode.path(CONNECT_POINT).asText()),
                    listenAddresses));
        });

        return speakers;
    }

    /**
     * Examines whether a name of BGP speaker exists in configuration.
     *
     * @param name name of BGP speaker being search
     * @return speaker
     */
    public BgpSpeakerConfig getSpeakerWithName(String name) {
        for (BgpConfig.BgpSpeakerConfig speaker : bgpSpeakers()) {
            if (speaker.name().isPresent() && speaker.name().get().equals(name)) {
                return speaker;
            }
        }
        return null;
    }

    /**
     * Adds BGP speaker to configuration.
     *
     * @param speaker BGP speaker configuration entry
     */
    public void addSpeaker(BgpSpeakerConfig speaker) {
        ObjectNode speakerNode = JsonNodeFactory.instance.objectNode();

        speakerNode.put(NAME, speaker.name().get());

        speakerNode.put(CONNECT_POINT, speaker.connectPoint().elementId().toString()
                + "/" + speaker.connectPoint().port().toString());

        ArrayNode peersNode = speakerNode.putArray(PEERS);
        for (IpAddress peerAddress: speaker.peers()) {
            peersNode.add(peerAddress.toString());
        }

        ArrayNode speakersArray = bgpSpeakers().isEmpty() ?
                initBgpConfiguration() : (ArrayNode) object.get(SPEAKERS);
        speakersArray.add(speakerNode);
    }

    /**
     * Removes BGP speaker from configuration.
     *
     * @param speakerName BGP speaker name
     */
    public void removeSpeaker(String speakerName) {
        ArrayNode speakersArray = (ArrayNode) object.get(SPEAKERS);

        for (int i = 0; i < speakersArray.size(); i++) {
            if (speakersArray.get(i).hasNonNull(NAME) &&
                    speakersArray.get(i).get(NAME).asText().equals(speakerName)) {
                speakersArray.remove(i);
                return;
            }
        }
    }

    /**
     * Adds peering address to BGP speaker.
     *
     * @param speakerName name of BGP speaker
     * @param peerAddress peering address to be added
     */
    public void addPeerToSpeaker(String speakerName, IpAddress peerAddress) {
        JsonNode speakersNode = object.get(SPEAKERS);
        speakersNode.forEach(jsonNode -> {
            if (jsonNode.hasNonNull(NAME) &&
                    jsonNode.get(NAME).asText().equals(speakerName)) {
                ArrayNode peersNode = (ArrayNode) jsonNode.get(PEERS);
                for (int i = 0; i < peersNode.size(); i++) {
                    if (peersNode.get(i).asText().equals(peerAddress.toString())) {
                        return; // Peer already exists.
                    }
                }
                peersNode.add(peerAddress.toString());
            }
        });
    }

    /**
     * Finds BGP speaker peering with a given external peer.
     *
     * @param peerAddress peering address to be removed
     * @return speaker
     */
    public BgpSpeakerConfig getSpeakerFromPeer(IpAddress peerAddress) {
        for (BgpConfig.BgpSpeakerConfig speaker : bgpSpeakers()) {
            if (speaker.peers().contains(peerAddress)) {
                return speaker;
            }
        }
        return null;
    }

    /**
     * Removes peering address from BGP speaker.
     *
     * @param speaker BGP speaker configuration entries
     * @param peerAddress peering address to be removed
     */
    public void removePeerFromSpeaker(BgpSpeakerConfig speaker, IpAddress peerAddress) {
        JsonNode speakersNode = object.get(SPEAKERS);
        speakersNode.forEach(jsonNode -> {
            if (jsonNode.hasNonNull(NAME) &&
                    jsonNode.get(NAME).asText().equals(speaker.name().get())) {
                ArrayNode peersNode = (ArrayNode) jsonNode.get(PEERS);
                for (int i = 0; i < peersNode.size(); i++) {
                    if (peersNode.get(i).asText().equals(peerAddress.toString())) {
                        peersNode.remove(i);
                        return;
                    }
                }
            }
        });
    }

    /**
     * Creates empty configuration for BGP speakers.
     *
     * @return empty array of BGP speakers
     */
    private ArrayNode initBgpConfiguration() {
        return object.putArray(SPEAKERS);
    }


    /**
     * Configuration for a BGP speaker.
     */
    public static class BgpSpeakerConfig {

        private Optional<String> name;
        private ConnectPoint connectPoint;
        private Set<IpAddress> peers;

        public BgpSpeakerConfig(Optional<String> name, ConnectPoint connectPoint,
                                Set<IpAddress> peers) {
            this.name = checkNotNull(name);
            this.connectPoint = checkNotNull(connectPoint);
            this.peers = checkNotNull(peers);
        }

        public Optional<String> name() {
            return name;
        }

        public ConnectPoint connectPoint() {
            return connectPoint;
        }

        public Set<IpAddress> peers() {
            return peers;
        }

        /**
         * Examines if BGP peer is connected.
         *
         * @param peer IP address of peer
         * @return result of search
         */
        public boolean isConnectedToPeer(IpAddress peer) {
            for (final IpAddress entry : peers()) {
                if (entry.equals(peer)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj instanceof BgpSpeakerConfig) {
                final BgpSpeakerConfig that = (BgpSpeakerConfig) obj;
                return Objects.equals(this.name, that.name) &&
                        Objects.equals(this.connectPoint, that.connectPoint) &&
                        Objects.equals(this.peers, that.peers);
            }
            return false;
        }

        @Override
        public int hashCode() {
            return Objects.hash(name, connectPoint, peers);
        }
    }
}