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 static org.junit.Assert.assertEquals;
 
  25 import com.google.common.collect.Sets;
 
  26 import java.io.IOException;
 
  27 import java.io.InputStream;
 
  28 import java.net.URISyntaxException;
 
  29 import java.time.Instant;
 
  30 import java.util.List;
 
  31 import java.util.Optional;
 
  32 import javax.xml.stream.XMLStreamException;
 
  33 import javax.xml.stream.XMLStreamReader;
 
  34 import org.junit.AfterClass;
 
  35 import org.junit.BeforeClass;
 
  36 import org.junit.Test;
 
  37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.impl.dom.util.TestYangParserUtil;
 
  38 import org.opendaylight.mdsal.dom.api.DOMEvent;
 
  39 import org.opendaylight.mdsal.dom.api.DOMNotification;
 
  40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity;
 
  41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough;
 
  42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
 
  43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 
  44 import org.opendaylight.yangtools.util.xml.UntrustedXML;
 
  45 import org.opendaylight.yangtools.yang.common.QName;
 
  46 import org.opendaylight.yangtools.yang.common.QNameModule;
 
  47 import org.opendaylight.yangtools.yang.common.Revision;
 
  48 import org.opendaylight.yangtools.yang.common.XMLNamespace;
 
  49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
  50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 
  51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 
  52 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 
  53 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
  54 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
 
  55 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
 
  56 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 
  57 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 
  58 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
 
  59 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
 
  60 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 
  61 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 
  62 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
 
  63 import org.xml.sax.SAXException;
 
  65 public class TestORanDOMToInternalDataModel {
 
  67     private static final QNameModule IETF_HARDWARE_MODULE =
 
  68             QNameModule.create(XMLNamespace.of("urn:ietf:params:xml:ns:yang:ietf-hardware"), Revision.of("2018-03-13"));
 
  69     private static final QName HW_CONTAINER = QName.create(IETF_HARDWARE_MODULE, "hardware");
 
  71     private static final QNameModule IETF_SYSTEM_MODULE =
 
  72             QNameModule.create(XMLNamespace.of("urn:ietf:params:xml:ns:yang:ietf-system"), Revision.of("2014-08-06"));
 
  73     private static final QName IETF_CONTAINER = QName.create(IETF_SYSTEM_MODULE, "system");
 
  75     private static EffectiveModelContext schemaContext;
 
  76     private static Inference hwContainerSchema;
 
  77     private static Inference systemSchema;
 
  79     private static final NodeId nodeId = new NodeId("nSky");
 
  82     public static void setup() throws IOException {
 
  83         schemaContext = TestYangParserUtil.parseYangResourceDirectory("/");
 
  84         hwContainerSchema = Inference.ofDataTreePath(schemaContext, HW_CONTAINER);
 
  85         systemSchema = Inference.ofDataTreePath(schemaContext, IETF_CONTAINER);
 
  89     public static void cleanup() {
 
  91         hwContainerSchema = null;
 
  96     public void testIetfHardwareFromXML() throws XMLStreamException, URISyntaxException, IOException, SAXException {
 
  98         final InputStream resourceAsStream = TestORanDOMToInternalDataModel.class.getResourceAsStream("/ietf-hardware.xml");
 
 101          * final XMLInputFactory factory = XMLInputFactory.newInstance();
 
 102          * XMLStreamReader reader = factory.createXMLStreamReader(resourceAsStream);
 
 104         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
 
 106         final NormalizedNodeResult result = new NormalizedNodeResult();
 
 107         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
 
 109         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, hwContainerSchema);
 
 110         xmlParser.parse(reader);
 
 115         NormalizedNode transformedInput = result.getResult();
 
 117         List<Inventory> inventoryList = ORanDOMToInternalDataModel.getInventoryList(nodeId, transformedInput);
 
 118         assertEquals("All elements", 27, inventoryList.size());
 
 119         assertEquals("Treelevel always there", 0,
 
 120                 inventoryList.stream().filter(inventory -> inventory.getTreeLevel() == null).count());
 
 124     public void testIetfSystemFromXML() throws XMLStreamException, URISyntaxException, IOException, SAXException {
 
 126         final InputStream resourceAsStream = TestORanDOMToInternalDataModel.class.getResourceAsStream("/onap-system.xml");
 
 128         final XMLStreamReader reader = UntrustedXML.createXMLStreamReader(resourceAsStream);
 
 130         final NormalizedNodeResult result = new NormalizedNodeResult();
 
 131         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
 
 133         final XmlParserStream xmlParser = XmlParserStream.create(streamWriter, systemSchema);
 
 134         xmlParser.parse(reader);
 
 139         NormalizedNode transformedInput = result.getResult();
 
 140         ContainerNode cn = (ContainerNode) transformedInput;
 
 141         AugmentationIdentifier onapSystemIID = YangInstanceIdentifier.AugmentationIdentifier.create(
 
 142                 Sets.newHashSet(ORanDeviceManagerQNames.ONAP_SYSTEM_NAME, ORanDeviceManagerQNames.ONAP_SYSTEM_WEB_UI));
 
 143         Optional<Guicutthrough> gc = ORanDOMToInternalDataModel.getGuicutthrough(cn.getChildByArg(onapSystemIID));
 
 144         assertEquals(gc.isPresent(), true);
 
 149     public void testORANFault() {
 
 150         ContainerNode cn = createORANDOMFault();
 
 151         NetconfDeviceNotification faultNotif = new NetconfDeviceNotification(cn, Instant.now());
 
 152         FaultlogEntity fle = ORanDOMToInternalDataModel.getFaultLog(faultNotif, nodeId, 1);
 
 153         assertEquals(fle.getId(), "47");
 
 156     public static ContainerNode createORANDOMFault() {
 
 157         final QName fault_id = QName.create(ORanDeviceManagerQNames.ORAN_FM_FAULT_ID, "fault-id");
 
 158         final QName fault_source = QName.create(ORanDeviceManagerQNames.ORAN_FM_FAULT_SOURCE, "fault-source");
 
 159         final QName fault_severity = QName.create(ORanDeviceManagerQNames.ORAN_FM_FAULT_SEVERITY, "fault-severity");
 
 160         final QName is_cleared = QName.create(ORanDeviceManagerQNames.ORAN_FM_FAULT_IS_CLEARED, "is-cleared");
 
 161         final QName fault_text = QName.create(ORanDeviceManagerQNames.ORAN_FM_FAULT_TEXT, "fault-text");
 
 162         return Builders.containerBuilder()
 
 163                 .withNodeIdentifier(NodeIdentifier.create(ORanDeviceManagerQNames.ORAN_FM_ALARM_NOTIF))
 
 164                 .withChild(ImmutableNodes.leafNode(fault_id, "47"))
 
 165                 .withChild(ImmutableNodes.leafNode(fault_source, "Slot-2-Port-B"))
 
 166                 .withChild(ImmutableNodes.leafNode(fault_severity, "MAJOR"))
 
 167                 .withChild(ImmutableNodes.leafNode(is_cleared, "true"))
 
 168                 .withChild(ImmutableNodes.leafNode(fault_text, "CPRI Port Down")).build();
 
 171     public static class NetconfDeviceNotification implements DOMNotification, DOMEvent {
 
 172         private final ContainerNode content;
 
 173         private final Absolute schemaPath;
 
 174         private final Instant eventTime;
 
 176         NetconfDeviceNotification(final ContainerNode content, final Instant eventTime) {
 
 177             this.content = content;
 
 178             this.eventTime = eventTime;
 
 179             this.schemaPath = Absolute.of(content.getIdentifier().getNodeType());
 
 182         NetconfDeviceNotification(final ContainerNode content, final Absolute schemaPath, final Instant eventTime) {
 
 183             this.content = content;
 
 184             this.eventTime = eventTime;
 
 185             this.schemaPath = schemaPath;
 
 189         public Absolute getType() {
 
 194         public ContainerNode getBody() {
 
 199         public Instant getEventInstant() {