Introducing simple merge strategy for config subsystem
[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
28     public ObjectName getByServiceAndRefName(String namespace, String serviceName, String refName) {
29         Map<String, Map<String, String>> serviceNameToRefNameToInstance = getMappedServices().get(namespace);
30
31         Preconditions.checkNotNull(serviceNameToRefNameToInstance, "No serviceInstances mapped to " + namespace);
32
33         Map<String, String> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
34         Preconditions.checkNotNull(refNameToInstance, "No serviceInstances mapped to " + serviceName + " , "
35                 + serviceNameToRefNameToInstance.keySet());
36
37         String instanceId = refNameToInstance.get(refName);
38         Preconditions.checkArgument(instanceId != null, "No serviceInstances mapped to " + serviceName + ":"
39                 + refName + ", " + serviceNameToRefNameToInstance.keySet());
40
41         Services.ServiceInstance serviceInstance = Services.ServiceInstance.fromString(instanceId);
42         Preconditions.checkArgument(serviceInstance != null, "No serviceInstance mapped to " + refName
43                 + " under service name " + serviceName + " , " + refNameToInstance.keySet());
44
45         String qNameOfService = configServiceRefRegistry.getServiceInterfaceName(namespace, serviceName);
46         try {
47             /*
48              Remove transaction name as this is redundant - will be stripped in DynamicWritableWrapper,
49              and makes it hard to compare with service references got from MXBean attributes
50             */
51             return ObjectNameUtil.withoutTransactionName(
52                     configServiceRefRegistry.getServiceReference(qNameOfService, refName));
53         } catch (InstanceNotFoundException e) {
54             throw new IllegalArgumentException("No serviceInstance mapped to " + refName
55                     + " under service name " + serviceName + " , " + refNameToInstance.keySet(), e);
56
57         }
58     }
59
60     public Map<String, Map<String, Map<String, String>>> getMappedServices() {
61         Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
62
63         Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
64         for (String serviceQName : serviceMapping.keySet()){
65             for (String refName : serviceMapping.get(serviceQName).keySet()) {
66
67                 ObjectName on = serviceMapping.get(serviceQName).get(refName);
68                 Services.ServiceInstance si = Services.ServiceInstance.fromObjectName(on);
69
70                 QName qname = QName.create(serviceQName);
71                 String namespace = qname.getNamespace().toString();
72                 Map<String, Map<String, String>> serviceToRefs = retVal.get(namespace);
73                 if(serviceToRefs==null) {
74                     serviceToRefs = Maps.newHashMap();
75                     retVal.put(namespace, serviceToRefs);
76                 }
77
78                 String localName = qname.getLocalName();
79                 Map<String, String> refsToSis = serviceToRefs.get(localName);
80                 if(refsToSis==null) {
81                     refsToSis = Maps.newHashMap();
82                     serviceToRefs.put(localName, refsToSis);
83                 }
84
85                 Preconditions.checkState(!refsToSis.containsKey(refName),
86                         "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
87                         localName, on);
88                 refsToSis.put(refName, si.toString());
89             }
90         }
91
92         return retVal;
93     }
94 }