2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.test.tool;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.collect.ArrayListMultimap;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.Lists;
16 import java.util.Collections;
18 import javax.annotation.Nullable;
19 import org.opendaylight.controller.netconf.api.Capability;
20 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
21 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.CapabilitiesBuilder;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SchemasBuilder;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Sessions;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SessionsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema.Location;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema.Location.Enumeration;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.SchemaBuilder;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.SchemaKey;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
37 public class DummyMonitoringService implements NetconfMonitoringService {
39 private static final Sessions EMPTY_SESSIONS = new SessionsBuilder().setSession(Collections.<Session>emptyList()).build();
40 private static final Function<Capability, Uri> CAPABILITY_URI_FUNCTION = new Function<Capability, Uri>() {
43 public Uri apply(Capability capability) {
44 return new Uri(capability.getCapabilityUri());
48 private static final Function<Capability, Schema> CAPABILITY_SCHEMA_FUNCTION = new Function<Capability, Schema>() {
51 public Schema apply(@Nullable Capability capability) {
52 return new SchemaBuilder()
53 .setIdentifier(capability.getModuleName().get())
54 .setNamespace(new Uri(capability.getModuleNamespace().get()))
55 .setFormat(Yang.class)
56 .setVersion(capability.getRevision().get())
57 .setLocation(Collections.singletonList(new Location(Enumeration.NETCONF)))
58 .setKey(new SchemaKey(Yang.class, capability.getModuleName().get(), capability.getRevision().get())).build();
62 private final Capabilities capabilities;
63 private final ArrayListMultimap<String, Capability> capabilityMultiMap;
64 private final Schemas schemas;
66 public DummyMonitoringService(Set<Capability> capabilities) {
68 this.capabilities = new CapabilitiesBuilder().setCapability(
69 Lists.newArrayList(Collections2.transform(capabilities, CAPABILITY_URI_FUNCTION))).build();
71 this.capabilityMultiMap = ArrayListMultimap.create();
72 for (Capability cap : capabilities) {
73 capabilityMultiMap.put(cap.getModuleName().get(), cap);
76 this.schemas = new SchemasBuilder().setSchema(Lists.newArrayList(Collections2.transform(capabilities, CAPABILITY_SCHEMA_FUNCTION))).build();
80 public Sessions getSessions() {
81 return EMPTY_SESSIONS;
85 public Schemas getSchemas() {
90 public String getSchemaForCapability(String moduleName, Optional<String> revision) {
92 for (Capability capability : capabilityMultiMap.get(moduleName)) {
93 if (capability.getRevision().get().equals(revision.get())) {
94 return capability.getCapabilitySchema().get();
97 throw new IllegalArgumentException("Module with name: " + moduleName + " and revision: " + revision + " does not exist");
101 public Capabilities getCapabilities() {
106 public AutoCloseable registerListener(MonitoringListener listener) {
111 public void onCapabilitiesAdded(Set<Capability> addedCaps) {
116 public void onCapabilitiesRemoved(Set<Capability> removedCaps) {
121 public void onSessionUp(NetconfManagementSession session) {
126 public void onSessionDown(NetconfManagementSession session) {