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.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 com.google.common.collect.Sets;
17 import java.util.Collections;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.controller.config.util.capability.Capability;
22 import org.opendaylight.netconf.api.monitoring.NetconfManagementSession;
23 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
24 import org.opendaylight.netconf.api.monitoring.SessionEvent;
25 import org.opendaylight.netconf.api.monitoring.SessionListener;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.CapabilitiesBuilder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SchemasBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Sessions;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SessionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema.Location;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema.Location.Enumeration;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.SchemaBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.SchemaKey;
40 public class DummyMonitoringService implements NetconfMonitoringService {
42 private static final Sessions EMPTY_SESSIONS = new SessionsBuilder().setSession(Collections.emptyList()).build();
43 private static final Function<Capability, Uri> CAPABILITY_URI_FUNCTION =
44 capability -> new Uri(capability.getCapabilityUri());
46 private static final Function<Capability, Schema> CAPABILITY_SCHEMA_FUNCTION = new Function<Capability, Schema>() {
49 public Schema apply(@Nonnull Capability capability) {
50 return new SchemaBuilder()
51 .setIdentifier(capability.getModuleName().get())
52 .setNamespace(new Uri(capability.getModuleNamespace().get()))
53 .setFormat(Yang.class)
54 .setVersion(capability.getRevision().get())
55 .setLocation(Collections.singletonList(new Location(Enumeration.NETCONF)))
56 .setKey(new SchemaKey(Yang.class, capability.getModuleName().get(), capability.getRevision().get()))
61 private final Capabilities capabilities;
62 private final ArrayListMultimap<String, Capability> capabilityMultiMap;
63 private final Schemas schemas;
65 public DummyMonitoringService(Set<Capability> capabilities) {
67 this.capabilities = new CapabilitiesBuilder().setCapability(
68 Lists.newArrayList(Collections2.transform(capabilities, CAPABILITY_URI_FUNCTION))).build();
70 Set<Capability> moduleCapabilities = Sets.newHashSet();
71 this.capabilityMultiMap = ArrayListMultimap.create();
72 for (Capability cap : capabilities) {
73 if (cap.getModuleName().isPresent()) {
74 capabilityMultiMap.put(cap.getModuleName().get(), cap);
75 moduleCapabilities.add(cap);
79 this.schemas = new SchemasBuilder().setSchema(
80 Lists.newArrayList(Collections2.transform(moduleCapabilities, CAPABILITY_SCHEMA_FUNCTION))).build();
84 public Sessions getSessions() {
85 return EMPTY_SESSIONS;
89 public SessionListener getSessionListener() {
90 return new SessionListener() {
92 public void onSessionUp(NetconfManagementSession session) {
97 public void onSessionDown(NetconfManagementSession session) {
102 public void onSessionEvent(SessionEvent event) {
109 public Schemas getSchemas() {
114 public String getSchemaForCapability(String moduleName, Optional<String> revision) {
116 for (Capability capability : capabilityMultiMap.get(moduleName)) {
117 if (capability.getRevision().get().equals(revision.get())) {
118 return capability.getCapabilitySchema().get();
121 throw new IllegalArgumentException(
122 "Module with name: " + moduleName + " and revision: " + revision + " does not exist");
126 public Capabilities getCapabilities() {
131 public AutoCloseable registerCapabilitiesListener(CapabilitiesListener listener) {
136 public AutoCloseable registerSessionsListener(SessionsListener listener) {