/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import java.net.URI; import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import org.opendaylight.yangtools.concepts.Immutable; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.common.QNameModule; import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.Deviation; import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition; import org.opendaylight.yangtools.yang.model.api.FeatureDefinition; import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; import org.opendaylight.yangtools.yang.model.api.ModuleImport; import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; import org.opendaylight.yangtools.yang.model.api.RpcDefinition; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; import org.opendaylight.yangtools.yang.model.api.UsesNode; import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleStatement; import org.opendaylight.yangtools.yang.model.util.ModuleImportImpl; import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable; import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource; import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils; abstract class AbstractEffectiveModule> extends AbstractEffectiveDocumentedNode implements Module, Immutable { private final String name; private final String sourcePath; private final String prefix; private final String yangVersion; private final String organization; private final String contact; private final Set imports; private final Set submodules; private final Set features; private final Set notifications; private final Set augmentations; private final Set rpcs; private final Set deviations; private final List extensionNodes; private final Set identities; private final List unknownNodes; private final Map childNodes; private final Set groupings; private final Set uses; private final Set> typeDefinitions; private final Set publicChildNodes; AbstractEffectiveModule(final StmtContext> ctx) { super(ctx); this.name = argument(); PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class); this.prefix = (prefixStmt == null) ? null : prefixStmt.argument(); YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class); this.yangVersion = (yangVersionStmt == null) ? "1" : yangVersionStmt.argument(); OrganizationEffectiveStatementImpl organizationStmt = firstEffective(OrganizationEffectiveStatementImpl.class); this.organization = (organizationStmt == null) ? null : organizationStmt.argument(); ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class); this.contact = (contactStmt == null) ? null : contactStmt.argument(); if (ctx.getStatementSourceReference() instanceof DeclarationInTextSource) { this.sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName(); } else { this.sourcePath = null; } // init submodules and substatements of submodules final List> substatementsOfSubmodules; final Map includedSubmodulesMap = ctx .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class); if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) { this.submodules = ImmutableSet.of(); substatementsOfSubmodules = ImmutableList.of(); } else { Collection includedSubmodules = includedSubmodulesMap.values(); Set submodulesInit = new HashSet<>(); List> substatementsOfSubmodulesInit = new LinkedList<>(); for (ModuleIdentifier submoduleIdentifier : includedSubmodules) { @SuppressWarnings("unchecked") Mutable> submoduleCtx = (Mutable>) ctx .getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier); SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx .buildEffective(); submodulesInit.add(submodule); substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements()); } this.submodules = ImmutableSet.copyOf(submodulesInit); substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit); } // init substatements collections List> effectiveSubstatements = new LinkedList<>(); effectiveSubstatements.addAll(effectiveSubstatements()); effectiveSubstatements.addAll(substatementsOfSubmodules); List unknownNodesInit = new LinkedList<>(); Set augmentationsInit = new HashSet<>(); Set importsInit = new HashSet<>(); Set notificationsInit = new HashSet<>(); Set rpcsInit = new HashSet<>(); Set deviationsInit = new HashSet<>(); Set identitiesInit = new HashSet<>(); Set featuresInit = new HashSet<>(); List extensionNodesInit = new LinkedList<>(); Map mutableChildNodes = new LinkedHashMap<>(); Set mutableGroupings = new HashSet<>(); Set mutableUses = new HashSet<>(); Set> mutableTypeDefinitions = new LinkedHashSet<>(); Set mutablePublicChildNodes = new LinkedHashSet<>(); for (EffectiveStatement effectiveStatement : effectiveSubstatements) { if (effectiveStatement instanceof UnknownSchemaNode) { unknownNodesInit.add((UnknownSchemaNode) effectiveStatement); } if (effectiveStatement instanceof AugmentationSchema) { augmentationsInit.add((AugmentationSchema) effectiveStatement); } if (effectiveStatement instanceof ModuleImport) { importsInit.add((ModuleImport) effectiveStatement); } if (effectiveStatement instanceof NotificationDefinition) { notificationsInit.add((NotificationDefinition) effectiveStatement); } if (effectiveStatement instanceof RpcDefinition) { rpcsInit.add((RpcDefinition) effectiveStatement); } if (effectiveStatement instanceof Deviation) { deviationsInit.add((Deviation) effectiveStatement); } if (effectiveStatement instanceof IdentitySchemaNode) { identitiesInit.add((IdentitySchemaNode) effectiveStatement); } if (effectiveStatement instanceof FeatureDefinition) { featuresInit.add((FeatureDefinition) effectiveStatement); } if (effectiveStatement instanceof ExtensionDefinition) { extensionNodesInit.add((ExtensionDefinition) effectiveStatement); } if (effectiveStatement instanceof DataSchemaNode) { DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement; if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) { mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode); mutablePublicChildNodes.add(dataSchemaNode); } else { throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement); } } if (effectiveStatement instanceof UsesNode) { UsesNode usesNode = (UsesNode) effectiveStatement; if (!mutableUses.contains(usesNode)) { mutableUses.add(usesNode); } else { throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement); } } if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) { TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement; TypeDefinition type = typeDef.getTypeDefinition(); if (!mutableTypeDefinitions.contains(type)) { mutableTypeDefinitions.add(type); } else { throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement); } } if (effectiveStatement instanceof GroupingDefinition) { GroupingDefinition grp = (GroupingDefinition) effectiveStatement; if (!mutableGroupings.contains(grp)) { mutableGroupings.add(grp); } else { throw EffectiveStmtUtils.createNameCollisionSourceException(ctx, effectiveStatement); } } } this.unknownNodes = ImmutableList.copyOf(unknownNodesInit); this.augmentations = ImmutableSet.copyOf(augmentationsInit); this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx)); this.notifications = ImmutableSet.copyOf(notificationsInit); this.rpcs = ImmutableSet.copyOf(rpcsInit); this.deviations = ImmutableSet.copyOf(deviationsInit); this.identities = ImmutableSet.copyOf(identitiesInit); this.features = ImmutableSet.copyOf(featuresInit); this.extensionNodes = ImmutableList.copyOf(extensionNodesInit); this.childNodes = ImmutableMap.copyOf(mutableChildNodes); this.groupings = ImmutableSet.copyOf(mutableGroupings); this.publicChildNodes = ImmutableSet.copyOf(mutablePublicChildNodes); this.typeDefinitions = ImmutableSet.copyOf(mutableTypeDefinitions); this.uses = ImmutableSet.copyOf(mutableUses); } private static Set resolveModuleImports(final Set importsInit, final StmtContext, ? extends EffectiveStatement> ctx) { Set resolvedModuleImports = new LinkedHashSet<>(); for (ModuleImport moduleImport : importsInit) { if (moduleImport.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_IMP)) { QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx, moduleImport.getPrefix()); if (!impModuleQName.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_REV)) { ModuleImport resolvedModuleImport = new ModuleImportImpl(moduleImport.getModuleName(), impModuleQName.getRevision(), moduleImport.getPrefix()); resolvedModuleImports.add(resolvedModuleImport); } } else { resolvedModuleImports.add(moduleImport); } } return resolvedModuleImports; } @Override public String getModuleSourcePath() { return sourcePath; } @Override public String getSource() { return null; } @Override public URI getNamespace() { return getQNameModule().getNamespace(); } @Override public String getName() { return name; } @Override public Date getRevision() { return getQNameModule().getRevision(); } @Override public String getPrefix() { return prefix; } @Override public String getYangVersion() { return yangVersion; } @Override public String getOrganization() { return organization; } @Override public String getContact() { return contact; } @Override public Set getImports() { return imports; } @Override public Set getSubmodules() { return submodules; } @Override public Set getFeatures() { return features; } @Override public Set getNotifications() { return notifications; } @Override public Set getAugmentations() { return augmentations; } @Override public Set getRpcs() { return rpcs; } @Override public Set getDeviations() { return deviations; } @Override public List getExtensionSchemaNodes() { return extensionNodes; } @Override public Set getIdentities() { return identities; } @Override public List getUnknownSchemaNodes() { return unknownNodes; } @Override public final Set> getTypeDefinitions() { return typeDefinitions; } @Override public final Set getChildNodes() { return publicChildNodes; } @Override public final Set getGroupings() { return groupings; } @Override public final DataSchemaNode getDataChildByName(final QName name) { // Child nodes are keyed by their container name, so we can do a direct // lookup return childNodes.get(name); } @Override public final DataSchemaNode getDataChildByName(final String name) { for (DataSchemaNode node : childNodes.values()) { if (node.getQName().getLocalName().equals(name)) { return node; } } return null; } @Override public Set getUses() { return uses; } @Override public String toString() { StringBuilder sb = new StringBuilder(this.getClass().getSimpleName()); sb.append("["); sb.append("name=").append(name); sb.append(", namespace=").append(getNamespace()); sb.append(", revision=").append(getRevision()); sb.append(", prefix=").append(prefix); sb.append(", yangVersion=").append(yangVersion); sb.append("]"); return sb.toString(); } }