Merge "BUG 2743 - Added support for runtime RPC's to netconf mdsal northbound."
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / netconf / mdsal / connector / MdsalNetconfOperationServiceFactory.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.mdsal.connector;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashSet;
16 import java.util.Set;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
19 import org.opendaylight.controller.netconf.api.Capability;
20 import org.opendaylight.controller.netconf.api.monitoring.CapabilityListener;
21 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
22 import org.opendaylight.controller.netconf.util.capability.BasicCapability;
23 import org.opendaylight.controller.netconf.util.capability.YangModuleCapability;
24 import org.opendaylight.controller.sal.core.api.Broker.ConsumerSession;
25 import org.opendaylight.controller.sal.core.api.Consumer;
26 import org.opendaylight.controller.sal.core.api.model.SchemaService;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class MdsalNetconfOperationServiceFactory implements NetconfOperationServiceFactory, Consumer, AutoCloseable {
33
34     private static final Logger LOG = LoggerFactory.getLogger(MdsalNetconfOperationServiceFactory.class);
35
36     private ConsumerSession session = null;
37     private DOMDataBroker dataBroker = null;
38     private DOMRpcService rpcService = null;
39     private final CurrentSchemaContext currentSchemaContext;
40
41     public MdsalNetconfOperationServiceFactory(final SchemaService schemaService) {
42         this.currentSchemaContext = new CurrentSchemaContext(Preconditions.checkNotNull(schemaService));
43     }
44
45     @Override
46     public MdsalNetconfOperationService createService(final String netconfSessionIdForReporting) {
47         Preconditions.checkState(dataBroker != null, "MD-SAL provider not yet initialized");
48         return new MdsalNetconfOperationService(currentSchemaContext, netconfSessionIdForReporting, dataBroker, rpcService);
49     }
50
51     @Override
52     public void close() throws Exception {
53         currentSchemaContext.close();
54     }
55
56     @Override
57     public Set<Capability> getCapabilities() {
58         return transformCapabilities(currentSchemaContext.getCurrentContext());
59     }
60
61     static Set<Capability> transformCapabilities(final SchemaContext currentContext) {
62         final Set<Capability> capabilities = new HashSet<>();
63         // [RFC6241] 8.3.  Candidate Configuration Capability
64         capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
65
66         final Set<Module> modules = currentContext.getModules();
67         for (final Module module : modules) {
68             Optional<YangModuleCapability> cap = moduleToCapability(module);
69             if(cap.isPresent()) {
70                 capabilities.add(cap.get());
71             }
72             for (final Module submodule : module.getSubmodules()) {
73                 cap = moduleToCapability(submodule);
74                 if(cap.isPresent()) {
75                     capabilities.add(cap.get());
76                 }
77             }
78         }
79
80         return capabilities;
81     }
82
83     private static Optional<YangModuleCapability> moduleToCapability(final Module module) {
84         final String source = module.getSource();
85         if(source !=null) {
86             return Optional.of(new YangModuleCapability(module, source));
87         } else {
88             LOG.warn("Missing source for module {}. This module will not be available from netconf server",
89                     module);
90         }
91         return Optional.absent();
92     }
93
94     @Override
95     public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
96         return currentSchemaContext.registerCapabilityListener(listener);
97     }
98
99     @Override
100     public void onSessionInitiated(ConsumerSession session) {
101         this.session = Preconditions.checkNotNull(session);
102         this.dataBroker = this.session.getService(DOMDataBroker.class);
103         this.rpcService = this.session.getService(DOMRpcService.class);
104     }
105
106     @Override
107     public Collection<ConsumerFunctionality> getConsumerFunctionality() {
108         return Collections.emptySet();
109     }
110 }

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