aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/apps/mfwd/src/main/java/org/onosproject/mfwd/impl/McastRouteBase.java
blob: 4da1f33cc98fe0b9fdf393ff3d355ec189ece96a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onosproject.mfwd.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import org.onlab.packet.IpPrefix;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.intent.SinglePointToMultiPointIntent;
import org.onosproject.net.intent.Key;

import java.util.Set;
import java.util.HashSet;

/**
 * McastRouteBase base class for McastRouteGroup and McastRouteSource.
 */
public class McastRouteBase implements McastRoute {
    protected final IpPrefix gaddr;
    protected final IpPrefix saddr;

    protected McastConnectPoint ingressPoint;
    protected Set<McastConnectPoint> egressPoints;

    protected boolean isGroup = false;

    protected boolean dirty = false;

    /**
     * How may times has this packet been punted.
     */
    private int puntCount = 0;

    /**
     * If the intentKey is null that means no intent has
     * been installed.
     */
    protected Key intentKey = null;

    /**
     * Create a multicast route. This is the parent class for both the Group
     * and the source.
     *
     * @param saddr source address
     * @param gaddr multicast group address
     */
    public McastRouteBase(String saddr, String gaddr) {
        this.gaddr = IpPrefix.valueOf(checkNotNull(gaddr));
        if (saddr == null || saddr.equals("*")) {
            this.saddr = IpPrefix.valueOf(0, 0);
        } else {
            this.saddr = IpPrefix.valueOf(checkNotNull(gaddr));
        }
        this.init();
    }

    /**
     * Create a multicast group table entry.
     * @param gaddr multicast group address
     */
    public McastRouteBase(String gaddr) {
        this("*", gaddr);
    }

    /**
     * Set the source and group address value of a (*, G) group.
     *
     * @param gpfx the group prefix address
     */
    public McastRouteBase(IpPrefix gpfx) {
        this(IpPrefix.valueOf(0, 0), gpfx);
    }

    /**
     * Create a multicast route constructor.
     *
     * @param saddr source address
     * @param gaddr group address
     */
    public McastRouteBase(IpPrefix saddr, IpPrefix gaddr) {
        this.saddr = checkNotNull(saddr);
        this.gaddr = checkNotNull(gaddr);

        this.init();
    }

    private void init() {
        this.isGroup = (this.saddr.prefixLength() == 0);
        this.ingressPoint = null;
        this.egressPoints = new HashSet();
    }

    /**
     * Get the multicast group address.
     *
     * @return the multicast group address
     */
    @Override
    public IpPrefix getGaddr() {
        return gaddr;
    }

    /**
     * Get the multicast source address.
     *
     * @return the multicast source address
     */
    @Override
    public IpPrefix getSaddr() {
        return saddr;
    }

    /**
     * Is this an IPv4 multicast route.
     *
     * @return true if it is an IPv4 route
     */
    @Override
    public boolean isIp4() {
        return gaddr.isIp4();
    }

    /**
     * Is this an IPv6 multicast route.
     *
     * @return true if it is an IPv6 route
     */
    @Override
    public boolean isIp6() {
        return gaddr.isIp6();
    }

    /**
     * Is this a multicast group route?
     *
     * @return true if it is a multicast group route.
     */
    public boolean isGroup() {
        return isGroup;
    }

    /**
     * @return true if this is (S, G) false if it (*, G).
     */
    public boolean isSource() {
        return (!isGroup);
    }

    /**
     * Get the dirty state.
     *
     * @return whether this route is dirty or not.
     */
    public boolean getDirty() {
        return this.dirty;
    }

    /**
     * Set the dirty state to indicate that something changed.
     * This may require an update to the flow tables (intents).
     *
     * @param dirty set the dirty bit
     */
    public void setDirty(boolean dirty) {
        this.dirty = dirty;
    }

    /**
     * Add an ingress point to this route.
     *
     * @param ingress incoming connect point
     * @return whether ingress has been added, only add if ingressPoint is null
     */
    public boolean addIngressPoint(ConnectPoint ingress) {

        // Do NOT add the ingressPoint if it is not null.
        if (this.ingressPoint != null) {
            // TODO: Log an warning.
            return false;
        }
        this.ingressPoint = new McastConnectPoint(checkNotNull(ingress));
        setDirty(true);
        return true;
    }

    /**
     * Add or modify the ingress connect point.
     *
     * @param connectPoint string switch device Id
     * @return whether ingress has been added, only add if ingressPoint is null
     */
    public boolean addIngressPoint(String connectPoint) {

        if (this.ingressPoint != null) {
            // TODO: log a warning.
            return false;
        }
        ConnectPoint cp = ConnectPoint.deviceConnectPoint(checkNotNull(connectPoint));
        return this.addIngressPoint(cp);
    }

    /**
     * Get the ingress McastConnectPoint.
     *
     * @return the ingress McastConnectPoint
     */
    public McastConnectPoint getIngressPoint() {
        return this.ingressPoint;
    }

    /**
     * Add an egress McastConnectPoint.
     *
     * @param cp egress connect point
     * @return return the McastConnectPoint
     */
    public McastConnectPoint addEgressPoint(ConnectPoint cp) {
        McastConnectPoint mcp = this.findEgressConnectPoint(cp);
        if (mcp == null) {
            mcp = new McastConnectPoint(checkNotNull(cp));
            egressPoints.add(mcp);
            setDirty(true);
        }
        return mcp;
    }

    /**
     * Add an egress connect point from a string.
     *
     * @param connectPoint string representing a connect point
     * @return the MulticastConnectPoint
     */
    public McastConnectPoint addEgressPoint(String connectPoint) {
        checkNotNull(connectPoint);
        return this.addEgressPoint(ConnectPoint.deviceConnectPoint(connectPoint));
    }

