Fix CS warnings in blueprint and enable enforcement
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / OpendaylightNamespaceHandler.java
index 297c3d448466478ec948587807ff8e4d3976687f..976eb7b9d5c1912c28b72720b822db3bbb51f45a 100644 (file)
@@ -8,9 +8,13 @@
 package org.opendaylight.controller.blueprint.ext;
 
 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;
@@ -38,6 +42,9 @@ import org.slf4j.LoggerFactory;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
 
 /**
  * The NamespaceHandler for Opendaylight blueprint extensions.
@@ -49,16 +56,20 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
     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 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 TYPE_ATTR = "type";
+    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
@@ -68,7 +79,7 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
 
     @Override
     public URL getSchemaLocation(String namespace) {
-        if(NAMESPACE_1_0_0.equals(namespace)) {
+        if (NAMESPACE_1_0_0.equals(namespace)) {
             URL url = getClass().getResource("/opendaylight-blueprint-ext-1.0.0.xsd");
             LOG.debug("getSchemaLocation for {} returning URL {}", namespace, url);
             return url;
@@ -89,6 +100,12 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
             return parseRpcService(element, context);
         } else if (nodeNameEquals(element, NotificationListenerBean.NOTIFICATION_LISTENER)) {
             return parseNotificationListener(element, context);
+        } else 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());
@@ -96,20 +113,20 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
 
     @Override
     public ComponentMetadata decorate(Node node, ComponentMetadata component, ParserContext context) {
-        if(node instanceof Attr) {
+        if (node instanceof Attr) {
             if (nodeNameEquals(node, RESTART_DEPENDENTS_ON_UPDATES)) {
-                return decorateRestartDependentsOnUpdates((Attr)node, component, context);
+                return decorateRestartDependentsOnUpdates((Attr) node, component, context);
             } else if (nodeNameEquals(node, USE_DEFAULT_FOR_REFERENCE_TYPES)) {
-                return decorateUseDefaultForReferenceTypes((Attr)node, component, context);
+                return decorateUseDefaultForReferenceTypes((Attr) node, component, context);
             } else if (nodeNameEquals(node, TYPE_ATTR)) {
-                if(component instanceof ServiceReferenceMetadata) {
-                    return decorateServiceReferenceType((Attr)node, component, context);
-                } else if(component instanceof ServiceMetadata) {
+                if (component instanceof ServiceReferenceMetadata) {
+                    return decorateServiceReferenceType((Attr) node, component, context);
+                } else if (component instanceof ServiceMetadata) {
                     return decorateServiceType((Attr)node, component, context);
                 }
 
-                throw new ComponentDefinitionException("Attribute " + node.getNodeName() +
-                        " can only be used on a <reference>, <reference-list> or <service> element");
+                throw new ComponentDefinitionException("Attribute " + node.getNodeName()
+                        " can only be used on a <reference>, <reference-list> or <service> element");
             }
 
             throw new ComponentDefinitionException("Unsupported attribute: " + node.getNodeName());
@@ -131,7 +148,8 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
         return component;
     }
 
-    private ComponentMetadata decorateServiceReferenceType(Attr attr, ComponentMetadata component, ParserContext context) {
+    private ComponentMetadata decorateServiceReferenceType(Attr attr, ComponentMetadata component,
+            ParserContext context) {
         if (!(component instanceof MutableServiceReferenceMetadata)) {
             throw new ComponentDefinitionException("Expected an instanceof MutableServiceReferenceMetadata");
         }
@@ -149,7 +167,7 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
             serviceRef.getExtendedFilter().getStringValue();
 
         String filter;
-        if(Strings.isNullOrEmpty(oldFilter)) {
+        if (Strings.isNullOrEmpty(oldFilter)) {
             filter = String.format("(type=%s)", attr.getValue());
         } else {
             filter = String.format("(&(%s)(type=%s))", oldFilter, attr.getValue());
@@ -174,14 +192,14 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
 
     private static ComponentMetadata enableComponentProcessorProperty(Attr attr, ComponentMetadata component,
             ParserContext context, String propertyName) {
-        if(component != null) {
-            throw new ComponentDefinitionException("Attribute " + attr.getNodeName() +
-                    " can only be used on the root <blueprint> element");
+        if (component != null) {
+            throw new ComponentDefinitionException("Attribute " + attr.getNodeName()
+                    " can only be used on the root <blueprint> element");
         }
 
         LOG.debug("{}: {}", propertyName, attr.getValue());
 
-        if(!Boolean.TRUE.equals(Boolean.valueOf(attr.getValue()))) {
+        if (!Boolean.parseBoolean(attr.getValue())) {
             return component;
         }
 
@@ -194,7 +212,7 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
     private static MutableBeanMetadata registerComponentProcessor(ParserContext context) {
         ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
         MutableBeanMetadata metadata = (MutableBeanMetadata) registry.getComponentDefinition(COMPONENT_PROCESSOR_NAME);
-        if(metadata == null) {
+        if (metadata == null) {
             metadata = context.createMetadata(MutableBeanMetadata.class);
             metadata.setProcessor(true);
             metadata.setId(COMPONENT_PROCESSOR_NAME);
@@ -228,7 +246,7 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
         metadata.addProperty("rpcRegistry", createRef(context, RPC_REGISTRY_NAME));
         metadata.addProperty("implementation", createRef(context, element.getAttribute(REF_ATTR)));
 
-        if(element.hasAttribute(INTERFACE)) {
+        if (element.hasAttribute(INTERFACE)) {
             metadata.addProperty("interfaceName", createValue(context, element.getAttribute(INTERFACE)));
         }
 
@@ -250,8 +268,6 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
     }
 
     private Metadata parseRpcService(Element element, ParserContext context) {
-        registerRpcRegistryServiceRefBean(context);
-
         ComponentFactoryMetadata metadata = new RpcServiceMetadata(getId(context, element),
                 element.getAttribute(INTERFACE));
 
@@ -262,7 +278,7 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
 
     private void registerRoutedRpcRegistrationConverter(ParserContext context) {
         ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
-        if(registry.getComponentDefinition(ROUTED_RPC_REG_CONVERTER_NAME) == null) {
+        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);
@@ -274,7 +290,7 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
 
     private void registerRpcRegistryServiceRefBean(ParserContext context) {
         ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
-        if(registry.getComponentDefinition(RPC_REGISTRY_NAME) == null) {
+        if (registry.getComponentDefinition(RPC_REGISTRY_NAME) == null) {
             MutableReferenceMetadata metadata = createServiceRef(context, RpcProviderRegistry.class, null);
             metadata.setId(RPC_REGISTRY_NAME);
             registry.registerComponentDefinition(metadata);
@@ -300,33 +316,113 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
         return metadata;
     }
 
-     private void registerNotificationServiceRefBean(ParserContext context) {
+    private void registerNotificationServiceRefBean(ParserContext context) {
         ComponentDefinitionRegistry registry = context.getComponentDefinitionRegistry();
-        if(registry.getComponentDefinition(NOTIFICATION_SERVICE_NAME) == null) {
+        if (registry.getComponentDefinition(NOTIFICATION_SERVICE_NAME) == null) {
             MutableReferenceMetadata metadata = createServiceRef(context, NotificationService.class, null);
             metadata.setId(NOTIFICATION_SERVICE_NAME);
             registry.registerComponentDefinition(metadata);
         }
     }
 
+    private Metadata parseClusteredAppConfig(Element element, ParserContext context) {
+        LOG.debug("parseClusteredAppConfig");
+
+        // Find the default-config child element representing the default app config XML, if present.
+        Element defaultConfigElement = null;
+        NodeList children = element.getChildNodes();
+        for (int i = 0; i < children.getLength(); i++) {
+            Node child = children.item(i);
+            if (nodeNameEquals(child, DataStoreAppConfigMetadata.DEFAULT_CONFIG)) {
+                defaultConfigElement = (Element) child;
+                break;
+            }
+        }
+
+        Element defaultAppConfigElement = null;
+        if (defaultConfigElement != null) {
+            // Find the CDATA element containing the default app config XML.
+            children = defaultConfigElement.getChildNodes();
+            for (int i = 0; i < children.getLength(); i++) {
+                Node child = children.item(i);
+                if (child.getNodeType() == Node.CDATA_SECTION_NODE) {
+                    defaultAppConfigElement = parseXML(DataStoreAppConfigMetadata.DEFAULT_CONFIG,
+                            child.getTextContent());
+                    break;
+                }
+            }
+        }
+
+        return new DataStoreAppConfigMetadata(getId(context, element), element.getAttribute(
+                DataStoreAppConfigMetadata.BINDING_CLASS), element.getAttribute(
+                        DataStoreAppConfigMetadata.LIST_KEY_VALUE), element.getAttribute(
+                        DataStoreAppConfigMetadata.DEFAULT_CONFIG_FILE_NAME), parseUpdateStrategy(
+                        element.getAttribute(UPDATE_STRATEGY_ATTR)), defaultAppConfigElement);
+    }
+
+    private UpdateStrategy parseUpdateStrategy(String updateStrategyValue) {
+        if (Strings.isNullOrEmpty(updateStrategyValue)
+                || updateStrategyValue.equalsIgnoreCase(UpdateStrategy.RELOAD.name())) {
+            return UpdateStrategy.RELOAD;
+        } else if (updateStrategyValue.equalsIgnoreCase(UpdateStrategy.NONE.name())) {
+            return UpdateStrategy.NONE;
+        } else {
+            LOG.warn("update-strategy {} not supported, using reload", updateStrategyValue);
+            return UpdateStrategy.RELOAD;
+        }
+    }
+
+    private Metadata parseSpecificReferenceList(Element element, ParserContext context) {
+        ComponentFactoryMetadata metadata = new SpecificReferenceListMetadata(getId(context, element),
+                element.getAttribute(INTERFACE));
+
+        LOG.debug("parseSpecificReferenceList returning {}", metadata);
+
+        return metadata;
+    }
+
+    private Metadata parseStaticReference(Element element, ParserContext context) {
+        ComponentFactoryMetadata metadata = new StaticReferenceMetadata(getId(context, element),
+                element.getAttribute(INTERFACE));
+
+        LOG.debug("parseStaticReference returning {}", metadata);
+
+        return metadata;
+    }
+
+    private Element parseXML(String name, 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) {
+            throw new ComponentDefinitionException(String.format("Error %s parsing XML: %s", name, xml), e);
+        }
+    }
+
     private static ValueMetadata createValue(ParserContext context, String value) {
-        MutableValueMetadata m = context.createMetadata(MutableValueMetadata.class);
-        m.setStringValue(value);
-        return m;
+        MutableValueMetadata metadata = context.createMetadata(MutableValueMetadata.class);
+        metadata.setStringValue(value);
+        return metadata;
     }
 
     private static MutableReferenceMetadata createServiceRef(ParserContext context, Class<?> cls, String filter) {
-        MutableReferenceMetadata m = context.createMetadata(MutableReferenceMetadata.class);
-        m.setRuntimeInterface(cls);
-        m.setInterface(cls.getName());
-        m.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
-        m.setAvailability(ReferenceMetadata.AVAILABILITY_MANDATORY);
-
-        if(filter != null) {
-            m.setFilter(filter);
+        MutableReferenceMetadata metadata = context.createMetadata(MutableReferenceMetadata.class);
+        metadata.setRuntimeInterface(cls);
+        metadata.setInterface(cls.getName());
+        metadata.setActivation(ReferenceMetadata.ACTIVATION_EAGER);
+        metadata.setAvailability(ReferenceMetadata.AVAILABILITY_MANDATORY);
+
+        if (filter != null) {
+            metadata.setFilter(filter);
         }
 
-        return m;
+        return metadata;
     }
 
     private static RefMetadata createRef(ParserContext context, String id) {
@@ -336,7 +432,7 @@ public class OpendaylightNamespaceHandler implements NamespaceHandler {
     }
 
     private static String getId(ParserContext context, Element element) {
-        if(element.hasAttribute(ID_ATTR)) {
+        if (element.hasAttribute(ID_ATTR)) {
             return element.getAttribute(ID_ATTR);
         } else {
             return context.generateId();