Improve RpcRoutingStrategyTest
[mdsal.git] / binding / mdsal-binding-generator-impl / src / main / java / org / opendaylight / mdsal / binding / generator / impl / GeneratorUtils.java
1 /*
2  * Copyright (c) 2020 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;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkState;
12
13 import java.util.Map;
14 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
15 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
16 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 final class GeneratorUtils {
24     private static final Logger LOG = LoggerFactory.getLogger(GeneratorUtils.class);
25
26     private GeneratorUtils() {
27
28     }
29
30     static void checkContext(final SchemaContext context) {
31         checkArgument(context != null, "Schema Context reference cannot be NULL.");
32         checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
33     }
34
35     static void rename(final Map<SchemaNode, JavaTypeName> renames, final RenameMappingException ex) {
36         final JavaTypeName name = ex.getName();
37         final SchemaNode def = ex.getDefinition();
38         final JavaTypeName existing = renames.get(def);
39         if (existing != null) {
40             throw new IllegalStateException("Attempted to relocate " + def + " to " + name + ", already remapped to "
41                     + existing, ex);
42         }
43
44         final String suffix;
45         if (def instanceof IdentitySchemaNode) {
46             suffix = "$I";
47         } else if (def instanceof GroupingDefinition) {
48             suffix = "$G";
49         } else if (def instanceof TypeDefinition) {
50             suffix = "$T";
51         } else {
52             throw new IllegalStateException("Unhandled remapping of " + def + " at " + name, ex);
53         }
54
55         final JavaTypeName newName = name.createSibling(name.simpleName() + suffix);
56         renames.put(def, newName);
57         LOG.debug("Restarting code generation after remapping {} to {}", name, newName);
58     }
59
60 }