/* * 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; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet.Builder; import java.util.Collection; import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier; import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator; import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement; import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.KeyEffectiveStatementImpl; public class KeyStatementImpl extends AbstractDeclaredStatement> implements KeyStatement { private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping .KEY) .build(); protected KeyStatementImpl(final StmtContext, KeyStatement, ?> context) { super(context); } public static class Definition extends AbstractStatementSupport, KeyStatement, EffectiveStatement, KeyStatement>> { public Definition() { super(Rfc6020Mapping.KEY); } @Override public Collection parseArgumentValue(final StmtContext ctx, final String value) { final Builder builder = ImmutableSet.builder(); int tokens = 0; for (String keyToken : StmtContextUtils.LIST_KEY_SPLITTER.split(value)) { builder.add(SchemaNodeIdentifier.create(false, Utils.qNameFromArgument(ctx, keyToken))); tokens++; } // Throws NPE on nulls, retains first inserted value, cannot be modified final Collection ret = builder.build(); Preconditions.checkArgument(ret.size() == tokens, "Key argument '%s' contains duplicates. At %s", value, ctx.getStatementSourceReference()); return ret; } @Override public KeyStatement createDeclared(final StmtContext, KeyStatement, ?> ctx) { return new KeyStatementImpl(ctx); } @Override public EffectiveStatement, KeyStatement> createEffective( final StmtContext, KeyStatement, EffectiveStatement, KeyStatement>> ctx) { return new KeyEffectiveStatementImpl(ctx); } @Override public void onFullDefinitionDeclared(StmtContext.Mutable, KeyStatement, EffectiveStatement, KeyStatement>> stmt) throws SourceException { super.onFullDefinitionDeclared(stmt); SUBSTATEMENT_VALIDATOR.validate(stmt); } } }