1c1f4cb8139ef9a4855d0a52a3a3759b44cab118
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / AbstractInvokableServiceMetadata.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.blueprint.ext;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Collection;
14 import java.util.Set;
15 import java.util.function.Predicate;
16 import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
20 import org.opendaylight.controller.md.sal.dom.broker.spi.rpc.RpcRoutingStrategy;
21 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
22 import org.opendaylight.controller.sal.core.api.model.SchemaService;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24 import org.opendaylight.yangtools.yang.binding.RpcService;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.osgi.service.blueprint.container.ComponentDefinitionException;
28
29 abstract class AbstractInvokableServiceMetadata extends AbstractDependentComponentFactoryMetadata {
30     private final String interfaceName;
31
32     private ListenerRegistration<DOMRpcAvailabilityListener> rpcListenerReg;
33     private RpcProviderRegistry rpcRegistry;
34     private Class<RpcService> rpcInterface;
35     private Set<SchemaPath> rpcSchemaPaths;
36
37     AbstractInvokableServiceMetadata(final String id, final String interfaceName) {
38         super(id);
39         this.interfaceName = Preconditions.checkNotNull(interfaceName);
40     }
41
42     Class<RpcService> rpcInterface() {
43         return rpcInterface;
44     }
45
46     @SuppressWarnings({ "checkstyle:IllegalCatch", "unchecked" })
47     @Override
48     public final void init(final ExtendedBlueprintContainer container) {
49         super.init(container);
50
51         final Class<?> interfaceClass;
52         try {
53             interfaceClass = container().getBundleContext().getBundle().loadClass(interfaceName);
54         } catch (Exception e) {
55             throw new ComponentDefinitionException(String.format("%s: Error obtaining interface class %s",
56                     logName(), interfaceName), e);
57         }
58
59         if (!RpcService.class.isAssignableFrom(interfaceClass)) {
60             throw new ComponentDefinitionException(String.format(
61                 "%s: The specified interface %s is not an RpcService", logName(), interfaceName));
62         }
63
64         rpcInterface = (Class<RpcService>)interfaceClass;
65     }
66
67     @Override
68     protected final void startTracking() {
69         // Request RpcProviderRegistry first ...
70         retrieveService("RpcProviderRegistry", RpcProviderRegistry.class, this::onRpcRegistry);
71     }
72
73     private void onRpcRegistry(final Object service) {
74         log.debug("{}: Retrieved RpcProviderRegistry {}", logName(), service);
75         rpcRegistry = (RpcProviderRegistry)service;
76
77         // Now acquire SchemaService...
78         retrieveService("SchemaService", SchemaService.class, this::onSchemaService);
79     }
80
81     private void onSchemaService(final Object service) {
82         log.debug("{}: Retrieved SchemaService {}", logName(), service);
83
84         // Now get the SchemaContext and trigger RPC resolution
85         retrievedSchemaContext(((SchemaService)service).getGlobalContext());
86     }
87
88     private void retrievedSchemaContext(final SchemaContext schemaContext) {
89         log.debug("{}: retrievedSchemaContext", logName());
90
91         final Collection<SchemaPath> schemaPaths = RpcUtil.decomposeRpcService(rpcInterface, schemaContext,
92             rpcFilter());
93         if (schemaPaths.isEmpty()) {
94             log.warn("{}: interface {} has no accptable entries, assuming it is satisfied");
95             setSatisfied();
96             return;
97         }
98
99         rpcSchemaPaths = ImmutableSet.copyOf(schemaPaths);
100         log.debug("{}: Got SchemaPaths: {}", logName(), rpcSchemaPaths);
101
102         // First get the DOMRpcService OSGi service. This will be used to register a listener to be notified
103         // when the underlying DOM RPC service is available.
104         retrieveService("DOMRpcService", DOMRpcService.class, this::retrievedDOMRpcService);
105     }
106
107     private void retrievedDOMRpcService(final Object service) {
108         log.debug("{}: retrievedDOMRpcService {}", logName(), service);
109         final DOMRpcService domRpcService = (DOMRpcService)service;
110
111         setDependencyDesc("Available DOM RPC for binding RPC: " + rpcInterface);
112         rpcListenerReg = domRpcService.registerRpcListener(new DOMRpcAvailabilityListener() {
113             @Override
114             public void onRpcAvailable(final Collection<DOMRpcIdentifier> rpcs) {
115                 onRpcsAvailable(rpcs);
116             }
117
118             @Override
119             public void onRpcUnavailable(final Collection<DOMRpcIdentifier> rpcs) {
120             }
121         });
122     }
123
124     abstract Predicate<RpcRoutingStrategy> rpcFilter();
125
126     @SuppressWarnings("checkstyle:IllegalCatch")
127     @Override
128     public final Object create() throws ComponentDefinitionException {
129         log.debug("{}: In create: interfaceName: {}", logName(), interfaceName);
130
131         super.onCreate();
132
133         try {
134             RpcService rpcService = rpcRegistry.getRpcService(rpcInterface);
135
136             log.debug("{}: create returning service {}", logName(), rpcService);
137
138             return rpcService;
139         } catch (RuntimeException e) {
140             throw new ComponentDefinitionException("Error getting RPC service for " + interfaceName, e);
141         }
142     }
143
144     protected final void onRpcsAvailable(final Collection<DOMRpcIdentifier> rpcs) {
145         for (DOMRpcIdentifier identifier : rpcs) {
146             if (rpcSchemaPaths.contains(identifier.getType())) {
147                 log.debug("{}: onRpcsAvailable - found SchemaPath {}", logName(), identifier.getType());
148                 setSatisfied();
149                 break;
150             }
151         }
152     }
153
154     @Override
155     public final void stopTracking() {
156         super.stopTracking();
157         closeRpcListenerReg();
158     }
159
160     private void closeRpcListenerReg() {
161         if (rpcListenerReg != null) {
162             rpcListenerReg.close();
163             rpcListenerReg = null;
164         }
165     }
166
167     @Override
168     public final void destroy(final Object instance) {
169         super.destroy(instance);
170         closeRpcListenerReg();
171     }
172
173     @Override
174     public final String toString() {
175         return MoreObjects.toStringHelper(this).add("id", getId()).add("interfaceName", interfaceName).toString();
176     }
177 }