2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.generator.impl.reactor;
10 import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.BASE_IDENTITY;
12 import java.util.List;
13 import java.util.stream.Collectors;
14 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultIdentityRuntimeType;
15 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
16 import org.opendaylight.mdsal.binding.model.api.Type;
17 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
18 import org.opendaylight.mdsal.binding.runtime.api.IdentityRuntimeType;
19 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
20 import org.opendaylight.yangtools.yang.binding.contract.Naming;
21 import org.opendaylight.yangtools.yang.binding.contract.StatementNamespace;
22 import org.opendaylight.yangtools.yang.model.api.stmt.BaseEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
27 * Generator corresponding to a {@code identity} statement.
29 public final class IdentityGenerator
30 extends AbstractDependentGenerator<IdentityEffectiveStatement, IdentityRuntimeType> {
31 private List<IdentityGenerator> baseIdentities = null;
33 IdentityGenerator(final IdentityEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
34 super(statement, parent);
38 StatementNamespace namespace() {
39 return StatementNamespace.IDENTITY;
43 void pushToInference(final SchemaInferenceStack dataTree) {
44 throw new UnsupportedOperationException("Cannot push " + statement() + " to data tree");
48 void linkDependencies(final GeneratorContext context) {
49 baseIdentities = statement().streamEffectiveSubstatements(BaseEffectiveStatement.class)
50 .map(BaseEffectiveStatement::argument)
51 .map(context::resolveIdentity)
52 .collect(Collectors.toUnmodifiableList());
56 GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
57 final var builder = builderFactory.newGeneratedTypeBuilder(typeName());
58 if (!baseIdentities.isEmpty()) {
59 for (var baseIdentity : baseIdentities) {
60 builder.addImplementsType(baseIdentity.getGeneratedType(builderFactory));
63 builder.addImplementsType(BASE_IDENTITY);
66 annotateDeprecatedIfNecessary(statement(), builder);
68 narrowImplementedInterface(builder);
70 final var module = currentModule();
71 module.addQNameConstant(builder, localName());
73 // Constant implementation
74 builder.addConstant(Type.of(builder), Naming.VALUE_STATIC_FIELD_NAME, BaseIdentity.class);
76 builderFactory.addCodegenInformation(module, statement(), builder);
77 builder.setModuleName(module.statement().argument().getLocalName());
78 // builder.setSchemaPath(identity.getPath());
80 return builder.build();
84 IdentityRuntimeType createExternalRuntimeType(final Type type) {
85 return new DefaultIdentityRuntimeType(verifyGeneratedType(type), statement());
89 IdentityRuntimeType createInternalRuntimeType(final AugmentResolver resolver,
90 final IdentityEffectiveStatement statement, final Type type) {
91 // 'identity' statements are not part of schema tree and hence should never an internal reference
92 throw new UnsupportedOperationException("Should never be called");
96 void addAsGetterMethod(final GeneratedTypeBuilderBase<?> builder, final TypeBuilderFactory builderFactory) {
97 // identities are a separate concept