alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [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 | package org.onosproject.mfwd.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | import java.util.HashMap; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | |
| 22 | /** |
| 23 | * The McastRouteGroup extends the McastRouteBase class and serves two purposes: |
| 24 | * first it represents a (*, G) multicast route entry. Second it serves |
| 25 | * as a container for all (S, G) multicast route entries that belong |
| 26 | * to the same group address. |
| 27 | */ |
| 28 | public class McastRouteGroup extends McastRouteBase { |
| 29 | private HashMap<IpPrefix, McastRouteSource> sources; |
| 30 | |
| 31 | /** |
| 32 | * Class constructor. |
| 33 | * |
| 34 | * @param gaddr - String representation of group address. |
| 35 | */ |
| 36 | public McastRouteGroup(String gaddr) { |
| 37 | super(checkNotNull(gaddr)); |
| 38 | this.init(); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Create a multicast group. |
| 43 | * |
| 44 | * @param gpfx - Group address |
| 45 | */ |
| 46 | public McastRouteGroup(IpPrefix gpfx) { |
| 47 | super(checkNotNull(gpfx)); |
| 48 | this.init(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Common initialization used by constructors. |
| 53 | */ |
| 54 | private void init() { |
| 55 | this.sources = new HashMap(); |
| 56 | super.isGroup = true; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Find a specific multicast source address for this group. |
| 61 | * |
| 62 | * @param saddr the source address |
| 63 | * @return the multicast source route or null if it does not exist |
| 64 | */ |
| 65 | public McastRouteSource findSource(IpPrefix saddr) { |
| 66 | return this.sources.get(checkNotNull(saddr)); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Return the entire set of multicast sources for this group. |
| 71 | * |
| 72 | * @return the set of multicast sources |
| 73 | */ |
| 74 | public HashMap<IpPrefix, McastRouteSource> getSources() { |
| 75 | return this.sources; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Add a new McastRouteSource to this group. |
| 80 | * |
| 81 | * @param src the multicast source |
| 82 | */ |
| 83 | public void addSource(McastRouteSource src) { |
| 84 | checkNotNull(src); |
| 85 | this.sources.put(src.getSaddr(), src); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Remove the source with this specific IpPrefix from this group entry. |
| 90 | * |
| 91 | * @param spfx IP Prefix of the source to be removed |
| 92 | * @return the source route that was just removed |
| 93 | */ |
| 94 | public McastRouteSource removeSource(IpPrefix spfx) { |
| 95 | McastRouteSource src = this.sources.remove(spfx); |
| 96 | src.withdrawIntent(); |
| 97 | return src; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Remove all sources from this. |
| 102 | */ |
| 103 | public void removeSources() { |
| 104 | for (McastRouteSource src : this.sources.values()) { |
| 105 | src.withdrawIntent(); |
| 106 | this.sources.remove(src.getSaddr()); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | } |