blob: 636249c684e1193d36708a866f5271177c38e245 [file] [log] [blame]
Shashikanth VH1c9587f2016-02-08 14:25:06 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Shashikanth VH1c9587f2016-02-08 14:25:06 +05303 *
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.bgp.controller.impl;
18
19import java.util.Map;
20import java.util.TreeMap;
21
22import org.onosproject.bgpio.types.RouteDistinguisher;
23import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecDetails;
24import org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecPrefix;
25
26import com.google.common.base.MoreObjects;
27
28/**
Shashikanth VHe3a73bc2016-02-22 20:11:31 +053029 * Implementation of BGP flow specification RIB.
Shashikanth VH1c9587f2016-02-08 14:25:06 +053030 */
Shashikanth VHe3a73bc2016-02-22 20:11:31 +053031public class BgpFlowSpecRib {
Shashikanth VH1c9587f2016-02-08 14:25:06 +053032 private Map<BgpFlowSpecPrefix, BgpFlowSpecDetails> flowSpecTree = new TreeMap<>();
33 private Map<RouteDistinguisher, Map<BgpFlowSpecPrefix, BgpFlowSpecDetails>> vpnFlowSpecTree = new TreeMap<>();
34
35 /**
36 * Returns the BGP flow spec info.
37 *
38 * @return BGP flow spec tree
39 */
40 public Map<BgpFlowSpecPrefix, BgpFlowSpecDetails> flowSpecTree() {
41 return flowSpecTree;
42 }
43
44 /**
45 * Gets VPN flowspec tree.
46 *
47 * @return vpn flow spec tree
48 */
49 public Map<RouteDistinguisher, Map<BgpFlowSpecPrefix, BgpFlowSpecDetails>> vpnFlowSpecTree() {
50 return vpnFlowSpecTree;
51 }
52
53
54 /**
55 * Update BGP flow spec details.
56 *
57 * @param prefix prefix Info
58 * @param flowSpec BGP flow specifications details
59 */
60 public void add(BgpFlowSpecPrefix prefix, BgpFlowSpecDetails flowSpec) {
61 if (flowSpecTree.containsKey(prefix)) {
62 flowSpecTree.replace(prefix, flowSpec);
63 } else {
64 flowSpecTree.put(prefix, flowSpec);
65 }
66 }
67
68 /**
69 * Removes flow spec.
70 *
71 * @param flowSpec BGP flow specification
72 */
73 public void delete(BgpFlowSpecPrefix flowSpec) {
74 if (flowSpecTree.containsKey(flowSpec)) {
75 flowSpecTree.remove(flowSpec);
76 }
77 }
78
79 /**
80 * Update BGP flow spec details with routedistinguisher.
81 *
82 * @param routeDistinguisher route distinguisher
83 * @param prefix prefix Info
84 * @param flowSpec BGP flow specifications details
85 */
86 public void add(RouteDistinguisher routeDistinguisher, BgpFlowSpecPrefix prefix, BgpFlowSpecDetails flowSpec) {
87 Map<BgpFlowSpecPrefix, BgpFlowSpecDetails> fsTree;
88 if (!vpnFlowSpecTree.containsKey(routeDistinguisher)) {
89
90 fsTree = new TreeMap<>();
91
92 vpnFlowSpecTree.put(routeDistinguisher, fsTree);
93 } else {
94 fsTree = vpnFlowSpecTree().get(routeDistinguisher);
95 }
96 if (fsTree.containsKey(prefix)) {
97 fsTree.replace(prefix, flowSpec);
98 } else {
99 fsTree.put(prefix, flowSpec);
100 }
101 }
102
103 /**
104 * Removes flow spec.
105 *
106 * @param routeDistinguisher route distinguisher
Brian O'Connor2f11bb32016-02-08 16:48:24 -0800107 * @param flowSpecPrefix BGP flow specification prefix
Shashikanth VH1c9587f2016-02-08 14:25:06 +0530108 */
109 public void delete(RouteDistinguisher routeDistinguisher, BgpFlowSpecPrefix flowSpecPrefix) {
110 if (vpnFlowSpecTree.containsKey(routeDistinguisher)) {
111 Map<BgpFlowSpecPrefix, BgpFlowSpecDetails> fsTree = vpnFlowSpecTree().get(routeDistinguisher);
112 fsTree.remove(flowSpecPrefix);
113 if (fsTree.size() == 0) {
114 vpnFlowSpecTree.remove(routeDistinguisher);
115 }
116 }
117 }
118
119 @Override
120 public String toString() {
121 return MoreObjects.toStringHelper(getClass())
122 .omitNullValues()
123 .add("flowSpecTree", flowSpecTree)
124 .add("vpnFlowSpecTree", vpnFlowSpecTree)
125 .toString();
126 }
127}