blob: 713383f5a9273bf9bd8a83d37c189bde6bbaf716 [file] [log] [blame]
alshabibed0951f2015-10-02 21:39:27 +02001/*
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.incubator.net.mcast.impl;
17
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
23import org.apache.felix.scr.annotations.Service;
24import org.onlab.packet.IpPrefix;
25import org.onlab.util.KryoNamespace;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
28import org.onosproject.event.AbstractListenerManager;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.mcast.McastEvent;
31import org.onosproject.net.mcast.McastListener;
32import org.onosproject.net.mcast.McastRoute;
33import org.onosproject.net.mcast.MulticastRouteService;
34import org.onosproject.store.service.ConsistentMap;
35import org.onosproject.store.service.Serializer;
36import org.onosproject.store.service.StorageService;
37import org.onosproject.store.service.Versioned;
38import org.slf4j.Logger;
39
40import java.util.List;
41
42import static org.slf4j.LoggerFactory.getLogger;
43
44/**
45 * An implementation of a multicast route table.
46 */
47@Component(immediate = true)
48@Service
49public class MulticastRouteManager
50 extends AbstractListenerManager<McastEvent, McastListener>
51 implements MulticastRouteService {
52 //TODO: add MulticastRouteAdminService
53
54 private static final String MCASTRIB = "mcast-rib-table";
55
56 private Logger log = getLogger(getClass());
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 private StorageService storageService;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 private CoreService coreService;
63
64
65 protected ApplicationId appId;
66 protected ConsistentMap<McastRoute, MulticastData> mcastRoutes;
67
68 @Activate
69 public void activate() {
70
71 eventDispatcher.addSink(McastEvent.class, listenerRegistry);
72
73 appId = coreService.registerApplication("org.onosproject.mcastrib");
74
75 mcastRoutes = storageService.<McastRoute, MulticastData>consistentMapBuilder()
76 .withApplicationId(appId)
77 .withName(MCASTRIB)
78 .withSerializer(Serializer.using(KryoNamespace.newBuilder().register(
79 MulticastData.class,
80 McastRoute.class,
81 McastRoute.Type.class,
82 IpPrefix.class,
83 List.class,
84 ConnectPoint.class
85 ).build())).build();
86
87 log.info("Started");
88 }
89
90 @Deactivate
91 public void deactivate() {
92 log.info("Stopped");
93 }
94
95 @Override
96 public void add(McastRoute route) {
97 mcastRoutes.put(route, MulticastData.empty());
98 post(new McastEvent(McastEvent.Type.ROUTE_ADDED, route, null, null));
99 }
100
101 @Override
102 public void remove(McastRoute route) {
103 mcastRoutes.remove(route);
104 post(new McastEvent(McastEvent.Type.ROUTE_REMOVED, route, null, null));
105 }
106
107 @Override
108 public void addSource(McastRoute route, ConnectPoint connectPoint) {
109 Versioned<MulticastData> d = mcastRoutes.compute(route, (k, v) -> {
110 if (v.isEmpty()) {
111 return new MulticastData(connectPoint);
112 } else {
113 log.warn("Route {} is already in use.", route);
114 return v;
115 }
116 });
117
118 if (d != null) {
119 post(new McastEvent(McastEvent.Type.SOURCE_ADDED,
120 route, null, connectPoint));
121 }
122 }
123
124 @Override
125 public void addSink(McastRoute route, ConnectPoint connectPoint) {
126 mcastRoutes.compute(route, (k, v) -> {
127 if (!v.isEmpty()) {
128 v.appendSink(connectPoint);
129 post(new McastEvent(McastEvent.Type.SINK_ADDED, route,
130 connectPoint, v.source()));
131 } else {
132 log.warn("Route {} does not exist");
133 }
134 return v;
135 });
136 }
137
138
139 @Override
140 public void removeSink(McastRoute route, ConnectPoint connectPoint) {
141 mcastRoutes.compute(route, (k, v) -> {
142 if (v.removeSink(connectPoint)) {
143 post(new McastEvent(McastEvent.Type.SINK_REMOVED, route,
144 connectPoint, v.source()));
145 }
146 return v;
147 });
148 }
149
150 @Override
151 public ConnectPoint fetchSource(McastRoute route) {
152 MulticastData d = mcastRoutes.asJavaMap().getOrDefault(route,
153 MulticastData.empty());
154 return d.source();
155 }
156
157 @Override
158 public List<ConnectPoint> fetchSinks(McastRoute route) {
159 MulticastData d = mcastRoutes.asJavaMap().getOrDefault(route,
160 MulticastData.empty());
161 return d.sinks();
162 }
163}