Initial code drop of netconf protocol implementation
[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.Function;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import com.google.common.collect.Sets;
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.w3c.dom.Document;
19 import org.w3c.dom.Element;
20
21 import javax.annotation.Nullable;
22 import javax.management.ObjectName;
23 import java.util.Collection;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Set;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30
31 public final class Services {
32
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";
37
38     private long suffix = 1;
39
40     private final Map<ServiceInstance, String> instanceToRef = Maps.newHashMap();
41     private final Map<String/* ServiceName */, Map<String/* refName */, ServiceInstance>> serviceNameToRefNameToInstance = Maps
42             .newHashMap();
43
44     public String addServiceEntry(String serviceName, ObjectName on) {
45
46         String moduleName = on.getKeyProperty("moduleFactoryName");
47         String instanceName = on.getKeyProperty("instanceName");
48
49         return addServiceEntry(serviceName, moduleName, instanceName);
50     }
51
52     public String addServiceEntry(String serviceName, String moduleName, String instanceName) {
53         ServiceInstance serviceInstance = new ServiceInstance(moduleName, instanceName);
54         serviceInstance.setServiceName(serviceName);
55
56         String refName = instanceToRef.get(serviceInstance);
57
58         Map<String, ServiceInstance> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
59         if (refNameToInstance == null) {
60             refNameToInstance = Maps.newHashMap();
61             serviceNameToRefNameToInstance.put(serviceName, refNameToInstance);
62         }
63
64         if (refName != null) {
65             if (serviceNameToRefNameToInstance.get(serviceName).containsKey(moduleName) == false) {
66                 refNameToInstance.put(refName, serviceInstance);
67             }
68             return refName;
69         } else {
70             refName = "ref_" + instanceName;
71
72             final Set<String> refNamesAsSet = toSet(instanceToRef.values());
73             if (refNamesAsSet.contains(refName)) {
74                 refName = findAvailableRefName(refName, refNamesAsSet);
75             }
76
77             instanceToRef.put(serviceInstance, refName);
78             refNameToInstance.put(refName, serviceInstance);
79
80             return refName;
81         }
82     }
83
84     private Set<String> toSet(Collection<String> values) {
85         Set<String> refNamesAsSet = Sets.newHashSet();
86
87         for (String refName : values) {
88             boolean resultAdd = refNamesAsSet.add(refName);
89             Preconditions.checkState(resultAdd,
90                     "Error occurred building services element, reference name {} was present twice", refName);
91         }
92
93         return refNamesAsSet;
94     }
95
96     public ServiceInstance getByServiceAndRefName(String serviceName, String refName) {
97         Map<String, ServiceInstance> refNameToInstance = serviceNameToRefNameToInstance.get(serviceName);
98         Preconditions.checkArgument(refNameToInstance != null, "No serviceInstances mapped to " + serviceName + " , "
99                 + serviceNameToRefNameToInstance.keySet());
100
101         ServiceInstance serviceInstance = refNameToInstance.get(refName);
102         Preconditions.checkArgument(serviceInstance != null, "No serviceInstance mapped to " + refName
103                 + " under service name " + serviceName + " , " + refNameToInstance.keySet());
104         return serviceInstance;
105     }
106
107     // TODO hide getMappedServices, call it explicitly in toXml
108
109     public Map<String, Map<String, String>> getMappedServices() {
110         Map<String, Map<String, String>> retVal = Maps.newHashMap();
111
112         for (String serviceName : serviceNameToRefNameToInstance.keySet()) {
113
114             Map<String, String> innerRetVal = Maps.transformValues(serviceNameToRefNameToInstance.get(serviceName),
115                     new Function<ServiceInstance, String>() {
116                         @Nullable
117                         @Override
118                         public String apply(@Nullable ServiceInstance serviceInstance) {
119                             return serviceInstance.toString();
120                         }
121                     });
122             retVal.put(serviceName, innerRetVal);
123         }
124
125         return retVal;
126     }
127
128     // TODO hide resolveServices, call it explicitly in fromXml
129
130     public static Services resolveServices(Map<String, Map<String, String>> mappedServices) {
131         Services tracker = new Services();
132
133         for (Entry<String, Map<String, String>> serviceEntry : mappedServices.entrySet()) {
134
135             String serviceName = serviceEntry.getKey();
136             for (Entry<String, String> refEntry : serviceEntry.getValue().entrySet()) {
137
138                 Map<String, ServiceInstance> refNameToInstance = tracker.serviceNameToRefNameToInstance
139                         .get(serviceName);
140                 if (refNameToInstance == null) {
141                     refNameToInstance = Maps.newHashMap();
142                     tracker.serviceNameToRefNameToInstance.put(serviceName, refNameToInstance);
143                 }
144
145                 String refName = refEntry.getKey();
146                 Preconditions.checkState(false == refNameToInstance.containsKey(refName),
147                         "Duplicate reference name to service " + refName + " under service " + serviceName);
148                 ServiceInstance serviceInstance = ServiceInstance.fromString(refEntry.getValue());
149                 refNameToInstance.put(refName, serviceInstance);
150
151                 tracker.instanceToRef.put(serviceInstance, refEntry.getKey());
152             }
153         }
154         return tracker;
155     }
156
157     public static Map<String, Map<String, String>> fromXml(XmlElement xml) {
158         Map<String, Map<String, String>> retVal = Maps.newHashMap();
159
160         List<XmlElement> services = xml.getChildElements(SERVICE_KEY);
161         xml.checkUnrecognisedElements(services);
162
163         for (XmlElement service : services) {
164
165             XmlElement typeElement = service.getOnlyChildElement(TYPE_KEY);
166             String serviceName = typeElement.getTextContent();
167
168             Map<String, String> innerMap = Maps.newHashMap();
169             retVal.put(serviceName, innerMap);
170
171             List<XmlElement> instances = service.getChildElements(XmlNetconfConstants.INSTANCE_KEY);
172             service.checkUnrecognisedElements(instances, typeElement);
173
174             for (XmlElement instance : instances) {
175                 XmlElement nameElement = instance.getOnlyChildElement(NAME_KEY);
176                 String refName = nameElement.getTextContent();
177
178                 XmlElement providerElement = instance.getOnlyChildElement(PROVIDER_KEY);
179                 String providerName = providerElement.getTextContent();
180
181                 instance.checkUnrecognisedElements(nameElement, providerElement);
182
183                 innerMap.put(refName, providerName);
184             }
185         }
186
187         return retVal;
188     }
189
190     private String findAvailableRefName(String refName, Set<String> refNamesAsSet) {
191         String intitialRefName = refName;
192
193         while (true) {
194             refName = intitialRefName + "_" + suffix++;
195             if (refNamesAsSet.contains(refName) == false)
196                 return refName;
197         }
198     }
199
200     public Element toXml(Map<String, Map<String, String>> mappedServices, Document document) {
201         Element root = document.createElement(XmlNetconfConstants.SERVICES_KEY);
202         XmlUtil.addNamespaceAttr(root, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
203
204         for (Entry<String, Map<String, String>> serviceEntry : mappedServices.entrySet()) {
205             Element serviceElement = document.createElement(SERVICE_KEY);
206             root.appendChild(serviceElement);
207
208             Element typeElement = XmlUtil.createTextElement(document, TYPE_KEY, serviceEntry.getKey());
209             serviceElement.appendChild(typeElement);
210
211             for (Entry<String, String> instanceEntry : serviceEntry.getValue().entrySet()) {
212                 Element instanceElement = document.createElement(XmlNetconfConstants.INSTANCE_KEY);
213                 serviceElement.appendChild(instanceElement);
214
215                 Element nameElement = XmlUtil.createTextElement(document, NAME_KEY, instanceEntry.getKey());
216                 instanceElement.appendChild(nameElement);
217
218                 Element providerElement = XmlUtil.createTextElement(document, PROVIDER_KEY, instanceEntry.getValue());
219                 instanceElement.appendChild(providerElement);
220             }
221         }
222
223         return root;
224     }
225
226     public static final class ServiceInstance {
227         public ServiceInstance(String moduleName, String instanceName) {
228             this.moduleName = moduleName;
229             this.instanceName = instanceName;
230         }
231
232         public static ServiceInstance fromString(String instanceId) {
233             instanceId = instanceId.trim();
234             Matcher matcher = p.matcher(instanceId);
235             Preconditions.checkArgument(matcher.matches(), "Unexpected format for provider, expected " + p.toString()
236                     + " but was " + instanceId);
237             String factoryName = matcher.group(1);
238             String instanceName = matcher.group(2);
239             return new ServiceInstance(factoryName, instanceName);
240         }
241
242         private final String moduleName, instanceName;
243         private String serviceName;
244
245         public String getServiceName() {
246             return serviceName;
247         }
248
249         public void setServiceName(String serviceName) {
250             this.serviceName = serviceName;
251         }
252
253         public String getModuleName() {
254             return moduleName;
255         }
256
257         public String getInstanceName() {
258             return instanceName;
259         }
260
261         private static final String blueprint = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
262                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "["
263                 + XmlNetconfConstants.NAME_KEY + "='%s']/" + XmlNetconfConstants.INSTANCE_KEY + "["
264                 + XmlNetconfConstants.NAME_KEY + "='%s']";
265
266         private static final String blueprintR = "/" + XmlNetconfConstants.CONFIG_KEY + "/"
267                 + XmlNetconfConstants.MODULES_KEY + "/" + XmlNetconfConstants.MODULE_KEY + "\\["
268                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]/" + XmlNetconfConstants.INSTANCE_KEY + "\\["
269                 + XmlNetconfConstants.NAME_KEY + "='%s'\\]";
270
271         private static final Pattern p = Pattern.compile(String.format(blueprintR, "(.+)", "(.+)"));
272
273         @Override
274         public String toString() {
275             return String.format(blueprint, moduleName, instanceName);
276         }
277
278         @Override
279         public int hashCode() {
280             final int prime = 31;
281             int result = 1;
282             result = prime * result + ((instanceName == null) ? 0 : instanceName.hashCode());
283             result = prime * result + ((moduleName == null) ? 0 : moduleName.hashCode());
284             return result;
285         }
286
287         @Override
288         public boolean equals(Object obj) {
289             if (this == obj)
290                 return true;
291             if (obj == null)
292                 return false;
293             if (getClass() != obj.getClass())
294                 return false;
295             ServiceInstance other = (ServiceInstance) obj;
296             if (instanceName == null) {
297                 if (other.instanceName != null)
298                     return false;
299             } else if (!instanceName.equals(other.instanceName))
300                 return false;
301             if (moduleName == null) {
302                 if (other.moduleName != null)
303                     return false;
304             } else if (!moduleName.equals(other.moduleName))
305                 return false;
306             return true;
307         }
308
309     }
310
311 }