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.controller.sal.binding.api.RpcProviderRegistry;
28 import org.opendaylight.mdsal.binding.api.NotificationService;
29 import org.opendaylight.mdsal.binding.api.RpcProviderService;
30 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
31 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
32 import org.opendaylight.yangtools.util.xml.UntrustedXML;
33 import org.osgi.service.blueprint.container.ComponentDefinitionException;
34 import org.osgi.service.blueprint.reflect.BeanMetadata;
35 import org.osgi.service.blueprint.reflect.ComponentMetadata;
36 import org.osgi.service.blueprint.reflect.Metadata;
37 import org.osgi.service.blueprint.reflect.RefMetadata;
38 import org.osgi.service.blueprint.reflect.ReferenceMetadata;
39 import org.osgi.service.blueprint.reflect.ServiceMetadata;
40 import org.osgi.service.blueprint.reflect.ServiceReferenceMetadata;
41 import org.osgi.service.blueprint.reflect.ValueMetadata;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.w3c.dom.Attr;
45 import org.w3c.dom.Element;
46 import org.w3c.dom.Node;
47 import org.w3c.dom.NodeList;
48 import org.xml.sax.InputSource;
49 import org.xml.sax.SAXException;
52 * The NamespaceHandler for Opendaylight blueprint extensions.
54 * @author Thomas Pantelis
56 public final class OpendaylightNamespaceHandler implements NamespaceHandler {
57 public static final String NAMESPACE_1_0_0 = "http://opendaylight.org/xmlns/blueprint/v1.0.0";
58 static final String ROUTED_RPC_REG_CONVERTER_NAME = "org.opendaylight.blueprint.RoutedRpcRegConverter";
59 static final String DOM_RPC_PROVIDER_SERVICE_NAME = "org.opendaylight.blueprint.DOMRpcProviderService";
60 static final String RPC_REGISTRY_NAME = "org.opendaylight.blueprint.RpcRegistry";
61 static final String BINDING_RPC_PROVIDER_SERVICE_NAME = "org.opendaylight.blueprint.RpcProviderService";
62 static final String SCHEMA_SERVICE_NAME = "org.opendaylight.blueprint.SchemaService";
63 static final String NOTIFICATION_SERVICE_NAME = "org.opendaylight.blueprint.NotificationService";
64 static final String TYPE_ATTR = "type";
65 static final String UPDATE_STRATEGY_ATTR = "update-strategy";
67 private static final Logger LOG = LoggerFactory.getLogger(OpendaylightNamespaceHandler.class);
68 private static final String COMPONENT_PROCESSOR_NAME = ComponentProcessor.class.getName();
69 private static final String RESTART_DEPENDENTS_ON_UPDATES = "restart-dependents-on-updates";
70 private static final String USE_DEFAULT_FOR_REFERENCE_TYPES = "use-default-for-reference-types";
71 private static final String CLUSTERED_APP_CONFIG = "clustered-app-config";
72 private static final String INTERFACE = "interface";
73 private static final String REF_ATTR = "ref";
74 private static final String ID_ATTR = "id";
75 private static final String RPC_SERVICE = "rpc-service";
76 private static final String ACTION_SERVICE = "action-service";
77 private static final String SPECIFIC_SERVICE_REF_LIST = "specific-reference-list";
78 private static final String STATIC_REFERENCE = "static-reference";
80 @SuppressWarnings("rawtypes")
82 public Set<Class> getManagedClasses() {
83 return Collections.emptySet();
87 public URL getSchemaLocation(final String namespace) {
88 if (NAMESPACE_1_0_0.equals(namespace)) {
89 URL url = getClass().getResource("/opendaylight-blueprint-ext-1.0.0.xsd");
90 LOG.debug("getSchemaLocation for {} returning URL {}", namespace, url);
98 public Metadata parse(final Element element, final ParserContext context) {
99 LOG.debug("In parse for {}", element);
101 if (nodeNameEquals(element, RpcImplementationBean.RPC_IMPLEMENTATION)) {
102 return parseRpcImplementation(element, context);
103 } else if (nodeNameEquals(element, RoutedRpcMetadata.ROUTED_RPC_IMPLEMENTATION)) {
104 return parseRoutedRpcImplementation(element, context);
105 } else if (nodeNameEquals(element, RPC_SERVICE)) {
106 return parseRpcService(element, context);
107 } else if (nodeNameEquals(element, NotificationListenerBean.NOTIFICATION_LISTENER)) {
108 return parseNotificationListener(element, context);
109 } else if (nodeNameEquals(element, CLUSTERED_APP_CONFIG)) {
110 return parseClusteredAppConfig(element, context);
111 } else if (nodeNameEquals(element, SPECIFIC_SERVICE_REF_LIST)) {
112 return parseSpecificReferenceList(element, context);
113 } else if (nodeNameEquals(element, STATIC_REFERENCE)) {
114 return parseStaticReference(element, context);
115 } else if (nodeNameEquals(element, ACTION_SERVICE)) {
116 return parseActionService(element, context);
117 } else if (nodeNameEquals(element, ActionProviderBean.ACTION_PROVIDER)) {
118 return parseActionProvider(element, context);
121 throw new ComponentDefinitionException("Unsupported standalone element: " + element.getNodeName());
125 public ComponentMetadata decorate(final Node node, final ComponentMetadata component, final ParserContext context) {
126 if (node instanceof Attr) {
127 if (nodeNameEquals(node, RESTART_DEPENDENTS_ON_UPDATES)) {
128 return decorateRestartDependentsOnUpdates((Attr) node, component, context);
129 } else if (nodeNameEquals(node, USE_DEFAULT_FOR_REFERENCE_TYPES)) {
130 return decorateUseDefaultForReferenceTypes((Attr) node, component, context);
131 } else if (nodeNameEquals(node, TYPE_ATTR)) {
132 if (component instanceof ServiceReferenceMetadata) {
133 return decorateServiceReferenceType((Attr) node, component, context);
134 } else if (component instanceof ServiceMetadata) {
135 return decorateServiceType((Attr)node, component, context);
138 throw new ComponentDefinitionException("Attribute " + node.getNodeName()
139 + " can only be used on a <reference>, <reference-list> or <service> element");
142 throw new ComponentDefinitionException("Unsupported attribute: " + node.getNodeName());
144 throw new ComponentDefinitionException("Unsupported node type: " + node);
148 private static ComponentMetadata decorateServiceType(final Attr attr, final ComponentMetadata component,
149 final ParserContext context) {
150 if (!(component instanceof MutableServiceMetadata)) {
151 throw new ComponentDefinitionException("Expected an instanceof MutableServiceMetadata");
154 MutableServiceMetadata service = (MutableServiceMetadata)component;
156 LOG.debug("decorateServiceType for {} - adding type property {}", service.getId(), attr.getValue());
158 service.addServiceProperty(createValue(context, TYPE_ATTR), createValue(context, attr.getValue()));
162 private static ComponentMetadata decorateServiceReferenceType(final Attr attr, final ComponentMetadata component,
163 final ParserContext context) {
164 if (!(component instanceof MutableServiceReferenceMetadata)) {
165 throw new ComponentDefinitionException("Expected an instanceof MutableServiceReferenceMetadata");
168 // We don't actually need the ComponentProcessor for augmenting the OSGi filter here but we create it
169 // to workaround an issue in Aries where it doesn't use the extended filter unless there's a
170 // Processor or ComponentDefinitionRegistryProcessor registered. This may actually be working as
171 // designed in Aries b/c the extended filter was really added to allow the OSGi filter to be
172 // substituted by a variable via the "cm:property-placeholder" processor. If so, it's a bit funky
173 // but as long as there's at least one processor registered, it correctly uses the extended filter.
174 registerComponentProcessor(context);
176 MutableServiceReferenceMetadata serviceRef = (MutableServiceReferenceMetadata)component;
177 String oldFilter = serviceRef.getExtendedFilter() == null ? null :
178 serviceRef.getExtendedFilter().getStringValue();
181 if (Strings.isNullOrEmpty(oldFilter)) {
182 filter = String.format("(type=%s)", attr.getValue());
184 filter = String.format("(&(%s)(type=%s))", oldFilter, attr.getValue());
187 LOG.debug("decorateServiceReferenceType for {} with type {}, old filter: {}, new filter: {}",
188 serviceRef.getId(), attr.getValue(), oldFilter, filter);
190 serviceRef.setExtendedFilter(createValue(context, filter));
194 private static ComponentMetadata decorateRestartDependentsOnUpdates(final Attr attr,
195 final ComponentMetadata component, final ParserContext context) {
196 return enableComponentProcessorProperty(attr, component, context, "restartDependentsOnUpdates");
199 private static ComponentMetadata decorateUseDefaultForReferenceTypes(final Attr attr,
200 final ComponentMetadata component, final ParserContext context) {
201 return enableComponentProcessorProperty(attr, component, context, "useDefaultForReferenceTypes");
204 private static ComponentMetadata enableComponentProcessorProperty(final Attr attr,
205 final ComponentMetadata component, final ParserContext context, final String propertyName) {
206 if (component != null) {
207 throw new ComponentDefinitionException("Attribute " + attr.getNodeName()
208 + " can only be used on the root <blueprint> element");
211 LOG.debug("Property {} = {}", propertyName, attr.getValue());
213 if (!Boolean.parseBoolean(attr.getValue())) {
217 MutableBeanMetadata metadata = registerComponentProcessor(context);
218 metadata.addProperty(propertyName, createValue(context, "true"));
223 private static MutableBeanMetadata registerComponentProcessor(final ParserContext context) {
224 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
225 MutableBeanMetadata metadata = (MutableBeanMetadata) registry.getComponentDefinition(COMPONENT_PROCESSOR_NAME);
226 if (metadata == null) {
227 metadata = createBeanMetadata(context, COMPONENT_PROCESSOR_NAME, ComponentProcessor.class, false, true);
228 metadata.setProcessor(true);
229 addBlueprintBundleRefProperty(context, metadata);
230 metadata.addProperty("blueprintContainerRestartService", createServiceRef(context,
231 BlueprintContainerRestartService.class, null));
233 LOG.debug("Registering ComponentProcessor bean: {}", metadata);
235 registry.registerComponentDefinition(metadata);
241 private static Metadata parseActionProvider(final Element element, final ParserContext context) {
242 registerDomRpcProviderServiceRefBean(context);
243 registerBindingRpcProviderServiceRefBean(context);
244 registerSchemaServiceRefBean(context);
246 MutableBeanMetadata metadata = createBeanMetadata(context, context.generateId(), ActionProviderBean.class,
248 addBlueprintBundleRefProperty(context, metadata);
249 metadata.addProperty("domRpcProvider", createRef(context, DOM_RPC_PROVIDER_SERVICE_NAME));
250 metadata.addProperty("bindingRpcProvider", createRef(context, BINDING_RPC_PROVIDER_SERVICE_NAME));
251 metadata.addProperty("schemaService", createRef(context, SCHEMA_SERVICE_NAME));
252 metadata.addProperty("interfaceName", createValue(context, element.getAttribute(INTERFACE)));
254 if (element.hasAttribute(REF_ATTR)) {
255 metadata.addProperty("implementation", createRef(context, element.getAttribute(REF_ATTR)));
258 LOG.debug("parseActionProvider returning {}", metadata);
263 private static Metadata parseRpcImplementation(final Element element, final ParserContext context) {
264 registerBindingRpcProviderServiceRefBean(context);
266 MutableBeanMetadata metadata = createBeanMetadata(context, context.generateId(), RpcImplementationBean.class,
268 addBlueprintBundleRefProperty(context, metadata);
269 metadata.addProperty("rpcProvider", createRef(context, BINDING_RPC_PROVIDER_SERVICE_NAME));
270 metadata.addProperty("implementation", createRef(context, element.getAttribute(REF_ATTR)));
272 if (element.hasAttribute(INTERFACE)) {
273 metadata.addProperty("interfaceName", createValue(context, element.getAttribute(INTERFACE)));
276 LOG.debug("parseRpcImplementation returning {}", metadata);
280 private static Metadata parseRoutedRpcImplementation(final Element element, final ParserContext context) {
281 registerRefBean(context, RPC_REGISTRY_NAME, RpcProviderRegistry.class);
282 registerRoutedRpcRegistrationConverter(context);
284 ComponentFactoryMetadata metadata = new RoutedRpcMetadata(getId(context, element),
285 element.getAttribute(INTERFACE), element.getAttribute(REF_ATTR));
287 LOG.debug("parseRoutedRpcImplementation returning {}", metadata);
292 private static Metadata parseActionService(final Element element, final ParserContext context) {
293 ComponentFactoryMetadata metadata = new ActionServiceMetadata(getId(context, element),
294 element.getAttribute(INTERFACE));
296 LOG.debug("parseActionService returning {}", metadata);
301 private static Metadata parseRpcService(final Element element, final ParserContext context) {
302 ComponentFactoryMetadata metadata = new RpcServiceMetadata(getId(context, element),
303 element.getAttribute(INTERFACE));
305 LOG.debug("parseRpcService returning {}", metadata);
310 private static void registerRoutedRpcRegistrationConverter(final ParserContext context) {
311 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
312 if (registry.getComponentDefinition(ROUTED_RPC_REG_CONVERTER_NAME) == null) {
313 MutableBeanMetadata metadata = createBeanMetadata(context, ROUTED_RPC_REG_CONVERTER_NAME,
314 RoutedRpcRegistrationConverter.class, false, false);
315 metadata.setActivation(ReferenceMetadata.ACTIVATION_LAZY);
316 registry.registerTypeConverter(metadata);
320 private static void registerDomRpcProviderServiceRefBean(final ParserContext context) {
321 registerRefBean(context, DOM_RPC_PROVIDER_SERVICE_NAME, DOMRpcProviderService.class);
324 private static void registerBindingRpcProviderServiceRefBean(final ParserContext context) {
325 registerRefBean(context, BINDING_RPC_PROVIDER_SERVICE_NAME, RpcProviderService.class);
328 private static void registerSchemaServiceRefBean(final ParserContext context) {
329 registerRefBean(context, SCHEMA_SERVICE_NAME, DOMSchemaService.class);
332 private static void registerRefBean(final ParserContext context, final String name, final Class<?> clazz) {
333 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
334 if (registry.getComponentDefinition(name) == null) {
335 MutableReferenceMetadata metadata = createServiceRef(context, clazz, null);
336 metadata.setId(name);
337 registry.registerComponentDefinition(metadata);
341 private static Metadata parseNotificationListener(final Element element, final ParserContext context) {
342 registerNotificationServiceRefBean(context);
344 MutableBeanMetadata metadata = createBeanMetadata(context, context.generateId(), NotificationListenerBean.class,
346 addBlueprintBundleRefProperty(context, metadata);
347 metadata.addProperty("notificationService", createRef(context, NOTIFICATION_SERVICE_NAME));
348 metadata.addProperty("notificationListener", createRef(context, element.getAttribute(REF_ATTR)));
350 LOG.debug("parseNotificationListener returning {}", metadata);
355 private static void registerNotificationServiceRefBean(final ParserContext context) {
356 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
357 if (registry.getComponentDefinition(NOTIFICATION_SERVICE_NAME) == null) {
358 MutableReferenceMetadata metadata = createServiceRef(context, NotificationService.class, null);
359 metadata.setId(NOTIFICATION_SERVICE_NAME);
360 registry.registerComponentDefinition(metadata);
364 private static Metadata parseClusteredAppConfig(final Element element, final ParserContext context) {
365 LOG.debug("parseClusteredAppConfig");
367 // Find the default-config child element representing the default app config XML, if present.
368 Element defaultConfigElement = null;
369 NodeList children = element.getChildNodes();
370 for (int i = 0; i < children.getLength(); i++) {
371 Node child = children.item(i);
372 if (nodeNameEquals(child, DataStoreAppConfigMetadata.DEFAULT_CONFIG)) {
373 defaultConfigElement = (Element) child;
378 Element defaultAppConfigElement = null;
379 if (defaultConfigElement != null) {
380 // Find the CDATA element containing the default app config XML.
381 children = defaultConfigElement.getChildNodes();
382 for (int i = 0; i < children.getLength(); i++) {
383 Node child = children.item(i);
384 if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
385 defaultAppConfigElement = parseXML(DataStoreAppConfigMetadata.DEFAULT_CONFIG,
386 child.getTextContent());
392 return new DataStoreAppConfigMetadata(getId(context, element), element.getAttribute(
393 DataStoreAppConfigMetadata.BINDING_CLASS), element.getAttribute(
394 DataStoreAppConfigMetadata.LIST_KEY_VALUE), element.getAttribute(
395 DataStoreAppConfigMetadata.DEFAULT_CONFIG_FILE_NAME), parseUpdateStrategy(
396 element.getAttribute(UPDATE_STRATEGY_ATTR)), defaultAppConfigElement);
399 private static UpdateStrategy parseUpdateStrategy(final String updateStrategyValue) {
400 if (Strings.isNullOrEmpty(updateStrategyValue)
401 || updateStrategyValue.equalsIgnoreCase(UpdateStrategy.RELOAD.name())) {
402 return UpdateStrategy.RELOAD;
403 } else if (updateStrategyValue.equalsIgnoreCase(UpdateStrategy.NONE.name())) {
404 return UpdateStrategy.NONE;
406 LOG.warn("update-strategy {} not supported, using reload", updateStrategyValue);
407 return UpdateStrategy.RELOAD;
411 private static Metadata parseSpecificReferenceList(final Element element, final ParserContext context) {
412 ComponentFactoryMetadata metadata = new SpecificReferenceListMetadata(getId(context, element),
413 element.getAttribute(INTERFACE));
415 LOG.debug("parseSpecificReferenceList returning {}", metadata);
420 private static Metadata parseStaticReference(final Element element, final ParserContext context) {
421 ComponentFactoryMetadata metadata = new StaticReferenceMetadata(getId(context, element),
422 element.getAttribute(INTERFACE));
424 LOG.debug("parseStaticReference returning {}", metadata);
429 private static Element parseXML(final String name, final String xml) {
431 return UntrustedXML.newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement();
432 } catch (SAXException | IOException e) {
433 throw new ComponentDefinitionException(String.format("Error %s parsing XML: %s", name, xml), e);
437 private static ValueMetadata createValue(final ParserContext context, final String value) {
438 MutableValueMetadata metadata = context.createMetadata(MutableValueMetadata.class);
439 metadata.setStringValue(value);
443 private static MutableReferenceMetadata createServiceRef(final ParserContext context, final Class<?> cls,
444 final String filter) {
445 MutableReferenceMetadata metadata = context.createMetadata(MutableReferenceMetadata.class);
446 metadata.setRuntimeInterface(cls);
447 metadata.setInterface(cls.getName());
448 metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
449 metadata.setAvailability(ReferenceMetadata.AVAILABILITY_MANDATORY);
451 if (filter != null) {
452 metadata.setFilter(filter);
458 private static RefMetadata createRef(final ParserContext context, final String id) {
459 MutableRefMetadata metadata = context.createMetadata(MutableRefMetadata.class);
460 metadata.setComponentId(id);
464 private static String getId(final ParserContext context, final Element element) {
465 if (element.hasAttribute(ID_ATTR)) {
466 return element.getAttribute(ID_ATTR);
468 return context.generateId();
472 private static boolean nodeNameEquals(final Node node, final String name) {
473 return name.equals(node.getNodeName()) || name.equals(node.getLocalName());
476 private static void addBlueprintBundleRefProperty(final ParserContext context, final MutableBeanMetadata metadata) {
477 metadata.addProperty("bundle", createRef(context, "blueprintBundle"));
480 private static MutableBeanMetadata createBeanMetadata(final ParserContext context, final String id,
481 final Class<?> runtimeClass, final boolean initMethod, final boolean destroyMethod) {
482 MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
484 metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
485 metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
486 metadata.setRuntimeClass(runtimeClass);
489 metadata.setInitMethod("init");
493 metadata.setDestroyMethod("destroy");