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;
11 import java.io.IOException;
12 import java.io.StringReader;
14 import java.util.Collections;
16 import org.apache.aries.blueprint.ComponentDefinitionRegistry;
17 import org.apache.aries.blueprint.NamespaceHandler;
18 import org.apache.aries.blueprint.ParserContext;
19 import org.apache.aries.blueprint.ext.ComponentFactoryMetadata;
20 import org.apache.aries.blueprint.mutable.MutableBeanMetadata;
21 import org.apache.aries.blueprint.mutable.MutableRefMetadata;
22 import org.apache.aries.blueprint.mutable.MutableReferenceMetadata;
23 import org.apache.aries.blueprint.mutable.MutableServiceMetadata;
24 import org.apache.aries.blueprint.mutable.MutableServiceReferenceMetadata;
25 import org.apache.aries.blueprint.mutable.MutableValueMetadata;
26 import org.opendaylight.controller.blueprint.BlueprintContainerRestartService;
27 import org.opendaylight.mdsal.binding.api.NotificationService;
28 import org.opendaylight.mdsal.binding.api.RpcProviderService;
29 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
30 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
31 import org.opendaylight.yangtools.util.xml.UntrustedXML;
32 import org.osgi.service.blueprint.container.ComponentDefinitionException;
33 import org.osgi.service.blueprint.reflect.BeanMetadata;
34 import org.osgi.service.blueprint.reflect.ComponentMetadata;
35 import org.osgi.service.blueprint.reflect.Metadata;
36 import org.osgi.service.blueprint.reflect.RefMetadata;
37 import org.osgi.service.blueprint.reflect.ReferenceMetadata;
38 import org.osgi.service.blueprint.reflect.ServiceMetadata;
39 import org.osgi.service.blueprint.reflect.ServiceReferenceMetadata;
40 import org.osgi.service.blueprint.reflect.ValueMetadata;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.w3c.dom.Attr;
44 import org.w3c.dom.Element;
45 import org.w3c.dom.Node;
46 import org.w3c.dom.NodeList;
47 import org.xml.sax.InputSource;
48 import org.xml.sax.SAXException;
51 * The NamespaceHandler for Opendaylight blueprint extensions.
53 * @author Thomas Pantelis
55 public final class OpendaylightNamespaceHandler implements NamespaceHandler {
56 public static final String NAMESPACE_1_0_0 = "http://opendaylight.org/xmlns/blueprint/v1.0.0";
57 static final String ROUTED_RPC_REG_CONVERTER_NAME = "org.opendaylight.blueprint.RoutedRpcRegConverter";
58 static final String DOM_RPC_PROVIDER_SERVICE_NAME = "org.opendaylight.blueprint.DOMRpcProviderService";
59 static final String BINDING_RPC_PROVIDER_SERVICE_NAME = "org.opendaylight.blueprint.RpcProviderService";
60 static final String SCHEMA_SERVICE_NAME = "org.opendaylight.blueprint.SchemaService";
61 static final String NOTIFICATION_SERVICE_NAME = "org.opendaylight.blueprint.NotificationService";
62 static final String TYPE_ATTR = "type";
63 static final String UPDATE_STRATEGY_ATTR = "update-strategy";
65 private static final Logger LOG = LoggerFactory.getLogger(OpendaylightNamespaceHandler.class);
66 private static final String COMPONENT_PROCESSOR_NAME = ComponentProcessor.class.getName();
67 private static final String RESTART_DEPENDENTS_ON_UPDATES = "restart-dependents-on-updates";
68 private static final String USE_DEFAULT_FOR_REFERENCE_TYPES = "use-default-for-reference-types";
69 private static final String CLUSTERED_APP_CONFIG = "clustered-app-config";
70 private static final String INTERFACE = "interface";
71 private static final String REF_ATTR = "ref";
72 private static final String ID_ATTR = "id";
73 private static final String RPC_SERVICE = "rpc-service";
74 private static final String ACTION_SERVICE = "action-service";
75 private static final String SPECIFIC_SERVICE_REF_LIST = "specific-reference-list";
76 private static final String STATIC_REFERENCE = "static-reference";
78 @SuppressWarnings("rawtypes")
80 public Set<Class> getManagedClasses() {
81 return Collections.emptySet();
85 public URL getSchemaLocation(final String namespace) {
86 if (NAMESPACE_1_0_0.equals(namespace)) {
87 URL url = getClass().getResource("/opendaylight-blueprint-ext-1.0.0.xsd");
88 LOG.debug("getSchemaLocation for {} returning URL {}", namespace, url);
96 public Metadata parse(final Element element, final ParserContext context) {
97 LOG.debug("In parse for {}", element);
99 if (nodeNameEquals(element, RpcImplementationBean.RPC_IMPLEMENTATION)) {
100 return parseRpcImplementation(element, context);
101 } else if (nodeNameEquals(element, RoutedRpcMetadata.ROUTED_RPC_IMPLEMENTATION)) {
102 return parseRoutedRpcImplementation(element, context);
103 } else if (nodeNameEquals(element, RPC_SERVICE)) {
104 return parseRpcService(element, context);
105 } else if (nodeNameEquals(element, NotificationListenerBean.NOTIFICATION_LISTENER)) {
106 return parseNotificationListener(element, context);
107 } else if (nodeNameEquals(element, CLUSTERED_APP_CONFIG)) {
108 return parseClusteredAppConfig(element, context);
109 } else if (nodeNameEquals(element, SPECIFIC_SERVICE_REF_LIST)) {
110 return parseSpecificReferenceList(element, context);
111 } else if (nodeNameEquals(element, STATIC_REFERENCE)) {
112 return parseStaticReference(element, context);
113 } else if (nodeNameEquals(element, ACTION_SERVICE)) {
114 return parseActionService(element, context);
115 } else if (nodeNameEquals(element, ActionProviderBean.ACTION_PROVIDER)) {
116 return parseActionProvider(element, context);
119 throw new ComponentDefinitionException("Unsupported standalone element: " + element.getNodeName());
123 public ComponentMetadata decorate(final Node node, final ComponentMetadata component, final ParserContext context) {
124 if (node instanceof Attr) {
125 if (nodeNameEquals(node, RESTART_DEPENDENTS_ON_UPDATES)) {
126 return decorateRestartDependentsOnUpdates((Attr) node, component, context);
127 } else if (nodeNameEquals(node, USE_DEFAULT_FOR_REFERENCE_TYPES)) {
128 return decorateUseDefaultForReferenceTypes((Attr) node, component, context);
129 } else if (nodeNameEquals(node, TYPE_ATTR)) {
130 if (component instanceof ServiceReferenceMetadata) {
131 return decorateServiceReferenceType((Attr) node, component, context);
132 } else if (component instanceof ServiceMetadata) {
133 return decorateServiceType((Attr)node, component, context);
136 throw new ComponentDefinitionException("Attribute " + node.getNodeName()
137 + " can only be used on a <reference>, <reference-list> or <service> element");
140 throw new ComponentDefinitionException("Unsupported attribute: " + node.getNodeName());
142 throw new ComponentDefinitionException("Unsupported node type: " + node);
146 private static ComponentMetadata decorateServiceType(final Attr attr, final ComponentMetadata component,
147 final ParserContext context) {
148 if (!(component instanceof MutableServiceMetadata)) {
149 throw new ComponentDefinitionException("Expected an instanceof MutableServiceMetadata");
152 MutableServiceMetadata service = (MutableServiceMetadata)component;
154 LOG.debug("decorateServiceType for {} - adding type property {}", service.getId(), attr.getValue());
156 service.addServiceProperty(createValue(context, TYPE_ATTR), createValue(context, attr.getValue()));
160 private static ComponentMetadata decorateServiceReferenceType(final Attr attr, final ComponentMetadata component,
161 final ParserContext context) {
162 if (!(component instanceof MutableServiceReferenceMetadata)) {
163 throw new ComponentDefinitionException("Expected an instanceof MutableServiceReferenceMetadata");
166 // We don't actually need the ComponentProcessor for augmenting the OSGi filter here but we create it
167 // to workaround an issue in Aries where it doesn't use the extended filter unless there's a
168 // Processor or ComponentDefinitionRegistryProcessor registered. This may actually be working as
169 // designed in Aries b/c the extended filter was really added to allow the OSGi filter to be
170 // substituted by a variable via the "cm:property-placeholder" processor. If so, it's a bit funky
171 // but as long as there's at least one processor registered, it correctly uses the extended filter.
172 registerComponentProcessor(context);
174 MutableServiceReferenceMetadata serviceRef = (MutableServiceReferenceMetadata)component;
175 String oldFilter = serviceRef.getExtendedFilter() == null ? null :
176 serviceRef.getExtendedFilter().getStringValue();
179 if (Strings.isNullOrEmpty(oldFilter)) {
180 filter = String.format("(type=%s)", attr.getValue());
182 filter = String.format("(&(%s)(type=%s))", oldFilter, attr.getValue());
185 LOG.debug("decorateServiceReferenceType for {} with type {}, old filter: {}, new filter: {}",
186 serviceRef.getId(), attr.getValue(), oldFilter, filter);
188 serviceRef.setExtendedFilter(createValue(context, filter));
192 private static ComponentMetadata decorateRestartDependentsOnUpdates(final Attr attr,
193 final ComponentMetadata component, final ParserContext context) {
194 return enableComponentProcessorProperty(attr, component, context, "restartDependentsOnUpdates");
197 private static ComponentMetadata decorateUseDefaultForReferenceTypes(final Attr attr,
198 final ComponentMetadata component, final ParserContext context) {
199 return enableComponentProcessorProperty(attr, component, context, "useDefaultForReferenceTypes");
202 private static ComponentMetadata enableComponentProcessorProperty(final Attr attr,
203 final ComponentMetadata component, final ParserContext context, final String propertyName) {
204 if (component != null) {
205 throw new ComponentDefinitionException("Attribute " + attr.getNodeName()
206 + " can only be used on the root <blueprint> element");
209 LOG.debug("Property {} = {}", propertyName, attr.getValue());
211 if (!Boolean.parseBoolean(attr.getValue())) {
215 MutableBeanMetadata metadata = registerComponentProcessor(context);
216 metadata.addProperty(propertyName, createValue(context, "true"));
221 private static MutableBeanMetadata registerComponentProcessor(final ParserContext context) {
222 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
223 MutableBeanMetadata metadata = (MutableBeanMetadata) registry.getComponentDefinition(COMPONENT_PROCESSOR_NAME);
224 if (metadata == null) {
225 metadata = createBeanMetadata(context, COMPONENT_PROCESSOR_NAME, ComponentProcessor.class, false, true);
226 metadata.setProcessor(true);
227 addBlueprintBundleRefProperty(context, metadata);
228 metadata.addProperty("blueprintContainerRestartService", createServiceRef(context,
229 BlueprintContainerRestartService.class, null));
231 LOG.debug("Registering ComponentProcessor bean: {}", metadata);
233 registry.registerComponentDefinition(metadata);
239 private static Metadata parseActionProvider(final Element element, final ParserContext context) {
240 registerDomRpcProviderServiceRefBean(context);
241 registerBindingRpcProviderServiceRefBean(context);
242 registerSchemaServiceRefBean(context);
244 MutableBeanMetadata metadata = createBeanMetadata(context, context.generateId(), ActionProviderBean.class,
246 addBlueprintBundleRefProperty(context, metadata);
247 metadata.addProperty("domRpcProvider", createRef(context, DOM_RPC_PROVIDER_SERVICE_NAME));
248 metadata.addProperty("bindingRpcProvider", createRef(context, BINDING_RPC_PROVIDER_SERVICE_NAME));
249 metadata.addProperty("schemaService", createRef(context, SCHEMA_SERVICE_NAME));
250 metadata.addProperty("interfaceName", createValue(context, element.getAttribute(INTERFACE)));
252 if (element.hasAttribute(REF_ATTR)) {
253 metadata.addProperty("implementation", createRef(context, element.getAttribute(REF_ATTR)));
256 LOG.debug("parseActionProvider returning {}", metadata);
261 private static Metadata parseRpcImplementation(final Element element, final ParserContext context) {
262 registerBindingRpcProviderServiceRefBean(context);
264 MutableBeanMetadata metadata = createBeanMetadata(context, context.generateId(), RpcImplementationBean.class,
266 addBlueprintBundleRefProperty(context, metadata);
267 metadata.addProperty("rpcProvider", createRef(context, BINDING_RPC_PROVIDER_SERVICE_NAME));
268 metadata.addProperty("implementation", createRef(context, element.getAttribute(REF_ATTR)));
270 if (element.hasAttribute(INTERFACE)) {
271 metadata.addProperty("interfaceName", createValue(context, element.getAttribute(INTERFACE)));
274 LOG.debug("parseRpcImplementation returning {}", metadata);
278 private static Metadata parseRoutedRpcImplementation(final Element element, final ParserContext context) {
279 registerBindingRpcProviderServiceRefBean(context);
280 registerRoutedRpcRegistrationConverter(context);
282 ComponentFactoryMetadata metadata = new RoutedRpcMetadata(getId(context, element),
283 element.getAttribute(INTERFACE), element.getAttribute(REF_ATTR));
285 LOG.debug("parseRoutedRpcImplementation returning {}", metadata);
290 private static Metadata parseActionService(final Element element, final ParserContext context) {
291 ComponentFactoryMetadata metadata = new ActionServiceMetadata(getId(context, element),
292 element.getAttribute(INTERFACE));
294 LOG.debug("parseActionService returning {}", metadata);
299 private static Metadata parseRpcService(final Element element, final ParserContext context) {
300 ComponentFactoryMetadata metadata = new RpcServiceMetadata(getId(context, element),
301 element.getAttribute(INTERFACE));
303 LOG.debug("parseRpcService returning {}", metadata);
308 private static void registerRoutedRpcRegistrationConverter(final ParserContext context) {
309 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
310 if (registry.getComponentDefinition(ROUTED_RPC_REG_CONVERTER_NAME) == null) {
311 MutableBeanMetadata metadata = createBeanMetadata(context, ROUTED_RPC_REG_CONVERTER_NAME,
312 RoutedRpcRegistrationConverter.class, false, false);
313 metadata.setActivation(ReferenceMetadata.ACTIVATION_LAZY);
314 registry.registerTypeConverter(metadata);
318 private static void registerDomRpcProviderServiceRefBean(final ParserContext context) {
319 registerRefBean(context, DOM_RPC_PROVIDER_SERVICE_NAME, DOMRpcProviderService.class);
322 private static void registerBindingRpcProviderServiceRefBean(final ParserContext context) {
323 registerRefBean(context, BINDING_RPC_PROVIDER_SERVICE_NAME, RpcProviderService.class);
326 private static void registerSchemaServiceRefBean(final ParserContext context) {
327 registerRefBean(context, SCHEMA_SERVICE_NAME, DOMSchemaService.class);
330 private static void registerRefBean(final ParserContext context, final String name, final Class<?> clazz) {
331 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
332 if (registry.getComponentDefinition(name) == null) {
333 MutableReferenceMetadata metadata = createServiceRef(context, clazz, null);
334 metadata.setId(name);
335 registry.registerComponentDefinition(metadata);
339 private static Metadata parseNotificationListener(final Element element, final ParserContext context) {
340 registerNotificationServiceRefBean(context);
342 MutableBeanMetadata metadata = createBeanMetadata(context, context.generateId(), NotificationListenerBean.class,
344 addBlueprintBundleRefProperty(context, metadata);
345 metadata.addProperty("notificationService", createRef(context, NOTIFICATION_SERVICE_NAME));
346 metadata.addProperty("notificationListener", createRef(context, element.getAttribute(REF_ATTR)));
348 LOG.debug("parseNotificationListener returning {}", metadata);
353 private static void registerNotificationServiceRefBean(final ParserContext context) {
354 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
355 if (registry.getComponentDefinition(NOTIFICATION_SERVICE_NAME) == null) {
356 MutableReferenceMetadata metadata = createServiceRef(context, NotificationService.class, null);
357 metadata.setId(NOTIFICATION_SERVICE_NAME);
358 registry.registerComponentDefinition(metadata);
362 private static Metadata parseClusteredAppConfig(final Element element, final ParserContext context) {
363 LOG.debug("parseClusteredAppConfig");
365 // Find the default-config child element representing the default app config XML, if present.
366 Element defaultConfigElement = null;
367 NodeList children = element.getChildNodes();
368 for (int i = 0; i < children.getLength(); i++) {
369 Node child = children.item(i);
370 if (nodeNameEquals(child, DataStoreAppConfigMetadata.DEFAULT_CONFIG)) {
371 defaultConfigElement = (Element) child;
376 Element defaultAppConfigElement = null;
377 if (defaultConfigElement != null) {
378 // Find the CDATA element containing the default app config XML.
379 children = defaultConfigElement.getChildNodes();
380 for (int i = 0; i < children.getLength(); i++) {
381 Node child = children.item(i);
382 if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
383 defaultAppConfigElement = parseXML(DataStoreAppConfigMetadata.DEFAULT_CONFIG,
384 child.getTextContent());
390 return new DataStoreAppConfigMetadata(getId(context, element), element.getAttribute(
391 DataStoreAppConfigMetadata.BINDING_CLASS), element.getAttribute(
392 DataStoreAppConfigMetadata.LIST_KEY_VALUE), element.getAttribute(
393 DataStoreAppConfigMetadata.DEFAULT_CONFIG_FILE_NAME), parseUpdateStrategy(
394 element.getAttribute(UPDATE_STRATEGY_ATTR)), defaultAppConfigElement);
397 private static UpdateStrategy parseUpdateStrategy(final String updateStrategyValue) {
398 if (Strings.isNullOrEmpty(updateStrategyValue)
399 || updateStrategyValue.equalsIgnoreCase(UpdateStrategy.RELOAD.name())) {
400 return UpdateStrategy.RELOAD;
401 } else if (updateStrategyValue.equalsIgnoreCase(UpdateStrategy.NONE.name())) {
402 return UpdateStrategy.NONE;
404 LOG.warn("update-strategy {} not supported, using reload", updateStrategyValue);
405 return UpdateStrategy.RELOAD;
409 private static Metadata parseSpecificReferenceList(final Element element, final ParserContext context) {
410 ComponentFactoryMetadata metadata = new SpecificReferenceListMetadata(getId(context, element),
411 element.getAttribute(INTERFACE));
413 LOG.debug("parseSpecificReferenceList returning {}", metadata);
418 private static Metadata parseStaticReference(final Element element, final ParserContext context) {
419 ComponentFactoryMetadata metadata = new StaticReferenceMetadata(getId(context, element),
420 element.getAttribute(INTERFACE));
422 LOG.debug("parseStaticReference returning {}", metadata);
427 private static Element parseXML(final String name, final String xml) {
429 return UntrustedXML.newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement();
430 } catch (SAXException | IOException e) {
431 throw new ComponentDefinitionException(String.format("Error %s parsing XML: %s", name, xml), e);
435 private static ValueMetadata createValue(final ParserContext context, final String value) {
436 MutableValueMetadata metadata = context.createMetadata(MutableValueMetadata.class);
437 metadata.setStringValue(value);
441 private static MutableReferenceMetadata createServiceRef(final ParserContext context, final Class<?> cls,
442 final String filter) {
443 MutableReferenceMetadata metadata = context.createMetadata(MutableReferenceMetadata.class);
444 metadata.setRuntimeInterface(cls);
445 metadata.setInterface(cls.getName());
446 metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
447 metadata.setAvailability(ReferenceMetadata.AVAILABILITY_MANDATORY);
449 if (filter != null) {
450 metadata.setFilter(filter);
456 private static RefMetadata createRef(final ParserContext context, final String id) {
457 MutableRefMetadata metadata = context.createMetadata(MutableRefMetadata.class);
458 metadata.setComponentId(id);
462 private static String getId(final ParserContext context, final Element element) {
463 if (element.hasAttribute(ID_ATTR)) {
464 return element.getAttribute(ID_ATTR);
466 return context.generateId();
470 private static boolean nodeNameEquals(final Node node, final String name) {
471 return name.equals(node.getNodeName()) || name.equals(node.getLocalName());
474 private static void addBlueprintBundleRefProperty(final ParserContext context, final MutableBeanMetadata metadata) {
475 metadata.addProperty("bundle", createRef(context, "blueprintBundle"));
478 private static MutableBeanMetadata createBeanMetadata(final ParserContext context, final String id,
479 final Class<?> runtimeClass, final boolean initMethod, final boolean destroyMethod) {
480 MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
482 metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
483 metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
484 metadata.setRuntimeClass(runtimeClass);
487 metadata.setInitMethod("init");
491 metadata.setDestroyMethod("destroy");