26814de84df06e3722e4381f8b7e7ea34661a8ae
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / reactor / IdentityGenerator.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl.reactor;
9
10 import static com.google.common.base.Verify.verify;
11 import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.BASE_IDENTITY;
12
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.yangtools.yang.binding.BaseIdentity;
21 import org.opendaylight.yangtools.yang.binding.contract.Naming;
22 import org.opendaylight.yangtools.yang.binding.contract.StatementNamespace;
23 import org.opendaylight.yangtools.yang.model.api.stmt.BaseEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
26
27 /**
28  * Generator corresponding to a {@code identity} statement.
29  */
30 public final class IdentityGenerator
31         extends AbstractDependentGenerator<IdentityEffectiveStatement, IdentityRuntimeType> {
32     private List<IdentityGenerator> baseIdentities = null;
33
34     IdentityGenerator(final IdentityEffectiveStatement statement, final AbstractCompositeGenerator<?, ?> parent) {
35         super(statement, parent);
36     }
37
38     @Override
39     StatementNamespace namespace() {
40         return StatementNamespace.IDENTITY;
41     }
42
43     @Override
44     void pushToInference(final SchemaInferenceStack dataTree) {
45         throw new UnsupportedOperationException("Cannot push " + statement() + " to data tree");
46     }
47
48     @Override
49     void linkDependencies(final GeneratorContext context) {
50         baseIdentities = statement().streamEffectiveSubstatements(BaseEffectiveStatement.class)
51             .map(BaseEffectiveStatement::argument)
52             .map(context::resolveIdentity)
53             .collect(Collectors.toUnmodifiableList());
54     }
55
56     @Override
57     GeneratedType createTypeImpl(final TypeBuilderFactory builderFactory) {
58         final var builder = builderFactory.newGeneratedTypeBuilder(typeName());
59         if (!baseIdentities.isEmpty()) {
60             for (var baseIdentity : baseIdentities) {
61                 builder.addImplementsType(baseIdentity.getGeneratedType(builderFactory));
62             }
63         } else {
64             builder.addImplementsType(BASE_IDENTITY);
65         }
66
67         annotateDeprecatedIfNecessary(statement(), builder);
68
69         narrowImplementedInterface(builder);
70
71         final ModuleGenerator module = currentModule();
72         module.addQNameConstant(builder, localName());
73
74         // Constant implementation
75         builder.addConstant(Type.of(builder), Naming.VALUE_STATIC_FIELD_NAME, BaseIdentity.class);
76
77         builderFactory.addCodegenInformation(module, statement(), builder);
78         builder.setModuleName(module.statement().argument().getLocalName());
79 //        builder.setSchemaPath(identity.getPath());
80
81         return builder.build();
82     }
83
84     @Override
85     IdentityRuntimeType createExternalRuntimeType(final Type type) {
86         verify(type instanceof GeneratedType, "Unexpected type %s", type);
87         return new DefaultIdentityRuntimeType((GeneratedType) type, statement());
88     }
89
90     @Override
91     IdentityRuntimeType createInternalRuntimeType(final AugmentResolver resolver,
92             final IdentityEffectiveStatement statement, final Type type) {
93         // 'identity' statements are not part of schema tree and hence should never an internal reference
94         throw new UnsupportedOperationException("Should never be called");
95     }
96
97     @Override
98     void addAsGetterMethod(final GeneratedTypeBuilderBase<?> builder, final TypeBuilderFactory builderFactory) {
99         // identities are a separate concept
100     }
101 }