Merge "Model dom-broker statistics"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / config / ServiceRegistryWrapper.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.netconf.confignetconfconnector.mapping.config;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Maps;
12 import java.util.Map;
13 import javax.management.InstanceNotFoundException;
14 import javax.management.ObjectName;
15 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
16 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
17 import org.opendaylight.yangtools.yang.common.QName;
18
19 public class ServiceRegistryWrapper {
20
21     private final ServiceReferenceReadableRegistry configServiceRefRegistry;
22
23     public ServiceRegistryWrapper(ServiceReferenceReadableRegistry configServiceRefRegistry) {
24         this.configServiceRefRegistry = configServiceRefRegistry;
25     }
26
27     public ObjectName getByServiceAndRefName(String namespace, String serviceType, String refName) {
28         Map<String, Map<String, Map<String, String>>> mappedServices = getMappedServices();
29         Map<String, Map<String, String>> serviceNameToRefNameToInstance = mappedServices.get(namespace);
30
31         Preconditions.checkArgument(serviceNameToRefNameToInstance != null,
32                 "No service mapped to %s:%s:%s. Wrong namespace, available namespaces: %s",
33                 namespace, serviceType, refName, mappedServices.keySet());
34
35         Map<String, String> refNameToInstance = serviceNameToRefNameToInstance.get(serviceType);
36         Preconditions.checkArgument(refNameToInstance != null,
37                 "No service mapped to %s:%s:%s. Wrong service type, available service types: %s"
38                 , namespace, serviceType, refName, serviceNameToRefNameToInstance.keySet());
39
40         String instanceId = refNameToInstance.get(refName);
41         Preconditions.checkArgument(instanceId != null,
42                 "No service mapped to %s:%s:%s. Wrong ref name, available ref names: %s"
43                 ,namespace, serviceType, refName, refNameToInstance.keySet());
44
45         Services.ServiceInstance serviceInstance = Services.ServiceInstance.fromString(instanceId);
46         Preconditions.checkArgument(serviceInstance != null,
47                 "No service mapped to %s:%s:%s. Wrong ref name, available ref names: %s"
48                 ,namespace, serviceType, refName, refNameToInstance.keySet());
49
50         String qNameOfService = configServiceRefRegistry.getServiceInterfaceName(namespace, serviceType);
51         try {
52             /*
53              Remove transaction name as this is redundant - will be stripped in DynamicWritableWrapper,
54              and makes it hard to compare with service references got from MXBean attributes
55             */
56             return ObjectNameUtil.withoutTransactionName(
57                     configServiceRefRegistry.getServiceReference(qNameOfService, refName));
58         } catch (InstanceNotFoundException e) {
59             throw new IllegalArgumentException("No serviceInstance mapped to " + refName
60                     + " under service name " + serviceType + " , " + refNameToInstance.keySet(), e);
61
62         }
63     }
64
65     public Map<String, Map<String, Map<String, String>>> getMappedServices() {
66         Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
67
68         Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
69         for (Map.Entry<String, Map<String, ObjectName>> qNameToRefNameEntry : serviceMapping.entrySet()){
70             for (String refName : qNameToRefNameEntry.getValue().keySet()) {
71
72                 ObjectName on = qNameToRefNameEntry.getValue().get(refName);
73                 Services.ServiceInstance si = Services.ServiceInstance.fromObjectName(on);
74
75                 QName qname = QName.create(qNameToRefNameEntry.getKey());
76                 String namespace = qname.getNamespace().toString();
77                 Map<String, Map<String, String>> serviceToRefs = retVal.get(namespace);
78                 if(serviceToRefs==null) {
79                     serviceToRefs = Maps.newHashMap();
80                     retVal.put(namespace, serviceToRefs);
81                 }
82
83                 String localName = qname.getLocalName();
84                 Map<String, String> refsToSis = serviceToRefs.get(localName);
85                 if(refsToSis==null) {
86                     refsToSis = Maps.newHashMap();
87                     serviceToRefs.put(localName, refsToSis);
88                 }
89
90                 Preconditions.checkState(!refsToSis.containsKey(refName),
91                         "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
92                         localName, on);
93                 refsToSis.put(refName, si.toString());
94             }
95         }
96
97         return retVal;
98     }
99 }