2 * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.config.yangjmxgenerator;
10 import com.google.common.base.Optional;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.Iterator;
15 import java.util.List;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import static com.google.common.base.Preconditions.checkNotNull;
25 import static java.lang.String.format;
26 import static org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants.SERVICE_TYPE_Q_NAME;
29 * Represents identity derived from {@link ConfigConstants#SERVICE_TYPE_Q_NAME}.
37 * "Service representing an event bus. The service acts as message
38 * router between event producers and event consumers";
40 * base "config:service-type";
41 * config:java-class "com.google.common.eventbus.EventBus";
48 public class ServiceInterfaceEntry extends AbstractEntry {
49 private static final Logger logger = LoggerFactory
50 .getLogger(ServiceInterfaceEntry.class);
52 private static final String CLASS_NAME_SUFFIX = "ServiceInterface";
53 private final Optional<ServiceInterfaceEntry> maybeBaseCache;
54 private final String exportedOsgiClassName;
55 private final QName qName;
56 private final String nullableDescription, packageName, typeName;
57 private final QName yangModuleQName;
59 private ServiceInterfaceEntry(IdentitySchemaNode id, String packageName, QName yangModuleQName) {
60 this(Optional.<ServiceInterfaceEntry> absent(), id, packageName, yangModuleQName);
63 private ServiceInterfaceEntry(Optional<ServiceInterfaceEntry> base,
64 IdentitySchemaNode id, String packageName, QName yangModuleQName) {
66 this.maybeBaseCache = base;
67 List<UnknownSchemaNode> unknownSchemaNodes = id.getUnknownSchemaNodes();
68 List<String> exportedOsgiClassNames = new ArrayList<>(
69 unknownSchemaNodes.size());
70 for (UnknownSchemaNode usn : unknownSchemaNodes) {
71 if (ConfigConstants.JAVA_CLASS_EXTENSION_QNAME.equals(usn
73 String localName = usn.getNodeParameter();
74 exportedOsgiClassNames.add(localName);
76 throw new IllegalStateException(format(
77 "Unexpected unknown schema node. Expected %s, got %s",
78 ConfigConstants.JAVA_CLASS_EXTENSION_QNAME,
82 if (exportedOsgiClassNames.size() != 1) {
83 throw new IllegalArgumentException(
84 format("Cannot find one to one mapping from %s to "
85 + "java class defined by %s language extension in %s",
87 ConfigConstants.JAVA_CLASS_EXTENSION_QNAME, id));
89 this.exportedOsgiClassName = exportedOsgiClassNames.get(0);
90 qName = id.getQName();
91 nullableDescription = id.getDescription();
92 typeName = getSimpleName(exportedOsgiClassName) + CLASS_NAME_SUFFIX;
93 this.packageName = packageName;
94 this.yangModuleQName = yangModuleQName;
97 private static final String getSimpleName(String fullyQualifiedName) {
98 int lastDotPosition = fullyQualifiedName.lastIndexOf(".");
99 return fullyQualifiedName.substring(lastDotPosition + 1);
102 public String getNullableDescription() {
103 return nullableDescription;
106 public Optional<ServiceInterfaceEntry> getBase() {
107 return maybeBaseCache;
110 public String getExportedOsgiClassName() {
111 return exportedOsgiClassName;
114 public QName getQName() {
119 * @return Map of QNames as keys and ServiceInterfaceEntry instances as
122 public static Map<QName, ServiceInterfaceEntry> create(Module currentModule,
123 String packageName,Map<IdentitySchemaNode, ServiceInterfaceEntry> definedSEItracker) {
124 logger.debug("Generating ServiceInterfaces from {} to package {}",
125 currentModule.getNamespace(), packageName);
127 Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
128 Set<IdentitySchemaNode> notVisited = new HashSet<>(
129 currentModule.getIdentities());
130 int lastSize = notVisited.size() + 1;
131 while (notVisited.size() > 0) {
132 if (notVisited.size() == lastSize) {
134 "Following identities will be ignored while generating ServiceInterfaces, as they are not derived from {} : {}",
135 SERVICE_TYPE_Q_NAME, notVisited);
138 lastSize = notVisited.size();
139 for (Iterator<IdentitySchemaNode> iterator = notVisited.iterator(); iterator
141 IdentitySchemaNode identity = iterator.next();
142 ServiceInterfaceEntry created = null;
143 if (identity.getBaseIdentity() == null) {
144 // this can happen while loading config module, just skip
147 } else if (identity.getBaseIdentity().getQName()
148 .equals(SERVICE_TYPE_Q_NAME)) {
149 // this is a base type
150 created = new ServiceInterfaceEntry(identity, packageName, ModuleUtil.getQName(currentModule));
152 ServiceInterfaceEntry foundBase = definedSEItracker
153 .get(identity.getBaseIdentity());
154 // derived type, did we convert the parent?
155 if (foundBase != null) {
156 created = new ServiceInterfaceEntry(
157 Optional.of(foundBase), identity, packageName, ModuleUtil.getQName(currentModule));
162 if (created != null) {
163 created.setYangModuleName(currentModule.getName());
164 // TODO how to get local name
165 created.setYangModuleLocalname(identity.getQName()
167 identitiesToSIs.put(identity, created);
168 definedSEItracker.put(identity, created);
174 Map<QName, ServiceInterfaceEntry> resultMap = new HashMap<>();
175 for (ServiceInterfaceEntry sie : identitiesToSIs.values()) {
176 resultMap.put(sie.getQName(), sie);
178 logger.debug("Number of ServiceInterfaces to be generated: {}",
183 public String getFullyQualifiedName() {
184 return packageName + "." + typeName;
187 public String getPackageName() {
191 public String getTypeName() {
195 public QName getYangModuleQName() {
196 return yangModuleQName;
200 public boolean equals(Object o) {
203 if (o == null || getClass() != o.getClass())
206 ServiceInterfaceEntry that = (ServiceInterfaceEntry) o;
208 if (!maybeBaseCache.equals(that.maybeBaseCache))
210 if (!nullableDescription.equals(that.nullableDescription))
212 if (!exportedOsgiClassName.equals(that.exportedOsgiClassName))
214 if (!qName.equals(that.qName))
216 if (!packageName.equals(that.packageName))
218 if (!typeName.equals(that.typeName))
225 public int hashCode() {
226 int result = maybeBaseCache.hashCode();
227 result = 31 * result + exportedOsgiClassName.hashCode();
228 result = 31 * result + nullableDescription.hashCode();
229 result = 31 * result + typeName.hashCode();
230 result = 31 * result + packageName.hashCode();
231 result = 31 * result + qName.hashCode();
236 public String toString() {
237 return "ServiceInterfaceEntry{" + "maybeBaseCache=" + maybeBaseCache
238 + ", qName='" + qName + '\'' + ", fullyQualifiedName='"
239 + getFullyQualifiedName() + '\'' + ", exportedOsgiClassName="
240 + exportedOsgiClassName + ", nullableDescription='"
241 + nullableDescription + '\'' + '}';