2 * Copyright (c) 2016 Brocade Communications 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.blueprint.ext;
10 import com.google.common.base.Strings;
12 import java.util.Collections;
14 import org.apache.aries.blueprint.ComponentDefinitionRegistry;
15 import org.apache.aries.blueprint.NamespaceHandler;
16 import org.apache.aries.blueprint.ParserContext;
17 import org.apache.aries.blueprint.ext.ComponentFactoryMetadata;
18 import org.apache.aries.blueprint.mutable.MutableBeanMetadata;
19 import org.apache.aries.blueprint.mutable.MutableRefMetadata;
20 import org.apache.aries.blueprint.mutable.MutableReferenceMetadata;
21 import org.apache.aries.blueprint.mutable.MutableServiceMetadata;
22 import org.apache.aries.blueprint.mutable.MutableServiceReferenceMetadata;
23 import org.apache.aries.blueprint.mutable.MutableValueMetadata;
24 import org.opendaylight.controller.blueprint.BlueprintContainerRestartService;
25 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
26 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
27 import org.osgi.service.blueprint.container.ComponentDefinitionException;
28 import org.osgi.service.blueprint.reflect.BeanMetadata;
29 import org.osgi.service.blueprint.reflect.ComponentMetadata;
30 import org.osgi.service.blueprint.reflect.Metadata;
31 import org.osgi.service.blueprint.reflect.RefMetadata;
32 import org.osgi.service.blueprint.reflect.ReferenceMetadata;
33 import org.osgi.service.blueprint.reflect.ServiceMetadata;
34 import org.osgi.service.blueprint.reflect.ServiceReferenceMetadata;
35 import org.osgi.service.blueprint.reflect.ValueMetadata;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.w3c.dom.Attr;
39 import org.w3c.dom.Element;
40 import org.w3c.dom.Node;
43 * The NamespaceHandler for Opendaylight blueprint extensions.
45 * @author Thomas Pantelis
47 public class OpendaylightNamespaceHandler implements NamespaceHandler {
48 public static final String NAMESPACE_1_0_0 = "http://opendaylight.org/xmlns/blueprint/v1.0.0";
49 static final String ROUTED_RPC_REG_CONVERTER_NAME = "org.opendaylight.blueprint.RoutedRpcRegConverter";
50 static final String RPC_REGISTRY_NAME = "org.opendaylight.blueprint.RpcRegistry";
51 static final String NOTIFICATION_SERVICE_NAME = "org.opendaylight.blueprint.NotificationService";
53 private static final Logger LOG = LoggerFactory.getLogger(OpendaylightNamespaceHandler.class);
54 private static final String COMPONENT_PROCESSOR_NAME = ComponentProcessor.class.getName();
55 private static final String RESTART_DEPENDENTS_ON_UPDATES = "restart-dependents-on-updates";
56 private static final String USE_DEFAULT_FOR_REFERENCE_TYPES = "use-default-for-reference-types";
57 private static final String TYPE_ATTR = "type";
58 private static final String INTERFACE = "interface";
59 private static final String REF_ATTR = "ref";
60 private static final String ID_ATTR = "id";
61 private static final String RPC_SERVICE = "rpc-service";
63 @SuppressWarnings("rawtypes")
65 public Set<Class> getManagedClasses() {
66 return Collections.emptySet();
70 public URL getSchemaLocation(String namespace) {
71 if(NAMESPACE_1_0_0.equals(namespace)) {
72 URL url = getClass().getResource("/opendaylight-blueprint-ext-1.0.0.xsd");
73 LOG.debug("getSchemaLocation for {} returning URL {}", namespace, url);
81 public Metadata parse(Element element, ParserContext context) {
82 LOG.debug("In parse for {}", element);
84 if (nodeNameEquals(element, RpcImplementationBean.RPC_IMPLEMENTATION)) {
85 return parseRpcImplementation(element, context);
86 } else if (nodeNameEquals(element, RoutedRpcMetadata.ROUTED_RPC_IMPLEMENTATION)) {
87 return parseRoutedRpcImplementation(element, context);
88 } else if (nodeNameEquals(element, RPC_SERVICE)) {
89 return parseRpcService(element, context);
90 } else if (nodeNameEquals(element, NotificationListenerBean.NOTIFICATION_LISTENER)) {
91 return parseNotificationListener(element, context);
94 throw new ComponentDefinitionException("Unsupported standalone element: " + element.getNodeName());
98 public ComponentMetadata decorate(Node node, ComponentMetadata component, ParserContext context) {
99 if(node instanceof Attr) {
100 if (nodeNameEquals(node, RESTART_DEPENDENTS_ON_UPDATES)) {
101 return decorateRestartDependentsOnUpdates((Attr)node, component, context);
102 } else if (nodeNameEquals(node, USE_DEFAULT_FOR_REFERENCE_TYPES)) {
103 return decorateUseDefaultForReferenceTypes((Attr)node, component, context);
104 } else if (nodeNameEquals(node, TYPE_ATTR)) {
105 if(component instanceof ServiceReferenceMetadata) {
106 return decorateServiceReferenceType((Attr)node, component, context);
107 } else if(component instanceof ServiceMetadata) {
108 return decorateServiceType((Attr)node, component, context);
111 throw new ComponentDefinitionException("Attribute " + node.getNodeName() +
112 " can only be used on a <reference>, <reference-list> or <service> element");
115 throw new ComponentDefinitionException("Unsupported attribute: " + node.getNodeName());
117 throw new ComponentDefinitionException("Unsupported node type: " + node);
121 private ComponentMetadata decorateServiceType(Attr attr, ComponentMetadata component, ParserContext context) {
122 if (!(component instanceof MutableServiceMetadata)) {
123 throw new ComponentDefinitionException("Expected an instanceof MutableServiceMetadata");
126 MutableServiceMetadata service = (MutableServiceMetadata)component;
128 LOG.debug("decorateServiceType for {} - adding type property {}", service.getId(), attr.getValue());
130 service.addServiceProperty(createValue(context, TYPE_ATTR), createValue(context, attr.getValue()));
134 private ComponentMetadata decorateServiceReferenceType(Attr attr, ComponentMetadata component, ParserContext context) {
135 if (!(component instanceof MutableServiceReferenceMetadata)) {
136 throw new ComponentDefinitionException("Expected an instanceof MutableServiceReferenceMetadata");
139 // We don't actually need the ComponentProcessor for augmenting the OSGi filter here but we create it
140 // to workaround an issue in Aries where it doesn't use the extended filter unless there's a
141 // Processor or ComponentDefinitionRegistryProcessor registered. This may actually be working as
142 // designed in Aries b/c the extended filter was really added to allow the OSGi filter to be
143 // substituted by a variable via the "cm:property-placeholder" processor. If so, it's a bit funky
144 // but as long as there's at least one processor registered, it correctly uses the extended filter.
145 registerComponentProcessor(context);
147 MutableServiceReferenceMetadata serviceRef = (MutableServiceReferenceMetadata)component;
148 String oldFilter = serviceRef.getExtendedFilter() == null ? null :
149 serviceRef.getExtendedFilter().getStringValue();
152 if(Strings.isNullOrEmpty(oldFilter)) {
153 filter = String.format("(type=%s)", attr.getValue());
155 filter = String.format("(&(%s)(type=%s))", oldFilter, attr.getValue());
158 LOG.debug("decorateServiceReferenceType for {} with type {}, old filter: {}, new filter: {}",
159 serviceRef.getId(), attr.getValue(), oldFilter, filter);
161 serviceRef.setExtendedFilter(createValue(context, filter));
165 private static ComponentMetadata decorateRestartDependentsOnUpdates(Attr attr, ComponentMetadata component,
166 ParserContext context) {
167 return enableComponentProcessorProperty(attr, component, context, "restartDependentsOnUpdates");
170 private static ComponentMetadata decorateUseDefaultForReferenceTypes(Attr attr, ComponentMetadata component,
171 ParserContext context) {
172 return enableComponentProcessorProperty(attr, component, context, "useDefaultForReferenceTypes");
175 private static ComponentMetadata enableComponentProcessorProperty(Attr attr, ComponentMetadata component,
176 ParserContext context, String propertyName) {
177 if(component != null) {
178 throw new ComponentDefinitionException("Attribute " + attr.getNodeName() +
179 " can only be used on the root <blueprint> element");
182 LOG.debug("{}: {}", propertyName, attr.getValue());
184 if(!Boolean.TRUE.equals(Boolean.valueOf(attr.getValue()))) {
188 MutableBeanMetadata metadata = registerComponentProcessor(context);
189 metadata.addProperty(propertyName, createValue(context, "true"));
194 private static MutableBeanMetadata registerComponentProcessor(ParserContext context) {
195 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
196 MutableBeanMetadata metadata = (MutableBeanMetadata) registry.getComponentDefinition(COMPONENT_PROCESSOR_NAME);
197 if(metadata == null) {
198 metadata = context.createMetadata(MutableBeanMetadata.class);
199 metadata.setProcessor(true);
200 metadata.setId(COMPONENT_PROCESSOR_NAME);
201 metadata.setActivation(BeanMetadata.ACTIVATION_EAGER);
202 metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
203 metadata.setRuntimeClass(ComponentProcessor.class);
204 metadata.setDestroyMethod("destroy");
205 metadata.addProperty("bundle", createRef(context, "blueprintBundle"));
206 metadata.addProperty("blueprintContainerRestartService", createServiceRef(context,
207 BlueprintContainerRestartService.class, null));
209 LOG.debug("Registering ComponentProcessor bean: {}", metadata);
211 registry.registerComponentDefinition(metadata);
217 private Metadata parseRpcImplementation(Element element, ParserContext context) {
218 registerRpcRegistryServiceRefBean(context);
220 MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
221 metadata.setId(context.generateId());
222 metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
223 metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
224 metadata.setRuntimeClass(RpcImplementationBean.class);
225 metadata.setInitMethod("init");
226 metadata.setDestroyMethod("destroy");
227 metadata.addProperty("bundle", createRef(context, "blueprintBundle"));
228 metadata.addProperty("rpcRegistry", createRef(context, RPC_REGISTRY_NAME));
229 metadata.addProperty("implementation", createRef(context, element.getAttribute(REF_ATTR)));
231 if(element.hasAttribute(INTERFACE)) {
232 metadata.addProperty("interfaceName", createValue(context, element.getAttribute(INTERFACE)));
235 LOG.debug("parseAddRpcImplementation returning {}", metadata);
240 private Metadata parseRoutedRpcImplementation(Element element, ParserContext context) {
241 registerRpcRegistryServiceRefBean(context);
242 registerRoutedRpcRegistrationConverter(context);
244 ComponentFactoryMetadata metadata = new RoutedRpcMetadata(getId(context, element),
245 element.getAttribute(INTERFACE), element.getAttribute(REF_ATTR));
247 LOG.debug("parseRoutedRpcImplementation returning {}", metadata);
252 private Metadata parseRpcService(Element element, ParserContext context) {
253 registerRpcRegistryServiceRefBean(context);
255 ComponentFactoryMetadata metadata = new RpcServiceMetadata(getId(context, element),
256 element.getAttribute(INTERFACE));
258 LOG.debug("parseRpcService returning {}", metadata);
263 private void registerRoutedRpcRegistrationConverter(ParserContext context) {
264 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
265 if(registry.getComponentDefinition(ROUTED_RPC_REG_CONVERTER_NAME) == null) {
266 MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
267 metadata.setId(ROUTED_RPC_REG_CONVERTER_NAME);
268 metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
269 metadata.setActivation(ReferenceMetadata.ACTIVATION_LAZY);
270 metadata.setRuntimeClass(RoutedRpcRegistrationConverter.class);
271 registry.registerTypeConverter(metadata);
275 private void registerRpcRegistryServiceRefBean(ParserContext context) {
276 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
277 if(registry.getComponentDefinition(RPC_REGISTRY_NAME) == null) {
278 MutableReferenceMetadata metadata = createServiceRef(context, RpcProviderRegistry.class, null);
279 metadata.setId(RPC_REGISTRY_NAME);
280 registry.registerComponentDefinition(metadata);
284 private Metadata parseNotificationListener(Element element, ParserContext context) {
285 registerNotificationServiceRefBean(context);
287 MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
288 metadata.setId(context.generateId());
289 metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
290 metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
291 metadata.setRuntimeClass(NotificationListenerBean.class);
292 metadata.setInitMethod("init");
293 metadata.setDestroyMethod("destroy");
294 metadata.addProperty("bundle", createRef(context, "blueprintBundle"));
295 metadata.addProperty("notificationService", createRef(context, NOTIFICATION_SERVICE_NAME));
296 metadata.addProperty("notificationListener", createRef(context, element.getAttribute(REF_ATTR)));
298 LOG.debug("parseNotificationListener returning {}", metadata);
303 private void registerNotificationServiceRefBean(ParserContext context) {
304 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
305 if(registry.getComponentDefinition(NOTIFICATION_SERVICE_NAME) == null) {
306 MutableReferenceMetadata metadata = createServiceRef(context, NotificationService.class, null);
307 metadata.setId(NOTIFICATION_SERVICE_NAME);
308 registry.registerComponentDefinition(metadata);
312 private static ValueMetadata createValue(ParserContext context, String value) {
313 MutableValueMetadata m = context.createMetadata(MutableValueMetadata.class);
314 m.setStringValue(value);
318 private static MutableReferenceMetadata createServiceRef(ParserContext context, Class<?> cls, String filter) {
319 MutableReferenceMetadata m = context.createMetadata(MutableReferenceMetadata.class);
320 m.setRuntimeInterface(cls);
321 m.setInterface(cls.getName());
322 m.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
323 m.setAvailability(ReferenceMetadata.AVAILABILITY_MANDATORY);
332 private static RefMetadata createRef(ParserContext context, String id) {
333 MutableRefMetadata metadata = context.createMetadata(MutableRefMetadata.class);
334 metadata.setComponentId(id);
338 private static String getId(ParserContext context, Element element) {
339 if(element.hasAttribute(ID_ATTR)) {
340 return element.getAttribute(ID_ATTR);
342 return context.generateId();
346 private static boolean nodeNameEquals(Node node, String name) {
347 return name.equals(node.getNodeName()) || name.equals(node.getLocalName());