652180725cdffeff4c82a2c5ac61b1cdf85e474c
[mdsal.git] / src / main / java / org / opendaylight / controller / blueprint / ext / RpcServiceMetadata.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 java.util.Collection;
11 import java.util.HashSet;
12 import java.util.Set;
13 import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
14 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
17 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
18 import org.opendaylight.controller.sal.core.api.model.SchemaService;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.binding.RpcService;
21 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
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 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Factory metadata corresponding to the "rpc-service" element that gets an RPC service implementation from
33  * the RpcProviderRegistry and provides it to the Blueprint container.
34  *
35  * @author Thomas Pantelis
36  */
37 class RpcServiceMetadata extends AbstractDependentComponentFactoryMetadata {
38     private static final Logger LOG = LoggerFactory.getLogger(RpcServiceMetadata.class);
39
40     private final String interfaceName;
41     private volatile Set<SchemaPath> rpcSchemaPaths;
42     private volatile RpcProviderRegistry rpcRegistry;
43     private volatile ListenerRegistration<DOMRpcAvailabilityListener> rpcListenerReg;
44     private volatile Class<RpcService> rpcInterface;
45
46     RpcServiceMetadata(String id, String interfaceName) {
47         super(id);
48         this.interfaceName = interfaceName;
49     }
50
51     @SuppressWarnings({ "checkstyle:IllegalCatch", "unchecked" })
52     @Override
53     public void init(ExtendedBlueprintContainer container) {
54         super.init(container);
55
56         try {
57             Class<?> interfaceClass = container().getBundleContext().getBundle().loadClass(interfaceName);
58             if (!RpcService.class.isAssignableFrom(interfaceClass)) {
59                 throw new ComponentDefinitionException(String.format(
60                         "%s: The specified interface %s is not an RpcService", logName(), interfaceName));
61             }
62
63             rpcInterface = (Class<RpcService>)interfaceClass;
64         } catch (ComponentDefinitionException e) {
65             throw e;
66         } catch (Exception e) {
67             throw new ComponentDefinitionException(String.format("%s: Error obtaining interface class %s",
68                     logName(), interfaceName), e);
69         }
70     }
71
72     @Override
73     protected void startTracking() {
74         // First get the SchemaContext. This will be used to get the RPC SchemaPaths.
75
76         retrieveService("SchemaService", SchemaService.class,
77             service -> retrievedSchemaContext(((SchemaService)service).getGlobalContext()));
78     }
79
80     private void retrievedSchemaContext(SchemaContext schemaContext) {
81         LOG.debug("{}: retrievedSchemaContext", logName());
82
83         QNameModule moduleName = BindingReflections.getQNameModule(rpcInterface);
84         Module module = schemaContext.findModuleByNamespaceAndRevision(moduleName.getNamespace(),
85                 moduleName.getRevision());
86
87         LOG.debug("{}: Got Module: {}", logName(), module);
88
89         rpcSchemaPaths = new HashSet<>();
90         for (RpcDefinition rpcDef : module.getRpcs()) {
91             rpcSchemaPaths.add(rpcDef.getPath());
92         }
93
94         LOG.debug("{}: Got SchemaPaths: {}", logName(), rpcSchemaPaths);
95
96         // First get the DOMRpcService OSGi service. This will be used to register a listener to be notified
97         // when the underlying DOM RPC service is available.
98
99         retrieveService("DOMRpcService", DOMRpcService.class,
100             service -> retrievedDOMRpcService((DOMRpcService)service));
101     }
102
103     private void retrievedDOMRpcService(DOMRpcService domRpcService) {
104         LOG.debug("{}: retrievedDOMRpcService", logName());
105
106         rpcListenerReg = domRpcService.registerRpcListener(new DOMRpcAvailabilityListener() {
107             @Override
108             public void onRpcAvailable(Collection<DOMRpcIdentifier> rpcs) {
109                 onRpcsAvailable(rpcs);
110             }
111
112             @Override
113             public void onRpcUnavailable(Collection<DOMRpcIdentifier> rpcs) {
114             }
115         });
116     }
117
118     protected void onRpcsAvailable(Collection<DOMRpcIdentifier> rpcs) {
119         for (DOMRpcIdentifier identifier : rpcs) {
120             if (rpcSchemaPaths.contains(identifier.getType())) {
121                 LOG.debug("{}: onRpcsAvailable - found SchemaPath {}", logName(), identifier.getType());
122
123                 retrieveService("RpcProviderRegistry", RpcProviderRegistry.class, service -> {
124                     rpcRegistry = (RpcProviderRegistry)service;
125                     setSatisfied();
126                 });
127
128                 break;
129             }
130         }
131     }
132
133     @SuppressWarnings("checkstyle:IllegalCatch")
134     @Override
135     public Object create() throws ComponentDefinitionException {
136         LOG.debug("{}: In create: interfaceName: {}", logName(), interfaceName);
137
138         super.onCreate();
139
140         try {
141             RpcService rpcService = rpcRegistry.getRpcService(rpcInterface);
142
143             LOG.debug("{}: create returning service {}", logName(), rpcService);
144
145             return rpcService;
146         } catch (RuntimeException e) {
147             throw new ComponentDefinitionException("Error getting RPC service for " + interfaceName, e);
148         }
149     }
150
151     @Override
152     public void stopTracking() {
153         super.stopTracking();
154         closeRpcListenerReg();
155     }
156
157     private void closeRpcListenerReg() {
158         if (rpcListenerReg != null) {
159             rpcListenerReg.close();
160             rpcListenerReg = null;
161         }
162     }
163
164     @Override
165     public void destroy(Object instance) {
166         super.destroy(instance);
167         closeRpcListenerReg();
168     }
169
170     @Override
171     public String toString() {
172         return "RpcServiceMetadata [id=" + getId() + ", interfaceName=" + interfaceName + "]";
173     }
174 }