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.opendaylight.yangtools.yang.data.api.ModifyAction;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
30 public final class Services {
32 private static final String EMPTY_PROVIDER = "";
33 private static final String PROVIDER_KEY = "provider";
34 private static final String NAME_KEY = "name";
35 public static final String TYPE_KEY = "type";
36 public static final String SERVICE_KEY = "service";
38 private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
44 public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
45 return namespaceToServiceNameToRefNameToInstance;
48 private static Services resolveServices(Map<String, Map<String, Map<String, String>>> mappedServices) {
49 Services tracker = new Services();
51 for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
52 String namespace = namespaceEntry.getKey();
54 for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
56 String serviceName = serviceEntry.getKey();
57 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
59 Map<String, Map<String, ServiceInstance>> namespaceToServices = tracker.namespaceToServiceNameToRefNameToInstance.get(namespace);
60 if (namespaceToServices == null) {
61 namespaceToServices = Maps.newHashMap();
62 tracker.namespaceToServiceNameToRefNameToInstance.put(namespace, namespaceToServices);
65 Map<String, ServiceInstance> refNameToInstance = namespaceToServices
67 if (refNameToInstance == null) {
68 refNameToInstance = Maps.newHashMap();
69 namespaceToServices.put(serviceName, refNameToInstance);
72 String refName = refEntry.getKey();
73 //we want to compare reference not value of the provider
74 refNameToInstance.put(refName, refEntry.getValue() == EMPTY_PROVIDER
75 //provider name cannot be EMPTY_PROVIDER instance unless we are executing delete
76 ? ServiceInstance.EMPTY_SERVICE_INSTANCE
77 : ServiceInstance.fromString(refEntry.getValue()));
85 // TODO support edit strategies on services
87 public static Services fromXml(XmlElement xml) throws NetconfDocumentedException {
88 Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
90 List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
91 xml.checkUnrecognisedElements(services);
93 for (XmlElement service : services) {
95 XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
96 Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
98 Preconditions.checkState(prefixNamespace.getKey()!=null && !prefixNamespace.getKey().equals(""), "Type attribute was not prefixed");
100 Map<String, Map<String, String>> namespaceToServices = retVal.get(prefixNamespace.getValue());
101 if(namespaceToServices == null) {
102 namespaceToServices = Maps.newHashMap();
103 retVal.put(prefixNamespace.getValue(), namespaceToServices);
106 String serviceName = ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
108 Map<String, String> innerMap = namespaceToServices.get(serviceName);
109 if (innerMap == null) {
110 innerMap = Maps.newHashMap();
111 namespaceToServices.put(serviceName, innerMap);
114 List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
115 service.checkUnrecognisedElements(instances, typeElement);
117 for (XmlElement instance : instances) {
118 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
119 String refName = nameElement.getTextContent();
121 if (!ModifyAction.DELETE.toString().toLowerCase().equals(
122 instance.getAttribute(
123 XmlNetconfConstants.OPERATION_ATTR_KEY,
124 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0)))
126 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
127 String providerName = providerElement.getTextContent();
129 instance.checkUnrecognisedElements(nameElement, providerElement);
131 innerMap.put(refName, providerName);
133 //since this is a delete we dont have a provider name - we want empty service instance
134 innerMap.put(refName, EMPTY_PROVIDER);
139 return resolveServices(retVal);
142 public static Element toXml(ServiceRegistryWrapper serviceRegistryWrapper, Document document) {
143 Element root = XmlUtil.createElement(document, XmlNetconfConstants.SERVICES_KEY, Optional.of(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
145 Map<String, Map<String, Map<String, String>>> mappedServices = serviceRegistryWrapper.getMappedServices();
146 for (Entry<String, Map<String, Map<String, String>>> namespaceToRefEntry : mappedServices.entrySet()) {
148 for (Entry<String, Map<String, String>> serviceEntry : namespaceToRefEntry.getValue().entrySet()) {
149 // service belongs to config.yang namespace
150 Element serviceElement = XmlUtil.createElement(document, SERVICE_KEY, Optional.<String>absent());
151 root.appendChild(serviceElement);
153 // type belongs to config.yang namespace
154 String serviceType = serviceEntry.getKey();
155 Element typeElement = XmlUtil.createTextElementWithNamespacedContent(document, XmlNetconfConstants.TYPE_KEY,
156 XmlNetconfConstants.PREFIX, namespaceToRefEntry.getKey(), serviceType);
158 serviceElement.appendChild(typeElement);
160 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
161 Element instanceElement = XmlUtil.createElement(document, XmlNetconfConstants.INSTANCE_KEY, Optional.<String>absent());
162 serviceElement.appendChild(instanceElement);
164 Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey(), Optional.<String>absent());
165 instanceElement.appendChild(nameElement);
167 Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue(), Optional.<String>absent());
168 instanceElement.appendChild(providerElement);
176 public static final class ServiceInstance {
177 public static final ServiceInstance EMPTY_SERVICE_INSTANCE = new ServiceInstance("", "");
179 public ServiceInstance(String moduleName, String instanceName) {
180 this.moduleName = moduleName;
181 this.instanceName = instanceName;
184 public static ServiceInstance fromString(String instanceId) {
185 instanceId = instanceId.trim();
186 Matcher matcher = p.matcher(instanceId);
187 if(!matcher.matches()) {
188 matcher = pDeprecated.matcher(instanceId);
191 Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
192 + " or " + pDeprecated.toString() + " but was " + instanceId);
194 String factoryName = matcher.group(1);
195 String instanceName = matcher.group(2);
196 return new ServiceInstance(factoryName, instanceName);
199 private final String moduleName, instanceName;
200 private String serviceName;
202 public String getServiceName() {
206 public void setServiceName(String serviceName) {
207 this.serviceName = serviceName;
210 public String getModuleName() {
214 public String getInstanceName() {
218 private static final String blueprint = "/"
219 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
220 + XmlNetconfConstants.TYPE_KEY + "='%s']["
221 + XmlNetconfConstants.NAME_KEY + "='%s']";
223 // TODO unify with xpath in RuntimeRpc
225 // Previous version of xpath, needs to be supported for backwards compatibility (persisted configs by config-persister)
226 private static final String blueprintRDeprecated = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
227 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
228 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
229 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
231 private static final String blueprintR = "/"
232 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
233 + XmlNetconfConstants.TYPE_KEY + "='%s'\\]\\["
234 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
236 private static final Pattern pDeprecated = Pattern.compile(String.format(blueprintRDeprecated, "(.+)", "(.+)"));
237 private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
240 public String toString() {
241 return String.format(blueprint, moduleName, instanceName);
245 public int hashCode() {
246 final int prime = 31;
248 result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
249 result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
254 public boolean equals(Object obj) {
261 if (getClass() != obj.getClass()){
264 ServiceInstance other = (ServiceInstance) obj;
265 if (instanceName == null) {
266 if (other.instanceName != null){
269 } else if (!instanceName.equals(other.instanceName)){
272 if (moduleName == null) {
273 if (other.moduleName != null){
276 } else if (!moduleName.equals(other.moduleName)){
282 public ObjectName getObjectName(String transactionName) {
283 return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
286 public static ServiceInstance fromObjectName(ObjectName on) {
287 return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));