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.sal.binding.api.RpcProviderRegistry;
26 import org.osgi.service.blueprint.container.ComponentDefinitionException;
27 import org.osgi.service.blueprint.reflect.BeanMetadata;
28 import org.osgi.service.blueprint.reflect.ComponentMetadata;
29 import org.osgi.service.blueprint.reflect.Metadata;
30 import org.osgi.service.blueprint.reflect.RefMetadata;
31 import org.osgi.service.blueprint.reflect.ReferenceMetadata;
32 import org.osgi.service.blueprint.reflect.ServiceMetadata;
33 import org.osgi.service.blueprint.reflect.ServiceReferenceMetadata;
34 import org.osgi.service.blueprint.reflect.ValueMetadata;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.w3c.dom.Attr;
38 import org.w3c.dom.Element;
39 import org.w3c.dom.Node;
42 * The NamespaceHandler for Opendaylight blueprint extensions.
44 * @author Thomas Pantelis
46 public class OpendaylightNamespaceHandler implements NamespaceHandler {
47 public static final String NAMESPACE_1_0_0 = "http://opendaylight.org/xmlns/blueprint/v1.0.0";
48 static final String ROUTED_RPC_REG_CONVERTER_NAME = "org.opendaylight.blueprint.RoutedRpcRegConverter";
49 static final String RPC_REGISTRY_NAME = "org.opendaylight.blueprint.RpcRegistry";
51 private static final Logger LOG = LoggerFactory.getLogger(OpendaylightNamespaceHandler.class);
52 private static final String COMPONENT_PROCESSOR_NAME = ComponentProcessor.class.getName();
53 private static final String RESTART_DEPENDENTS_ON_UPDATES = "restart-dependents-on-updates";
54 private static final String USE_DEFAULT_FOR_REFERENCE_TYPES = "use-default-for-reference-types";
55 private static final String TYPE_ATTR = "type";
56 private static final String INTERFACE = "interface";
57 private static final String REF_ATTR = "ref";
58 private static final String ID_ATTR = "id";
59 private static final String RPC_SERVICE = "rpc-service";
61 @SuppressWarnings("rawtypes")
63 public Set<Class> getManagedClasses() {
64 return Collections.emptySet();
68 public URL getSchemaLocation(String namespace) {
69 if(NAMESPACE_1_0_0.equals(namespace)) {
70 URL url = getClass().getResource("/opendaylight-blueprint-ext-1.0.0.xsd");
71 LOG.debug("getSchemaLocation for {} returning URL {}", namespace, url);
79 public Metadata parse(Element element, ParserContext context) {
80 LOG.debug("In parse for {}", element);
82 if (nodeNameEquals(element, RpcImplementationBean.RPC_IMPLEMENTATION)) {
83 return parseRpcImplementation(element, context);
84 } else if (nodeNameEquals(element, RoutedRpcMetadata.ROUTED_RPC_IMPLEMENTATION)) {
85 return parseRoutedRpcImplementation(element, context);
86 } else if (nodeNameEquals(element, RPC_SERVICE)) {
87 return parseRpcService(element, context);
90 throw new ComponentDefinitionException("Unsupported standalone element: " + element.getNodeName());
94 public ComponentMetadata decorate(Node node, ComponentMetadata component, ParserContext context) {
95 if(node instanceof Attr) {
96 if (nodeNameEquals(node, RESTART_DEPENDENTS_ON_UPDATES)) {
97 return decorateRestartDependentsOnUpdates((Attr)node, component, context);
98 } else if (nodeNameEquals(node, USE_DEFAULT_FOR_REFERENCE_TYPES)) {
99 return decorateUseDefaultForReferenceTypes((Attr)node, component, context);
100 } else if (nodeNameEquals(node, TYPE_ATTR)) {
101 if(component instanceof ServiceReferenceMetadata) {
102 return decorateServiceReferenceType((Attr)node, component, context);
103 } else if(component instanceof ServiceMetadata) {
104 return decorateServiceType((Attr)node, component, context);
107 throw new ComponentDefinitionException("Attribute " + node.getNodeName() +
108 " can only be used on a <reference>, <reference-list> or <service> element");
111 throw new ComponentDefinitionException("Unsupported attribute: " + node.getNodeName());
113 throw new ComponentDefinitionException("Unsupported node type: " + node);
117 private ComponentMetadata decorateServiceType(Attr attr, ComponentMetadata component, ParserContext context) {
118 if (!(component instanceof MutableServiceMetadata)) {
119 throw new ComponentDefinitionException("Expected an instanceof MutableServiceMetadata");
122 MutableServiceMetadata service = (MutableServiceMetadata)component;
124 LOG.debug("decorateServiceType for {} - adding type property {}", service.getId(), attr.getValue());
126 service.addServiceProperty(createValue(context, TYPE_ATTR), createValue(context, attr.getValue()));
130 private ComponentMetadata decorateServiceReferenceType(Attr attr, ComponentMetadata component, ParserContext context) {
131 if (!(component instanceof MutableServiceReferenceMetadata)) {
132 throw new ComponentDefinitionException("Expected an instanceof MutableServiceReferenceMetadata");
135 // We don't actually need the ComponentProcessor for augmenting the OSGi filter here but we create it
136 // to workaround an issue in Aries where it doesn't use the extended filter unless there's a
137 // Processor or ComponentDefinitionRegistryProcessor registered. This may actually be working as
138 // designed in Aries b/c the extended filter was really added to allow the OSGi filter to be
139 // substituted by a variable via the "cm:property-placeholder" processor. If so, it's a bit funky
140 // but as long as there's at least one processor registered, it correctly uses the extended filter.
141 registerComponentProcessor(context);
143 MutableServiceReferenceMetadata serviceRef = (MutableServiceReferenceMetadata)component;
144 String oldFilter = serviceRef.getExtendedFilter() == null ? null :
145 serviceRef.getExtendedFilter().getStringValue();
148 if(Strings.isNullOrEmpty(oldFilter)) {
149 filter = String.format("(type=%s)", attr.getValue());
151 filter = String.format("(&(%s)(type=%s))", oldFilter, attr.getValue());
154 LOG.debug("decorateServiceReferenceType for {} with type {}, old filter: {}, new filter: {}",
155 serviceRef.getId(), attr.getValue(), oldFilter, filter);
157 serviceRef.setExtendedFilter(createValue(context, filter));
161 private static ComponentMetadata decorateRestartDependentsOnUpdates(Attr attr, ComponentMetadata component,
162 ParserContext context) {
163 return enableComponentProcessorProperty(attr, component, context, "restartDependentsOnUpdates");
166 private static ComponentMetadata decorateUseDefaultForReferenceTypes(Attr attr, ComponentMetadata component,
167 ParserContext context) {
168 return enableComponentProcessorProperty(attr, component, context, "useDefaultForReferenceTypes");
171 private static ComponentMetadata enableComponentProcessorProperty(Attr attr, ComponentMetadata component,
172 ParserContext context, String propertyName) {
173 if(component != null) {
174 throw new ComponentDefinitionException("Attribute " + attr.getNodeName() +
175 " can only be used on the root <blueprint> element");
178 LOG.debug("{}: {}", propertyName, attr.getValue());
180 if(!Boolean.TRUE.equals(Boolean.valueOf(attr.getValue()))) {
184 MutableBeanMetadata metadata = registerComponentProcessor(context);
185 metadata.addProperty(propertyName, createValue(context, "true"));
190 private static MutableBeanMetadata registerComponentProcessor(ParserContext context) {
191 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
192 MutableBeanMetadata metadata = (MutableBeanMetadata) registry.getComponentDefinition(COMPONENT_PROCESSOR_NAME);
193 if(metadata == null) {
194 metadata = context.createMetadata(MutableBeanMetadata.class);
195 metadata.setProcessor(true);
196 metadata.setId(COMPONENT_PROCESSOR_NAME);
197 metadata.setActivation(BeanMetadata.ACTIVATION_EAGER);
198 metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
199 metadata.setRuntimeClass(ComponentProcessor.class);
200 metadata.setDestroyMethod("destroy");
201 metadata.addProperty("bundle", createRef(context, "blueprintBundle"));
202 metadata.addProperty("blueprintContainerRestartService", createServiceRef(context,
203 BlueprintContainerRestartService.class, null));
205 LOG.debug("Registering ComponentProcessor bean: {}", metadata);
207 registry.registerComponentDefinition(metadata);
213 private Metadata parseRpcImplementation(Element element, ParserContext context) {
214 registerRpcRegistryServiceRefBean(context);
216 MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
217 metadata.setId(context.generateId());
218 metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
219 metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
220 metadata.setRuntimeClass(RpcImplementationBean.class);
221 metadata.setInitMethod("init");
222 metadata.setDestroyMethod("destroy");
223 metadata.addProperty("bundle", createRef(context, "blueprintBundle"));
224 metadata.addProperty("rpcRegistry", createRef(context, RPC_REGISTRY_NAME));
225 metadata.addProperty("implementation", createRef(context, element.getAttribute(REF_ATTR)));
227 if(element.hasAttribute(INTERFACE)) {
228 metadata.addProperty("interfaceName", createValue(context, element.getAttribute(INTERFACE)));
231 LOG.debug("parseAddRpcImplementation returning {}", metadata);
236 private Metadata parseRoutedRpcImplementation(Element element, ParserContext context) {
237 registerRpcRegistryServiceRefBean(context);
238 registerRoutedRpcRegistrationConverter(context);
240 ComponentFactoryMetadata metadata = new RoutedRpcMetadata(getId(context, element),
241 element.getAttribute(INTERFACE), element.getAttribute(REF_ATTR));
243 LOG.debug("parseRoutedRpcImplementation returning {}", metadata);
248 private Metadata parseRpcService(Element element, ParserContext context) {
249 registerRpcRegistryServiceRefBean(context);
251 ComponentFactoryMetadata metadata = new RpcServiceMetadata(getId(context, element),
252 element.getAttribute(INTERFACE));
254 LOG.debug("parseRpcService returning {}", metadata);
259 private void registerRoutedRpcRegistrationConverter(ParserContext context) {
260 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
261 if(registry.getComponentDefinition(ROUTED_RPC_REG_CONVERTER_NAME) == null) {
262 MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class);
263 metadata.setId(ROUTED_RPC_REG_CONVERTER_NAME);
264 metadata.setScope(BeanMetadata.SCOPE_SINGLETON);
265 metadata.setActivation(ReferenceMetadata.ACTIVATION_LAZY);
266 metadata.setRuntimeClass(RoutedRpcRegistrationConverter.class);
267 registry.registerTypeConverter(metadata);
271 private void registerRpcRegistryServiceRefBean(ParserContext context) {
272 ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
273 if(registry.getComponentDefinition(RPC_REGISTRY_NAME) == null) {
274 MutableReferenceMetadata metadata = createServiceRef(context, RpcProviderRegistry.class, null);
275 metadata.setId(RPC_REGISTRY_NAME);
276 registry.registerComponentDefinition(metadata);
280 private static ValueMetadata createValue(ParserContext context, String value) {
281 MutableValueMetadata m = context.createMetadata(MutableValueMetadata.class);
282 m.setStringValue(value);
286 private static MutableReferenceMetadata createServiceRef(ParserContext context, Class<?> cls, String filter) {
287 MutableReferenceMetadata m = context.createMetadata(MutableReferenceMetadata.class);
288 m.setRuntimeInterface(cls);
289 m.setInterface(cls.getName());
290 m.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
291 m.setAvailability(ReferenceMetadata.AVAILABILITY_MANDATORY);
300 private static RefMetadata createRef(ParserContext context, String id) {
301 MutableRefMetadata metadata = context.createMetadata(MutableRefMetadata.class);
302 metadata.setComponentId(id);
306 private static String getId(ParserContext context, Element element) {
307 if(element.hasAttribute(ID_ATTR)) {
308 return element.getAttribute(ID_ATTR);
310 return context.generateId();
314 private static boolean nodeNameEquals(Node node, String name) {
315 return name.equals(node.getNodeName()) || name.equals(node.getLocalName());