Merge "Bug 1029: Remove dead code: sal-dom-it"
[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.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
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.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 String PROVIDER_KEY = "provider";
33     private static final String NAME_KEY = "name";
34     public static final String TYPE_KEY = "type";
35     public static final String SERVICE_KEY = "service";
36
37     private final Map<String /*Namespace*/, Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>>> namespaceToServiceNameToRefNameToInstance = Maps
38             .newHashMap();
39
40     /**
41      *
42      */
43     public Map<String, Map<String, Map<String, ServiceInstance>>> getNamespaceToServiceNameToRefNameToInstance() {
44         return namespaceToServiceNameToRefNameToInstance;
45     }
46
47     private static Services resolveServices(Map<String, Map<String, Map<String, String>>> mappedServices) {
48         Services tracker = new Services();
49
50         for (Entry<String, Map<String, Map<String, String>>> namespaceEntry : mappedServices.entrySet()) {
51             String namespace = namespaceEntry.getKey();
52
53             for (Entry<String, Map<String, String>> serviceEntry : namespaceEntry.getValue().entrySet()) {
54
55                 String serviceName = serviceEntry.getKey();
56                 for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
57
58                     Map<String, Map<String, ServiceInstance>> namespaceToServices = tracker.namespaceToServiceNameToRefNameToInstance.get(namespace);
59                     if (namespaceToServices == null) {
60                         namespaceToServices = Maps.newHashMap();
61                         tracker.namespaceToServiceNameToRefNameToInstance.put(namespace, namespaceToServices);
62                     }
63
64                     Map<String, ServiceInstance> refNameToInstance = namespaceToServices
65                             .get(serviceName);
66                     if (refNameToInstance == null) {
67                         refNameToInstance = Maps.newHashMap();
68                         namespaceToServices.put(serviceName, refNameToInstance);
69                     }
70
71                     String refName = refEntry.getKey();
72
73                     ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
74                     refNameToInstance.put(refName, serviceInstance);
75
76                 }
77             }
78         }
79         return tracker;
80     }
81
82     // TODO support edit strategies on services
83
84     public static Services fromXml(XmlElement xml) throws NetconfDocumentedException {
85         Map<String, Map<String, Map<String, String>>> retVal = Maps.newHashMap();
86
87         List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
88         xml.checkUnrecognisedElements(services);
89
90         for (XmlElement service : services) {
91
92             XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
93             Entry<String, String> prefixNamespace = typeElement.findNamespaceOfTextContent();
94
95             Preconditions.checkState(prefixNamespace.getKey()!=null && !prefixNamespace.getKey().equals(""), "Type attribute was not prefixed");
96
97             Map<String, Map<String, String>> namespaceToServices = retVal.get(prefixNamespace.getValue());
98             if(namespaceToServices == null) {
99                 namespaceToServices = Maps.newHashMap();
100                 retVal.put(prefixNamespace.getValue(), namespaceToServices);
101             }
102
103             String serviceName =  ObjectNameAttributeReadingStrategy.checkPrefixAndExtractServiceName(typeElement, prefixNamespace);
104
105             Map<String, String> innerMap = Maps.newHashMap();
106             namespaceToServices.put(serviceName, innerMap);
107
108             List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
109             service.checkUnrecognisedElements(instances, typeElement);
110
111             for (XmlElement instance : instances) {
112                 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
113                 String refName = nameElement.getTextContent();
114
115                 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
116                 String providerName = providerElement.getTextContent();
117
118                 instance.checkUnrecognisedElements(nameElement, providerElement);
119
120                 innerMap.put(refName, providerName);
121             }
122         }
123
124         return resolveServices(retVal);
125     }
126
127     public static Element toXml(ServiceRegistryWrapper serviceRegistryWrapper, Document document) {
128         Element root = XmlUtil.createElement(document, XmlNetconfConstants.SERVICES_KEY, Optional.of(XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG));
129
130         Map<String, Map<String, Map<String, String>>> mappedServices = serviceRegistryWrapper.getMappedServices();
131         for (String namespace : mappedServices.keySet()) {
132
133             for (Entry<String, Map<String, String>> serviceEntry : mappedServices.get(namespace).entrySet()) {
134                 // service belongs to config.yang namespace
135                 Element serviceElement = XmlUtil.createElement(document, SERVICE_KEY, Optional.<String>absent());
136                 root.appendChild(serviceElement);
137
138                 // type belongs to config.yang namespace
139                 String serviceType = serviceEntry.getKey();
140                 Element typeElement = XmlUtil.createTextElementWithNamespacedContent(document, XmlNetconfConstants.TYPE_KEY,
141                         XmlNetconfConstants.PREFIX, namespace, serviceType);
142
143                 serviceElement.appendChild(typeElement);
144
145                 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
146                     Element instanceElement = XmlUtil.createElement(document, XmlNetconfConstants.INSTANCE_KEY, Optional.<String>absent());
147                     serviceElement.appendChild(instanceElement);
148
149                     Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey(), Optional.<String>absent());
150                     instanceElement.appendChild(nameElement);
151
152                     Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue(), Optional.<String>absent());
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()) {
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             }
241             if (obj == null){
242                 return false;
243             }
244             if (getClass() != obj.getClass()){
245                 return false;
246             }
247             ServiceInstance other = (ServiceInstance) obj;
248             if (instanceName == null) {
249                 if (other.instanceName != null){
250                     return false;
251                 }
252             } else if (!instanceName.equals(other.instanceName)){
253                 return false;
254             }
255             if (moduleName == null) {
256                 if (other.moduleName != null){
257                     return false;
258                 }
259             } else if (!moduleName.equals(other.moduleName)){
260                 return false;
261             }
262             return true;
263         }
264
265         public ObjectName getObjectName(String transactionName) {
266             return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
267         }
268
269         public static ServiceInstance fromObjectName(ObjectName on) {
270             return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
271         }
272     }
273
274 }