Merge "Fix a race PingPongTransactionChain"
[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 com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Sets;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20 import org.opendaylight.controller.config.api.LookupRegistry;
21 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
22 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
23 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
24 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
25 import org.opendaylight.controller.netconf.mapping.api.Capability;
26 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
27 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29
30 /**
31  * Manages life cycle of {@link YangStoreSnapshot}.
32  */
33 public class NetconfOperationServiceImpl implements NetconfOperationService {
34
35     private final YangStoreSnapshot yangStoreSnapshot;
36     private final NetconfOperationProvider operationProvider;
37     private final Set<Capability> capabilities;
38     private final TransactionProvider transactionProvider;
39
40     public NetconfOperationServiceImpl(final YangStoreService yangStoreService, final ConfigRegistryJMXClient jmxClient,
41             final String netconfSessionIdForReporting) throws YangStoreException {
42
43         yangStoreSnapshot = yangStoreService.getYangStoreSnapshot();
44         checkConsistencyBetweenYangStoreAndConfig(jmxClient, yangStoreSnapshot);
45
46         transactionProvider = new TransactionProvider(jmxClient, netconfSessionIdForReporting);
47         operationProvider = new NetconfOperationProvider(yangStoreSnapshot, jmxClient, transactionProvider,
48                 netconfSessionIdForReporting);
49         capabilities = setupCapabilities(yangStoreSnapshot);
50     }
51
52
53     @VisibleForTesting
54     static void checkConsistencyBetweenYangStoreAndConfig(final LookupRegistry jmxClient, final YangStoreSnapshot yangStoreSnapshot) {
55         Set<String> missingModulesFromConfig = Sets.newHashSet();
56
57         Set<String> modulesSeenByConfig = jmxClient.getAvailableModuleFactoryQNames();
58         Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = yangStoreSnapshot.getModuleMXBeanEntryMap();
59
60         for (Map<String, ModuleMXBeanEntry> moduleNameToMBE : moduleMXBeanEntryMap.values()) {
61             for (ModuleMXBeanEntry moduleMXBeanEntry : moduleNameToMBE.values()) {
62                 String moduleSeenByYangStore = moduleMXBeanEntry.getYangModuleQName().toString();
63                 if(!modulesSeenByConfig.contains(moduleSeenByYangStore)){
64                     missingModulesFromConfig.add(moduleSeenByYangStore);
65                 }
66             }
67         }
68
69         Preconditions
70                 .checkState(
71                         missingModulesFromConfig.isEmpty(),
72                         "There are inconsistencies between configuration subsystem and yangstore in terms of discovered yang modules, yang modules missing from config subsystem but present in yangstore: %s, %sAll modules present in config: %s",
73                         missingModulesFromConfig, System.lineSeparator(), modulesSeenByConfig);
74
75     }
76
77     @Override
78     public void close() {
79         yangStoreSnapshot.close();
80         transactionProvider.close();
81     }
82
83     @Override
84     public Set<Capability> getCapabilities() {
85         return capabilities;
86     }
87
88     @Override
89     public Set<NetconfOperation> getNetconfOperations() {
90         return operationProvider.getOperations();
91     }
92
93     private static Set<Capability> setupCapabilities(final YangStoreSnapshot yangStoreSnapshot) {
94         Set<Capability> capabilities = new HashSet<>();
95         // [RFC6241] 8.3.  Candidate Configuration Capability
96         capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
97
98         // TODO rollback on error not supported EditConfigXmlParser:100
99         // [RFC6241] 8.5.  Rollback-on-Error Capability
100         // capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:rollback-on-error:1.0"));
101
102         Set<Module> modules = yangStoreSnapshot.getModules();
103         for (Module module : modules) {
104             capabilities.add(new YangStoreCapability(module, yangStoreSnapshot.getModuleSource(module)));
105         }
106
107         return capabilities;
108     }
109
110     private static class BasicCapability implements Capability {
111
112         private final String capability;
113
114         private BasicCapability(final String capability) {
115             this.capability = capability;
116         }
117
118         @Override
119         public String getCapabilityUri() {
120             return capability;
121         }
122
123         @Override
124         public Optional<String> getModuleNamespace() {
125             return Optional.absent();
126         }
127
128         @Override
129         public Optional<String> getModuleName() {
130             return Optional.absent();
131         }
132
133         @Override
134         public Optional<String> getRevision() {
135             return Optional.absent();
136         }
137
138         @Override
139         public Optional<String> getCapabilitySchema() {
140             return Optional.absent();
141         }
142
143         @Override
144         public Collection<String> getLocation() {
145             return Collections.emptyList();
146         }
147
148         @Override
149         public String toString() {
150             return capability;
151         }
152     }
153
154     private static final class YangStoreCapability extends BasicCapability {
155
156         private final String content;
157         private final String revision;
158         private final String moduleName;
159         private final String moduleNamespace;
160
161         public YangStoreCapability(final Module module, final String moduleContent) {
162             super(toCapabilityURI(module));
163             this.content = moduleContent;
164             this.moduleName = module.getName();
165             this.moduleNamespace = module.getNamespace().toString();
166             this.revision = Util.writeDate(module.getRevision());
167         }
168
169         @Override
170         public Optional<String> getCapabilitySchema() {
171             return Optional.of(content);
172         }
173
174         private static String toCapabilityURI(final Module module) {
175             return String.valueOf(module.getNamespace()) + "?module="
176                     + module.getName() + "&revision=" + Util.writeDate(module.getRevision());
177         }
178
179         @Override
180         public Optional<String> getModuleName() {
181             return Optional.of(moduleName);
182         }
183
184         @Override
185         public Optional<String> getModuleNamespace() {
186             return Optional.of(moduleNamespace);
187         }
188
189         @Override
190         public Optional<String> getRevision() {
191             return Optional.of(revision);
192         }
193     }
194 }

©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.