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