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 | |
| 82 | if (mroute.getIngressPoint() == null || |
| 83 | mroute.getEgressPoints().isEmpty()) { |
| 84 | return null; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Match the group AND source addresses. We will also check ether type to |
| 89 | * determine if we are doing ipv4 or ipv6. |
| 90 | * |
| 91 | * If we really wanted to be pendantic we could put in a |
| 92 | * condition to make sure the ethernet MAC address was also |
| 93 | * mcast. |
| 94 | */ |
| 95 | selector.matchEthType(Ethernet.TYPE_IPV4) |
| 96 | .matchIPDst(mroute.getGaddr()) |
| 97 | .matchIPSrc(mroute.getSaddr()); |
| 98 | |
| 99 | SinglePointToMultiPointIntent intent = |
| 100 | SinglePointToMultiPointIntent.builder() |
| 101 | .appId(McastForwarding.getAppId()) |
| 102 | .selector(selector.build()) |
| 103 | .treatment(treatment) |
Rusty Eddy | ddef893 | 2015-09-25 01:15:53 +0000 | [diff] [blame^] | 104 | .ingressPoint(mroute.getIngressPoint().getConnectPoint()) |
| 105 | .egressPoints(mroute.getEgressConnectPoints()). |
alshabib | eff0054 | 2015-09-23 13:22:33 -0700 | [diff] [blame] | 106 | build(); |
| 107 | |
| 108 | intentService.submit(intent); |
| 109 | return intent; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Withdraw the intent represented by this route. |
| 114 | * |
| 115 | * @param mroute the mcast route whose intent we want to remove |
| 116 | */ |
| 117 | public void withdrawIntent(McastRouteBase mroute) { |
| 118 | Intent intent = intentService.getIntent(mroute.getIntentKey()); |
| 119 | intentService.withdraw(intent); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Withdraw all intents. |
| 124 | * |
| 125 | * This will be called from the deactivate method so we don't leave |
| 126 | * a mess behind us after we leave. |
| 127 | */ |
| 128 | public void withdrawAllIntents() { |
| 129 | for (Intent intent : intentService.getIntents()) { |
| 130 | intentService.withdraw(intent); |
| 131 | } |
| 132 | } |
| 133 | } |