X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=opendaylight%2Fblueprint%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fblueprint%2Fext%2FOpendaylightNamespaceHandler.java;h=371b7efecdec1fa6e67f700acce75e10aa4beebd;hb=HEAD;hp=05359b9e39a09d32c6fea5fbf60491feb06dbd3f;hpb=26ab6692366c5fd42669bc1bff80b4ec7a394f0d;p=controller.git diff --git a/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/OpendaylightNamespaceHandler.java b/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/OpendaylightNamespaceHandler.java index 05359b9e39..371b7efecd 100644 --- a/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/OpendaylightNamespaceHandler.java +++ b/opendaylight/blueprint/src/main/java/org/opendaylight/controller/blueprint/ext/OpendaylightNamespaceHandler.java @@ -11,14 +11,10 @@ import com.google.common.base.Strings; import java.io.IOException; import java.io.StringReader; import java.net.URL; -import java.util.Collections; import java.util.Set; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import org.apache.aries.blueprint.ComponentDefinitionRegistry; import org.apache.aries.blueprint.NamespaceHandler; import org.apache.aries.blueprint.ParserContext; -import org.apache.aries.blueprint.ext.ComponentFactoryMetadata; import org.apache.aries.blueprint.mutable.MutableBeanMetadata; import org.apache.aries.blueprint.mutable.MutableRefMetadata; import org.apache.aries.blueprint.mutable.MutableReferenceMetadata; @@ -26,8 +22,7 @@ import org.apache.aries.blueprint.mutable.MutableServiceMetadata; import org.apache.aries.blueprint.mutable.MutableServiceReferenceMetadata; import org.apache.aries.blueprint.mutable.MutableValueMetadata; import org.opendaylight.controller.blueprint.BlueprintContainerRestartService; -import org.opendaylight.controller.md.sal.binding.api.NotificationService; -import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.yangtools.util.xml.UntrustedXML; import org.osgi.service.blueprint.container.ComponentDefinitionException; import org.osgi.service.blueprint.reflect.BeanMetadata; import org.osgi.service.blueprint.reflect.ComponentMetadata; @@ -53,28 +48,20 @@ import org.xml.sax.SAXException; */ public final class OpendaylightNamespaceHandler implements NamespaceHandler { public static final String NAMESPACE_1_0_0 = "http://opendaylight.org/xmlns/blueprint/v1.0.0"; - static final String ROUTED_RPC_REG_CONVERTER_NAME = "org.opendaylight.blueprint.RoutedRpcRegConverter"; - static final String RPC_REGISTRY_NAME = "org.opendaylight.blueprint.RpcRegistry"; - static final String NOTIFICATION_SERVICE_NAME = "org.opendaylight.blueprint.NotificationService"; - static final String TYPE_ATTR = "type"; - static final String UPDATE_STRATEGY_ATTR = "update-strategy"; private static final Logger LOG = LoggerFactory.getLogger(OpendaylightNamespaceHandler.class); + private static final String TYPE_ATTR = "type"; + private static final String UPDATE_STRATEGY_ATTR = "update-strategy"; private static final String COMPONENT_PROCESSOR_NAME = ComponentProcessor.class.getName(); private static final String RESTART_DEPENDENTS_ON_UPDATES = "restart-dependents-on-updates"; private static final String USE_DEFAULT_FOR_REFERENCE_TYPES = "use-default-for-reference-types"; private static final String CLUSTERED_APP_CONFIG = "clustered-app-config"; - private static final String INTERFACE = "interface"; - private static final String REF_ATTR = "ref"; private static final String ID_ATTR = "id"; - private static final String RPC_SERVICE = "rpc-service"; - private static final String SPECIFIC_SERVICE_REF_LIST = "specific-reference-list"; - private static final String STATIC_REFERENCE = "static-reference"; @SuppressWarnings("rawtypes") @Override public Set getManagedClasses() { - return Collections.emptySet(); + return Set.of(); } @Override @@ -92,20 +79,8 @@ public final class OpendaylightNamespaceHandler implements NamespaceHandler { public Metadata parse(final Element element, final ParserContext context) { LOG.debug("In parse for {}", element); - if (nodeNameEquals(element, RpcImplementationBean.RPC_IMPLEMENTATION)) { - return parseRpcImplementation(element, context); - } else if (nodeNameEquals(element, RoutedRpcMetadata.ROUTED_RPC_IMPLEMENTATION)) { - return parseRoutedRpcImplementation(element, context); - } else if (nodeNameEquals(element, RPC_SERVICE)) { - return parseRpcService(element, context); - } else if (nodeNameEquals(element, NotificationListenerBean.NOTIFICATION_LISTENER)) { - return parseNotificationListener(element, context); - } else if (nodeNameEquals(element, CLUSTERED_APP_CONFIG)) { + if (nodeNameEquals(element, CLUSTERED_APP_CONFIG)) { return parseClusteredAppConfig(element, context); - } else if (nodeNameEquals(element, SPECIFIC_SERVICE_REF_LIST)) { - return parseSpecificReferenceList(element, context); - } else if (nodeNameEquals(element, STATIC_REFERENCE)) { - return parseStaticReference(element, context); } throw new ComponentDefinitionException("Unsupported standalone element: " + element.getNodeName()); @@ -137,12 +112,10 @@ public final class OpendaylightNamespaceHandler implements NamespaceHandler { private static ComponentMetadata decorateServiceType(final Attr attr, final ComponentMetadata component, final ParserContext context) { - if (!(component instanceof MutableServiceMetadata)) { + if (!(component instanceof MutableServiceMetadata service)) { throw new ComponentDefinitionException("Expected an instanceof MutableServiceMetadata"); } - MutableServiceMetadata service = (MutableServiceMetadata)component; - LOG.debug("decorateServiceType for {} - adding type property {}", service.getId(), attr.getValue()); service.addServiceProperty(createValue(context, TYPE_ATTR), createValue(context, attr.getValue())); @@ -198,7 +171,7 @@ public final class OpendaylightNamespaceHandler implements NamespaceHandler { + " can only be used on the root element"); } - LOG.debug("{}: {}", propertyName, attr.getValue()); + LOG.debug("Property {} = {}", propertyName, attr.getValue()); if (!Boolean.parseBoolean(attr.getValue())) { return component; @@ -214,14 +187,9 @@ public final class OpendaylightNamespaceHandler implements NamespaceHandler { ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry(); MutableBeanMetadata metadata = (MutableBeanMetadata) registry.getComponentDefinition(COMPONENT_PROCESSOR_NAME); if (metadata == null) { - metadata = context.createMetadata(MutableBeanMetadata.class); + metadata = createBeanMetadata(context, COMPONENT_PROCESSOR_NAME, ComponentProcessor.class, false, true); metadata.setProcessor(true); - metadata.setId(COMPONENT_PROCESSOR_NAME); - metadata.setActivation(BeanMetadata.ACTIVATION_EAGER); - metadata.setScope(BeanMetadata.SCOPE_SINGLETON); - metadata.setRuntimeClass(ComponentProcessor.class); - metadata.setDestroyMethod("destroy"); - metadata.addProperty("bundle", createRef(context, "blueprintBundle")); + addBlueprintBundleRefProperty(context, metadata); metadata.addProperty("blueprintContainerRestartService", createServiceRef(context, BlueprintContainerRestartService.class, null)); @@ -233,99 +201,6 @@ public final class OpendaylightNamespaceHandler implements NamespaceHandler { return metadata; } - private static Metadata parseRpcImplementation(final Element element, final ParserContext context) { - registerRpcRegistryServiceRefBean(context); - - MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class); - metadata.setId(context.generateId()); - metadata.setScope(BeanMetadata.SCOPE_SINGLETON); - metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER); - metadata.setRuntimeClass(RpcImplementationBean.class); - metadata.setInitMethod("init"); - metadata.setDestroyMethod("destroy"); - metadata.addProperty("bundle", createRef(context, "blueprintBundle")); - metadata.addProperty("rpcRegistry", createRef(context, RPC_REGISTRY_NAME)); - metadata.addProperty("implementation", createRef(context, element.getAttribute(REF_ATTR))); - - if (element.hasAttribute(INTERFACE)) { - metadata.addProperty("interfaceName", createValue(context, element.getAttribute(INTERFACE))); - } - - LOG.debug("parseAddRpcImplementation returning {}", metadata); - - return metadata; - } - - private static Metadata parseRoutedRpcImplementation(final Element element, final ParserContext context) { - registerRpcRegistryServiceRefBean(context); - registerRoutedRpcRegistrationConverter(context); - - ComponentFactoryMetadata metadata = new RoutedRpcMetadata(getId(context, element), - element.getAttribute(INTERFACE), element.getAttribute(REF_ATTR)); - - LOG.debug("parseRoutedRpcImplementation returning {}", metadata); - - return metadata; - } - - private static Metadata parseRpcService(final Element element, final ParserContext context) { - ComponentFactoryMetadata metadata = new RpcServiceMetadata(getId(context, element), - element.getAttribute(INTERFACE)); - - LOG.debug("parseRpcService returning {}", metadata); - - return metadata; - } - - private static void registerRoutedRpcRegistrationConverter(final ParserContext context) { - ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry(); - if (registry.getComponentDefinition(ROUTED_RPC_REG_CONVERTER_NAME) == null) { - MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class); - metadata.setId(ROUTED_RPC_REG_CONVERTER_NAME); - metadata.setScope(BeanMetadata.SCOPE_SINGLETON); - metadata.setActivation(ReferenceMetadata.ACTIVATION_LAZY); - metadata.setRuntimeClass(RoutedRpcRegistrationConverter.class); - registry.registerTypeConverter(metadata); - } - } - - private static void registerRpcRegistryServiceRefBean(final ParserContext context) { - ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry(); - if (registry.getComponentDefinition(RPC_REGISTRY_NAME) == null) { - MutableReferenceMetadata metadata = createServiceRef(context, RpcProviderRegistry.class, null); - metadata.setId(RPC_REGISTRY_NAME); - registry.registerComponentDefinition(metadata); - } - } - - private static Metadata parseNotificationListener(final Element element, final ParserContext context) { - registerNotificationServiceRefBean(context); - - MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class); - metadata.setId(context.generateId()); - metadata.setScope(BeanMetadata.SCOPE_SINGLETON); - metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER); - metadata.setRuntimeClass(NotificationListenerBean.class); - metadata.setInitMethod("init"); - metadata.setDestroyMethod("destroy"); - metadata.addProperty("bundle", createRef(context, "blueprintBundle")); - metadata.addProperty("notificationService", createRef(context, NOTIFICATION_SERVICE_NAME)); - metadata.addProperty("notificationListener", createRef(context, element.getAttribute(REF_ATTR))); - - LOG.debug("parseNotificationListener returning {}", metadata); - - return metadata; - } - - private static void registerNotificationServiceRefBean(final ParserContext context) { - ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry(); - if (registry.getComponentDefinition(NOTIFICATION_SERVICE_NAME) == null) { - MutableReferenceMetadata metadata = createServiceRef(context, NotificationService.class, null); - metadata.setId(NOTIFICATION_SERVICE_NAME); - registry.registerComponentDefinition(metadata); - } - } - private static Metadata parseClusteredAppConfig(final Element element, final ParserContext context) { LOG.debug("parseClusteredAppConfig"); @@ -373,35 +248,10 @@ public final class OpendaylightNamespaceHandler implements NamespaceHandler { } } - private static Metadata parseSpecificReferenceList(final Element element, final ParserContext context) { - ComponentFactoryMetadata metadata = new SpecificReferenceListMetadata(getId(context, element), - element.getAttribute(INTERFACE)); - - LOG.debug("parseSpecificReferenceList returning {}", metadata); - - return metadata; - } - - private static Metadata parseStaticReference(final Element element, final ParserContext context) { - ComponentFactoryMetadata metadata = new StaticReferenceMetadata(getId(context, element), - element.getAttribute(INTERFACE)); - - LOG.debug("parseStaticReference returning {}", metadata); - - return metadata; - } - private static Element parseXML(final String name, final String xml) { - DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); - builderFactory.setNamespaceAware(true); - builderFactory.setCoalescing(true); - builderFactory.setIgnoringElementContentWhitespace(true); - builderFactory.setIgnoringComments(true); - try { - return builderFactory.newDocumentBuilder().parse(new InputSource( - new StringReader(xml))).getDocumentElement(); - } catch (SAXException | IOException | ParserConfigurationException e) { + return UntrustedXML.newDocumentBuilder().parse(new InputSource(new StringReader(xml))).getDocumentElement(); + } catch (SAXException | IOException e) { throw new ComponentDefinitionException(String.format("Error %s parsing XML: %s", name, xml), e); } } @@ -444,4 +294,27 @@ public final class OpendaylightNamespaceHandler implements NamespaceHandler { private static boolean nodeNameEquals(final Node node, final String name) { return name.equals(node.getNodeName()) || name.equals(node.getLocalName()); } + + private static void addBlueprintBundleRefProperty(final ParserContext context, final MutableBeanMetadata metadata) { + metadata.addProperty("bundle", createRef(context, "blueprintBundle")); + } + + private static MutableBeanMetadata createBeanMetadata(final ParserContext context, final String id, + final Class runtimeClass, final boolean initMethod, final boolean destroyMethod) { + MutableBeanMetadata metadata = context.createMetadata(MutableBeanMetadata.class); + metadata.setId(id); + metadata.setScope(BeanMetadata.SCOPE_SINGLETON); + metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER); + metadata.setRuntimeClass(runtimeClass); + + if (initMethod) { + metadata.setInitMethod("init"); + } + + if (destroyMethod) { + metadata.setDestroyMethod("destroy"); + } + + return metadata; + } }