bdb4c1b067ff19947a01484165ba25563ba53e0a
[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
15 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.confignetconfconnector.mapping.attributes.fromxml.ObjectNameAttributeReadingStrategy;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23
24 import javax.management.ObjectName;
25
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 public final class Services {
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) throws NetconfDocumentedException {
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(""), "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 = namespaceToServices.get(serviceName);
108             if (innerMap == null) {
109                 innerMap = Maps.newHashMap();
110                 namespaceToServices.put(serviceName, innerMap);
111             }
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 (Entry<String, Map<String, Map<String, String>>> namespaceToRefEntry : mappedServices.entrySet()) {
137
138             for (Entry<String, Map<String, String>> serviceEntry : namespaceToRefEntry.getValue().entrySet()) {
139                 // service belongs to config.yang namespace
140                 Element serviceElement = XmlUtil.createElement(document, SERVICE_KEY, Optional.<String>absent());
141                 root.appendChild(serviceElement);
142
143                 // type belongs to config.yang namespace
144                 String serviceType = serviceEntry.getKey();
145                 Element typeElement = XmlUtil.createTextElementWithNamespacedContent(document, XmlNetconfConstants.TYPE_KEY,
146                         XmlNetconfConstants.PREFIX, namespaceToRefEntry.getKey(), serviceType);
147
148                 serviceElement.appendChild(typeElement);
149
150                 for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
151                     Element instanceElement = XmlUtil.createElement(document, XmlNetconfConstants.INSTANCE_KEY, Optional.<String>absent());
152                     serviceElement.appendChild(instanceElement);
153
154                     Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey(), Optional.<String>absent());
155                     instanceElement.appendChild(nameElement);
156
157                     Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue(), Optional.<String>absent());
158                     instanceElement.appendChild(providerElement);
159                 }
160             }
161
162         }
163         return root;
164     }
165
166     public static final class ServiceInstance {
167         public ServiceInstance(String moduleName, String instanceName) {
168             this.moduleName = moduleName;
169             this.instanceName = instanceName;
170         }
171
172         public static ServiceInstance fromString(String instanceId) {
173             instanceId = instanceId.trim();
174             Matcher matcher = p.matcher(instanceId);
175             if(!matcher.matches()) {
176                 matcher = pDeprecated.matcher(instanceId);
177             }
178
179             Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
180                     + " or " + pDeprecated.toString() + " but was " + instanceId);
181
182             String factoryName = matcher.group(1);
183             String instanceName = matcher.group(2);
184             return new ServiceInstance(factoryName, instanceName);
185         }
186
187         private final String moduleName, instanceName;
188         private String serviceName;
189
190         public String getServiceName() {
191             return serviceName;
192         }
193
194         public void setServiceName(String serviceName) {
195             this.serviceName = serviceName;
196         }
197
198         public String getModuleName() {
199             return moduleName;
200         }
201
202         public String getInstanceName() {
203             return instanceName;
204         }
205
206         private static final String blueprint = "/"
207                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
208                 + XmlNetconfConstants.TYPE_KEY + "='%s']["
209                 + XmlNetconfConstants.NAME_KEY + "='%s']";
210
211         // TODO unify with xpath in RuntimeRpc
212
213         // Previous version of xpath, needs to be supported for backwards compatibility (persisted configs by config-persister)
214         private static final String blueprintRDeprecated = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
215                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
216                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
217                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
218
219         private static final String blueprintR = "/"
220                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
221                 + XmlNetconfConstants.TYPE_KEY + "='%s'\\]\\["
222                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
223
224         private static final Pattern pDeprecated = Pattern.compile(String.format(blueprintRDeprecated, "(.+)", "(.+)"));
225         private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
226
227         @Override
228         public String toString() {
229             return String.format(blueprint, moduleName, instanceName);
230         }
231
232         @Override
233         public int hashCode() {
234             final int prime = 31;
235             int result = 1;
236             result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
237             result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
238             return result;
239         }
240
241         @Override
242         public boolean equals(Object obj) {
243             if (this == obj){
244                 return true;
245             }
246             if (obj == null){
247                 return false;
248             }
249             if (getClass() != obj.getClass()){
250                 return false;
251             }
252             ServiceInstance other = (ServiceInstance) obj;
253             if (instanceName == null) {
254                 if (other.instanceName != null){
255                     return false;
256                 }
257             } else if (!instanceName.equals(other.instanceName)){
258                 return false;
259             }
260             if (moduleName == null) {
261                 if (other.moduleName != null){
262                     return false;
263                 }
264             } else if (!moduleName.equals(other.moduleName)){
265                 return false;
266             }
267             return true;
268         }
269
270         public ObjectName getObjectName(String transactionName) {
271             return ObjectNameUtil.createTransactionModuleON(transactionName, moduleName, instanceName);
272         }
273
274         public static ServiceInstance fromObjectName(ObjectName on) {
275             return new ServiceInstance(ObjectNameUtil.getFactoryName(on), ObjectNameUtil.getInstanceName(on));
276         }
277     }
278
279 }