2  * ============LICENSE_START=======================================================
 
   3  * ONAP : ccsdk features
 
   4  * ================================================================================
 
   5  * Copyright (C) 2021 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.devicemanager.oran.impl.dom;
 
  24 import com.fasterxml.jackson.core.JsonProcessingException;
 
  25 import com.google.common.collect.Sets;
 
  26 import java.util.Collection;
 
  27 import java.util.Collections;
 
  28 import java.util.List;
 
  30 import java.util.Objects;
 
  31 import java.util.Optional;
 
  32 import org.eclipse.jdt.annotation.NonNull;
 
  33 import org.eclipse.jdt.annotation.Nullable;
 
  34 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
 
  35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElement;
 
  36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.ne.service.NetworkElementService;
 
  37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
 
  38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
 
  39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.NotificationService;
 
  40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
 
  41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESCommonEventHeaderPOJO;
 
  42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.VESPNFRegistrationFieldsPOJO;
 
  43 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
 
  44 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
 
  45 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
 
  46 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 
  47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
 
  48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamKey;
 
  49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough;
 
  50 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
 
  51 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.NetworkElementDeviceType;
 
  52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 
  53 import org.opendaylight.yangtools.yang.common.QName;
 
  54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
  55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 
  56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
 
  57 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 
  58 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 
  59 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 
  60 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
 
  61 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
  62 import org.slf4j.Logger;
 
  63 import org.slf4j.LoggerFactory;
 
  65 public class ORanDOMNetworkElement implements NetworkElement {
 
  67     private static final Logger LOG = LoggerFactory.getLogger(ORanDOMNetworkElement.class);
 
  69     private final @NonNull NetconfDomAccessor netconfDomAccessor;
 
  70     private final @NonNull DataProvider databaseService;
 
  71     private final @NonNull FaultService faultService;
 
  72     private final @NonNull NotificationService notificationService;
 
  73     private final @NonNull ORanDOMChangeNotificationListener oranDomChangeNotificationListener;
 
  74     private final @NonNull ORanDOMFaultNotificationListener oranDomFaultNotificationListener;
 
  75     private final @NonNull VESCollectorService vesCollectorService;
 
  76     private final @NonNull ORanRegistrationToVESpnfRegistrationMapper mapper;
 
  78     public ORanDOMNetworkElement(@NonNull NetconfDomAccessor netconfDomAccessor,
 
  79             @NonNull DeviceManagerServiceProvider serviceProvider) {
 
  80         LOG.debug("Create {}", ORanDOMNetworkElement.class.getSimpleName());
 
  81         this.netconfDomAccessor = Objects.requireNonNull(netconfDomAccessor);
 
  82         Objects.requireNonNull(serviceProvider);
 
  83         this.databaseService = serviceProvider.getDataProvider();
 
  84         this.vesCollectorService = serviceProvider.getVESCollectorService();
 
  85         this.faultService = serviceProvider.getFaultService();
 
  86         this.notificationService = serviceProvider.getNotificationService();
 
  88         this.oranDomChangeNotificationListener =
 
  89                 new ORanDOMChangeNotificationListener(netconfDomAccessor, vesCollectorService, databaseService);
 
  91         this.oranDomFaultNotificationListener =
 
  92                 new ORanDOMFaultNotificationListener(netconfDomAccessor, vesCollectorService,
 
  93                         serviceProvider.getFaultService(), serviceProvider.getWebsocketService(), databaseService);
 
  95         this.mapper = new ORanRegistrationToVESpnfRegistrationMapper(netconfDomAccessor, vesCollectorService);
 
  99     public void register() {
 
 100         Collection<MapEntryNode> componentList = initialReadFromNetworkElement();
 
 101         oranDomFaultNotificationListener.setComponentList(componentList);
 
 102         publishMountpointToVES(componentList);
 
 103         QName[] notifications = {ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE,
 
 104                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIRMED_COMMIT,
 
 105                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_SESSION_START,
 
 106                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_SESSION_END,
 
 107                 ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CAPABILITY_CHANGE};
 
 108         netconfDomAccessor.doRegisterNotificationListener(oranDomChangeNotificationListener, notifications);
 
 109         QName[] faultNotification = {ORanDeviceManagerQNames.ORAN_FM_ALARM_NOTIF};
 
 110         netconfDomAccessor.doRegisterNotificationListener(oranDomFaultNotificationListener, faultNotification);
 
 111         // Output notification streams to LOG
 
 112         @SuppressWarnings("unused")
 
 113         Map<StreamKey, Stream> streams = netconfDomAccessor.getNotificationStreamsAsMap();
 
 114         // Register to default stream
 
 115         netconfDomAccessor.invokeCreateSubscription();
 
 118     public Collection<MapEntryNode> initialReadFromNetworkElement() {
 
 119         Collection<MapEntryNode> componentMapEntries = null;
 
 120         NormalizedNode hwData = readHardware();
 
 122         if (hwData != null) {
 
 123             ContainerNode hwContainer = (ContainerNode) hwData;
 
 124             MapNode componentMap = (MapNode) hwContainer
 
 125                     .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST));
 
 126             if (componentMap != null) {
 
 127                 componentMapEntries = componentMap.body();
 
 128                 List<Inventory> inventoryList =
 
 129                         ORanDOMToInternalDataModel.getInventoryList(netconfDomAccessor.getNodeId(), hwData);
 
 130                 databaseService.writeInventory(netconfDomAccessor.getNodeId().getValue(), inventoryList);
 
 133             componentMapEntries = Collections.emptyList();
 
 136         Optional<Guicutthrough> oGuicutthrough = ORanDOMToInternalDataModel.getGuicutthrough(getOnapSystemData());
 
 137         if (oGuicutthrough.isPresent()) {
 
 138             databaseService.writeGuiCutThroughData(oGuicutthrough.get(), netconfDomAccessor.getNodeId().getValue());
 
 140         return componentMapEntries;
 
 144     public void deregister() {
 
 146          * if (oranDomChangeNotificationListener != null) {
 
 147          * this.oranDomChangeNotificationListener.close(); } if
 
 148          * (oRanFaultListenerRegistrationResult != null) {
 
 149          * this.oRanFaultListenerRegistrationResult.close(); } ;
 
 151         databaseService.clearGuiCutThroughEntriesOfNode(getMountpointId());
 
 155     public NodeId getNodeId() {
 
 156         return netconfDomAccessor.getNodeId();
 
 160     public NetworkElementDeviceType getDeviceType() {
 
 161         return NetworkElementDeviceType.ORAN;
 
 165     public <L extends NetworkElementService> Optional<L> getService(Class<L> clazz) {
 
 166         return Optional.empty();
 
 170     public void warmstart() {}
 
 173     public Optional<NetconfAccessor> getAcessor() {
 
 174         return Optional.of(netconfDomAccessor);
 
 179     private String getMountpointId() {
 
 180         return getNodeId().getValue();
 
 183     private NormalizedNode readHardware() {
 
 184         InstanceIdentifierBuilder hardwareIIDBuilder =
 
 185                 YangInstanceIdentifier.builder().node(ORanDeviceManagerQNames.IETF_HW_CONTAINER);
 
 187         Optional<NormalizedNode> oData =
 
 188                 netconfDomAccessor.readDataNode(LogicalDatastoreType.OPERATIONAL, hardwareIIDBuilder.build());
 
 189         if (oData.isPresent()) {
 
 197      * Read system data with GUI cut through information from device if ONAP_SYSTEM YANG is supported.
 
 199      * @return NormalizedNode data with GUI cut through information or null if not available.
 
 201     private @Nullable NormalizedNode getOnapSystemData() {
 
 202         LOG.debug("Get System1 for mountpoint {}", netconfDomAccessor.getNodeId().getValue());
 
 204         InstanceIdentifierBuilder ietfSystemIID =
 
 205                 YangInstanceIdentifier.builder().node(ORanDeviceManagerQNames.IETF_SYSTEM_CONTAINER);
 
 207         AugmentationIdentifier onapSystemIID = YangInstanceIdentifier.AugmentationIdentifier
 
 208                 .create(Sets.newHashSet(ORanDeviceManagerQNames.ONAP_SYSTEM_NAME,
 
 209                         ORanDeviceManagerQNames.ONAP_SYSTEM_WEB_UI, ORanDeviceManagerQNames.ONAP_SYSTEM_GEOLOCATION));
 
 210         InstanceIdentifierBuilder augmentedOnapSystem =
 
 211                 YangInstanceIdentifier.builder(ietfSystemIID.build()).node(onapSystemIID);
 
 212         Capabilities x = netconfDomAccessor.getCapabilites();
 
 213         LOG.debug("Capabilites: {}", x);
 
 214         if (x.isSupportingNamespace(ORanDeviceManagerQNames.ONAP_SYSTEM_QNAME)) {
 
 215             Optional<NormalizedNode> res =
 
 216                     netconfDomAccessor.readDataNode(LogicalDatastoreType.OPERATIONAL, augmentedOnapSystem.build());
 
 217             LOG.debug("Result of System1 = {}", res);
 
 218             return res.isPresent() ? res.get() : null;
 
 220             LOG.debug("No GUI cut through support");
 
 226     private void publishMountpointToVES(Collection<MapEntryNode> componentList) {
 
 228          * 1. Check if this device is in the list of allowed-devices. 2. If device
 
 229          * exists in allowed-devices, then create VES pnfRegistration event and publish
 
 232         if (vesCollectorService.getConfig().isVESCollectorEnabled() && inAllowedDevices(getMountpointId())) {
 
 233             for (MapEntryNode component : ORanDOMToInternalDataModel.getRootComponents(componentList)) {
 
 234                 // Just get one component. At the moment we don't care which one. Also since
 
 235                 // there is only one management address, we assume there will be only one
 
 237                 // If the device supports subtended configuration then it is assumed that the
 
 238                 // Chassis containing the management interface will be the root component and
 
 239                 // there will be only one root.
 
 240                 VESCommonEventHeaderPOJO header = mapper.mapCommonEventHeader(component);
 
 241                 VESPNFRegistrationFieldsPOJO body = mapper.mapPNFRegistrationFields(component);
 
 243                     vesCollectorService.publishVESMessage(vesCollectorService.generateVESEvent(header, body));
 
 244                 } catch (JsonProcessingException e) {
 
 245                     LOG.warn("Error while serializing VES Event to String ", e);
 
 251     private boolean inAllowedDevices(String mountpointName) {
 
 252         InstanceIdentifierBuilder callhomeServerIID =
 
 253                 YangInstanceIdentifier.builder().node(ORanDeviceManagerQNames.CALLHOME_SERVER_CONTAINER);
 
 254         final InstanceIdentifierBuilder allowedDevicesIID = YangInstanceIdentifier.builder(callhomeServerIID.build())
 
 255                 .node(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE);
 
 257         Optional<NormalizedNode> allowedDevices = netconfDomAccessor
 
 258                 .readControllerDataNode(LogicalDatastoreType.CONFIGURATION, allowedDevicesIID.build());
 
 260         if (allowedDevices.isPresent()) {
 
 261             ContainerNode allowedDevicesNode = (ContainerNode) allowedDevices.get();
 
 262             MapNode deviceList = (MapNode) allowedDevicesNode
 
 263                     .childByArg(new NodeIdentifier(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_DEVICE_LIST));
 
 264             if (deviceList != null) {
 
 265                 Collection<MapEntryNode> deviceListCollection = deviceList.body();
 
 266                 for (MapEntryNode device : deviceListCollection) {
 
 267                     //                                  String deviceName = device.getIdentifier()
 
 268                     //                                                  .getValue(ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_KEY).toString();
 
 269                     String deviceName = ORanDMDOMUtility.getLeafValue(device,
 
 270                             ORanDeviceManagerQNames.CALLHOME_SERVER_ALLOWED_DEVICE_KEY);
 
 271                     if (deviceName != null && deviceName.equals(mountpointName)) {
 
 272                         LOG.debug("Mountpoint {} is part of allowed-devices list", mountpointName);
 
 279         LOG.debug("Mountpoint {} is not part of allowed-devices list", mountpointName);