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