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 com.google.common.base.Verify.verify;
11 import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.BASE_IDENTITY;
13 import java.util.List;
14 import java.util.stream.Collectors;
15 import org.opendaylight.mdsal.binding.generator.impl.rt.DefaultIdentityRuntimeType;
16 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
17 import org.opendaylight.mdsal.binding.model.api.Type;
18 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
19 import org.opendaylight.mdsal.binding.runtime.api.IdentityRuntimeType;
20 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping;
21 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
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 narrowImplementedInterface(builder);
68 final ModuleGenerator module = currentModule();
69 module.addQNameConstant(builder, localName());
71 // Constant implementation
72 builder.addConstant(Type.of(builder), BindingMapping.VALUE_STATIC_FIELD_NAME, BaseIdentity.class);
74 builderFactory.addCodegenInformation(module, statement(), builder);
75 builder.setModuleName(module.statement().argument().getLocalName());
76 // builder.setSchemaPath(identity.getPath());
78 return builder.build();
82 IdentityRuntimeType createExternalRuntimeType(final Type type) {
83 verify(type instanceof GeneratedType, "Unexpected type %s", type);
84 return new DefaultIdentityRuntimeType((GeneratedType) type, statement());
88 IdentityRuntimeType createInternalRuntimeType(final AugmentResolver resolver,
89 final IdentityEffectiveStatement statement, final Type type) {
90 // 'identity' statements are not part of schema tree and hence should never an internal reference
91 throw new UnsupportedOperationException("Should never be called");
95 void addAsGetterMethod(final GeneratedTypeBuilderBase<?> builder, final TypeBuilderFactory builderFactory) {
96 // identities are a separate concept