Honeynode Enhancements
[transportpce.git] / tests / honeynode / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / read / DeviceReaderFactory.java
index c16a1a8b339331c8760f378b850fa782b51e0234..94d473a160b06173c7d887238bb8754eb699e029 100644 (file)
@@ -19,10 +19,7 @@ import com.google.common.collect.Sets;
 import com.google.common.util.concurrent.Futures;
 import com.google.inject.Inject;
 import com.google.inject.name.Named;
-import io.fd.honeycomb.translate.read.ReaderFactory;
-import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
-import io.fd.honeycomb.translate.util.read.BindingBrokerReader;
-import io.fd.honeycomb.transportpce.device.DeviceConfiguration;
+
 import java.io.File;
 import java.io.IOException;
 import java.text.DateFormat;
@@ -32,6 +29,7 @@ import java.util.List;
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
+
 import org.opendaylight.controller.config.util.capability.Capability;
 import org.opendaylight.controller.config.util.capability.YangModuleCapability;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -39,6 +37,9 @@ import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDeviceBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.StreamsBuilder;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfStateBuilder;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
@@ -64,14 +65,20 @@ import org.opendaylight.yangtools.yang.parser.util.TextToASTTransformer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import io.fd.honeycomb.translate.read.ReaderFactory;
+import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
+import io.fd.honeycomb.translate.util.read.BindingBrokerReader;
+import io.fd.honeycomb.transportpce.device.configuration.DeviceConfiguration;
+import io.fd.honeycomb.transportpce.device.configuration.NetconfConfiguration;
+
 /**
  * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
  */
 public final class DeviceReaderFactory implements ReaderFactory {
 
     private static final Logger LOG = LoggerFactory.getLogger(DeviceReaderFactory.class);
-    public static final InstanceIdentifier<OrgOpenroadmDevice> DEVICE_CONTAINER_ID =
-            InstanceIdentifier.create(OrgOpenroadmDevice.class);
+    public static final InstanceIdentifier<OrgOpenroadmDevice> DEVICE_CONTAINER_ID = InstanceIdentifier
+            .create(OrgOpenroadmDevice.class);
     private static final String YANG_MODELS = "yang";
 
     @Inject
@@ -81,33 +88,35 @@ public final class DeviceReaderFactory implements ReaderFactory {
     @Inject
     private DeviceConfiguration deviceConfiguration;
 
+    @Inject
+    private NetconfConfiguration netconfConfiguration;
 
     @Override
     public void init(final ModifiableReaderRegistryBuilder registry) {
-        registry.add(new BindingBrokerReader<>(DEVICE_CONTAINER_ID, dataBroker,LogicalDatastoreType.OPERATIONAL,
+        registry.add(new BindingBrokerReader<>(DEVICE_CONTAINER_ID, dataBroker, LogicalDatastoreType.OPERATIONAL,
                 OrgOpenroadmDeviceBuilder.class));
-        if(writeXMLDataToOper()) {
+        if (writeXMLDataToOper()) {
             writeNetconfState();
+            writeNetconfStream();
             loadConfigData();
         }
     }
 
     /**
-     * Write xml data from {@link DeviceConfiguration}
-     * to operational data.
+     * Write xml data from {@link DeviceConfiguration} to operational data.
      *
      */
     public boolean writeXMLDataToOper() {
         Boolean res = false;
         LOG.info("writting xml file data to oper datastore");
         OrgOpenroadmDevice device = this.deviceConfiguration.getDataDevice();
-        if (device !=null) {
+        if (device != null) {
             String deviceId = device.getInfo().getNodeId();
             LOG.info("Getting device info from xml file for device '{}'", deviceId);
-            OrgOpenroadmDeviceBuilder result  = new OrgOpenroadmDeviceBuilder(device);
+            OrgOpenroadmDeviceBuilder result = new OrgOpenroadmDeviceBuilder(device);
             InstanceIdentifier<OrgOpenroadmDevice> iid = InstanceIdentifier.create(OrgOpenroadmDevice.class);
             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
-            if (writeTx != null ) {
+            if (writeTx != null) {
                 LOG.info("WriteTransaction is ok, copy device info to oper datastore");
                 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, result.build());
                 Future<Void> future = writeTx.submit();
@@ -128,8 +137,7 @@ public final class DeviceReaderFactory implements ReaderFactory {
     }
 
     /**
-     * Load data to config
-     * device datastore.
+     * Load data to config device datastore.
      *
      */
     public boolean loadConfigData() {
@@ -138,20 +146,20 @@ public final class DeviceReaderFactory implements ReaderFactory {
         String xml = this.deviceConfiguration.getConfigDevice();
         LOG.info("device info gets from xml file !");
         if (xml != null) {
-            OrgOpenroadmDevice result  = this.deviceConfiguration.getDeviceFromXML(xml);
+            OrgOpenroadmDevice result = this.deviceConfiguration.getDeviceFromXML(xml);
             if (result != null) {
                 LOG.info("OrgOpenroadmDevice info gets : {}", result.getInfo().getNodeId());
                 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
-                if (writeTx != null ) {
+                if (writeTx != null) {
                     LOG.info("WriteTransaction is ok, copy device info to config datastore");
                     writeTx.put(LogicalDatastoreType.CONFIGURATION, DEVICE_CONTAINER_ID, result);
                     Future<Void> future = writeTx.submit();
                     try {
                         Futures.getChecked(future, ExecutionException.class);
                         LOG.info("device writed to config datastore");
-                      } catch (ExecutionException e) {
-                          LOG.error("Failed to write device to config datastore");
-                      }
+                    } catch (ExecutionException e) {
+                        LOG.error("Failed to write device to config datastore");
+                    }
                 } else {
                     LOG.error("WriteTransaction object is null");
                 }
@@ -165,9 +173,43 @@ public final class DeviceReaderFactory implements ReaderFactory {
     }
 
     /**
-     * Write xml data from {@link DeviceConfiguration}
-     * to operational data.
+     * write {@link Streams} data to operational device datastore.
+     *
+     * @return result {@link Boolean}
+     */
+    public boolean writeNetconfStream() {
+        Boolean result = false;
+        LOG.info("writting netconf stream to oper datastore");
+        Streams streams = this.netconfConfiguration.getNetconfStreamsData();
+        if (streams != null) {
+            LOG.info("Netconf Data gets from xml file is present");
+            InstanceIdentifier<Streams> iid = InstanceIdentifier.create(Netconf.class).child(Streams.class);
+            Streams netconfStreams = new StreamsBuilder(streams).build();
+            WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
+            if (writeTx != null) {
+                LOG.info("WriteTransaction is ok");
+                writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, netconfStreams);
+                Future<Void> future = writeTx.submit();
+                try {
+                    Futures.getChecked(future, ExecutionException.class);
+                    LOG.info("netconf stream writed to oper datastore");
+                    result = true;
+                } catch (ExecutionException e) {
+                    LOG.error("Failed to write netconf stream to oper datastore");
+                }
+            } else {
+                LOG.error("WriteTransaction object is null");
+            }
+        } else {
+            LOG.error("Netconf data gets from xml file is null !");
+        }
+        return result;
+    }
+
+    /**
+     * Write {@link NetconfState} data to operational device datastore.
      *
+     * @return result {@link Boolean}
      */
     public boolean writeNetconfState() {
         Boolean res = false;
@@ -179,28 +221,20 @@ public final class DeviceReaderFactory implements ReaderFactory {
         List<Schema> schemaList = new ArrayList<Schema>();
         List<Location> locationList = new ArrayList<Location>();
         Location location = new Location(Enumeration.NETCONF);
-        locationList.add(location );
+        locationList.add(location);
         Schema schematobuild = null;
         for (final Schema schema : monitor.getSchemas().getSchema()) {
-            schematobuild = new SchemaBuilder()
-                    .setIdentifier(schema.getIdentifier())
-                    .setNamespace(schema.getNamespace())
-                    .setVersion(schema.getVersion())
-                    .setFormat(Yang.class)
-                    .setLocation(locationList)
-                    .build();
+            schematobuild = new SchemaBuilder().setIdentifier(schema.getIdentifier())
+                    .setNamespace(schema.getNamespace()).setVersion(schema.getVersion()).setFormat(Yang.class)
+                    .setLocation(locationList).build();
             schemaList.add(schematobuild);
         }
-        Schemas schemas = new SchemasBuilder()
-                .setSchema(schemaList)
-                .build();
-        NetconfState netconfState = new NetconfStateBuilder()
-                .setSchemas(schemas)
-                .build();
-        if (netconfState !=null) {
+        Schemas schemas = new SchemasBuilder().setSchema(schemaList).build();
+        NetconfState netconfState = new NetconfStateBuilder().setSchemas(schemas).build();
+        if (netconfState != null) {
             InstanceIdentifier<NetconfState> iid = InstanceIdentifier.create(NetconfState.class);
             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
-            if (writeTx != null ) {
+            if (writeTx != null) {
                 LOG.info("WriteTransaction is ok, copy device info to oper datastore");
                 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, netconfState);
                 Future<Void> future = writeTx.submit();
@@ -225,7 +259,8 @@ public final class DeviceReaderFactory implements ReaderFactory {
         consumer.registerSchemaSourceListener(TextToASTTransformer.create(consumer, consumer));
         consumer.registerSchemaSourceListener(new SchemaSourceListener() {
             @Override
-            public void schemaSourceEncountered(final SchemaSourceRepresentation schemaSourceRepresentation) {}
+            public void schemaSourceEncountered(final SchemaSourceRepresentation schemaSourceRepresentation) {
+            }
 
             @Override
             public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> potentialSchemaSources) {
@@ -235,25 +270,26 @@ public final class DeviceReaderFactory implements ReaderFactory {
             }
 
             @Override
-            public void schemaSourceUnregistered(final PotentialSchemaSource<?> potentialSchemaSource) {}
+            public void schemaSourceUnregistered(final PotentialSchemaSource<?> potentialSchemaSource) {
+            }
         });
         LOG.info("Loading models from directory.");
         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
         File models = new File(classLoader.getResource(YANG_MODELS).getFile());
         if (models.exists() && models.isDirectory()) {
             LOG.info("folder '{}' exists !", models.getAbsolutePath());
-            final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(
-                    consumer, YangTextSchemaSource.class, models);
-                consumer.registerSchemaSourceListener(cache);
+            final FilesystemSchemaSourceCache<YangTextSchemaSource> cache = new FilesystemSchemaSourceCache<>(consumer,
+                    YangTextSchemaSource.class, models);
+            consumer.registerSchemaSourceListener(cache);
         } else {
             LOG.warn("folder '{}' not exists !", models.getAbsolutePath());
             LOG.info("Custom module loading skipped.");
         }
         SchemaContext schemaContext;
         try {
-            //necessary for creating mdsal data stores and operations
+            // necessary for creating mdsal data stores and operations
             schemaContext = consumer.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT)
-                .createSchemaContext(loadedSources).get();
+                    .createSchemaContext(loadedSources).get();
         } catch (final InterruptedException | ExecutionException e) {
             throw new RuntimeException("Cannot parse schema context", e);
         }
@@ -270,21 +306,20 @@ public final class DeviceReaderFactory implements ReaderFactory {
     }
 
     private static void addModuleCapability(final SharedSchemaRepository consumer, final Set<Capability> capabilities,
-                                     final Module module) {
+            final Module module) {
         DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 
-        //to convert Date to String, use format method of SimpleDateFormat class.
+        // to convert Date to String, use format method of SimpleDateFormat class.
         String revision = dateFormat.format(module.getRevision());
-        final SourceIdentifier moduleSourceIdentifier = RevisionSourceIdentifier.create(module.getName(),
-            revision);
+        final SourceIdentifier moduleSourceIdentifier = RevisionSourceIdentifier.create(module.getName(), revision);
         try {
             final String moduleContent = new String(
-                consumer.getSchemaSource(moduleSourceIdentifier, YangTextSchemaSource.class).get().read());
+                    consumer.getSchemaSource(moduleSourceIdentifier, YangTextSchemaSource.class).get().read());
             capabilities.add(new YangModuleCapability(module, moduleContent));
-            //IOException would be thrown in creating SchemaContext already
+            // IOException would be thrown in creating SchemaContext already
         } catch (ExecutionException | InterruptedException | IOException e) {
             LOG.warn("Cannot retrieve schema source for module {} from schema repository",
                     moduleSourceIdentifier.toString(), e);
         }
-}
+    }
 }