Merge changes I1474351f,I2ddc5ffa
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / mapping / config / Services.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
9 package org.opendaylight.controller.netconf.confignetconfconnector.mapping.config;
10
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;
28
29 import javax.management.ObjectName;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Map.Entry;
35 import java.util.Set;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
38
39 public final class Services {
40
41     private static final Logger logger = LoggerFactory.getLogger(Services.class);
42
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";
47
48     private long suffix = 1;
49
50     private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
51             .newHashMap();
52     private ServiceReferenceReadableRegistry configServiceRefRegistry;
53
54     public Services(ServiceReferenceReadableRegistry configServiceRefRegistry) {
55         this.configServiceRefRegistry = configServiceRefRegistry;
56     }
57
58     @VisibleForTesting
59     public String getNewDefaultRefName(String namespace, String serviceName, String moduleName, String instanceName) {
60         String refName;
61         refName = "ref_" + instanceName;
62
63         Map<String, Map<String, String>> serviceNameToRefNameToInstance = getMappedServices().get(namespace);
64
65         Map<String, String> refNameToInstance;
66         if(serviceNameToRefNameToInstance == null || serviceNameToRefNameToInstance.containsKey(serviceName) == false) {
67             refNameToInstance = Collections.emptyMap();
68         } else
69             refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
70
71         final Set<String> refNamesAsSet = toSet(refNameToInstance.keySet());
72         if (refNamesAsSet.contains(refName)) {
73             refName = findAvailableRefName(refName, refNamesAsSet);
74         }
75
76         return refName;
77     }
78
79     private Set<String> toSet(Collection<String> values) {
80         Set<String> refNamesAsSet = Sets.newHashSet();
81
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);
86         }
87
88         return refNamesAsSet;
89     }
90
91     public ServiceInstance getByServiceAndRefName(String namespace, String serviceName, String refName) {
92         Map<String, Map<String, String>> serviceNameToRefNameToInstance = getMappedServices().get(namespace);
93
94         Preconditions.checkArgument(serviceNameToRefNameToInstance != null, "No serviceInstances mapped to " + namespace);
95
96         Map<String, String> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
97         Preconditions.checkArgument(refNameToInstance != null, "No serviceInstances mapped to " + serviceName + " , "
98                 + serviceNameToRefNameToInstance.keySet());
99
100         String instanceId = refNameToInstance.get(refName);
101         Preconditions.checkArgument(instanceId != null, "No serviceInstances mapped to " + serviceName + ":"
102                 + refName + ", " + serviceNameToRefNameToInstance.keySet());
103
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;
108     }
109
110     // TODO hide getMappedServices, call it explicitly in toXml
111
112     public Map<String, Map<String, Map<String, String>>> getMappedServices() {
113         Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
114
115         for (String namespace : namespaceToServiceNameToRefNameToInstance.keySet()) {
116
117             Map<String, Map<String, ServiceInstance>> serviceNameToRefNameToInstance = namespaceToServiceNameToRefNameToInstance
118                     .get(namespace);
119             Map<String, Map<String, String>> innerRetVal = Maps.newHashMap();
120
121             for (String serviceName : serviceNameToRefNameToInstance.keySet()) {
122
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());
126                 }
127                 innerRetVal.put(serviceName, innerInnerRetVal);
128             }
129             retVal.put(namespace, innerRetVal);
130         }
131
132         Map<String, Map<String, ObjectName>> serviceMapping = configServiceRefRegistry.getServiceMapping();
133         for (String serviceQName : serviceMapping.keySet())
134             for (String refName : serviceMapping.get(serviceQName).keySet()) {
135
136                 ObjectName on = serviceMapping.get(serviceQName).get(refName);
137                 ServiceInstance si = ServiceInstance.fromObjectName(on);
138
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);
145
146                 Map<String, Map<String, String>> serviceToRefs = retVal.get(namespace);
147                 if(serviceToRefs==null) {
148                     serviceToRefs = Maps.newHashMap();
149                     retVal.put(namespace, serviceToRefs);
150                 }
151
152                 Map<String, String> refsToSis = serviceToRefs.get(localName);
153                 if(refsToSis==null) {
154                     refsToSis = Maps.newHashMap();
155                     serviceToRefs.put(localName, refsToSis);
156                 }
157
158                 Preconditions.checkState(refsToSis.containsKey(refName) == false,
159                         "Duplicate reference name %s for service %s:%s, now for instance %s", refName, namespace,
160                         localName, on);
161                 refsToSis.put(refName, si.toString());
162             }
163
164         return retVal;
165     }
166
167     /**
168      *
169      */
170     public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
171         return namespaceToServiceNameToRefNameToInstance;
172     }
173
174     // TODO hide resolveServices, call it explicitly in fromXml
175
176     public static Services resolveServices(Map<String, Map<String, Map<String, String>>> mappedServices, ServiceReferenceReadableRegistry taClient) {
177         Services tracker = new Services(taClient);
178
179         for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
180             String namespace = namespaceEntry.getKey();
181
182             for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
183
184                 String serviceName = serviceEntry.getKey();
185                 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
186
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);
191                     }
192
193                     Map<String, ServiceInstance> refNameToInstance = namespaceToServices
194                             .get(serviceName);
195                     if (refNameToInstance == null) {
196                         refNameToInstance = Maps.newHashMap();
197                         namespaceToServices.put(serviceName, refNameToInstance);
198                     }
199
200                     String refName = refEntry.getKey();
201
202                     ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
203                     refNameToInstance.put(refName, serviceInstance);
204
205                 }
206             }
207         }
208         return tracker;
209     }
210
211     // TODO support edit strategies on services
212
213     public static Map<String, Map<String, Map<String, String>>> fromXml(XmlElement xml) {
214         Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
215
216         List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
217         xml.checkUnrecognisedElements(services);
218
219         for (XmlElement service : services) {
220
221             XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
222             Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
223
224             Preconditions.checkState(prefixNamespace.getKey()!=null && prefixNamespace.getKey().equals("") == false, "Type attribute was not prefixed");
225
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);
230             }
231
232             String serviceName =  ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
233
234             Map<String, String> innerMap = Maps.newHashMap();
235             namespaceToServices.put(serviceName, innerMap);
236
237             List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
238             service.checkUnrecognisedElements(instances, typeElement);
239
240             for (XmlElement instance : instances) {
241                 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
242                 String refName = nameElement.getTextContent();
243
244                 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
245                 String providerName = providerElement.getTextContent();
246
247                 instance.checkUnrecognisedElements(nameElement, providerElement);
248
249                 innerMap.put(refName, providerName);
250             }
251         }
252
253         return retVal;
254     }
255
256     private String findAvailableRefName(String refName, Set<String> refNamesAsSet) {
257         String intitialRefName = refName;
258
259         while (true) {
260             refName = intitialRefName + "_" + suffix++;
261             if (refNamesAsSet.contains(refName) == false)
262                 return refName;
263         }
264     }
265
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);
269
270         for (String namespace : mappedServices.keySet()) {
271
272             for (Entry<String, Map<String, String>> serviceEntry : mappedServices.get(namespace).entrySet()) {
273                 Element serviceElement = document.createElement(SERVICE_KEY);
274                 root.appendChild(serviceElement);
275
276                 Element typeElement = XmlUtil.createPrefixedTextElement(document, TYPE_KEY, XmlNetconfConstants.PREFIX,
277                         serviceEntry.getKey());
278                 XmlUtil.addPrefixedNamespaceAttr(typeElement, XmlNetconfConstants.PREFIX, namespace);
279                 serviceElement.appendChild(typeElement);
280
281                 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
282                     Element instanceElement = document.createElement(XmlNetconfConstants.INSTANCE_KEY);
283                     serviceElement.appendChild(instanceElement);
284
285                     Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey());
286                     instanceElement.appendChild(nameElement);
287
288                     Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue());
289                     instanceElement.appendChild(providerElement);
290                 }
291             }
292
293         }
294         return root;
295     }
296
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,
300                 serviceName, on);
301         return refNameOptional.get();
302     }
303
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);
307
308         if(services == null) return Optional.absent();
309         Map<String, String> refs = services.get(serviceName);
310
311         if(refs == null) return Optional.absent();
312         Multimap<ServiceInstance, String> reverted = revertMap(refs);
313
314         ServiceInstance serviceInstance = ServiceInstance.fromObjectName(on);
315         Collection<String> references = reverted.get(serviceInstance);
316
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);
324         } else
325             return Optional.absent();
326     }
327
328     private Multimap<ServiceInstance, String> revertMap(Map<String, String> refs) {
329         Multimap<ServiceInstance, String> multimap = HashMultimap.create();
330
331         for (Entry<String, String> e : refs.entrySet()) {
332             multimap.put(ServiceInstance.fromString(e.getValue()), e.getKey());
333         }
334
335         return multimap;
336     }
337
338     public boolean hasRefName(String key, String value, ObjectName on) {
339         return getRefNameOptional(key, value, on, Optional.<String>absent()).isPresent();
340     }
341
342     public static final class ServiceInstance {
343         public ServiceInstance(String moduleName, String instanceName) {
344             this.moduleName = moduleName;
345             this.instanceName = instanceName;
346         }
347
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);
353             }
354
355             Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
356                     + " or " + pDeprecated.toString() + " but was " + instanceId);
357
358             String factoryName = matcher.group(1);
359             String instanceName = matcher.group(2);
360             return new ServiceInstance(factoryName, instanceName);
361         }
362
363         private final String moduleName, instanceName;
364         private String serviceName;
365
366         public String getServiceName() {
367             return serviceName;
368         }
369
370         public void setServiceName(String serviceName) {
371             this.serviceName = serviceName;
372         }
373
374         public String getModuleName() {
375             return moduleName;
376         }
377
378         public String getInstanceName() {
379             return instanceName;
380         }
381
382         private static final String blueprint = "/"
383                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
384                 + XmlNetconfConstants.TYPE_KEY + "='%s']["
385                 + XmlNetconfConstants.NAME_KEY + "='%s']";
386
387         // TODO unify with xpath in RuntimeRpc
388
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'\\]";
394
395         private static final String blueprintR = "/"
396                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
397                 + XmlNetconfConstants.TYPE_KEY + "='%s'\\]\\["
398                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
399
400         private static final Pattern pDeprecated = Pattern.compile(String.format(blueprintRDeprecated, "(.+)", "(.+)"));
401         private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
402
403         @Override
404         public String toString() {
405             return String.format(blueprint, moduleName, instanceName);
406         }
407
408         @Override
409         public int hashCode() {
410             final int prime = 31;
411             int result = 1;
412             result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
413             result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
414             return result;
415         }
416
417         @Override
418         public boolean equals(Object obj) {
419             if (this == obj)
420                 return true;
421             if (obj == null)
422                 return false;
423             if (getClass() != obj.getClass())
424                 return false;
425             ServiceInstance other = (ServiceInstance) obj;
426             if (instanceName == null) {
427                 if (other.instanceName != null)
428                     return false;
429             } else if (!instanceName.equals(other.instanceName))
430                 return false;
431             if (moduleName == null) {
432                 if (other.moduleName != null)
433                     return false;
434             } else if (!moduleName.equals(other.moduleName))
435                 return false;
436             return true;
437         }
438
439         public ObjectName getObjectName(String transactionName) {
440             return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
441         }
442
443         public static ServiceInstance fromObjectName(ObjectName on) {
444             return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
445         }
446     }
447
448 }