import com.google.common.collect.Collections2;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
-
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-
import javax.annotation.Nullable;
-
import org.opendaylight.controller.config.yangjmxgenerator.attribute.AbstractDependencyAttribute;
import org.opendaylight.controller.config.yangjmxgenerator.attribute.AttributeIfc;
import org.opendaylight.controller.config.yangjmxgenerator.attribute.DependencyAttribute;
import org.opendaylight.controller.config.yangjmxgenerator.attribute.ListDependenciesAttribute;
import org.opendaylight.controller.config.yangjmxgenerator.attribute.TOAttribute;
import org.opendaylight.controller.config.yangjmxgenerator.plugin.util.NameConflictException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.ServiceRef;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
final class ModuleMXBeanEntryBuilder {
+ private static final String TYPE = "type";
+
private Module currentModule;
private Map<QName, ServiceInterfaceEntry> qNamesToSIEs;
private SchemaContext schemaContext;
return this;
}
- private static final Logger logger = LoggerFactory
+ private static final Logger LOG = LoggerFactory
.getLogger(ModuleMXBeanEntryBuilder.class);
// TODO: the XPath should be parsed by code generator IMO
private static final String MAGIC_STRING = "MAGIC_STRING";
private static final String MODULE_CONDITION_XPATH_TEMPLATE = "^/MAGIC_STRING:modules/MAGIC_STRING:module/MAGIC_STRING:type\\s*=\\s*['\"](.+)['\"]$";
- private static final SchemaPath expectedConfigurationAugmentationSchemaPath = SchemaPath.create(true,
- createConfigQName("modules"), createConfigQName("module"), createConfigQName("configuration"));
- private static final SchemaPath expectedStateAugmentationSchemaPath = SchemaPath.create(true,
- createConfigQName("modules"), createConfigQName("module"), createConfigQName("state"));
- private static final Pattern PREFIX_COLON_LOCAL_NAME = Pattern
- .compile("^(.+):(.+)$");
+ private static final SchemaPath AUGMENT_SCHEMAPATH = SchemaPath.create(true,
+ createConfigQName("modules"), createConfigQName("module"));
+
+ private static final SchemaPath EXPECTED_CONFIGURATION_AUGMENTATION_SCHEMA_PATH =
+ AUGMENT_SCHEMAPATH.createChild(createConfigQName("configuration"));
+ private static final SchemaPath EXPECTED_STATE_AUGMENTATION_SCHEMA_PATH =
+ AUGMENT_SCHEMAPATH.createChild(createConfigQName("state"));
+ private static final Pattern PREFIX_COLON_LOCAL_NAME = Pattern.compile("^(.+):(.+)$");
public Map<String, ModuleMXBeanEntry> build() {
- logger.debug("Generating ModuleMXBeans of {} to package {}",
+ LOG.debug("Generating ModuleMXBeans of {} to package {}",
currentModule.getNamespace(), packageName);
String configModulePrefix;
Map<String, ModuleMXBeanEntry> result = new HashMap<>();
- for (AugmentationSchema augmentation : currentModule.getAugmentations()) {
- Set<DataSchemaNode> childNodes = augmentation.getChildNodes();
- if (areAllChildrenChoiceCaseNodes(childNodes)) {
- for (ChoiceCaseNode childCase : castChildNodesToChoiceCases(childNodes)) {
+ for (AugmentationSchemaNode augmentation : currentModule.getAugmentations()) {
+ Collection<DataSchemaNode> childNodes = augmentation.getChildNodes();
+ if (areAllChildrenCaseSchemaNodes(childNodes)) {
+ for (CaseSchemaNode childCase : castChildNodesToChoiceCases(childNodes)) {
// TODO refactor, extract to standalone builder class
- processChoiceCaseNode(result, uniqueGeneratedClassesNames, configModulePrefix, moduleIdentities,
+ processCaseSchemaNode(result, uniqueGeneratedClassesNames, configModulePrefix, moduleIdentities,
unaugmentedModuleIdentities, augmentation, childCase);
}
} // skip if child nodes are not all cases
checkAttributeNamesUniqueness(uniqueGeneratedClassesNames, result);
checkUnaugumentedIdentities(unaugmentedModuleIdentities);
- logger.debug("Number of ModuleMXBeans to be generated: {}", result.size());
+ LOG.debug("Number of ModuleMXBeans to be generated: {}", result.size());
return result;
}
private static void checkUnaugumentedIdentities(final Map<String, IdentitySchemaNode> unaugmentedModuleIdentities) {
if (unaugmentedModuleIdentities.size() > 0) {
- logger.warn("Augmentation not found for all currentModule identities: {}",
+ LOG.warn("Augmentation not found for all currentModule identities: {}",
unaugmentedModuleIdentities.keySet());
}
}
- private static void checkAttributeNamesUniqueness(final Map<String, QName> uniqueGeneratedClassesNames, final Map<String, ModuleMXBeanEntry> result) {
+ private static void checkAttributeNamesUniqueness(final Map<String, QName> uniqueGeneratedClassesNames,
+ final Map<String, ModuleMXBeanEntry> result) {
for (Map.Entry<String, ModuleMXBeanEntry> entry : result.entrySet()) {
checkUniqueRuntimeBeanAttributesName(entry.getValue(),
uniqueGeneratedClassesNames);
Map<String, IdentitySchemaNode> moduleIdentities = Maps.newHashMap();
for (IdentitySchemaNode id : currentModule.getIdentities()) {
- if (id.getBaseIdentity() != null
- && ConfigConstants.MODULE_TYPE_Q_NAME.equals(id.getBaseIdentity().getQName())) {
+ if (!id.getBaseIdentities().isEmpty()
+ && ConfigConstants.MODULE_TYPE_Q_NAME.equals(id.getBaseIdentities().iterator().next().getQName())) {
String identityLocalName = id.getQName().getLocalName();
if (moduleIdentities.containsKey(identityLocalName)) {
throw new IllegalStateException("Module name already defined in this currentModule: "
+ identityLocalName);
} else {
moduleIdentities.put(identityLocalName, id);
- logger.debug("Found identity {}", identityLocalName);
+ LOG.debug("Found identity {}", identityLocalName);
}
// validation check on unknown schema nodes
boolean providedServiceWasSet = false;
return moduleIdentities;
}
- private Collection<ChoiceCaseNode> castChildNodesToChoiceCases(final Set<DataSchemaNode> childNodes) {
- return Collections2.transform(childNodes, new Function<DataSchemaNode, ChoiceCaseNode>() {
+ private static Collection<CaseSchemaNode> castChildNodesToChoiceCases(final Collection<DataSchemaNode> childNodes) {
+ return Collections2.transform(childNodes, new Function<DataSchemaNode, CaseSchemaNode>() {
@Nullable
@Override
- public ChoiceCaseNode apply(@Nullable final DataSchemaNode input) {
- return (ChoiceCaseNode) input;
+ public CaseSchemaNode apply(@Nullable final DataSchemaNode input) {
+ return (CaseSchemaNode) input;
}
});
}
- private boolean areAllChildrenChoiceCaseNodes(final Set<DataSchemaNode> childNodes) {
+ private static boolean areAllChildrenCaseSchemaNodes(final Iterable<DataSchemaNode> childNodes) {
for (DataSchemaNode childNode : childNodes) {
- if (childNode instanceof ChoiceCaseNode == false) {
+ if (childNode instanceof CaseSchemaNode == false) {
return false;
}
}
return true;
}
- private <HAS_CHILDREN_AND_QNAME extends DataNodeContainer & SchemaNode> void processChoiceCaseNode(final Map<String, ModuleMXBeanEntry> result,
+ private <HAS_CHILDREN_AND_QNAME extends DataNodeContainer & SchemaNode> void processCaseSchemaNode(
+ final Map<String, ModuleMXBeanEntry> result,
final Map<String, QName> uniqueGeneratedClassesNames, final String configModulePrefix,
final Map<String, IdentitySchemaNode> moduleIdentities,
- final Map<String, IdentitySchemaNode> unaugmentedModuleIdentities, final AugmentationSchema augmentation,
- final DataSchemaNode when) {
+ final Map<String, IdentitySchemaNode> unaugmentedModuleIdentities,
+ final AugmentationSchemaNode augmentation, final DataSchemaNode when) {
- ChoiceCaseNode choiceCaseNode = (ChoiceCaseNode) when;
- if (choiceCaseNode.getConstraints() == null || choiceCaseNode.getConstraints().getWhenCondition() == null) {
+ CaseSchemaNode choiceCaseNode = (CaseSchemaNode) when;
+ if (!choiceCaseNode.getWhenCondition().isPresent()) {
return;
}
- RevisionAwareXPath xPath = choiceCaseNode.getConstraints().getWhenCondition();
- Matcher matcher = getWhenConditionMatcher(configModulePrefix, xPath);
+ java.util.Optional<RevisionAwareXPath> xPath = choiceCaseNode.getWhenCondition();
+ checkState(xPath.isPresent(), "Choice node %s does not have a when condition", choiceCaseNode);
+ Matcher matcher = getWhenConditionMatcher(configModulePrefix, xPath.get());
if (matcher.matches() == false) {
return;
}
String moduleLocalNameFromXPath = matcher.group(1);
IdentitySchemaNode moduleIdentity = moduleIdentities.get(moduleLocalNameFromXPath);
unaugmentedModuleIdentities.remove(moduleLocalNameFromXPath);
- checkState(moduleIdentity != null, "Cannot find identity " + moduleLocalNameFromXPath
- + " matching augmentation " + augmentation);
+ checkState(moduleIdentity != null, "Cannot find identity %s matching augmentation %s", moduleLocalNameFromXPath, augmentation);
Map<String, QName> providedServices = findProvidedServices(moduleIdentity, currentModule, qNamesToSIEs,
schemaContext);
- if (moduleIdentity == null) {
- throw new IllegalStateException("Cannot find identity specified by augmentation xpath constraint: "
- + moduleLocalNameFromXPath + " of " + augmentation);
- }
String javaNamePrefix = TypeProviderWrapper.findJavaNamePrefix(moduleIdentity);
Map<String, AttributeIfc> yangToAttributes = null;
HAS_CHILDREN_AND_QNAME dataNodeContainer = getDataNodeContainer(choiceCaseNode);
- if (expectedConfigurationAugmentationSchemaPath.equals(augmentation.getTargetPath())) {
- logger.debug("Parsing configuration of {}", moduleLocalNameFromXPath);
+ if (EXPECTED_CONFIGURATION_AUGMENTATION_SCHEMA_PATH.equals(augmentation.getTargetPath())) {
+ LOG.debug("Parsing configuration of {}", moduleLocalNameFromXPath);
yangToAttributes = fillConfiguration(dataNodeContainer, currentModule, typeProviderWrapper, qNamesToSIEs,
schemaContext, packageName);
checkUniqueAttributesWithGeneratedClass(uniqueGeneratedClassesNames, when.getQName(), yangToAttributes);
- } else if (expectedStateAugmentationSchemaPath.equals(augmentation.getTargetPath())) {
- logger.debug("Parsing state of {}", moduleLocalNameFromXPath);
+ } else if (EXPECTED_STATE_AUGMENTATION_SCHEMA_PATH.equals(augmentation.getTargetPath())) {
+ LOG.debug("Parsing state of {}", moduleLocalNameFromXPath);
try {
runtimeBeans = fillRuntimeBeans(dataNodeContainer, currentModule, typeProviderWrapper, packageName,
moduleLocalNameFromXPath, javaNamePrefix);
" both state and configuration");
} else {
ModuleMXBeanEntry.ModuleMXBeanEntryInitial initial = new ModuleMXBeanEntry.ModuleMXBeanEntryInitialBuilder()
- .setIdSchemaNode(moduleIdentity).setPackageName(packageName).setJavaNamePrefix(javaNamePrefix)
- .setNamespace(currentModule.getNamespace().toString()).setqName(ModuleUtil.getQName(currentModule))
- .build();
+ .setIdSchemaNode(moduleIdentity).setPackageName(packageName).setJavaNamePrefix(javaNamePrefix)
+ .setNamespace(currentModule.getNamespace().toString()).setqName(ModuleUtil.getQName(currentModule))
+ .build();
// construct ModuleMXBeanEntry
ModuleMXBeanEntry moduleMXBeanEntry = new ModuleMXBeanEntry(initial, yangToAttributes, providedServices,
}
}
- private void checkUniqueRuntimeBeansGeneratedClasses(final Map<String, QName> uniqueGeneratedClassesNames,
+ private static void checkUniqueRuntimeBeansGeneratedClasses(final Map<String, QName> uniqueGeneratedClassesNames,
final DataSchemaNode when, final Collection<RuntimeBeanEntry> runtimeBeans) {
for (RuntimeBeanEntry runtimeBean : runtimeBeans) {
final String javaNameOfRuntimeMXBean = runtimeBean.getJavaNameOfRuntimeMXBean();
}
}
- private void checkUniqueAttributesWithGeneratedClass(final Map<String, QName> uniqueGeneratedClassNames,
+ private static void checkUniqueAttributesWithGeneratedClass(final Map<String, QName> uniqueGeneratedClassNames,
final QName parentQName, final Map<String, AttributeIfc> yangToAttributes) {
for (Map.Entry<String, AttributeIfc> attr : yangToAttributes.entrySet()) {
if (attr.getValue() instanceof TOAttribute) {
}
}
- private void checkUniqueTOAttr(final Map<String, QName> uniqueGeneratedClassNames, final QName parentQName, final TOAttribute attr) {
+ private static void checkUniqueTOAttr(final Map<String, QName> uniqueGeneratedClassNames, final QName parentQName, final TOAttribute attr) {
final String upperCaseCamelCase = attr.getUpperCaseCammelCase();
if (uniqueGeneratedClassNames.containsKey(upperCaseCamelCase)) {
QName firstDefinedQName = uniqueGeneratedClassNames.get(upperCaseCamelCase);
final String javaNamePrefix) {
return RuntimeBeanEntry.extractClassNameToRuntimeBeanMap(packageName, dataNodeContainer, moduleLocalNameFromXPath,
- typeProviderWrapper, javaNamePrefix, currentModule).values();
+ typeProviderWrapper, javaNamePrefix, currentModule, schemaContext).values();
}
* @param choiceCaseNode state or configuration case statement
* @return either choiceCaseNode or its only child container
*/
- private <HAS_CHILDREN_AND_QNAME extends DataNodeContainer & SchemaNode> HAS_CHILDREN_AND_QNAME getDataNodeContainer(final ChoiceCaseNode choiceCaseNode) {
- Set<DataSchemaNode> childNodes = choiceCaseNode.getChildNodes();
+ private static <HAS_CHILDREN_AND_QNAME extends DataNodeContainer & SchemaNode> HAS_CHILDREN_AND_QNAME getDataNodeContainer(final CaseSchemaNode choiceCaseNode) {
+ Collection<DataSchemaNode> childNodes = choiceCaseNode.getChildNodes();
if (childNodes.size() == 1) {
DataSchemaNode onlyChild = childNodes.iterator().next();
if (onlyChild instanceof ContainerSchemaNode) {
return (HAS_CHILDREN_AND_QNAME) choiceCaseNode;
}
- private Map<String, AttributeIfc> fillConfiguration(final DataNodeContainer dataNodeContainer, final Module currentModule,
+ private static Map<String, AttributeIfc> fillConfiguration(final DataNodeContainer dataNodeContainer, final Module currentModule,
final TypeProviderWrapper typeProviderWrapper, final Map<QName, ServiceInterfaceEntry> qNamesToSIEs,
final SchemaContext schemaContext, final String packageName) {
Map<String, AttributeIfc> yangToAttributes = new HashMap<>();
- Set<DataSchemaNode> childNodes = dataNodeContainer.getChildNodes();
- for (DataSchemaNode attrNode : childNodes) {
+ for (DataSchemaNode attrNode : dataNodeContainer.getChildNodes()) {
AttributeIfc attributeValue = getAttributeValue(attrNode, currentModule, qNamesToSIEs, typeProviderWrapper,
schemaContext, packageName);
yangToAttributes.put(attributeValue.getAttributeYangName(), attributeValue);
return yangToAttributes;
}
- private Map<String, QName> findProvidedServices(final IdentitySchemaNode moduleIdentity, final Module currentModule,
+ private static Map<String, QName> findProvidedServices(final IdentitySchemaNode moduleIdentity, final Module currentModule,
final Map<QName, ServiceInterfaceEntry> qNamesToSIEs, final SchemaContext schemaContext) {
Map<String, QName> result = new HashMap<>();
for (UnknownSchemaNode unknownNode : moduleIdentity.getUnknownSchemaNodes()) {
return result;
}
- private AttributeIfc getAttributeValue(final DataSchemaNode attrNode, final Module currentModule,
+ private static AttributeIfc getAttributeValue(final DataSchemaNode attrNode, final Module currentModule,
final Map<QName, ServiceInterfaceEntry> qNamesToSIEs, final TypeProviderWrapper typeProviderWrapper,
final SchemaContext schemaContext, final String packageName) {
}
}
- private Optional<? extends AbstractDependencyAttribute> extractDependency(final DataNodeContainer dataNodeContainer,
+ private static Optional<? extends AbstractDependencyAttribute> extractDependency(final DataNodeContainer dataNodeContainer,
final DataSchemaNode attrNode, final Module currentModule, final Map<QName, ServiceInterfaceEntry> qNamesToSIEs,
final SchemaContext schemaContext) {
- if (dataNodeContainer.getUses().size() == 1 && getChildNodeSizeWithoutUses(dataNodeContainer) == 0) {
+ if (isDependencyContainer(dataNodeContainer)) {
// reference
UsesNode usesNode = dataNodeContainer.getUses().iterator().next();
- checkState(usesNode.getRefines().size() == 1, "Unexpected 'refine' child node size of " + dataNodeContainer);
- LeafSchemaNode refine = (LeafSchemaNode) usesNode.getRefines().values().iterator().next();
- checkState(refine.getUnknownSchemaNodes().size() == 1, "Unexpected unknown schema node size of " + refine);
- UnknownSchemaNode requiredIdentity = refine.getUnknownSchemaNodes().iterator().next();
- checkState(ConfigConstants.REQUIRED_IDENTITY_EXTENSION_QNAME.equals(requiredIdentity.getNodeType()),
- "Unexpected language extension " + requiredIdentity);
- String prefixAndIdentityLocalName = requiredIdentity.getNodeParameter();
- // import should point to a module
- ServiceInterfaceEntry serviceInterfaceEntry = findSIE(prefixAndIdentityLocalName, currentModule,
- qNamesToSIEs, schemaContext);
- boolean mandatory = refine.getConstraints().isMandatory();
- AbstractDependencyAttribute reference;
- if (dataNodeContainer instanceof ContainerSchemaNode) {
- reference = new DependencyAttribute(attrNode, serviceInterfaceEntry, mandatory,
- attrNode.getDescription());
- } else {
- reference = new ListDependenciesAttribute(attrNode, serviceInterfaceEntry, mandatory,
- attrNode.getDescription());
+ for (SchemaNode refineNode : usesNode.getRefines().values()) {
+ // this will ignore name nodes, since they are not needed here
+ if (TYPE.equals(refineNode.getQName().getLocalName())){
+ checkState(refineNode.getUnknownSchemaNodes().size() == 1, "Unexpected unknown schema node size of " + refineNode);
+ UnknownSchemaNode requiredIdentity = refineNode.getUnknownSchemaNodes().iterator().next();
+ checkState(ConfigConstants.REQUIRED_IDENTITY_EXTENSION_QNAME.equals(requiredIdentity.getNodeType()),
+ "Unexpected language extension " + requiredIdentity);
+ String prefixAndIdentityLocalName = requiredIdentity.getNodeParameter();
+ // import should point to a module
+ ServiceInterfaceEntry serviceInterfaceEntry = findSIE(prefixAndIdentityLocalName, currentModule,
+ qNamesToSIEs, schemaContext);
+ LeafSchemaNode refine = (LeafSchemaNode) usesNode.getRefines().values().iterator().next();
+
+ boolean mandatory = refine.isMandatory();
+ AbstractDependencyAttribute reference;
+ if (dataNodeContainer instanceof ContainerSchemaNode) {
+ reference = new DependencyAttribute(attrNode, serviceInterfaceEntry, mandatory,
+ attrNode.getDescription().orElse(null));
+ } else {
+ reference = new ListDependenciesAttribute(attrNode, serviceInterfaceEntry, mandatory,
+ attrNode.getDescription().orElse(null));
+ }
+ return Optional.of(reference);
+ }
}
- return Optional.of(reference);
}
return Optional.absent();
}
- private int getChildNodeSizeWithoutUses(final DataNodeContainer csn) {
+ private static boolean isDependencyContainer(final DataNodeContainer dataNodeContainer) {
+ if(dataNodeContainer.getUses().size() != 1) {
+ return false;
+ }
+ UsesNode onlyUses = dataNodeContainer.getUses().iterator().next();
+ if(onlyUses.getGroupingPath().getLastComponent().equals(ServiceRef.QNAME) == false) {
+ return false;
+ }
+
+ return getChildNodeSizeWithoutUses(dataNodeContainer) == 0;
+ }
+
+ private static int getChildNodeSizeWithoutUses(final DataNodeContainer csn) {
int result = 0;
for (DataSchemaNode dsn : csn.getChildNodes()) {
if (dsn.isAddedByUses() == false) {
return result;
}
- private ServiceInterfaceEntry findSIE(final String prefixAndIdentityLocalName, final Module currentModule,
+ private static ServiceInterfaceEntry findSIE(final String prefixAndIdentityLocalName, final Module currentModule,
final Map<QName, ServiceInterfaceEntry> qNamesToSIEs, final SchemaContext schemaContext) {
Matcher m = PREFIX_COLON_LOCAL_NAME.matcher(prefixAndIdentityLocalName);
// Module from SchemaContext
String prefix = m.group(1);
ModuleImport moduleImport = findModuleImport(currentModule, prefix);
- foundModule = schemaContext.findModuleByName(moduleImport.getModuleName(), moduleImport.getRevision());
+ foundModule = schemaContext.findModule(moduleImport.getModuleName(), moduleImport.getRevision()).orElse(null);
checkNotNull(foundModule, format("Module not found in SchemaContext by %s", moduleImport));
localSIName = m.group(2);
} else {
return sie;
}
- private ModuleImport findModuleImport(final Module module, final String prefix) {
+ private static ModuleImport findModuleImport(final Module module, final String prefix) {
for (ModuleImport moduleImport : module.getImports()) {
if (moduleImport.getPrefix().equals(prefix)) {
return moduleImport;
return pattern.matcher(whenConstraint.toString());
}
- String getConfigModulePrefixFromImport(final Module currentModule) {
+ private static String getConfigModulePrefixFromImport(final Module currentModule) {
for (ModuleImport currentImport : currentModule.getImports()) {
if (currentImport.getModuleName().equals(ConfigConstants.CONFIG_MODULE)) {
return currentImport.getPrefix();