2 * Copyright (c) 2013 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.netconf.confignetconfconnector.mapping.config;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.HashMultimap;
15 import com.google.common.collect.Maps;
16 import com.google.common.collect.Multimap;
17 import com.google.common.collect.Sets;
18 import org.opendaylight.controller.config.api.ServiceReferenceReadableRegistry;
19 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
20 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectNameAttributeReadingStrategy;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
23 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
29 import javax.management.ObjectName;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
34 import java.util.Map.Entry;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
39 public final class Services {
41 private static final Logger logger = LoggerFactory.getLogger(Services.class);
43 private static final String PROVIDER_KEY = "provider";
44 private static final String NAME_KEY = "name";
45 public static final String TYPE_KEY = "type";
46 public static final String SERVICE_KEY = "service";
48 private long suffix = 1;
50 private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
52 private ServiceReferenceReadableRegistry configServiceRefRegistry;
54 public Services(ServiceReferenceReadableRegistry configServiceRefRegistry) {
55 this.configServiceRefRegistry = configServiceRefRegistry;
59 public String getNewDefaultRefName(String namespace, String serviceName, String moduleName, String instanceName) {
61 refName = "ref_" + instanceName;
63 Map<String, Map<String, String>> serviceNameToRefNameToInstance = getMappedServices().get(namespace);
65 Map<String, String> refNameToInstance;
66 if(serviceNameToRefNameToInstance == null || serviceNameToRefNameToInstance.containsKey(serviceName) == false) {
67 refNameToInstance = Collections.emptyMap();
69 refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
71 final Set<String> refNamesAsSet = toSet(refNameToInstance.keySet());
72 if (refNamesAsSet.contains(refName)) {
73 refName = findAvailableRefName(refName, refNamesAsSet);
79 private Set<String> toSet(Collection<String> values) {
80 Set<String> refNamesAsSet = Sets.newHashSet();
82 for (String refName : values) {
83 boolean resultAdd = refNamesAsSet.add(refName);
84 Preconditions.checkState(resultAdd,
85 "Error occurred building services element, reference name {} was present twice", refName);
91 public ServiceInstance getByServiceAndRefName(String namespace, String serviceName, String refName) {
92 Map<String, Map<String, String>> serviceNameToRefNameToInstance = getMappedServices().get(namespace);
94 Preconditions.checkArgument(serviceNameToRefNameToInstance != null, "No serviceInstances mapped to " + namespace);
96 Map<String, String> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
97 Preconditions.checkArgument(refNameToInstance != null, "No serviceInstances mapped to " + serviceName + " , "
98 + serviceNameToRefNameToInstance.keySet());
100 String instanceId = refNameToInstance.get(refName);
101 Preconditions.checkArgument(instanceId != null, "No serviceInstances mapped to " + serviceName + ":"
102 + refName + ", " + serviceNameToRefNameToInstance.keySet());
104 ServiceInstance serviceInstance = ServiceInstance.fromString(instanceId);
105 Preconditions.checkArgument(serviceInstance != null, "No serviceInstance mapped to " + refName
106 + " under service name " + serviceName + " , " + refNameToInstance.keySet());
107 return serviceInstance;
110 // TODO hide getMappedServices, call it explicitly in toXml
112 public Map<String, Map<String, Map<String, String>>> getMappedServices() {
113 Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
115 for (String namespace : namespaceToServiceNameToRefNameToInstance.keySet()) {
117 Map<String, Map<String, ServiceInstance>> serviceNameToRefNameToInstance = namespaceToServiceNameToRefNameToInstance
119 Map<String, Map<String, String>> innerRetVal = Maps.newHashMap();
121 for (String serviceName : serviceNameToRefNameToInstance.keySet()) {
123 Map<String, String> innerInnerRetVal = Maps.newHashMap();
124 for (Entry<String, ServiceInstance> refNameToSi : serviceNameToRefNameToInstance.get(serviceName).entrySet()) {
125 innerInnerRetVal.put(refNameToSi.getKey(), refNameToSi.getValue().toString());
127 innerRetVal.put(serviceName, innerInnerRetVal);
129 retVal.put(namespace, innerRetVal);
132 Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
133 for (String serviceQName : serviceMapping.keySet())
134 for (String refName : serviceMapping.get(serviceQName).keySet()) {
136 ObjectName on = serviceMapping.get(serviceQName).get(refName);
137 ServiceInstance si = ServiceInstance.fromObjectName(on);
139 // FIXME use QName's new String constructor, after its implemented
140 Pattern p = Pattern.compile("\\(([^\\(\\?]+)\\?[^\\?\\)]*\\)([^\\)]+)");
141 Matcher matcher = p.matcher(serviceQName);
142 Preconditions.checkArgument(matcher.matches());
143 String namespace = matcher.group(1);
144 String localName = matcher.group(2);
146 Map<String, Map<String, String>> serviceToRefs = retVal.get(namespace);
147 if(serviceToRefs==null) {
148 serviceToRefs = Maps.newHashMap();
149 retVal.put(namespace, serviceToRefs);
152 Map<String, String> refsToSis = serviceToRefs.get(localName);
153 if(refsToSis==null) {
154 refsToSis = Maps.newHashMap();
155 serviceToRefs.put(localName, refsToSis);
158 Preconditions.checkState(refsToSis.containsKey(refName) == false,
159 "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
161 refsToSis.put(refName, si.toString());
170 public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
171 return namespaceToServiceNameToRefNameToInstance;
174 // TODO hide resolveServices, call it explicitly in fromXml
176 public static Services resolveServices(Map<String, Map<String, Map<String, String>>> mappedServices, ServiceReferenceReadableRegistry taClient) {
177 Services tracker = new Services(taClient);
179 for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
180 String namespace = namespaceEntry.getKey();
182 for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
184 String serviceName = serviceEntry.getKey();
185 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
187 Map<String, Map<String, ServiceInstance>> namespaceToServices = tracker.namespaceToServiceNameToRefNameToInstance.get(namespace);
188 if (namespaceToServices == null) {
189 namespaceToServices = Maps.newHashMap();
190 tracker.namespaceToServiceNameToRefNameToInstance.put(namespace, namespaceToServices);
193 Map<String, ServiceInstance> refNameToInstance = namespaceToServices
195 if (refNameToInstance == null) {
196 refNameToInstance = Maps.newHashMap();
197 namespaceToServices.put(serviceName, refNameToInstance);
200 String refName = refEntry.getKey();
202 ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
203 refNameToInstance.put(refName, serviceInstance);
211 // TODO support edit strategies on services
213 public static Map<String, Map<String, Map<String, String>>> fromXml(XmlElement xml) {
214 Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
216 List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
217 xml.checkUnrecognisedElements(services);
219 for (XmlElement service : services) {
221 XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
222 Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
224 Preconditions.checkState(prefixNamespace.getKey()!=null && prefixNamespace.getKey().equals("") == false, "Type attribute was not prefixed");
226 Map<String, Map<String, String>> namespaceToServices = retVal.get(prefixNamespace.getValue());
227 if(namespaceToServices == null) {
228 namespaceToServices = Maps.newHashMap();
229 retVal.put(prefixNamespace.getValue(), namespaceToServices);
232 String serviceName = ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
234 Map<String, String> innerMap = Maps.newHashMap();
235 namespaceToServices.put(serviceName, innerMap);
237 List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
238 service.checkUnrecognisedElements(instances, typeElement);
240 for (XmlElement instance : instances) {
241 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
242 String refName = nameElement.getTextContent();
244 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
245 String providerName = providerElement.getTextContent();
247 instance.checkUnrecognisedElements(nameElement, providerElement);
249 innerMap.put(refName, providerName);
256 private String findAvailableRefName(String refName, Set<String> refNamesAsSet) {
257 String intitialRefName = refName;
260 refName = intitialRefName + "_" + suffix++;
261 if (refNamesAsSet.contains(refName) == false)
266 public Element toXml(Map<String, Map<String, Map<String, String>>> mappedServices, Document document) {
267 Element root = document.createElement(XmlNetconfConstants.SERVICES_KEY);
268 XmlUtil.addNamespaceAttr(root, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
270 for (String namespace : mappedServices.keySet()) {
272 for (Entry<String, Map<String, String>> serviceEntry : mappedServices.get(namespace).entrySet()) {
273 Element serviceElement = document.createElement(SERVICE_KEY);
274 root.appendChild(serviceElement);
276 Element typeElement = XmlUtil.createPrefixedTextElement(document, TYPE_KEY, XmlNetconfConstants.PREFIX,
277 serviceEntry.getKey());
278 XmlUtil.addPrefixedNamespaceAttr(typeElement, XmlNetconfConstants.PREFIX, namespace);
279 serviceElement.appendChild(typeElement);
281 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
282 Element instanceElement = document.createElement(XmlNetconfConstants.INSTANCE_KEY);
283 serviceElement.appendChild(instanceElement);
285 Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey());
286 instanceElement.appendChild(nameElement);
288 Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue());
289 instanceElement.appendChild(providerElement);
297 public String getRefName(String namespace, String serviceName, ObjectName on, Optional<String> expectedRefName) {
298 Optional<String> refNameOptional = getRefNameOptional(namespace, serviceName, on, expectedRefName);
299 Preconditions.checkState(refNameOptional.isPresent(), "No reference names mapped to %s, %s, %s", namespace,
301 return refNameOptional.get();
304 public Optional<String> getRefNameOptional(String namespace, String serviceName, ObjectName on,
305 Optional<String> expectedRefName) {
306 Map<String, Map<String, String>> services = getMappedServices().get(namespace);
308 if(services == null) return Optional.absent();
309 Map<String, String> refs = services.get(serviceName);
311 if(refs == null) return Optional.absent();
312 Multimap<ServiceInstance, String> reverted = revertMap(refs);
314 ServiceInstance serviceInstance = ServiceInstance.fromObjectName(on);
315 Collection<String> references = reverted.get(serviceInstance);
317 if (expectedRefName.isPresent() && references.contains(expectedRefName.get())) {
318 logger.debug("Returning expected ref name {} for {}", expectedRefName.get(), on);
319 return expectedRefName;
320 } else if (references.size() > 0) {
321 String next = references.iterator().next();
322 logger.debug("Returning random ref name {} for {}", next, on);
323 return Optional.of(next);
325 return Optional.absent();
328 private Multimap<ServiceInstance, String> revertMap(Map<String, String> refs) {
329 Multimap<ServiceInstance, String> multimap = HashMultimap.create();
331 for (Entry<String, String> e : refs.entrySet()) {
332 multimap.put(ServiceInstance.fromString(e.getValue()), e.getKey());
338 public boolean hasRefName(String key, String value, ObjectName on) {
339 return getRefNameOptional(key, value, on, Optional.<String>absent()).isPresent();
342 public static final class ServiceInstance {
343 public ServiceInstance(String moduleName, String instanceName) {
344 this.moduleName = moduleName;
345 this.instanceName = instanceName;
348 public static ServiceInstance fromString(String instanceId) {
349 instanceId = instanceId.trim();
350 Matcher matcher = p.matcher(instanceId);
351 if(matcher.matches() == false) {
352 matcher = pDeprecated.matcher(instanceId);
355 Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
356 + " or " + pDeprecated.toString() + " but was " + instanceId);
358 String factoryName = matcher.group(1);
359 String instanceName = matcher.group(2);
360 return new ServiceInstance(factoryName, instanceName);
363 private final String moduleName, instanceName;
364 private String serviceName;
366 public String getServiceName() {
370 public void setServiceName(String serviceName) {
371 this.serviceName = serviceName;
374 public String getModuleName() {
378 public String getInstanceName() {
382 private static final String blueprint = "/"
383 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
384 + XmlNetconfConstants.TYPE_KEY + "='%s']["
385 + XmlNetconfConstants.NAME_KEY + "='%s']";
387 // TODO unify with xpath in RuntimeRpc
389 // Previous version of xpath, needs to be supported for backwards compatibility (persisted configs by config-persister)
390 private static final String blueprintRDeprecated = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
391 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
392 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
393 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
395 private static final String blueprintR = "/"
396 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
397 + XmlNetconfConstants.TYPE_KEY + "='%s'\\]\\["
398 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
400 private static final Pattern pDeprecated = Pattern.compile(String.format(blueprintRDeprecated, "(.+)", "(.+)"));
401 private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
404 public String toString() {
405 return String.format(blueprint, moduleName, instanceName);
409 public int hashCode() {
410 final int prime = 31;
412 result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
413 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
418 public boolean equals(Object obj) {
423 if (getClass() != obj.getClass())
425 ServiceInstance other = (ServiceInstance) obj;
426 if (instanceName == null) {
427 if (other.instanceName != null)
429 } else if (!instanceName.equals(other.instanceName))
431 if (moduleName == null) {
432 if (other.moduleName != null)
434 } else if (!moduleName.equals(other.moduleName))
439 public ObjectName getObjectName(String transactionName) {
440 return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
443 public static ServiceInstance fromObjectName(ObjectName on) {
444 return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));