Update features for Argon release
[ccsdk/features.git] / sdnr / wt / netconfnode-state-service / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / netconfnodestateservice / impl / access / NetconfCommunicatorManager.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access;
23
24 import java.util.Optional;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
27 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
28 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.binding.NetconfBindingAccessorImpl;
29 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom.DomContext;
30 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom.NetconfDomAccessorImpl;
31 import org.opendaylight.mdsal.binding.api.DataBroker;
32 import org.opendaylight.mdsal.binding.api.MountPoint;
33 import org.opendaylight.mdsal.binding.api.MountPointService;
34 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
35 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
36 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.network.topology.topology.topology.types.TopologyNetconf;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.common.QName;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class NetconfCommunicatorManager {
52
53     private static final Logger LOG = LoggerFactory.getLogger(NetconfCommunicatorManager.class);
54
55     private static final @NonNull InstanceIdentifier<Topology> NETCONF_TOPO_IID =
56             InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
57                     new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
58
59     private static final @NonNull InstanceIdentifier<Node> NETCONF_NODE_TOPO_IID =
60             InstanceIdentifier.create(NetworkTopology.class)
61                     .child(Topology.class, new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())))
62                     .child(Node.class);
63
64     private final MountPointService mountPointService;
65     private final DOMMountPointService domMountPointService;
66     private final DomContext domContext;
67
68     public NetconfCommunicatorManager(MountPointService mountPointService, DOMMountPointService domMountPointService,
69             DomContext domContext) {
70         super();
71         this.mountPointService = mountPointService;
72         this.domMountPointService = domMountPointService;
73         this.domContext = domContext;
74     }
75
76     public Optional<NetconfBindingAccessor> getNetconfBindingAccessor(NetconfAccessorImpl accessor) {
77         String mountPointNodeName = accessor.getNodeId().getValue();
78         InstanceIdentifier<Node> instanceIdentifier =
79                 NETCONF_TOPO_IID.child(Node.class, new NodeKey(new NodeId(mountPointNodeName)));
80
81         final Optional<MountPoint> optionalMountPoint = mountPointService.getMountPoint(instanceIdentifier);
82         if (!optionalMountPoint.isPresent()) {
83             LOG.warn("No mountpoint available for Netconf device :: Name : {} ", mountPointNodeName);
84         } else {
85             final MountPoint mountPoint = optionalMountPoint.get();
86
87             LOG.debug("Mountpoint with id: {} class:{}", mountPoint.getIdentifier(), mountPoint.getClass().getName());
88
89             Optional<DataBroker> optionalNetconfNodeDatabroker = mountPoint.getService(DataBroker.class);
90
91             if (!optionalNetconfNodeDatabroker.isPresent()) {
92                 LOG.debug("Slave mountpoint {} without databroker", mountPointNodeName);
93             } else {
94                 LOG.debug("Master mountpoint {}", mountPointNodeName);
95                 return Optional.of(
96                         new NetconfBindingAccessorImpl(accessor, optionalNetconfNodeDatabroker.get(), mountPoint));
97             }
98         }
99         return Optional.empty();
100     }
101
102     public Optional<NetconfDomAccessor> getNetconfDomAccessor(NetconfAccessorImpl accessor) {
103
104         final YangInstanceIdentifier mountpointPath = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME)
105                 .node(Topology.QNAME)
106                 .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id").intern(), "topology-netconf")
107                 .node(Node.QNAME)
108                 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id").intern(), accessor.getNodeId().getValue())
109                 .build();
110         final Optional<DOMMountPoint> oMountPoint = domMountPointService.getMountPoint(mountpointPath);
111         if (oMountPoint.isEmpty()) {
112             return Optional.empty();
113         }
114
115         final Optional<DOMDataBroker> domDataBroker = oMountPoint.get().getService(DOMDataBroker.class);
116         if (domDataBroker.isPresent()) {
117             return Optional
118                     .of(new NetconfDomAccessorImpl(accessor, domDataBroker.get(), oMountPoint.get(), domContext));
119         }
120         return Optional.empty();
121     }
122
123 }