2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.sal.binding.codegen.impl;
10 import static org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper.setRoutingTable;
12 import java.util.HashMap;
16 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
17 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
20 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
21 import org.opendaylight.controller.sal.binding.api.rpc.RpcRoutingTable;
22 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper;
23 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
26 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.binding.RpcService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.common.collect.ImmutableMap;
33 import com.google.common.collect.ImmutableSet;
35 public class RpcRouterCodegenInstance<T extends RpcService> implements //
36 RpcRouter<T>, RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
38 private static final Logger LOG = LoggerFactory.getLogger(RpcRouterCodegenInstance.class);
40 private T defaultService;
42 private final Class<T> serviceType;
44 private final T invocationProxy;
46 private final Set<Class<? extends BaseIdentity>> contexts;
48 private final ListenerRegistry<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listeners;
50 private final Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> routingTables;
52 private final String name;
54 @SuppressWarnings("unchecked")
55 public RpcRouterCodegenInstance(final String name,final Class<T> type, final T routerImpl, final Iterable<Class<? extends BaseIdentity>> contexts) {
57 this.listeners = ListenerRegistry.create();
58 this.serviceType = type;
59 this.invocationProxy = routerImpl;
60 this.contexts = ImmutableSet.copyOf(contexts);
61 Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> mutableRoutingTables = new HashMap<>();
62 for (Class<? extends BaseIdentity> ctx : contexts) {
63 RpcRoutingTableImpl<? extends BaseIdentity, T> table = new RpcRoutingTableImpl<>(name,ctx,type);
65 @SuppressWarnings("rawtypes")
66 Map invokerView = table.getRoutes();
68 setRoutingTable(invocationProxy, ctx, invokerView);
69 mutableRoutingTables.put(ctx, table);
70 table.registerRouteChangeListener(this);
72 this.routingTables = ImmutableMap.copyOf(mutableRoutingTables);
76 public Class<T> getServiceType() {
81 public T getInvocationProxy() {
82 return invocationProxy;
86 @SuppressWarnings("unchecked")
87 public <C extends BaseIdentity> RpcRoutingTable<C, T> getRoutingTable(final Class<C> routeContext) {
88 return (RpcRoutingTable<C, T>) routingTables.get(routeContext);
92 public T getDefaultService() {
93 return defaultService;
97 public Set<Class<? extends BaseIdentity>> getContexts() {
102 public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
104 return listeners.registerWithType(listener);
108 public void onRouteChange(final RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
109 for (ListenerRegistration<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listener : listeners) {
111 listener.getInstance().onRouteChange(change);
112 } catch (Exception e) {
113 LOG.error("Error occured during invoker listener {}", listener.getInstance(), e);
119 public T getService(final Class<? extends BaseIdentity> context, final InstanceIdentifier<?> path) {
120 return routingTables.get(context).getRoute(path);
124 public RoutedRpcRegistration<T> addRoutedRpcImplementation(final T service) {
125 return new RoutedRpcRegistrationImpl(service);
129 public RpcRegistration<T> registerDefaultService(final T service) {
130 // TODO Auto-generated method stub
131 RuntimeCodeHelper.setDelegate(invocationProxy, service);
135 private class RoutedRpcRegistrationImpl extends AbstractObjectRegistration<T> implements RoutedRpcRegistration<T> {
137 public RoutedRpcRegistrationImpl(final T instance) {
142 public Class<T> getServiceType() {
147 public void registerPath(final Class<? extends BaseIdentity> context, final InstanceIdentifier<?> path) {
148 routingTables.get(context).updateRoute(path, getInstance());
152 public void unregisterPath(final Class<? extends BaseIdentity> context, final InstanceIdentifier<?> path) {
153 routingTables.get(context).removeRoute(path, getInstance());
157 public void registerInstance(final Class<? extends BaseIdentity> context, final InstanceIdentifier<?> instance) {
158 registerPath(context, instance);
162 public void unregisterInstance(final Class<? extends BaseIdentity> context, final InstanceIdentifier<?> instance) {
163 unregisterPath(context, instance);
167 protected void removeRegistration() {