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.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import java.util.List;
16 import java.util.Map.Entry;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 import javax.management.ObjectName;
20 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
21 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
22 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
23 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectNameAttributeReadingStrategy;
24 import org.opendaylight.controller.netconf.util.xml.XmlElement;
25 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
29 public final class Services {
31 private static final String PROVIDER_KEY = "provider";
32 private static final String NAME_KEY = "name";
33 public static final String TYPE_KEY = "type";
34 public static final String SERVICE_KEY = "service";
36 private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
42 public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
43 return namespaceToServiceNameToRefNameToInstance;
46 private static Services resolveServices(Map<String, Map<String, Map<String, String>>> mappedServices) {
47 Services tracker = new Services();
49 for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
50 String namespace = namespaceEntry.getKey();
52 for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
54 String serviceName = serviceEntry.getKey();
55 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
57 Map<String, Map<String, ServiceInstance>> namespaceToServices = tracker.namespaceToServiceNameToRefNameToInstance.get(namespace);
58 if (namespaceToServices == null) {
59 namespaceToServices = Maps.newHashMap();
60 tracker.namespaceToServiceNameToRefNameToInstance.put(namespace, namespaceToServices);
63 Map<String, ServiceInstance> refNameToInstance = namespaceToServices
65 if (refNameToInstance == null) {
66 refNameToInstance = Maps.newHashMap();
67 namespaceToServices.put(serviceName, refNameToInstance);
70 String refName = refEntry.getKey();
72 ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
73 refNameToInstance.put(refName, serviceInstance);
81 // TODO support edit strategies on services
83 public static Services fromXml(XmlElement xml) throws NetconfDocumentedException {
84 Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
86 List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
87 xml.checkUnrecognisedElements(services);
89 for (XmlElement service : services) {
91 XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
92 Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
94 Preconditions.checkState(prefixNamespace.getKey()!=null && !prefixNamespace.getKey().equals(""), "Type attribute was not prefixed");
96 Map<String, Map<String, String>> namespaceToServices = retVal.get(prefixNamespace.getValue());
97 if(namespaceToServices == null) {
98 namespaceToServices = Maps.newHashMap();
99 retVal.put(prefixNamespace.getValue(), namespaceToServices);
102 String serviceName = ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
104 Map<String, String> innerMap = namespaceToServices.get(serviceName);
105 if (innerMap == null) {
106 innerMap = Maps.newHashMap();
107 namespaceToServices.put(serviceName, innerMap);
110 List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
111 service.checkUnrecognisedElements(instances, typeElement);
113 for (XmlElement instance : instances) {
114 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
115 String refName = nameElement.getTextContent();
117 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
118 String providerName = providerElement.getTextContent();
120 instance.checkUnrecognisedElements(nameElement, providerElement);
122 innerMap.put(refName, providerName);
126 return resolveServices(retVal);
129 public static Element toXml(ServiceRegistryWrapper serviceRegistryWrapper, Document document) {
130 Element root = XmlUtil.createElement(document, XmlNetconfConstants.SERVICES_KEY, Optional.of(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
132 Map<String, Map<String, Map<String, String>>> mappedServices = serviceRegistryWrapper.getMappedServices();
133 for (Entry<String, Map<String, Map<String, String>>> namespaceToRefEntry : mappedServices.entrySet()) {
135 for (Entry<String, Map<String, String>> serviceEntry : namespaceToRefEntry.getValue().entrySet()) {
136 // service belongs to config.yang namespace
137 Element serviceElement = XmlUtil.createElement(document, SERVICE_KEY, Optional.<String>absent());
138 root.appendChild(serviceElement);
140 // type belongs to config.yang namespace
141 String serviceType = serviceEntry.getKey();
142 Element typeElement = XmlUtil.createTextElementWithNamespacedContent(document, XmlNetconfConstants.TYPE_KEY,
143 XmlNetconfConstants.PREFIX, namespaceToRefEntry.getKey(), serviceType);
145 serviceElement.appendChild(typeElement);
147 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
148 Element instanceElement = XmlUtil.createElement(document, XmlNetconfConstants.INSTANCE_KEY, Optional.<String>absent());
149 serviceElement.appendChild(instanceElement);
151 Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey(), Optional.<String>absent());
152 instanceElement.appendChild(nameElement);
154 Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue(), Optional.<String>absent());
155 instanceElement.appendChild(providerElement);
163 public static final class ServiceInstance {
164 public ServiceInstance(String moduleName, String instanceName) {
165 this.moduleName = moduleName;
166 this.instanceName = instanceName;
169 public static ServiceInstance fromString(String instanceId) {
170 instanceId = instanceId.trim();
171 Matcher matcher = p.matcher(instanceId);
172 if(!matcher.matches()) {
173 matcher = pDeprecated.matcher(instanceId);
176 Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
177 + " or " + pDeprecated.toString() + " but was " + instanceId);
179 String factoryName = matcher.group(1);
180 String instanceName = matcher.group(2);
181 return new ServiceInstance(factoryName, instanceName);
184 private final String moduleName, instanceName;
185 private String serviceName;
187 public String getServiceName() {
191 public void setServiceName(String serviceName) {
192 this.serviceName = serviceName;
195 public String getModuleName() {
199 public String getInstanceName() {
203 private static final String blueprint = "/"
204 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
205 + XmlNetconfConstants.TYPE_KEY + "='%s']["
206 + XmlNetconfConstants.NAME_KEY + "='%s']";
208 // TODO unify with xpath in RuntimeRpc
210 // Previous version of xpath, needs to be supported for backwards compatibility (persisted configs by config-persister)
211 private static final String blueprintRDeprecated = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
212 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
213 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
214 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
216 private static final String blueprintR = "/"
217 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
218 + XmlNetconfConstants.TYPE_KEY + "='%s'\\]\\["
219 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
221 private static final Pattern pDeprecated = Pattern.compile(String.format(blueprintRDeprecated, "(.+)", "(.+)"));
222 private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
225 public String toString() {
226 return String.format(blueprint, moduleName, instanceName);
230 public int hashCode() {
231 final int prime = 31;
233 result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
234 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
239 public boolean equals(Object obj) {
246 if (getClass() != obj.getClass()){
249 ServiceInstance other = (ServiceInstance) obj;
250 if (instanceName == null) {
251 if (other.instanceName != null){
254 } else if (!instanceName.equals(other.instanceName)){
257 if (moduleName == null) {
258 if (other.moduleName != null){
261 } else if (!moduleName.equals(other.moduleName)){
267 public ObjectName getObjectName(String transactionName) {
268 return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
271 public static ServiceInstance fromObjectName(ObjectName on) {
272 return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));