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 static com.google.common.base.Preconditions.checkNotNull;
11 import static java.lang.String.format;
12 import static org.opendaylight.controller.config.yangjmxgenerator.ConfigConstants.SERVICE_TYPE_Q_NAME;
13 import com.google.common.base.Optional;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Iterator;
18 import java.util.List;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
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";
47 public class ServiceInterfaceEntry extends AbstractEntry {
48 private static final Logger LOG = LoggerFactory
49 .getLogger(ServiceInterfaceEntry.class);
51 private static final String CLASS_NAME_SUFFIX = "ServiceInterface";
52 private final Optional<ServiceInterfaceEntry> maybeBaseCache;
53 private final String exportedOsgiClassName;
54 private final QName qName;
55 private final String nullableDescription, packageName, typeName;
56 private final QName yangModuleQName;
57 private final boolean registerToOsgi;
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());
71 boolean disableOsgiServiceRegistration = false;
72 for (UnknownSchemaNode usn : unknownSchemaNodes) {
73 if (ConfigConstants.JAVA_CLASS_EXTENSION_QNAME.equals(usn.getNodeType())) {
74 String localName = usn.getNodeParameter();
75 exportedOsgiClassNames.add(localName);
76 } else if (ConfigConstants.DISABLE_OSGI_SERVICE_REG_QNAME.equals(usn.getNodeType())) {
77 disableOsgiServiceRegistration = true;
79 throw new IllegalStateException(format(
80 "Unexpected unknown schema node. Expected %s, got %s",
81 ConfigConstants.JAVA_CLASS_EXTENSION_QNAME,
85 if (exportedOsgiClassNames.size() != 1) {
86 throw new IllegalArgumentException(
87 format("Cannot find one to one mapping from %s to "
88 + "java class defined by %s language extension in %s",
90 ConfigConstants.JAVA_CLASS_EXTENSION_QNAME, id));
93 this.registerToOsgi = !disableOsgiServiceRegistration;
94 this.exportedOsgiClassName = exportedOsgiClassNames.get(0);
95 qName = id.getQName();
96 nullableDescription = id.getDescription();
97 typeName = getSimpleName(exportedOsgiClassName) + CLASS_NAME_SUFFIX;
98 this.packageName = packageName;
99 this.yangModuleQName = yangModuleQName;
102 private static final String getSimpleName(String fullyQualifiedName) {
103 int lastDotPosition = fullyQualifiedName.lastIndexOf(".");
104 return fullyQualifiedName.substring(lastDotPosition + 1);
107 public String getNullableDescription() {
108 return nullableDescription;
111 public Optional<ServiceInterfaceEntry> getBase() {
112 return maybeBaseCache;
115 public String getExportedOsgiClassName() {
116 return exportedOsgiClassName;
119 public QName getQName() {
123 public boolean isRegisterToOsgi() {
124 return registerToOsgi;
128 * @return Map of QNames as keys and ServiceInterfaceEntry instances as
131 public static Map<QName, ServiceInterfaceEntry> create(Module currentModule,
132 String packageName,Map<IdentitySchemaNode, ServiceInterfaceEntry> definedSEItracker) {
133 LOG.debug("Generating ServiceInterfaces from {} to package {}",
134 currentModule.getNamespace(), packageName);
136 Map<IdentitySchemaNode, ServiceInterfaceEntry> identitiesToSIs = new HashMap<>();
137 Set<IdentitySchemaNode> notVisited = new HashSet<>(
138 currentModule.getIdentities());
139 int lastSize = notVisited.size() + 1;
140 while (!notVisited.isEmpty()) {
141 if (notVisited.size() == lastSize) {
143 "Following identities will be ignored while generating ServiceInterfaces, as they are not derived from {} : {}",
144 SERVICE_TYPE_Q_NAME, notVisited);
147 lastSize = notVisited.size();
148 for (Iterator<IdentitySchemaNode> iterator = notVisited.iterator(); iterator
150 IdentitySchemaNode identity = iterator.next();
151 ServiceInterfaceEntry created = null;
152 if (identity.getBaseIdentity() == null) {
153 // this can happen while loading config module, just skip
156 } else if (identity.getBaseIdentity().getQName()
157 .equals(SERVICE_TYPE_Q_NAME)) {
158 // this is a base type
159 created = new ServiceInterfaceEntry(identity, packageName, ModuleUtil.getQName(currentModule));
161 ServiceInterfaceEntry foundBase = definedSEItracker
162 .get(identity.getBaseIdentity());
163 // derived type, did we convert the parent?
164 if (foundBase != null) {
165 created = new ServiceInterfaceEntry(
166 Optional.of(foundBase), identity, packageName, ModuleUtil.getQName(currentModule));
171 if (created != null) {
172 created.setYangModuleName(currentModule.getName());
173 // TODO how to get local name
174 created.setYangModuleLocalname(identity.getQName()
176 identitiesToSIs.put(identity, created);
177 definedSEItracker.put(identity, created);
183 Map<QName, ServiceInterfaceEntry> resultMap = new HashMap<>();
184 for (ServiceInterfaceEntry sie : identitiesToSIs.values()) {
185 resultMap.put(sie.getQName(), sie);
187 LOG.debug("Number of ServiceInterfaces to be generated: {}",
192 public String getFullyQualifiedName() {
193 return packageName + "." + typeName;
196 public String getPackageName() {
200 public String getTypeName() {
204 public QName getYangModuleQName() {
205 return yangModuleQName;
209 public boolean equals(Object o) {
213 if (o == null || getClass() != o.getClass()) {
217 ServiceInterfaceEntry that = (ServiceInterfaceEntry) o;
219 if (!maybeBaseCache.equals(that.maybeBaseCache)) {
222 if (!nullableDescription.equals(that.nullableDescription)) {
225 if (!exportedOsgiClassName.equals(that.exportedOsgiClassName)) {
228 if (!qName.equals(that.qName)) {
231 if (!packageName.equals(that.packageName)) {
234 if (!typeName.equals(that.typeName)) {
242 public int hashCode() {
243 int result = maybeBaseCache.hashCode();
244 result = 31 * result + exportedOsgiClassName.hashCode();
245 result = 31 * result + nullableDescription.hashCode();
246 result = 31 * result + typeName.hashCode();
247 result = 31 * result + packageName.hashCode();
248 result = 31 * result + qName.hashCode();
253 public String toString() {
254 return "ServiceInterfaceEntry{" + "maybeBaseCache=" + maybeBaseCache
255 + ", qName='" + qName + '\'' + ", fullyQualifiedName='"
256 + getFullyQualifiedName() + '\'' + ", exportedOsgiClassName="
257 + exportedOsgiClassName + ", nullableDescription='"
258 + nullableDescription + '\'' + '}';