Fix typo in match types yang model
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / RpcRouterCodegenInstance.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.binding.codegen.impl;
9
10 import org.opendaylight.yangtools.yang.binding.RpcService;
11 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
13 import org.opendaylight.controller.sal.binding.api.rpc.RpcRouter;
14 import org.opendaylight.controller.sal.binding.api.rpc.RpcRoutingTable;
15 import org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper;
16 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18
19 import static org.opendaylight.controller.sal.binding.codegen.RuntimeCodeHelper.*;
20
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.HashMap;
24
25 import org.opendaylight.yangtools.yang.binding.DataContainer;
26 import org.opendaylight.yangtools.yang.binding.RpcImplementation;
27 import org.opendaylight.controller.md.sal.common.api.routing.MutableRoutingTable;
28 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
29 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
30 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import com.google.common.collect.ImmutableMap;
37 import com.google.common.collect.ImmutableSet;
38
39 public class RpcRouterCodegenInstance<T extends RpcService> implements //
40         RpcRouter<T>, RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
41
42     private static final Logger LOG = LoggerFactory.getLogger(RpcRouterCodegenInstance.class);
43
44     private T defaultService;
45
46     private final Class<T> serviceType;
47
48     private final T invocationProxy;
49
50     private final Set<Class<? extends BaseIdentity>> contexts;
51
52     private final ListenerRegistry<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listeners;
53
54     private final Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> routingTables;
55
56     private final String name;
57
58     @SuppressWarnings("unchecked")
59     public RpcRouterCodegenInstance(String name,Class<T> type, T routerImpl, Set<Class<? extends BaseIdentity>> contexts,
60             Set<Class<? extends DataContainer>> inputs) {
61         this.name = name;
62         this.listeners = ListenerRegistry.create();
63         this.serviceType = type;
64         this.invocationProxy = routerImpl;
65         this.contexts = ImmutableSet.copyOf(contexts);
66         Map<Class<? extends BaseIdentity>, RpcRoutingTableImpl<? extends BaseIdentity, T>> mutableRoutingTables = new HashMap<>();
67         for (Class<? extends BaseIdentity> ctx : contexts) {
68             RpcRoutingTableImpl<? extends BaseIdentity, T> table = new RpcRoutingTableImpl<>(name,ctx,type);
69             
70             @SuppressWarnings("rawtypes")
71             Map invokerView = table.getRoutes();
72             
73             setRoutingTable((RpcService) invocationProxy, ctx, invokerView);
74             mutableRoutingTables.put(ctx, table);
75             table.registerRouteChangeListener(this);
76         }
77         this.routingTables = ImmutableMap.copyOf(mutableRoutingTables);
78     }
79
80     @Override
81     public Class<T> getServiceType() {
82         return serviceType;
83     }
84
85     @Override
86     public T getInvocationProxy() {
87         return invocationProxy;
88     }
89
90     @Override
91     @SuppressWarnings("unchecked")
92     public <C extends BaseIdentity> RpcRoutingTable<C, T> getRoutingTable(Class<C> routeContext) {
93         return (RpcRoutingTable<C, T>) routingTables.get(routeContext);
94     }
95
96     @Override
97     public T getDefaultService() {
98         return defaultService;
99     }
100
101     @Override
102     public Set<Class<? extends BaseIdentity>> getContexts() {
103         return contexts;
104     }
105
106     @Override
107     public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
108             L listener) {
109         return listeners.registerWithType(listener);
110     }
111
112     @Override
113     public void onRouteChange(RouteChange<Class<? extends BaseIdentity>, InstanceIdentifier<?>> change) {
114         for (ListenerRegistration<RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> listener : listeners) {
115             try {
116                 listener.getInstance().onRouteChange(change);
117             } catch (Exception e) {
118                 LOG.error("Error occured during invoker listener {}", listener.getInstance(), e);
119             }
120         }
121     }
122
123     @Override
124     public T getService(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
125         return routingTables.get(context).getRoute(path);
126     }
127
128     @Override
129     public RoutedRpcRegistration<T> addRoutedRpcImplementation(T service) {
130         return new RoutedRpcRegistrationImpl(service);
131     }
132
133     @Override
134     public RpcRegistration<T> registerDefaultService(T service) {
135         // TODO Auto-generated method stub
136         RuntimeCodeHelper.setDelegate(invocationProxy, service);
137         return null;
138     }
139
140     private class RoutedRpcRegistrationImpl extends AbstractObjectRegistration<T> implements RoutedRpcRegistration<T> {
141
142         public RoutedRpcRegistrationImpl(T instance) {
143             super(instance);
144         }
145
146         @Override
147         public Class<T> getServiceType() {
148             return serviceType;
149         }
150
151         @Override
152         public void registerPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
153             routingTables.get(context).updateRoute(path, getInstance());
154         }
155
156         @Override
157         public void unregisterPath(Class<? extends BaseIdentity> context, InstanceIdentifier<?> path) {
158             routingTables.get(context).removeRoute(path, getInstance());
159         }
160
161         @Override
162         public void registerInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
163             registerPath(context, instance);
164         }
165
166         @Override
167         public void unregisterInstance(Class<? extends BaseIdentity> context, InstanceIdentifier<?> instance) {
168             unregisterPath(context, instance);
169         }
170
171         @Override
172         protected void removeRegistration() {
173
174         }
175     }
176 }