Honeynode Enhancements
[transportpce.git] / tests / honeynode / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / read / PmReaderFactory.java
diff --git a/tests/honeynode/honeynode-plugin-impl/src/main/java/io/fd/honeycomb/transportpce/device/read/PmReaderFactory.java b/tests/honeynode/honeynode-plugin-impl/src/main/java/io/fd/honeycomb/transportpce/device/read/PmReaderFactory.java
new file mode 100644 (file)
index 0000000..ebf5904
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) 2018 Orange and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package io.fd.honeycomb.transportpce.device.read;
+
+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.configuration.PmConfiguration;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+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.pm.rev161014.CurrentPmlist;
+import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.CurrentPmlistBuilder;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
+ */
+public class PmReaderFactory implements ReaderFactory {
+
+    private static final Logger LOG = LoggerFactory.getLogger(PmReaderFactory.class);
+    public static final InstanceIdentifier<CurrentPmlist> PM_CONTAINER_ID =
+            InstanceIdentifier.create(CurrentPmlist.class);
+
+    @Inject
+    @Named("device-databroker")
+    private DataBroker dataBroker;
+
+    @Inject
+    private PmConfiguration pmConfiguration;
+
+
+    @Override
+    public void init(final ModifiableReaderRegistryBuilder registry) {
+        registry.add(new BindingBrokerReader<>(PM_CONTAINER_ID, dataBroker,LogicalDatastoreType.OPERATIONAL,
+                CurrentPmlistBuilder.class));
+        writeXMLDataToOper();
+    }
+
+    /**
+     * Write xml data from {@link PmConfiguration}
+     * to operational data.
+     *
+     */
+    public boolean writeXMLDataToOper() {
+        Boolean res = false;
+        LOG.info("writting xml pm file data to oper datastore");
+        CurrentPmlist pmList = this.pmConfiguration.getDataPm();
+        if (pmList !=null && pmList.getCurrentPm().size() > 0) {
+            LOG.info("Getting pm info from xml file for device ");
+            CurrentPmlistBuilder result  = new CurrentPmlistBuilder(pmList);
+            InstanceIdentifier<CurrentPmlist> iid = InstanceIdentifier.create(CurrentPmlist.class);
+            WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
+            if (writeTx != null ) {
+                LOG.info("WriteTransaction is ok, copy currentPmList to oper datastore");
+                writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, result.build());
+                Future<Void> future = writeTx.submit();
+                try {
+                    Futures.getChecked(future, ExecutionException.class);
+                    LOG.info("currentPmList writed to oper datastore");
+                    res = true;
+                } catch (ExecutionException e) {
+                    LOG.error("Failed to write currentPmList  to oper datastore");
+                }
+            } else {
+                LOG.error("WriteTransaction object is null");
+            }
+        } else {
+            LOG.error("currentPmList data operation gets from xml file is null !");
+        }
+        return res;
+    }
+
+}