Updated xpath pointing to a module instance under services subtree.
[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.Function;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.Sets;
16 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectNameAttributeReadingStrategy;
17 import org.opendaylight.controller.netconf.util.xml.XmlElement;
18 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24
25 import javax.annotation.Nullable;
26 import javax.management.ObjectName;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31 import java.util.Set;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
34
35 public final class Services {
36     private static final Logger logger = LoggerFactory.getLogger(Services.class);
37
38     private static final String PROVIDER_KEY = "provider";
39     private static final String NAME_KEY = "name";
40     public static final String TYPE_KEY = "type";
41     public static final String SERVICE_KEY = "service";
42
43     private long suffix = 1;
44
45     private final Map<ServiceInstance, String> instanceToRef = Maps.newHashMap();
46     private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
47             .newHashMap();
48
49     public String addServiceEntry(String namespace, String serviceName, ObjectName on) {
50
51         String moduleName = on.getKeyProperty("moduleFactoryName");
52         String instanceName = on.getKeyProperty("instanceName");
53
54         String refName = addServiceEntry(namespace, serviceName, moduleName, instanceName);
55         logger.trace("Added service entry to tracker. Service name {}, ref name {}, module name {}, instance name {}",
56                 serviceName, refName, moduleName, instanceName);
57         return refName;
58     }
59
60     @VisibleForTesting
61     public String addServiceEntry(String namespace, String serviceName, String moduleName, String instanceName) {
62         ServiceInstance serviceInstance = new ServiceInstance(moduleName, instanceName);
63         serviceInstance.setServiceName(serviceName);
64
65         String refName = instanceToRef.get(serviceInstance);
66
67         Map<String, Map<String, ServiceInstance>> serviceNameToRefNameToInstance = namespaceToServiceNameToRefNameToInstance.get(namespace);
68         if (serviceNameToRefNameToInstance == null) {
69             serviceNameToRefNameToInstance = Maps.newHashMap();
70             namespaceToServiceNameToRefNameToInstance.put(namespace, serviceNameToRefNameToInstance);
71         }
72
73         Map<String, ServiceInstance> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
74         if (refNameToInstance == null) {
75             refNameToInstance = Maps.newHashMap();
76             serviceNameToRefNameToInstance.put(serviceName, refNameToInstance);
77         }
78
79         if (refName != null) {
80             if (serviceNameToRefNameToInstance.get(serviceName).containsKey(moduleName) == false) {
81                 refNameToInstance.put(refName, serviceInstance);
82             }
83             return refName;
84         } else {
85             refName = "ref_" + instanceName;
86
87             final Set<String> refNamesAsSet = toSet(instanceToRef.values());
88             if (refNamesAsSet.contains(refName)) {
89                 refName = findAvailableRefName(refName, refNamesAsSet);
90             }
91
92             instanceToRef.put(serviceInstance, refName);
93             refNameToInstance.put(refName, serviceInstance);
94
95             return refName;
96         }
97     }
98
99     private Set<String> toSet(Collection<String> values) {
100         Set<String> refNamesAsSet = Sets.newHashSet();
101
102         for (String refName : values) {
103             boolean resultAdd = refNamesAsSet.add(refName);
104             Preconditions.checkState(resultAdd,
105                     "Error occurred building services element, reference name {} was present twice", refName);
106         }
107
108         return refNamesAsSet;
109     }
110
111     public ServiceInstance getByServiceAndRefName(String namespace, String serviceName, String refName) {
112         Map<String, Map<String, ServiceInstance>> serviceNameToRefNameToInstance = namespaceToServiceNameToRefNameToInstance.get(namespace);
113         Preconditions.checkArgument(serviceNameToRefNameToInstance != null, "No serviceInstances mapped to " + namespace + " , "
114                 + serviceNameToRefNameToInstance.keySet());
115
116         Map<String, ServiceInstance> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
117         Preconditions.checkArgument(refNameToInstance != null, "No serviceInstances mapped to " + serviceName + " , "
118                 + serviceNameToRefNameToInstance.keySet());
119
120         ServiceInstance serviceInstance = refNameToInstance.get(refName);
121         Preconditions.checkArgument(serviceInstance != null, "No serviceInstance mapped to " + refName
122                 + " under service name " + serviceName + " , " + refNameToInstance.keySet());
123         return serviceInstance;
124     }
125
126     // TODO hide getMappedServices, call it explicitly in toXml
127
128     public Map<String, Map<String, Map<String, String>>> getMappedServices() {
129         Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
130
131         for (String namespace : namespaceToServiceNameToRefNameToInstance.keySet()) {
132
133             Map<String, Map<String, ServiceInstance>> serviceNameToRefNameToInstance = namespaceToServiceNameToRefNameToInstance
134                     .get(namespace);
135             Map<String, Map<String, String>> innerRetVal = Maps.newHashMap();
136
137             for (String serviceName : serviceNameToRefNameToInstance.keySet()) {
138
139                 Map<String, String> innerInnerRetVal = Maps.transformValues(
140                         serviceNameToRefNameToInstance.get(serviceName), new Function<ServiceInstance, String>() {
141                             @Nullable
142                             @Override
143                             public String apply(@Nullable ServiceInstance serviceInstance) {
144                                 return serviceInstance.toString();
145                             }
146                         });
147                 innerRetVal.put(serviceName, innerInnerRetVal);
148             }
149             retVal.put(namespace, innerRetVal);
150         }
151
152         return retVal;
153     }
154
155     // TODO hide resolveServices, call it explicitly in fromXml
156
157     public static Services resolveServices(Map<String, Map<String, Map<String, String>>> mappedServices) {
158         Services tracker = new Services();
159
160         for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
161             String namespace = namespaceEntry.getKey();
162
163             for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
164
165                 String serviceName = serviceEntry.getKey();
166                 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
167
168                     Map<String, Map<String, ServiceInstance>> namespaceToServices = tracker.namespaceToServiceNameToRefNameToInstance.get(namespace);
169                     if (namespaceToServices == null) {
170                         namespaceToServices = Maps.newHashMap();
171                         tracker.namespaceToServiceNameToRefNameToInstance.put(namespace, namespaceToServices);
172                     }
173
174                     Map<String, ServiceInstance> refNameToInstance = namespaceToServices
175                             .get(serviceName);
176                     if (refNameToInstance == null) {
177                         refNameToInstance = Maps.newHashMap();
178                         namespaceToServices.put(serviceName, refNameToInstance);
179                     }
180
181                     String refName = refEntry.getKey();
182                     Preconditions.checkState(false == refNameToInstance.containsKey(refName),
183                             "Duplicate reference name to service " + refName + " under service " + serviceName);
184                     ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
185                     refNameToInstance.put(refName, serviceInstance);
186
187                     tracker.instanceToRef.put(serviceInstance, refEntry.getKey());
188                 }
189             }
190         }
191         return tracker;
192     }
193
194     public static Map<String, Map<String, Map<String, String>>> fromXml(XmlElement xml) {
195         Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
196
197         List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
198         xml.checkUnrecognisedElements(services);
199
200         for (XmlElement service : services) {
201
202             XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
203             Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
204
205             Preconditions.checkState(prefixNamespace.getKey()!=null && prefixNamespace.getKey().equals("") == false, "Type attribute was not prefixed");
206
207             Map<String, Map<String, String>> namespaceToServices = retVal.get(prefixNamespace.getValue());
208             if(namespaceToServices == null) {
209                 namespaceToServices = Maps.newHashMap();
210                 retVal.put(prefixNamespace.getValue(), namespaceToServices);
211             }
212
213             String serviceName =  ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
214
215             Map<String, String> innerMap = Maps.newHashMap();
216             namespaceToServices.put(serviceName, innerMap);
217
218             List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
219             service.checkUnrecognisedElements(instances, typeElement);
220
221             for (XmlElement instance : instances) {
222                 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
223                 String refName = nameElement.getTextContent();
224
225                 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
226                 String providerName = providerElement.getTextContent();
227
228                 instance.checkUnrecognisedElements(nameElement, providerElement);
229
230                 innerMap.put(refName, providerName);
231             }
232         }
233
234         return retVal;
235     }
236
237     private String findAvailableRefName(String refName, Set<String> refNamesAsSet) {
238         String intitialRefName = refName;
239
240         while (true) {
241             refName = intitialRefName + "_" + suffix++;
242             if (refNamesAsSet.contains(refName) == false)
243                 return refName;
244         }
245     }
246
247     public Element toXml(Map<String, Map<String, Map<String, String>>> mappedServices, Document document) {
248         Element root = document.createElement(XmlNetconfConstants.SERVICES_KEY);
249         XmlUtil.addNamespaceAttr(root, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
250
251         for (String namespace : mappedServices.keySet()) {
252
253             for (Entry<String, Map<String, String>> serviceEntry : mappedServices.get(namespace).entrySet()) {
254                 Element serviceElement = document.createElement(SERVICE_KEY);
255                 root.appendChild(serviceElement);
256
257                 Element typeElement = XmlUtil.createPrefixedTextElement(document, TYPE_KEY, XmlNetconfConstants.PREFIX,
258                         serviceEntry.getKey());
259                 XmlUtil.addPrefixedNamespaceAttr(typeElement, XmlNetconfConstants.PREFIX, namespace);
260                 serviceElement.appendChild(typeElement);
261
262                 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
263                     Element instanceElement = document.createElement(XmlNetconfConstants.INSTANCE_KEY);
264                     serviceElement.appendChild(instanceElement);
265
266                     Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey());
267                     instanceElement.appendChild(nameElement);
268
269                     Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue());
270                     instanceElement.appendChild(providerElement);
271                 }
272             }
273
274         }
275         return root;
276     }
277
278     public static final class ServiceInstance {
279         public ServiceInstance(String moduleName, String instanceName) {
280             this.moduleName = moduleName;
281             this.instanceName = instanceName;
282         }
283
284         public static ServiceInstance fromString(String instanceId) {
285             instanceId = instanceId.trim();
286             Matcher matcher = p.matcher(instanceId);
287             if(matcher.matches() == false) {
288                 matcher = pDeprecated.matcher(instanceId);
289             }
290
291             Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
292                     + " or " + pDeprecated.toString() + " but was " + instanceId);
293
294             String factoryName = matcher.group(1);
295             String instanceName = matcher.group(2);
296             return new ServiceInstance(factoryName, instanceName);
297         }
298
299         private final String moduleName, instanceName;
300         private String serviceName;
301
302         public String getServiceName() {
303             return serviceName;
304         }
305
306         public void setServiceName(String serviceName) {
307             this.serviceName = serviceName;
308         }
309
310         public String getModuleName() {
311             return moduleName;
312         }
313
314         public String getInstanceName() {
315             return instanceName;
316         }
317
318         private static final String blueprint = "/"
319                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
320                 + XmlNetconfConstants.TYPE_KEY + "='%s']["
321                 + XmlNetconfConstants.NAME_KEY + "='%s']";
322
323         // TODO unify with xpath in RuntimeRpc
324
325         // Previous version of xpath, needs to be supported for backwards compatibility (persisted configs by config-persister)
326         private static final String blueprintRDeprecated = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
327                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
328                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
329                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
330
331         private static final String blueprintR = "/"
332                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
333                 + XmlNetconfConstants.TYPE_KEY + "='%s'\\]\\["
334                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
335
336         private static final Pattern pDeprecated = Pattern.compile(String.format(blueprintRDeprecated, "(.+)", "(.+)"));
337         private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
338
339         @Override
340         public String toString() {
341             return String.format(blueprint, moduleName, instanceName);
342         }
343
344         @Override
345         public int hashCode() {
346             final int prime = 31;
347             int result = 1;
348             result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
349             result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
350             return result;
351         }
352
353         @Override
354         public boolean equals(Object obj) {
355             if (this == obj)
356                 return true;
357             if (obj == null)
358                 return false;
359             if (getClass() != obj.getClass())
360                 return false;
361             ServiceInstance other = (ServiceInstance) obj;
362             if (instanceName == null) {
363                 if (other.instanceName != null)
364                     return false;
365             } else if (!instanceName.equals(other.instanceName))
366                 return false;
367             if (moduleName == null) {
368                 if (other.moduleName != null)
369                     return false;
370             } else if (!moduleName.equals(other.moduleName))
371                 return false;
372             return true;
373         }
374
375     }
376
377 }