Update dependendency desc properly in RpcServiceMetadata
[controller.git] / opendaylight / blueprint / 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     @Override
52     @SuppressWarnings("unchecked")
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(Exception e) {
65             throw new ComponentDefinitionException(String.format("%s: Error obtaining interface class %s",
66                     logName(), interfaceName), e);
67         }
68     }
69
70     @Override
71     protected void startTracking() {
72         // First get the SchemaContext. This will be used to get the RPC SchemaPaths.
73
74         retrieveService("SchemaService", SchemaService.class,
75                 service -> retrievedSchemaContext(((SchemaService)service).getGlobalContext()));
76     }
77
78     private void retrievedSchemaContext(SchemaContext schemaContext) {
79         LOG.debug("{}: retrievedSchemaContext", logName());
80
81         QNameModule moduleName = BindingReflections.getQNameModule(rpcInterface);
82         Module module = schemaContext.findModuleByNamespaceAndRevision(moduleName.getNamespace(), moduleName.getRevision());
83
84         LOG.debug("{}: Got Module: {}", logName(), module);
85
86         rpcSchemaPaths = new HashSet<>();
87         for(RpcDefinition rpcDef : module.getRpcs()) {
88             rpcSchemaPaths.add(rpcDef.getPath());
89         }
90
91         LOG.debug("{}: Got SchemaPaths: {}", logName(), rpcSchemaPaths);
92
93         // First get the DOMRpcService OSGi service. This will be used to register a listener to be notified
94         // when the underlying DOM RPC service is available.
95
96         retrieveService("DOMRpcService", DOMRpcService.class, service -> retrievedDOMRpcService((DOMRpcService)service));
97     }
98
99     private void retrievedDOMRpcService(DOMRpcService domRpcService) {
100         LOG.debug("{}: retrievedDOMRpcService", logName());
101
102         setDependendencyDesc("Available DOM RPC for binding RPC: " + rpcInterface);
103         rpcListenerReg = domRpcService.registerRpcListener(new DOMRpcAvailabilityListener() {
104             @Override
105             public void onRpcAvailable(Collection<DOMRpcIdentifier> rpcs) {
106                 onRpcsAvailable(rpcs);
107             }
108
109             @Override
110             public void onRpcUnavailable(Collection<DOMRpcIdentifier> rpcs) {
111             }
112         });
113     }
114
115     protected void onRpcsAvailable(Collection<DOMRpcIdentifier> rpcs) {
116         for(DOMRpcIdentifier identifier: rpcs) {
117             if(rpcSchemaPaths.contains(identifier.getType())) {
118                 LOG.debug("{}: onRpcsAvailable - found SchemaPath {}", logName(), identifier.getType());
119
120                 retrieveService("RpcProviderRegistry", RpcProviderRegistry.class, service -> {
121                     rpcRegistry = (RpcProviderRegistry)service;
122                     setSatisfied();
123                 });
124
125                 break;
126             }
127         }
128     }
129
130     @Override
131     public Object create() throws ComponentDefinitionException {
132         LOG.debug("{}: In create: interfaceName: {}", logName(), interfaceName);
133
134         super.onCreate();
135
136         try {
137             RpcService rpcService = rpcRegistry.getRpcService(rpcInterface);
138
139             LOG.debug("{}: create returning service {}", logName(), rpcService);
140
141             return rpcService;
142         } catch(Exception e) {
143             throw new ComponentDefinitionException("Error getting RPC service for " + interfaceName, e);
144         }
145     }
146
147     @Override
148     public void stopTracking() {
149         super.stopTracking();
150         closeRpcListenerReg();
151     }
152
153     private void closeRpcListenerReg() {
154         if(rpcListenerReg != null) {
155             rpcListenerReg.close();
156             rpcListenerReg = null;
157         }
158     }
159
160     @Override
161     public void destroy(Object instance) {
162         super.destroy(instance);
163         closeRpcListenerReg();
164     }
165
166     @Override
167     public String toString() {
168         return "RpcServiceMetadata [id=" + getId() + ", interfaceName=" + interfaceName + "]";
169     }
170 }