BUG-2310 - widen yang model for netconf-node.
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / main / java / org / opendaylight / controller / sal / connect / netconf / listener / NetconfSessionPreferences.java
1 package org.opendaylight.controller.sal.connect.netconf.listener;
2
3 import com.google.common.base.Objects;
4 import com.google.common.base.Optional;
5 import com.google.common.base.Preconditions;
6 import com.google.common.base.Predicate;
7 import com.google.common.base.Splitter;
8 import com.google.common.collect.ImmutableSet;
9 import com.google.common.collect.Iterables;
10 import com.google.common.collect.Sets;
11 import java.net.URI;
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.Set;
15 import org.opendaylight.controller.netconf.client.NetconfClientSession;
16 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public final class NetconfSessionPreferences {
22
23     private static final class ParameterMatcher {
24         private final Predicate<String> predicate;
25         private final int skipLength;
26
27         ParameterMatcher(final String name) {
28             predicate = new Predicate<String>() {
29                 @Override
30                 public boolean apply(final String input) {
31                     return input.startsWith(name);
32                 }
33             };
34
35             this.skipLength = name.length();
36         }
37
38         private String from(final Iterable<String> params) {
39             final Optional<String> o = Iterables.tryFind(params, predicate);
40             if (!o.isPresent()) {
41                 return null;
42             }
43
44             return o.get().substring(skipLength);
45         }
46     }
47
48     private static final Logger LOG = LoggerFactory.getLogger(NetconfSessionPreferences.class);
49     private static final ParameterMatcher MODULE_PARAM = new ParameterMatcher("module=");
50     private static final ParameterMatcher REVISION_PARAM = new ParameterMatcher("revision=");
51     private static final ParameterMatcher BROKEN_REVISON_PARAM = new ParameterMatcher("amp;revision=");
52     private static final Splitter AMP_SPLITTER = Splitter.on('&');
53     private static final Predicate<String> CONTAINS_REVISION = new Predicate<String>() {
54         @Override
55         public boolean apply(final String input) {
56             return input.contains("revision=");
57         }
58     };
59
60     private final Set<QName> moduleBasedCaps;
61     private final Set<String> nonModuleCaps;
62
63     private NetconfSessionPreferences(final Set<String> nonModuleCaps, final Set<QName> moduleBasedCaps) {
64         this.nonModuleCaps = Preconditions.checkNotNull(nonModuleCaps);
65         this.moduleBasedCaps = Preconditions.checkNotNull(moduleBasedCaps);
66     }
67
68     public Set<QName> getModuleBasedCaps() {
69         return moduleBasedCaps;
70     }
71
72     public Set<String> getNonModuleCaps() {
73         return nonModuleCaps;
74     }
75
76     public boolean containsNonModuleCapability(final String capability) {
77         return nonModuleCaps.contains(capability);
78     }
79
80     public boolean containsModuleCapability(final QName capability) {
81         return moduleBasedCaps.contains(capability);
82     }
83
84     @Override
85     public String toString() {
86         return Objects.toStringHelper(this)
87                 .add("capabilities", nonModuleCaps)
88                 .add("moduleBasedCapabilities", moduleBasedCaps)
89                 .add("rollback", isRollbackSupported())
90                 .add("monitoring", isMonitoringSupported())
91                 .add("candidate", isCandidateSupported())
92                 .add("writableRunning", isRunningWritable())
93                 .toString();
94     }
95
96     public boolean isRollbackSupported() {
97         return containsNonModuleCapability(NetconfMessageTransformUtil.NETCONF_ROLLBACK_ON_ERROR_URI.toString());
98     }
99
100     public boolean isCandidateSupported() {
101         return containsNonModuleCapability(NetconfMessageTransformUtil.NETCONF_CANDIDATE_URI.toString());
102     }
103
104     public boolean isRunningWritable() {
105         return containsNonModuleCapability(NetconfMessageTransformUtil.NETCONF_RUNNING_WRITABLE_URI.toString());
106     }
107
108     public boolean isMonitoringSupported() {
109         return containsModuleCapability(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING)
110                 || containsNonModuleCapability(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
111     }
112
113     public NetconfSessionPreferences replaceModuleCaps(final NetconfSessionPreferences netconfSessionModuleCapabilities) {
114         final Set<QName> moduleBasedCaps = Sets.newHashSet(netconfSessionModuleCapabilities.getModuleBasedCaps());
115
116         // Preserve monitoring module, since it indicates support for ietf-netconf-monitoring
117         if(containsModuleCapability(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING)) {
118             moduleBasedCaps.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING);
119         }
120         return new NetconfSessionPreferences(getNonModuleCaps(), moduleBasedCaps);
121     }
122
123     public static NetconfSessionPreferences fromNetconfSession(final NetconfClientSession session) {
124         return fromStrings(session.getServerCapabilities());
125     }
126
127     private static QName cachedQName(final String namespace, final String revision, final String moduleName) {
128         return QName.cachedReference(QName.create(namespace, revision, moduleName));
129     }
130
131     private static QName cachedQName(final String namespace, final String moduleName) {
132         return QName.cachedReference(QName.create(URI.create(namespace), null, moduleName).withoutRevision());
133     }
134
135     public static NetconfSessionPreferences fromStrings(final Collection<String> capabilities) {
136         final Set<QName> moduleBasedCaps = new HashSet<>();
137         final Set<String> nonModuleCaps = Sets.newHashSet(capabilities);
138
139         for (final String capability : capabilities) {
140             final int qmark = capability.indexOf('?');
141             if (qmark == -1) {
142                 continue;
143             }
144
145             final String namespace = capability.substring(0, qmark);
146             final Iterable<String> queryParams = AMP_SPLITTER.split(capability.substring(qmark + 1));
147             final String moduleName = MODULE_PARAM.from(queryParams);
148             if (moduleName == null) {
149                 continue;
150             }
151
152             String revision = REVISION_PARAM.from(queryParams);
153             if (revision != null) {
154                 addModuleQName(moduleBasedCaps, nonModuleCaps, capability, cachedQName(namespace, revision, moduleName));
155                 continue;
156             }
157
158             /*
159              * We have seen devices which mis-escape revision, but the revision may not
160              * even be there. First check if there is a substring that matches revision.
161              */
162             if (Iterables.any(queryParams, CONTAINS_REVISION)) {
163
164                 LOG.debug("Netconf device was not reporting revision correctly, trying to get amp;revision=");
165                 revision = BROKEN_REVISON_PARAM.from(queryParams);
166                 if (revision == null) {
167                     LOG.warn("Netconf device returned revision incorrectly escaped for {}, ignoring it", capability);
168                     addModuleQName(moduleBasedCaps, nonModuleCaps, capability, cachedQName(namespace, moduleName));
169                 } else {
170                     addModuleQName(moduleBasedCaps, nonModuleCaps, capability, cachedQName(namespace, revision, moduleName));
171                 }
172                 continue;
173             }
174
175             // Fallback, no revision provided for module
176             addModuleQName(moduleBasedCaps, nonModuleCaps, capability, cachedQName(namespace, moduleName));
177         }
178
179         return new NetconfSessionPreferences(ImmutableSet.copyOf(nonModuleCaps), ImmutableSet.copyOf(moduleBasedCaps));
180     }
181
182
183     private static void addModuleQName(final Set<QName> moduleBasedCaps, final Set<String> nonModuleCaps, final String capability, final QName qName) {
184         moduleBasedCaps.add(qName);
185         nonModuleCaps.remove(capability);
186     }
187
188     private NetconfDeviceCapabilities capabilities = new NetconfDeviceCapabilities();
189
190     public NetconfDeviceCapabilities getNetconfDeviceCapabilities() {
191         return capabilities;
192     }
193
194
195 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.