Merge "Bug 714 - Fixed creating DOM Document's element with namespace"
[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 java.util.List;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16
17 import javax.management.ObjectName;
18
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 com.google.common.base.Optional;
30 import com.google.common.base.Preconditions;
31 import com.google.common.collect.Maps;
32
33 public final class Services {
34
35     private static final Logger logger = LoggerFactory.getLogger(Services.class);
36
37     private static final String PROVIDER_KEY = "provider";
38     private static final String NAME_KEY = "name";
39     public static final String TYPE_KEY = "type";
40     public static final String SERVICE_KEY = "service";
41
42     private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
43             .newHashMap();
44
45     /**
46      *
47      */
48     public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
49         return namespaceToServiceNameToRefNameToInstance;
50     }
51
52     private static Services resolveServices(Map<String, Map<String, Map<String, String>>> mappedServices) {
53         Services tracker = new Services();
54
55         for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
56             String namespace = namespaceEntry.getKey();
57
58             for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
59
60                 String serviceName = serviceEntry.getKey();
61                 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
62
63                     Map<String, Map<String, ServiceInstance>> namespaceToServices = tracker.namespaceToServiceNameToRefNameToInstance.get(namespace);
64                     if (namespaceToServices == null) {
65                         namespaceToServices = Maps.newHashMap();
66                         tracker.namespaceToServiceNameToRefNameToInstance.put(namespace, namespaceToServices);
67                     }
68
69                     Map<String, ServiceInstance> refNameToInstance = namespaceToServices
70                             .get(serviceName);
71                     if (refNameToInstance == null) {
72                         refNameToInstance = Maps.newHashMap();
73                         namespaceToServices.put(serviceName, refNameToInstance);
74                     }
75
76                     String refName = refEntry.getKey();
77
78                     ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
79                     refNameToInstance.put(refName, serviceInstance);
80
81                 }
82             }
83         }
84         return tracker;
85     }
86
87     // TODO support edit strategies on services
88
89     public static Services fromXml(XmlElement xml) {
90         Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
91
92         List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
93         xml.checkUnrecognisedElements(services);
94
95         for (XmlElement service : services) {
96
97             XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
98             Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
99
100             Preconditions.checkState(prefixNamespace.getKey()!=null && prefixNamespace.getKey().equals("") == false, "Type attribute was not prefixed");
101
102             Map<String, Map<String, String>> namespaceToServices = retVal.get(prefixNamespace.getValue());
103             if(namespaceToServices == null) {
104                 namespaceToServices = Maps.newHashMap();
105                 retVal.put(prefixNamespace.getValue(), namespaceToServices);
106             }
107
108             String serviceName =  ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
109
110             Map<String, String> innerMap = Maps.newHashMap();
111             namespaceToServices.put(serviceName, innerMap);
112
113             List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
114             service.checkUnrecognisedElements(instances, typeElement);
115
116             for (XmlElement instance : instances) {
117                 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
118                 String refName = nameElement.getTextContent();
119
120                 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
121                 String providerName = providerElement.getTextContent();
122
123                 instance.checkUnrecognisedElements(nameElement, providerElement);
124
125                 innerMap.put(refName, providerName);
126             }
127         }
128
129         return resolveServices(retVal);
130     }
131
132     public static Element toXml(ServiceRegistryWrapper serviceRegistryWrapper, Document document) {
133         Element root = XmlUtil.createElement(document, XmlNetconfConstants.SERVICES_KEY, Optional.of(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
134
135         Map<String, Map<String, Map<String, String>>> mappedServices = serviceRegistryWrapper.getMappedServices();
136         for (String namespace : mappedServices.keySet()) {
137
138             for (Entry<String, Map<String, String>> serviceEntry : mappedServices.get(namespace).entrySet()) {
139                 Element serviceElement = XmlUtil.createElement(document, SERVICE_KEY, Optional.<String>absent());
140                 root.appendChild(serviceElement);
141
142                 Element typeElement = XmlUtil.createPrefixedTextElement(document, XmlUtil.createPrefixedValue(XmlNetconfConstants.PREFIX, TYPE_KEY), XmlNetconfConstants.PREFIX,
143                         serviceEntry.getKey(), Optional.of(namespace));
144                 serviceElement.appendChild(typeElement);
145
146                 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
147                     Element instanceElement = XmlUtil.createElement(document, XmlNetconfConstants.INSTANCE_KEY, Optional.<String>absent());
148                     serviceElement.appendChild(instanceElement);
149
150                     Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey(), Optional.<String>absent());
151                     instanceElement.appendChild(nameElement);
152
153                     Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue(), Optional.<String>absent());
154                     instanceElement.appendChild(providerElement);
155                 }
156             }
157
158         }
159         return root;
160     }
161
162     public static final class ServiceInstance {
163         public ServiceInstance(String moduleName, String instanceName) {
164             this.moduleName = moduleName;
165             this.instanceName = instanceName;
166         }
167
168         public static ServiceInstance fromString(String instanceId) {
169             instanceId = instanceId.trim();
170             Matcher matcher = p.matcher(instanceId);
171             if(matcher.matches() == false) {
172                 matcher = pDeprecated.matcher(instanceId);
173             }
174
175             Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
176                     + " or " + pDeprecated.toString() + " but was " + instanceId);
177
178             String factoryName = matcher.group(1);
179             String instanceName = matcher.group(2);
180             return new ServiceInstance(factoryName, instanceName);
181         }
182
183         private final String moduleName, instanceName;
184         private String serviceName;
185
186         public String getServiceName() {
187             return serviceName;
188         }
189
190         public void setServiceName(String serviceName) {
191             this.serviceName = serviceName;
192         }
193
194         public String getModuleName() {
195             return moduleName;
196         }
197
198         public String getInstanceName() {
199             return instanceName;
200         }
201
202         private static final String blueprint = "/"
203                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
204                 + XmlNetconfConstants.TYPE_KEY + "='%s']["
205                 + XmlNetconfConstants.NAME_KEY + "='%s']";
206
207         // TODO unify with xpath in RuntimeRpc
208
209         // Previous version of xpath, needs to be supported for backwards compatibility (persisted configs by config-persister)
210         private static final String blueprintRDeprecated = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
211                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
212                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
213                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
214
215         private static final String blueprintR = "/"
216                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
217                 + XmlNetconfConstants.TYPE_KEY + "='%s'\\]\\["
218                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
219
220         private static final Pattern pDeprecated = Pattern.compile(String.format(blueprintRDeprecated, "(.+)", "(.+)"));
221         private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
222
223         @Override
224         public String toString() {
225             return String.format(blueprint, moduleName, instanceName);
226         }
227
228         @Override
229         public int hashCode() {
230             final int prime = 31;
231             int result = 1;
232             result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
233             result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
234             return result;
235         }
236
237         @Override
238         public boolean equals(Object obj) {
239             if (this == obj)
240                 return true;
241             if (obj == null)
242                 return false;
243             if (getClass() != obj.getClass())
244                 return false;
245             ServiceInstance other = (ServiceInstance) obj;
246             if (instanceName == null) {
247                 if (other.instanceName != null)
248                     return false;
249             } else if (!instanceName.equals(other.instanceName))
250                 return false;
251             if (moduleName == null) {
252                 if (other.moduleName != null)
253                     return false;
254             } else if (!moduleName.equals(other.moduleName))
255                 return false;
256             return true;
257         }
258
259         public ObjectName getObjectName(String transactionName) {
260             return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
261         }
262
263         public static ServiceInstance fromObjectName(ObjectName on) {
264             return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
265         }
266     }
267
268 }