2  * ============LICENSE_START=======================================================
 
   3  * ONAP : ccsdk features
 
   4  * ================================================================================
 
   5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
 
   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
 
  12  *     http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  22 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access;
 
  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.rev150114.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;
 
  51 public class NetconfCommunicatorManager {
 
  53     private static final Logger LOG = LoggerFactory.getLogger(NetconfCommunicatorManager.class);
 
  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())));
 
  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())))
 
  64     private final MountPointService mountPointService;
 
  65     private final DOMMountPointService domMountPointService;
 
  66     private final DomContext domContext;
 
  68     public NetconfCommunicatorManager(MountPointService mountPointService, DOMMountPointService domMountPointService,
 
  69             DomContext domContext) {
 
  71         this.mountPointService = mountPointService;
 
  72         this.domMountPointService = domMountPointService;
 
  73         this.domContext = domContext;
 
  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)));
 
  81         final Optional<MountPoint> optionalMountPoint = mountPointService.getMountPoint(instanceIdentifier);
 
  82         if (!optionalMountPoint.isPresent()) {
 
  83             LOG.warn("No mountpoint available for Netconf device :: Name : {} ", mountPointNodeName);
 
  85             final MountPoint mountPoint = optionalMountPoint.get();
 
  87             LOG.debug("Mountpoint with id: {} class:{}", mountPoint.getIdentifier(), mountPoint.getClass().getName());
 
  89             Optional<DataBroker> optionalNetconfNodeDatabroker = mountPoint.getService(DataBroker.class);
 
  91             if (!optionalNetconfNodeDatabroker.isPresent()) {
 
  92                 LOG.debug("Slave mountpoint {} without databroker", mountPointNodeName);
 
  94                 LOG.debug("Master mountpoint {}", mountPointNodeName);
 
  96                         new NetconfBindingAccessorImpl(accessor, optionalNetconfNodeDatabroker.get(), mountPoint));
 
  99         return Optional.empty();
 
 102     public Optional<NetconfDomAccessor> getNetconfDomAccessor(NetconfAccessorImpl accessor) {
 
 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")
 
 108                 .nodeWithKey(Node.QNAME, QName.create(Node.QNAME, "node-id").intern(), accessor.getNodeId().getValue())
 
 110         final Optional<DOMMountPoint> oMountPoint = domMountPointService.getMountPoint(mountpointPath);
 
 111         if (oMountPoint.isEmpty()) {
 
 112             return Optional.empty();
 
 115         final Optional<DOMDataBroker> domDataBroker = oMountPoint.get().getService(DOMDataBroker.class);
 
 116         if (domDataBroker.isPresent()) {
 
 118                     .of(new NetconfDomAccessorImpl(accessor, domDataBroker.get(), oMountPoint.get(), domContext));
 
 120         return Optional.empty();