blob: b7f1f3ce92d536fdb5ef44ef077ce416006ec996 [file] [log] [blame]
alshabibeff00542015-09-23 13:22:33 -07001/*
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 */
16package org.onosproject.mfwd.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Service;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onlab.packet.Ethernet;
25import org.onosproject.net.intent.Intent;
26import org.onosproject.net.intent.SinglePointToMultiPointIntent;
27import org.onosproject.net.intent.IntentService;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.DefaultTrafficTreatment;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
32
33@Component(immediate = true)
34@Service(value = org.onosproject.mfwd.impl.McastIntentManager.class)
35public 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 Lawrence14c73182015-10-08 18:31:52 -070082 if (mroute.getIngressPoint() == null) {
alshabibeff00542015-09-23 13:22:33 -070083 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 Lawrence14c73182015-10-08 18:31:52 -070098
99 SinglePointToMultiPointIntent.Builder builder = SinglePointToMultiPointIntent.builder()
alshabibeff00542015-09-23 13:22:33 -0700100 .appId(McastForwarding.getAppId())
101 .selector(selector.build())
102 .treatment(treatment)
Julian Lawrence14c73182015-10-08 18:31:52 -0700103 .ingressPoint(mroute.getIngressPoint().getConnectPoint());
alshabibeff00542015-09-23 13:22:33 -0700104
Julian Lawrence14c73182015-10-08 18:31:52 -0700105 // 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();
alshabibeff00542015-09-23 13:22:33 -0700111 intentService.submit(intent);
Julian Lawrence14c73182015-10-08 18:31:52 -0700112 mroute.setDirty(false);
113
alshabibeff00542015-09-23 13:22:33 -0700114 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 Lawrence14c73182015-10-08 18:31:52 -0700122 public void withdrawIntent(McastRoute mroute) {
alshabibeff00542015-09-23 13:22:33 -0700123 Intent intent = intentService.getIntent(mroute.getIntentKey());
124 intentService.withdraw(intent);
Julian Lawrence14c73182015-10-08 18:31:52 -0700125 mroute.setDirty(false);
alshabibeff00542015-09-23 13:22:33 -0700126 }
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}