4883042ba388a11e1c0a371f76c9d2f82426f3af
[netconf.git] / apps / netconf-topology-impl / src / main / java / org / opendaylight / netconf / topology / impl / NetconfTopologyImpl.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 package org.opendaylight.netconf.topology.impl;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.util.List;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.aaa.encrypt.AAAEncryptionService;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
18 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
20 import org.opendaylight.mdsal.binding.api.RpcProviderService;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
23 import org.opendaylight.netconf.client.NetconfClientFactory;
24 import org.opendaylight.netconf.client.mdsal.api.BaseNetconfSchemas;
25 import org.opendaylight.netconf.client.mdsal.api.DeviceActionFactory;
26 import org.opendaylight.netconf.client.mdsal.api.SchemaResourceManager;
27 import org.opendaylight.netconf.common.NetconfTimer;
28 import org.opendaylight.netconf.topology.spi.AbstractNetconfTopology;
29 import org.opendaylight.netconf.topology.spi.NetconfClientConfigurationBuilderFactory;
30 import org.opendaylight.netconf.topology.spi.NetconfNodeUtils;
31 import org.opendaylight.netconf.topology.spi.NetconfTopologyRPCProvider;
32 import org.opendaylight.netconf.topology.spi.NetconfTopologySchemaAssembler;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yangtools.concepts.Registration;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.osgi.service.component.annotations.Activate;
41 import org.osgi.service.component.annotations.Component;
42 import org.osgi.service.component.annotations.Deactivate;
43 import org.osgi.service.component.annotations.Reference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 // Non-final for testing
48 @Singleton
49 @Component(service = { })
50 public class NetconfTopologyImpl extends AbstractNetconfTopology
51         implements DataTreeChangeListener<Node>, AutoCloseable {
52     private static final Logger LOG = LoggerFactory.getLogger(NetconfTopologyImpl.class);
53
54     private Registration dtclReg;
55     private NetconfTopologyRPCProvider rpcProvider;
56
57     @Inject
58     @Activate
59     public NetconfTopologyImpl(
60             @Reference final NetconfClientFactory clientFactory, @Reference final NetconfTimer timer,
61             @Reference final NetconfTopologySchemaAssembler schemaAssembler,
62             @Reference final SchemaResourceManager schemaRepositoryProvider, @Reference final DataBroker dataBroker,
63             @Reference final DOMMountPointService mountPointService,
64             @Reference final AAAEncryptionService encryptionService,
65             @Reference final NetconfClientConfigurationBuilderFactory builderFactory,
66             @Reference final RpcProviderService rpcProviderService, @Reference final BaseNetconfSchemas baseSchemas,
67             @Reference final DeviceActionFactory deviceActionFactory) {
68         this(NetconfNodeUtils.DEFAULT_TOPOLOGY_NAME, clientFactory, timer, schemaAssembler, schemaRepositoryProvider,
69             dataBroker, mountPointService, encryptionService, builderFactory, rpcProviderService, baseSchemas,
70             deviceActionFactory);
71     }
72
73     public NetconfTopologyImpl(final String topologyId, final NetconfClientFactory clientclientFactory,
74             final NetconfTimer timer, final NetconfTopologySchemaAssembler schemaAssembler,
75             final SchemaResourceManager schemaRepositoryProvider, final DataBroker dataBroker,
76             final DOMMountPointService mountPointService, final AAAEncryptionService encryptionService,
77             final NetconfClientConfigurationBuilderFactory builderFactory, final RpcProviderService rpcProviderService,
78             final BaseNetconfSchemas baseSchemas) {
79         this(topologyId, clientclientFactory, timer, schemaAssembler, schemaRepositoryProvider, dataBroker,
80             mountPointService, encryptionService, builderFactory, rpcProviderService, baseSchemas, null);
81     }
82
83     @SuppressFBWarnings(value = "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR",
84         justification = "DTCL registration of 'this'")
85     public NetconfTopologyImpl(final String topologyId, final NetconfClientFactory clientFactory,
86             final NetconfTimer timer, final NetconfTopologySchemaAssembler schemaAssembler,
87             final SchemaResourceManager schemaRepositoryProvider, final DataBroker dataBroker,
88             final DOMMountPointService mountPointService, final AAAEncryptionService encryptionService,
89             final NetconfClientConfigurationBuilderFactory builderFactory, final RpcProviderService rpcProviderService,
90             final BaseNetconfSchemas baseSchemas, final DeviceActionFactory deviceActionFactory) {
91         super(topologyId, clientFactory, timer, schemaAssembler, schemaRepositoryProvider, dataBroker,
92             mountPointService, builderFactory, deviceActionFactory, baseSchemas);
93
94         LOG.debug("Registering datastore listener");
95         dtclReg = dataBroker.registerLegacyTreeChangeListener(DataTreeIdentifier.of(
96             LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.builder(NetworkTopology.class)
97                 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
98                 .child(Node.class)
99                 .build()), this);
100         rpcProvider = new NetconfTopologyRPCProvider(rpcProviderService, dataBroker, encryptionService, topologyId);
101     }
102
103     @PreDestroy
104     @Deactivate
105     @Override
106     public void close() {
107         if (rpcProvider != null) {
108             rpcProvider.close();
109             rpcProvider = null;
110         }
111
112         // close all existing connectors, delete whole topology in datastore?
113         deleteAllNodes();
114
115         if (dtclReg != null) {
116             dtclReg.close();
117             dtclReg = null;
118         }
119     }
120
121     @Override
122     public void onDataTreeChanged(final List<DataTreeModification<Node>> changes) {
123         for (var change : changes) {
124             final var rootNode = change.getRootNode();
125             final var modType = rootNode.modificationType();
126             switch (modType) {
127                 case SUBTREE_MODIFIED -> ensureNode("updated", rootNode.dataAfter());
128                 case WRITE -> ensureNode("created", rootNode.dataAfter());
129                 case DELETE -> {
130                     final var nodeId = InstanceIdentifier.keyOf(change.getRootPath().path()).getNodeId();
131                     LOG.debug("Config for node {} deleted", nodeId);
132                     deleteNode(nodeId);
133                 }
134                 default -> LOG.debug("Unsupported modification type: {}.", modType);
135             }
136         }
137     }
138
139     private void ensureNode(final String operation, final Node node) {
140         final var nodeId = node.getNodeId();
141         LOG.debug("Config for node {} {}", nodeId, operation);
142         ensureNode(node);
143     }
144 }