Merge "DOMDataTree APIs"
[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.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.sal.core.api.model.SchemaService;
22 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class MdsalNetconfOperationServiceFactory implements NetconfOperationServiceFactory, AutoCloseable {
29
30     private static final Logger LOG = LoggerFactory.getLogger(MdsalNetconfOperationServiceFactory.class);
31
32     private final DOMDataBroker dataBroker;
33     private final CurrentSchemaContext currentSchemaContext;
34
35     public MdsalNetconfOperationServiceFactory(final SchemaService schemaService, final DOMDataBroker domDataBroker) {
36         this.currentSchemaContext = new CurrentSchemaContext(Preconditions.checkNotNull(schemaService));
37         this.dataBroker = Preconditions.checkNotNull(domDataBroker);
38     }
39
40     @Override
41     public MdsalNetconfOperationService createService(final String netconfSessionIdForReporting) {
42         return new MdsalNetconfOperationService(currentSchemaContext, netconfSessionIdForReporting, dataBroker);
43     }
44
45     @Override
46     public void close() throws Exception {
47         currentSchemaContext.close();
48     }
49
50     @Override
51     public Set<Capability> getCapabilities() {
52         final Set<Capability> capabilities = new HashSet<>();
53         // [RFC6241] 8.3.  Candidate Configuration Capability
54         capabilities.add(new BasicCapability("urn:ietf:params:netconf:capability:candidate:1.0"));
55
56         final SchemaContext currentContext = currentSchemaContext.getCurrentContext();
57         final Set<Module> modules = currentContext.getModules();
58         for (final Module module : modules) {
59             if(currentContext.getModuleSource(module).isPresent()) {
60                 capabilities.add(new YangStoreCapability(module, currentContext.getModuleSource(module).get()));
61             } else {
62                 LOG.warn("Missing source for module {}. This module will not be available from netconf server",
63                         module);
64             }
65         }
66
67         return capabilities;
68     }
69
70     @Override
71     public AutoCloseable registerCapabilityListener(final CapabilityListener listener) {
72         // TODO provide notifications about changed schemas
73         return new AutoCloseable() {
74             @Override
75             public void close() throws Exception {
76
77             }
78         };
79     }
80
81     private static class BasicCapability implements Capability {
82
83         private final String capability;
84
85         private BasicCapability(final String capability) {
86             this.capability = capability;
87         }
88
89         @Override
90         public String getCapabilityUri() {
91             return capability;
92         }
93
94         @Override
95         public Optional<String> getModuleNamespace() {
96             return Optional.absent();
97         }
98
99         @Override
100         public Optional<String> getModuleName() {
101             return Optional.absent();
102         }
103
104         @Override
105         public Optional<String> getRevision() {
106             return Optional.absent();
107         }
108
109         @Override
110         public Optional<String> getCapabilitySchema() {
111             return Optional.absent();
112         }
113
114         @Override
115         public Collection<String> getLocation() {
116             return Collections.emptyList();
117         }
118
119         @Override
120         public String toString() {
121             return capability;
122         }
123     }
124
125     private static final class YangStoreCapability extends BasicCapability {
126
127         private final String content;
128         private final String revision;
129         private final String moduleName;
130         private final String moduleNamespace;
131
132         public YangStoreCapability(final Module module, final String moduleContent) {
133             super(toCapabilityURI(module));
134             this.content = moduleContent;
135             this.moduleName = module.getName();
136             this.moduleNamespace = module.getNamespace().toString();
137             this.revision = SimpleDateFormatUtil.getRevisionFormat().format(module.getRevision());
138         }
139
140         @Override
141         public Optional<String> getCapabilitySchema() {
142             return Optional.of(content);
143         }
144
145         private static String toCapabilityURI(final Module module) {
146             return String.valueOf(module.getNamespace()) + "?module="
147                     + module.getName() + "&revision=" + SimpleDateFormatUtil.getRevisionFormat().format(module.getRevision());
148         }
149
150         @Override
151         public Optional<String> getModuleName() {
152             return Optional.of(moduleName);
153         }
154
155         @Override
156         public Optional<String> getModuleNamespace() {
157             return Optional.of(moduleNamespace);
158         }
159
160         @Override
161         public Optional<String> getRevision() {
162             return Optional.of(revision);
163         }
164     }
165 }

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