Config-netconf: Remove unnecessary capability advertistments from hello message
[controller.git] / opendaylight / netconf / config-netconf-connector / src / main / java / org / opendaylight / controller / netconf / confignetconfconnector / osgi / NetconfOperationServiceImpl.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.osgi;
10
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.Map;
14 import java.util.Set;
15
16 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
17 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
18 import org.opendaylight.controller.config.yang.store.api.YangStoreService;
19 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
20 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
21 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
22 import org.opendaylight.controller.netconf.mapping.api.Capability;
23 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
24 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilter;
25 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27
28 import com.google.common.base.Optional;
29 import com.google.common.collect.Sets;
30
31 /**
32  * Manages life cycle of {@link YangStoreSnapshot}.
33  */
34 public class NetconfOperationServiceImpl implements NetconfOperationService {
35
36     private final YangStoreSnapshot yangStoreSnapshot;
37     private final NetconfOperationProvider operationProvider;
38     private final Set<Capability> capabilities;
39     private final TransactionProvider transactionProvider;
40
41     public NetconfOperationServiceImpl(YangStoreService yangStoreService, ConfigRegistryJMXClient jmxClient,
42             String netconfSessionIdForReporting) throws YangStoreException {
43
44         yangStoreSnapshot = yangStoreService.getYangStoreSnapshot();
45         transactionProvider = new TransactionProvider(jmxClient, netconfSessionIdForReporting);
46         operationProvider = new NetconfOperationProvider(yangStoreSnapshot, jmxClient, transactionProvider,
47                 netconfSessionIdForReporting);
48         capabilities = setupCapabilities(yangStoreSnapshot);
49     }
50
51     @Override
52     public void close() {
53         yangStoreSnapshot.close();
54         transactionProvider.close();
55     }
56
57     @Override
58     public Set<Capability> getCapabilities() {
59         return capabilities;
60     }
61
62     @Override
63     public Set<NetconfOperation> getNetconfOperations() {
64         return operationProvider.getOperations();
65     }
66
67     @Override
68     public Set<NetconfOperationFilter> getFilters() {
69         return Collections.emptySet();
70     }
71
72     private static Set<Capability> setupCapabilities(YangStoreSnapshot yangStoreSnapshot) {
73         Set<Capability> capabilities = Sets.newHashSet();
74
75         // [RFC6241] 8.3.  Candidate Configuration Capability
76         capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
77         // [RFC6241] 8.5.  Rollback-on-Error Capability
78         capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:rollback-on-error:1.0"));
79         // [RFC6022] get-schema RPC. TODO: implement rest of the RFC
80         capabilities.add(new BasicCapability("urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?module=ietf-netconf-monitoring&revision=2010-10-04"));
81
82         final Collection<Map.Entry<Module, String>> modulesAndContents = yangStoreSnapshot.getModuleMap().values();
83         for (Map.Entry<Module, String> moduleAndContent : modulesAndContents) {
84             capabilities.add(new YangStoreCapability(moduleAndContent));
85         }
86
87         return capabilities;
88     }
89
90     private static class BasicCapability implements Capability {
91
92         private final String capability;
93
94         private BasicCapability(String capability) {
95             this.capability = capability;
96         }
97
98         @Override
99         public String getCapabilityUri() {
100             return capability;
101         }
102
103         @Override
104         public Optional<String> getModuleName() {
105             return Optional.absent();
106         }
107
108         @Override
109         public Optional<String> getRevision() {
110             return Optional.absent();
111         }
112
113         @Override
114         public Optional<String> getCapabilitySchema() {
115             return Optional.absent();
116         }
117     }
118
119     private static class YangStoreCapability extends BasicCapability {
120
121         private final String content;
122         private final String revision;
123         private final String moduleName;
124
125         public YangStoreCapability(Map.Entry<Module, String> moduleAndContent) {
126             super(getAsString(moduleAndContent.getKey()));
127             this.content = moduleAndContent.getValue();
128             Module module = moduleAndContent.getKey();
129             this.moduleName = module.getName();
130             this.revision = Util.writeDate(module.getRevision());
131         }
132
133         @Override
134         public Optional<String> getCapabilitySchema() {
135             return Optional.of(content);
136         }
137
138         private static String getAsString(Module module) {
139             final StringBuffer capabilityContent = new StringBuffer();
140             capabilityContent.append(module.getNamespace());
141             capabilityContent.append("?module=");
142             capabilityContent.append(module.getName());
143             capabilityContent.append("&revision=");
144             capabilityContent.append(Util.writeDate(module.getRevision()));
145             return capabilityContent.toString();
146         }
147
148         @Override
149         public Optional<String> getModuleName() {
150             return Optional.of(moduleName);
151         }
152
153         @Override
154         public Optional<String> getRevision() {
155             return Optional.of(revision);
156         }
157     }
158 }