2 * Copyright (c) 2015 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
9 package org.opendaylight.controller.config.facade.xml.mapping.config;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
14 import javax.management.InstanceNotFoundException;
15 import javax.management.ObjectName;
16 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
17 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
18 import org.opendaylight.yangtools.yang.common.QName;
20 public class ServiceRegistryWrapper {
22 private final ServiceReferenceReadableRegistry configServiceRefRegistry;
24 public ServiceRegistryWrapper(ServiceReferenceReadableRegistry configServiceRefRegistry) {
25 this.configServiceRefRegistry = configServiceRefRegistry;
28 public ObjectName getByServiceAndRefName(String namespace, String serviceType, String refName) {
29 Map<String, Map<String, Map<String, String>>> mappedServices = getMappedServices();
30 Map<String, Map<String, String>> serviceNameToRefNameToInstance = mappedServices.get(namespace);
32 Preconditions.checkArgument(serviceNameToRefNameToInstance != null,
33 "No service mapped to %s:%s:%s. Wrong namespace, available namespaces: %s",
34 namespace, serviceType, refName, mappedServices.keySet());
36 Map<String, String> refNameToInstance = serviceNameToRefNameToInstance.get(serviceType);
37 Preconditions.checkArgument(refNameToInstance != null,
38 "No service mapped to %s:%s:%s. Wrong service type, available service types: %s"
39 , namespace, serviceType, refName, serviceNameToRefNameToInstance.keySet());
41 String instanceId = refNameToInstance.get(refName);
42 Preconditions.checkArgument(instanceId != null,
43 "No service mapped to %s:%s:%s. Wrong ref name, available ref names: %s"
44 ,namespace, serviceType, refName, refNameToInstance.keySet());
46 Services.ServiceInstance serviceInstance = Services.ServiceInstance.fromString(instanceId);
47 Preconditions.checkArgument(serviceInstance != null,
48 "No service mapped to %s:%s:%s. Wrong ref name, available ref names: %s"
49 ,namespace, serviceType, refName, refNameToInstance.keySet());
51 String qNameOfService = configServiceRefRegistry.getServiceInterfaceName(namespace, serviceType);
54 Remove transaction name as this is redundant - will be stripped in DynamicWritableWrapper,
55 and makes it hard to compare with service references got from MXBean attributes
57 return ObjectNameUtil.withoutTransactionName(
58 configServiceRefRegistry.getServiceReference(qNameOfService, refName));
59 } catch (InstanceNotFoundException e) {
60 throw new IllegalArgumentException("No serviceInstance mapped to " + refName
61 + " under service name " + serviceType + " , " + refNameToInstance.keySet(), e);
66 public Map<String, Map<String, Map<String, String>>> getMappedServices() {
67 Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
69 Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
70 for (Map.Entry<String, Map<String, ObjectName>> qNameToRefNameEntry : serviceMapping.entrySet()){
71 for (String refName : qNameToRefNameEntry.getValue().keySet()) {
73 ObjectName on = qNameToRefNameEntry.getValue().get(refName);
74 Services.ServiceInstance si = Services.ServiceInstance.fromObjectName(on);
76 QName qname = QName.create(qNameToRefNameEntry.getKey());
77 String namespace = qname.getNamespace().toString();
78 Map<String, Map<String, String>> serviceToRefs = retVal.get(namespace);
79 if(serviceToRefs==null) {
80 serviceToRefs = Maps.newHashMap();
81 retVal.put(namespace, serviceToRefs);
84 String localName = qname.getLocalName();
85 Map<String, String> refsToSis = serviceToRefs.get(localName);
87 refsToSis = Maps.newHashMap();
88 serviceToRefs.put(localName, refsToSis);
91 Preconditions.checkState(!refsToSis.containsKey(refName),
92 "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
94 refsToSis.put(refName, si.toString());