    /**
     * Add an egress McastConnectPoint.
     *
     * @param cp the egress connect point
     * @param interest the source of interest for mcast traffic
     */
    public McastConnectPoint addEgressPoint(ConnectPoint cp, McastConnectPoint.JoinSource interest) {
        checkNotNull(cp);
        checkNotNull(interest);
        McastConnectPoint mcp = this.addEgressPoint(cp);
        if (mcp != null) {
            mcp.interest.add(interest);
            setDirty(true);
        }
        return mcp;
    }

    /**
     * Remove an egress from McastConnectPoint.
     *
     * @param connectPoint the egress connect point
     * @return boolean result of removal
     */
    public boolean removeEgressPoint(String connectPoint) {
        checkNotNull(connectPoint);
        return this.removeEgressPoint(ConnectPoint.deviceConnectPoint(connectPoint));
    }

    /**
     * Remove an egress from McastConnectPoint.
     *
     * @param cp the egress connect point
     * @return boolean result of removal
     */
    public boolean removeEgressPoint(ConnectPoint cp) {
        boolean removed = false;
        McastConnectPoint mcp = this.findEgressConnectPoint(checkNotNull(cp));
        if (mcp != null) {
            removed = egressPoints.remove(mcp);
            setDirty(true);
        }
        return removed;
    }

    /**
     * Add an egress McastConnectPoint.
     *
     * @param cpstr deviceId/port of the connect point
     */
    public McastConnectPoint addEgressPoint(String cpstr, McastConnectPoint.JoinSource interest) {
        checkNotNull(cpstr);
        checkNotNull(interest);
        return this.addEgressPoint(ConnectPoint.deviceConnectPoint(cpstr), interest);
    }

    /**
     * Get egress connect points for the route.
     *
     * @return Set of egress connect points
     */
    public Set<McastConnectPoint> getEgressPoints() {
        return egressPoints;
    }

    /**
     * Get egress McastConnectPoints points as ConnectPoints for intent system.
     *
     * @return Set of egress ConnectPoints
     */
    public Set<ConnectPoint> getEgressConnectPoints() {
        Set<ConnectPoint> cps = new HashSet<ConnectPoint>();

        for (McastConnectPoint mcp : egressPoints) {
            cps.add(mcp.getConnectPoint());
        }
        return cps;
    }

    /**
     * Find the Multicast Connect Point that contains the ConnectPoint.
     *
     * @param cp the regular ConnectPoint to match
     * @return the McastConnectPoint that contains cp or null if not found.
     */
    public McastConnectPoint findEgressConnectPoint(ConnectPoint cp) {
        for (McastConnectPoint mcp : this.egressPoints) {
            if (mcp.getConnectPoint().equals(cp)) {
                return mcp;
            }
        }
        return null;
    }

    /**
     * Remove specified interest from the given ConnectPoint.
     *
     * @param mcp connect point.
     * @param interest the protocol interested in this multicast stream
     * @return true if removed, false otherwise
     */
    public boolean removeInterest(McastConnectPoint mcp, McastConnectPoint.JoinSource interest) {
        checkNotNull(mcp);
        if (mcp.interest.contains(interest)) {
            mcp.interest.remove(interest);
            setDirty(true);
            return true;
        }
        return false;
    }

    /**
     * Get the number of times the packet has been punted.
     *
     * @return the punt count
     */
    @Override
    public int getPuntCount() {
        return puntCount;
    }

    /**
     * Increment the punt count.
     *
     * TODO: we need to handle wrapping.
     */
    @Override
    public void incrementPuntCount() {
        puntCount++;
    }

    /**
     * Have the McastIntentManager create and set the intent, then save the intent key.
     *
     * If we already have an intent, we will first withdraw the existing intent and
     * replace it with a new one.  This will support the case where the ingress connectPoint
     * or group of egress connectPoints change.
     */
    @Override
    public void setIntent() {
        if (this.intentKey != null) {
            this.withdrawIntent();
        }
        McastIntentManager im = McastIntentManager.getInstance();
        SinglePointToMultiPointIntent intent = im.setIntent(this);
        this.intentKey = intent.key();
    }

    /**
     * Set the Intent key.
     *
     * @param intent the multicast intent
     */
    @Override
    public void setIntent(SinglePointToMultiPointIntent intent) {
        intentKey = intent.key();
    }

    /**
     * Get the intent key represented by this route.
     *
     * @return intentKey
     */
    @Override
    public Key getIntentKey() {
        return this.intentKey;
    }


    /**
     * Withdraw the intent and set the key to null.
     */
    @Override
    public void withdrawIntent() {
        if (intentKey == null) {
            // nothing to withdraw
            return;
        }
        McastIntentManager im = McastIntentManager.getInstance();
        im.withdrawIntent(this);
        this.intentKey = null;
    }

    /**
     * Pretty Print this Multicast Route.  Works for McastRouteSource and McastRouteGroup.
     *
     * @return pretty string of the multicast route
     */
    @Override
    public String toString() {
        String out = String.format("(%s, %s)\n\t",
                saddr.toString(), gaddr.toString());

        out += "intent: ";
        out += (intentKey == null) ? "not installed" : this.intentKey.toString();
        out += "\n\tingress: ";
        out += (ingressPoint == null) ? "NULL" : ingressPoint.getConnectPoint().toString();
        out += "\n\tegress: {\n";
        if (egressPoints != null && !egressPoints.isEmpty()) {
            for (McastConnectPoint eg : egressPoints) {
                out += "\t\t" + eg.getConnectPoint().toString() + "\n";
            }
        }
        out += ("\t}\n");
        out += ("\tpunted: " + this.getPuntCount() + "\n");
        return out;
    }
}