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

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