Update portmapping YANG model
[transportpce.git] / test-common / src / main / java / org / opendaylight / transportpce / test / utils / TopologyDataUtils.java
1 /*
2  * Copyright © 2019 Orange, 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.transportpce.test.utils;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.WriteTransaction;
19 import org.opendaylight.mdsal.common.api.CommitInfo;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.transportpce.test.DataStoreContext;
22 import org.opendaylight.transportpce.test.converter.XMLDataObjectConverter;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.portmapping.rev210315.Network;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public final class TopologyDataUtils {
32
33     private static final Logger LOG = LoggerFactory.getLogger(TopologyDataUtils.class);
34
35     public static <T> void writeTopologyFromFileToDatastore(DataStoreContext dataStoreContextUtil, String file,
36         InstanceIdentifier ii) throws InterruptedException, ExecutionException {
37         Networks networks = null;
38         File topoFile = new File(file);
39         if (topoFile.exists()) {
40             String fileName = topoFile.getName();
41             try (InputStream targetStream = new FileInputStream(topoFile)) {
42                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = XMLDataObjectConverter
43                         .createWithDataStoreUtil(dataStoreContextUtil).transformIntoNormalizedNode(targetStream);
44                 if (!transformIntoNormalizedNode.isPresent()) {
45                     throw new IllegalStateException(String.format(
46                         "Could not transform the input %s into normalized nodes", fileName));
47                 }
48                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
49                     .getDataObject(transformIntoNormalizedNode.get(), Networks.QNAME);
50                 if (!dataObject.isPresent()) {
51                     throw new IllegalStateException("Could not transform normalized nodes into data object");
52                 } else {
53                     networks = (Networks) dataObject.get();
54                 }
55             } catch (IOException e) {
56                 LOG.error("An error occured while reading file {}", file, e);
57             }
58         } else {
59             LOG.error("xml file {} not found at {}", topoFile.getName(), topoFile.getAbsolutePath());
60         }
61         if (networks == null) {
62             throw new IllegalStateException("Network is null cannot write it to datastore");
63         }
64         FluentFuture<? extends CommitInfo> commitFuture = writeTransaction(dataStoreContextUtil.getDataBroker(), ii,
65                 networks.nonnullNetwork().values().stream().findFirst().get());
66         commitFuture.get();
67         LOG.info("extraction from {} stored with success in datastore", topoFile.getName());
68     }
69
70     @SuppressWarnings("unchecked")
71     private static FluentFuture<? extends CommitInfo> writeTransaction(DataBroker dataBroker,
72         InstanceIdentifier instanceIdentifier, DataObject object) {
73         WriteTransaction transaction = dataBroker.newWriteOnlyTransaction();
74         transaction.put(LogicalDatastoreType.CONFIGURATION, instanceIdentifier, object);
75         return transaction.commit();
76     }
77
78     public static void writePortmappingFromFileToDatastore(DataStoreContext dataStoreContextUtil, String file)
79         throws InterruptedException, ExecutionException {
80         Network result = null;
81         File portmappingFile = new File(file);
82         if (portmappingFile.exists()) {
83             String fileName = portmappingFile.getName();
84             try (InputStream targetStream = new FileInputStream(portmappingFile)) {
85                 Optional<NormalizedNode<?, ?>> transformIntoNormalizedNode = null;
86                 transformIntoNormalizedNode = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
87                     .transformIntoNormalizedNode(targetStream);
88                 if (!transformIntoNormalizedNode.isPresent()) {
89                     throw new IllegalStateException(String.format(
90                         "Could not transform the input %s into normalized nodes", fileName));
91                 }
92                 Optional<DataObject> dataObject = XMLDataObjectConverter.createWithDataStoreUtil(dataStoreContextUtil)
93                     .getDataObject(transformIntoNormalizedNode.get(), Network.QNAME);
94                 if (!dataObject.isPresent()) {
95                     throw new IllegalStateException("Could not transform normalized nodes into data object");
96                 } else {
97                     result = (Network) dataObject.get();
98                 }
99             } catch (IOException e) {
100                 LOG.error("An error occured while reading file {}", file, e);
101             }
102         } else {
103             LOG.error("xml file {} not found at {}", portmappingFile.getName(), portmappingFile.getAbsolutePath());
104         }
105         InstanceIdentifier<Network> portmappingIID = InstanceIdentifier.builder(Network.class).build();
106         FluentFuture<? extends CommitInfo> writeTransaction = writeTransaction(dataStoreContextUtil.getDataBroker(),
107             portmappingIID, result);
108         writeTransaction.get();
109         LOG.info("portmapping-example stored with success in datastore");
110     }
111
112     private TopologyDataUtils() {
113     }
114
115 }