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 org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Service; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onosproject.net.intent.Intent; |
| 26 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
| 27 | import org.onosproject.net.intent.IntentService; |
| 28 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 29 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
| 31 | import org.onosproject.net.flow.TrafficTreatment; |
| 32 | |
| 33 | @Component(immediate = true) |
| 34 | @Service(value = org.onosproject.mfwd.impl.McastIntentManager.class) |
| 35 | public class McastIntentManager { |
| 36 | |
| 37 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 38 | protected IntentService intentService; |
| 39 | |
| 40 | private static McastIntentManager instance; |
| 41 | |
| 42 | public McastIntentManager() { |
| 43 | instance = this; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Active this component. |
| 48 | */ |
| 49 | @Activate |
| 50 | public void activate() { } |
| 51 | |
| 52 | /** |
| 53 | * Deactivate this component. |
| 54 | */ |
| 55 | @Deactivate |
| 56 | public void deactivate() { |
| 57 | withdrawAllIntents(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get instance of this intentManager. |
| 62 | * |
| 63 | * @return the instance of this intent manager. |
| 64 | */ |
| 65 | public static McastIntentManager getInstance() { |
| 66 | if (instance == null) { |
| 67 | instance = new McastIntentManager(); |
| 68 | } |
| 69 | return instance; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Install the PointToMultipoint forwarding intent. |
| 74 | * |
| 75 | * @param mroute multicast route entry |
| 76 | * @return the intent that has been set or null otherwise |
| 77 | */ |
| 78 | public SinglePointToMultiPointIntent setIntent(McastRoute mroute) { |
| 79 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 80 | TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment(); |
| 81 | |
Julian Lawrence | 14c7318 | 2015-10-08 18:31:52 -0700 | [diff] [blame] | 82 | if (mroute.getIngressPoint() == null) { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 83 | return null; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Match the group AND source addresses. We will also check ether type to |
| 88 | * determine if we are doing ipv4 or ipv6. |
| 89 | * |
| 90 | * If we really wanted to be pendantic we could put in a |
| 91 | * condition to make sure the ethernet MAC address was also |
| 92 | * mcast. |
| 93 | */ |
| 94 | selector.matchEthType(Ethernet.TYPE_IPV4) |
| 95 | .matchIPDst(mroute.getGaddr()) |
| 96 | .matchIPSrc(mroute.getSaddr()); |
| 97 | |
Julian Lawrence | 14c7318 | 2015-10-08 18:31:52 -0700 | [diff] [blame] | 98 | |
| 99 | SinglePointToMultiPointIntent.Builder builder = SinglePointToMultiPointIntent.builder() |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 100 | .appId(McastForwarding.getAppId()) |
| 101 | .selector(selector.build()) |
| 102 | .treatment(treatment) |
Julian Lawrence | 14c7318 | 2015-10-08 18:31:52 -0700 | [diff] [blame] | 103 | .ingressPoint(mroute.getIngressPoint().getConnectPoint()); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 104 | |
Julian Lawrence | 14c7318 | 2015-10-08 18:31:52 -0700 | [diff] [blame] | 105 | // allowing intent to be pushed without egress points means we can drop packets. |
| 106 | if (!mroute.getEgressPoints().isEmpty()) { |
| 107 | builder.egressPoints(mroute.getEgressConnectPoints()); |
| 108 | } |
| 109 | |
| 110 | SinglePointToMultiPointIntent intent = builder.build(); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 111 | intentService.submit(intent); |
Julian Lawrence | 14c7318 | 2015-10-08 18:31:52 -0700 | [diff] [blame] | 112 | mroute.setDirty(false); |
| 113 | |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 114 | return intent; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Withdraw the intent represented by this route. |
| 119 | * |
| 120 | * @param mroute the mcast route whose intent we want to remove |
| 121 | */ |
Julian Lawrence | 14c7318 | 2015-10-08 18:31:52 -0700 | [diff] [blame] | 122 | public void withdrawIntent(McastRoute mroute) { |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 123 | Intent intent = intentService.getIntent(mroute.getIntentKey()); |
| 124 | intentService.withdraw(intent); |
Julian Lawrence | 14c7318 | 2015-10-08 18:31:52 -0700 | [diff] [blame] | 125 | mroute.setDirty(false); |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Withdraw all intents. |
| 130 | * |
| 131 | * This will be called from the deactivate method so we don't leave |
| 132 | * a mess behind us after we leave. |
| 133 | */ |
| 134 | public void withdrawAllIntents() { |
| 135 | for (Intent intent : intentService.getIntents()) { |
| 136 | intentService.withdraw(intent); |
| 137 | } |
| 138 | } |
| 139 | } |