Fix findbugs violations in netconf
[netconf.git] / netconf / tools / netconf-testtool / src / main / java / org / opendaylight / netconf / test / tool / DummyMonitoringService.java
1 /*
2  * Copyright (c) 2015 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.netconf.test.tool;
10
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;
18 import java.util.Set;
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;
39
40 public class DummyMonitoringService implements NetconfMonitoringService {
41
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());
45
46     private static final Function<Capability, Schema> CAPABILITY_SCHEMA_FUNCTION = new Function<Capability, Schema>() {
47         @Nullable
48         @Override
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()))
57                     .build();
58         }
59     };
60
61     private final Capabilities capabilities;
62     private final ArrayListMultimap<String, Capability> capabilityMultiMap;
63     private final Schemas schemas;
64
65     public DummyMonitoringService(Set<Capability> capabilities) {
66
67         this.capabilities = new CapabilitiesBuilder().setCapability(
68                 Lists.newArrayList(Collections2.transform(capabilities, CAPABILITY_URI_FUNCTION))).build();
69
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);
76             }
77         }
78
79         this.schemas = new SchemasBuilder().setSchema(
80             Lists.newArrayList(Collections2.transform(moduleCapabilities, CAPABILITY_SCHEMA_FUNCTION))).build();
81     }
82
83     @Override
84     public Sessions getSessions() {
85         return EMPTY_SESSIONS;
86     }
87
88     @Override
89     public SessionListener getSessionListener() {
90         return new SessionListener() {
91             @Override
92             public void onSessionUp(NetconfManagementSession session) {
93                 //no op
94             }
95
96             @Override
97             public void onSessionDown(NetconfManagementSession session) {
98                 //no op
99             }
100
101             @Override
102             public void onSessionEvent(SessionEvent event) {
103                 //no op
104             }
105         };
106     }
107
108     @Override
109     public Schemas getSchemas() {
110         return schemas;
111     }
112
113     @Override
114     public String getSchemaForCapability(String moduleName, Optional<String> revision) {
115
116         for (Capability capability : capabilityMultiMap.get(moduleName)) {
117             if (capability.getRevision().get().equals(revision.get())) {
118                 return capability.getCapabilitySchema().get();
119             }
120         }
121         throw new IllegalArgumentException(
122             "Module with name: " + moduleName + " and revision: " + revision + " does not exist");
123     }
124
125     @Override
126     public Capabilities getCapabilities() {
127         return capabilities;
128     }
129
130     @Override
131     public AutoCloseable registerCapabilitiesListener(CapabilitiesListener listener) {
132         return null;
133     }
134
135     @Override
136     public AutoCloseable registerSessionsListener(SessionsListener listener) {
137         return null;
138     }
139
140 }