Bug 1062 - Disallow implicit serviceref creation.
[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.yangtools.yang.common.QName;
17
18 public class ServiceRegistryWrapper {
19
20     private ServiceReferenceReadableRegistry configServiceRefRegistry;
21
22     public ServiceRegistryWrapper(ServiceReferenceReadableRegistry configServiceRefRegistry) {
23         this.configServiceRefRegistry = configServiceRefRegistry;
24     }
25
26
27     public ObjectName getByServiceAndRefName(String namespace, String serviceName, String refName) {
28         Map<String, Map<String, String>> serviceNameToRefNameToInstance = getMappedServices().get(namespace);
29
30         Preconditions.checkNotNull(serviceNameToRefNameToInstance, "No serviceInstances mapped to " + namespace);
31
32         Map<String, String> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
33         Preconditions.checkNotNull(refNameToInstance, "No serviceInstances mapped to " + serviceName + " , "
34                 + serviceNameToRefNameToInstance.keySet());
35
36         String instanceId = refNameToInstance.get(refName);
37         Preconditions.checkArgument(instanceId != null, "No serviceInstances mapped to " + serviceName + ":"
38                 + refName + ", " + serviceNameToRefNameToInstance.keySet());
39
40         Services.ServiceInstance serviceInstance = Services.ServiceInstance.fromString(instanceId);
41         Preconditions.checkArgument(serviceInstance != null, "No serviceInstance mapped to " + refName
42                 + " under service name " + serviceName + " , " + refNameToInstance.keySet());
43
44         String qNameOfService = configServiceRefRegistry.getServiceInterfaceName(namespace, serviceName);
45         try {
46             return configServiceRefRegistry.getServiceReference(qNameOfService, refName);
47         } catch (InstanceNotFoundException e) {
48             throw new IllegalArgumentException("No serviceInstance mapped to " + refName
49                     + " under service name " + serviceName + " , " + refNameToInstance.keySet(), e);
50
51         }
52     }
53
54     public Map<String, Map<String, Map<String, String>>> getMappedServices() {
55         Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
56
57         Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
58         for (String serviceQName : serviceMapping.keySet()){
59             for (String refName : serviceMapping.get(serviceQName).keySet()) {
60
61                 ObjectName on = serviceMapping.get(serviceQName).get(refName);
62                 Services.ServiceInstance si = Services.ServiceInstance.fromObjectName(on);
63
64                 QName qname = QName.create(serviceQName);
65                 String namespace = qname.getNamespace().toString();
66                 Map<String, Map<String, String>> serviceToRefs = retVal.get(namespace);
67                 if(serviceToRefs==null) {
68                     serviceToRefs = Maps.newHashMap();
69                     retVal.put(namespace, serviceToRefs);
70                 }
71
72                 String localName = qname.getLocalName();
73                 Map<String, String> refsToSis = serviceToRefs.get(localName);
74                 if(refsToSis==null) {
75                     refsToSis = Maps.newHashMap();
76                     serviceToRefs.put(localName, refsToSis);
77                 }
78
79                 Preconditions.checkState(!refsToSis.containsKey(refName),
80                         "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
81                         localName, on);
82                 refsToSis.put(refName, si.toString());
83             }
84         }
85
86         return retVal;
87     }
88 }