*/
package org.opendaylight.controller.netconf.confignetconfconnector.mapping.config;
-import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
-import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
-
+import java.util.Map;
import javax.management.InstanceNotFoundException;
import javax.management.ObjectName;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Map;
-import java.util.Set;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
+import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
+import org.opendaylight.yangtools.yang.common.QName;
public class ServiceRegistryWrapper {
- private ServiceReferenceReadableRegistry configServiceRefRegistry;
-
- private long suffix = 1;
+ private final ServiceReferenceReadableRegistry configServiceRefRegistry;
public ServiceRegistryWrapper(ServiceReferenceReadableRegistry configServiceRefRegistry) {
this.configServiceRefRegistry = configServiceRefRegistry;
}
-
- public boolean hasRefName(String namespace, String serviceName, ObjectName on) {
- String qname = configServiceRefRegistry.getServiceInterfaceName(namespace, serviceName);
- Map<String, ObjectName> forQName = configServiceRefRegistry.getServiceMapping().get(qname);
- if(forQName==null) return false;
- return forQName.values().contains(on);
- }
-
public ObjectName getByServiceAndRefName(String namespace, String serviceName, String refName) {
Map<String, Map<String, String>> serviceNameToRefNameToInstance = getMappedServices().get(namespace);
- Preconditions.checkArgument(serviceNameToRefNameToInstance != null, "No serviceInstances mapped to " + namespace);
+ Preconditions.checkNotNull(serviceNameToRefNameToInstance, "No serviceInstances mapped to " + namespace);
Map<String, String> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
- Preconditions.checkArgument(refNameToInstance != null, "No serviceInstances mapped to " + serviceName + " , "
+ Preconditions.checkNotNull(refNameToInstance, "No serviceInstances mapped to " + serviceName + " , "
+ serviceNameToRefNameToInstance.keySet());
String instanceId = refNameToInstance.get(refName);
String qNameOfService = configServiceRefRegistry.getServiceInterfaceName(namespace, serviceName);
try {
- return configServiceRefRegistry.getServiceReference(qNameOfService, refName);
+ /*
+ Remove transaction name as this is redundant - will be stripped in DynamicWritableWrapper,
+ and makes it hard to compare with service references got from MXBean attributes
+ */
+ return ObjectNameUtil.withoutTransactionName(
+ configServiceRefRegistry.getServiceReference(qNameOfService, refName));
} catch (InstanceNotFoundException e) {
throw new IllegalArgumentException("No serviceInstance mapped to " + refName
+ " under service name " + serviceName + " , " + refNameToInstance.keySet(), e);
Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
- for (String serviceQName : serviceMapping.keySet())
- for (String refName : serviceMapping.get(serviceQName).keySet()) {
+ for (Map.Entry<String, Map<String, ObjectName>> qNameToRefNameEntry : serviceMapping.entrySet()){
+ for (String refName : qNameToRefNameEntry.getValue().keySet()) {
- ObjectName on = serviceMapping.get(serviceQName).get(refName);
+ ObjectName on = qNameToRefNameEntry.getValue().get(refName);
Services.ServiceInstance si = Services.ServiceInstance.fromObjectName(on);
- // FIXME use QName's new String constructor, after it is fixed
-// QName qname;
-// try {
-// qname = new QName(serviceQName);
-// } catch (ParseException e) {
-// throw new IllegalStateException("Unable to parse qname of a service " + serviceQName, e);
-// }
- Pattern p = Pattern.compile("\\(([^\\(\\?]+)\\?[^\\?\\)]*\\)([^\\)]+)");
- Matcher matcher = p.matcher(serviceQName);
- Preconditions.checkArgument(matcher.matches());
- String namespace = matcher.group(1);
- String localName = matcher.group(2);
-
-// String namespace = qname.getNamespace().toString();
+ QName qname = QName.create(qNameToRefNameEntry.getKey());
+ String namespace = qname.getNamespace().toString();
Map<String, Map<String, String>> serviceToRefs = retVal.get(namespace);
if(serviceToRefs==null) {
serviceToRefs = Maps.newHashMap();
retVal.put(namespace, serviceToRefs);
}
-// String localName = qname.getLocalName();
+ String localName = qname.getLocalName();
Map<String, String> refsToSis = serviceToRefs.get(localName);
if(refsToSis==null) {
refsToSis = Maps.newHashMap();
serviceToRefs.put(localName, refsToSis);
}
- Preconditions.checkState(refsToSis.containsKey(refName) == false,
+ Preconditions.checkState(!refsToSis.containsKey(refName),
"Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
localName, on);
refsToSis.put(refName, si.toString());
}
-
- return retVal;
- }
-
- @VisibleForTesting
- public String getNewDefaultRefName(String namespace, String serviceName, String moduleName, String instanceName) {
- String refName;
- refName = "ref_" + instanceName;
-
- Map<String, Map<String, String>> serviceNameToRefNameToInstance = getMappedServices().get(namespace);
-
- Map<String, String> refNameToInstance;
- if(serviceNameToRefNameToInstance == null || serviceNameToRefNameToInstance.containsKey(serviceName) == false) {
- refNameToInstance = Collections.emptyMap();
- } else
- refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
-
- final Set<String> refNamesAsSet = toSet(refNameToInstance.keySet());
- if (refNamesAsSet.contains(refName)) {
- refName = findAvailableRefName(refName, refNamesAsSet);
- }
-
- return refName;
- }
-
-
- private Set<String> toSet(Collection<String> values) {
- Set<String> refNamesAsSet = Sets.newHashSet();
-
- for (String refName : values) {
- boolean resultAdd = refNamesAsSet.add(refName);
- Preconditions.checkState(resultAdd,
- "Error occurred building services element, reference name {} was present twice", refName);
}
- return refNamesAsSet;
- }
-
- private String findAvailableRefName(String refName, Set<String> refNamesAsSet) {
- String intitialRefName = refName;
-
- while (true) {
- refName = intitialRefName + "_" + suffix++;
- if (refNamesAsSet.contains(refName) == false)
- return refName;
- }
+ return retVal;
}
}