2 * Copyright (c) 2018 Orange and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.fd.honeycomb.transportpce.device.read;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.inject.Inject;
20 import com.google.inject.name.Named;
22 import io.fd.honeycomb.translate.read.ReaderFactory;
23 import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
24 import io.fd.honeycomb.translate.util.read.BindingBrokerReader;
25 import io.fd.honeycomb.transportpce.device.DeviceConfiguration;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.Future;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDeviceBuilder;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
42 public final class DeviceReaderFactory implements ReaderFactory {
44 private static final Logger LOG = LoggerFactory.getLogger(DeviceReaderFactory.class);
45 public static final InstanceIdentifier<OrgOpenroadmDevice> DEVICE_CONTAINER_ID =
46 InstanceIdentifier.create(OrgOpenroadmDevice.class);
49 @Named("device-databroker")
50 private DataBroker dataBroker;
53 private DeviceConfiguration deviceConfiguration;
57 public void init(final ModifiableReaderRegistryBuilder registry) {
58 registry.add(new BindingBrokerReader<>(DEVICE_CONTAINER_ID, dataBroker,LogicalDatastoreType.OPERATIONAL,
59 OrgOpenroadmDeviceBuilder.class));
60 if(writeXMLDataToOper()) {
66 * Write xml data from {@link DeviceConfiguration}
67 * to operational data.
70 public boolean writeXMLDataToOper() {
72 LOG.info("writting xml file data to oper datastore");
73 OrgOpenroadmDevice device = this.deviceConfiguration.getDataDevice();
75 String deviceId = device.getInfo().getNodeId();
76 LOG.info("Getting device info from xml file for device '{}'", deviceId);
77 OrgOpenroadmDeviceBuilder result = new OrgOpenroadmDeviceBuilder(device);
78 InstanceIdentifier<OrgOpenroadmDevice> iid = InstanceIdentifier.create(OrgOpenroadmDevice.class);
79 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
80 if (writeTx != null ) {
81 LOG.info("WriteTransaction is ok, copy device info to oper datastore");
82 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, result.build());
83 Future<Void> future = writeTx.submit();
85 Futures.getChecked(future, ExecutionException.class);
86 LOG.info("device '{}' writed to oper datastore", deviceId);
88 } catch (ExecutionException e) {
89 LOG.error("Failed to write Element '{}' to oper datastore", deviceId);
92 LOG.error("WriteTransaction object is null");
95 LOG.error("device data operation gets from xml file is null !");
101 * Load data to config
105 public boolean loadConfigData() {
107 LOG.info("loading device configuration info from xml file...");
108 String xml = this.deviceConfiguration.getConfigDevice();
109 LOG.info("device info gets from xml file !");
111 OrgOpenroadmDevice result = this.deviceConfiguration.getDeviceFromXML(xml);
112 if (result != null) {
113 LOG.info("OrgOpenroadmDevice info gets : {}", result.getInfo().getNodeId());
114 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
115 if (writeTx != null ) {
116 LOG.info("WriteTransaction is ok, copy device info to config datastore");
117 writeTx.put(LogicalDatastoreType.CONFIGURATION, DEVICE_CONTAINER_ID, result);
118 Future<Void> future = writeTx.submit();
120 Futures.getChecked(future, ExecutionException.class);
121 LOG.info("device writed to config datastore");
122 } catch (ExecutionException e) {
123 LOG.error("Failed to write device to config datastore");
126 LOG.error("WriteTransaction object is null");
129 LOG.error("device gets from xml is null !!");
132 LOG.error("device ID from input is not the same from xml file");