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 java.util.concurrent.ExecutionException;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.binding.api.WriteTransaction;
23 import org.opendaylight.mdsal.common.api.CommitInfo;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDeviceBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.StreamsBuilder;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.util.concurrent.FluentFuture;
35 import com.google.common.util.concurrent.Futures;
36 import com.google.inject.Inject;
37 import com.google.inject.name.Named;
39 import io.fd.honeycomb.translate.read.ReaderFactory;
40 import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
41 import io.fd.honeycomb.translate.util.read.BindingBrokerReader;
42 import io.fd.honeycomb.transportpce.device.configuration.DeviceConfiguration;
43 import io.fd.honeycomb.transportpce.device.configuration.NetconfConfiguration;
46 * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
48 public final class DeviceReaderFactory implements ReaderFactory {
50 private static final Logger LOG = LoggerFactory.getLogger(DeviceReaderFactory.class);
51 public static final InstanceIdentifier<OrgOpenroadmDevice> DEVICE_CONTAINER_ID = InstanceIdentifier
52 .create(OrgOpenroadmDevice.class);
53 private static final String YANG_MODELS = "yang";
56 @Named("device-databroker")
57 private DataBroker dataBroker;
60 private DeviceConfiguration deviceConfiguration;
63 private NetconfConfiguration netconfConfiguration;
66 public void init(final ModifiableReaderRegistryBuilder registry) {
67 registry.add(new BindingBrokerReader<>(DEVICE_CONTAINER_ID, dataBroker, LogicalDatastoreType.OPERATIONAL,
68 OrgOpenroadmDeviceBuilder.class));
69 if (writeXMLDataToOper()) {
76 * Write xml data from {@link DeviceConfiguration} to operational data.
79 public boolean writeXMLDataToOper() {
81 LOG.info("writting xml file data to oper datastore");
82 OrgOpenroadmDevice device = this.deviceConfiguration.getDataDevice();
84 String deviceId = device.getInfo().getNodeId().getValue();
85 LOG.info("Getting device info from xml file for device '{}'", deviceId);
86 OrgOpenroadmDeviceBuilder result = new OrgOpenroadmDeviceBuilder(device);
87 InstanceIdentifier<OrgOpenroadmDevice> iid = InstanceIdentifier.create(OrgOpenroadmDevice.class);
88 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
89 if (writeTx != null) {
90 LOG.info("WriteTransaction is ok, copy device info to oper datastore");
91 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, result.build());
92 FluentFuture< ? extends @NonNull CommitInfo> future = writeTx.commit();
94 Futures.getChecked(future, ExecutionException.class);
95 LOG.info("device '{}' writed to oper datastore", deviceId);
97 } catch (ExecutionException e) {
98 LOG.error("Failed to write Element '{}' to oper datastore", deviceId);
101 LOG.error("WriteTransaction object is null");
104 LOG.error("device data operation gets from xml file is null !");
110 * Load data to config device datastore.
113 private boolean loadConfigData() {
115 LOG.info("loading device configuration info from xml file...");
116 String xml = this.deviceConfiguration.getConfigDevice();
117 LOG.info("device info gets from xml file !");
119 OrgOpenroadmDevice result = this.deviceConfiguration.getDeviceFromXML(xml);
120 if (result != null) {
121 LOG.info("OrgOpenroadmDevice info gets : {}", result.getInfo().getNodeId());
122 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
123 if (writeTx != null) {
124 LOG.info("WriteTransaction is ok, copy device info to config datastore");
125 writeTx.put(LogicalDatastoreType.CONFIGURATION, DEVICE_CONTAINER_ID, result);
126 FluentFuture< ? extends @NonNull CommitInfo> future = writeTx.commit();
128 Futures.getChecked(future, ExecutionException.class);
129 LOG.info("device writed to config datastore");
130 } catch (ExecutionException e) {
131 LOG.error("Failed to write device to config datastore");
134 LOG.error("WriteTransaction object is null");
137 LOG.error("device gets from xml is null !!");
140 LOG.error("device ID from input is not the same from xml file");
146 * write {@link Streams} data to operational device datastore.
148 * @return result {@link Boolean}
150 public boolean writeNetconfStream() {
151 Boolean result = false;
152 LOG.info("writting netconf stream to oper datastore");
153 Streams streams = this.netconfConfiguration.getNetconfStreamsData();
154 if (streams != null) {
155 LOG.info("Netconf Data gets from xml file is present");
156 InstanceIdentifier<Streams> iid = InstanceIdentifier.create(Netconf.class).child(Streams.class);
157 // StreamNameType name = new StreamNameType("OPENROADM");
158 // Streams netconfStreams = new StreamsBuilder()
159 // .setStream(Arrays.asList(new StreamBuilder()
160 // .setKey(new StreamKey(name))
164 Streams netconfStreams = new StreamsBuilder(streams).build();
165 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
166 if (writeTx != null) {
167 LOG.info("WriteTransaction is ok");
168 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, netconfStreams);
169 FluentFuture< ? extends @NonNull CommitInfo> future = writeTx.commit();
171 Futures.getChecked(future, ExecutionException.class);
172 LOG.info("netconf stream writed to oper datastore");
174 } catch (ExecutionException e) {
175 LOG.error("Failed to write netconf stream to oper datastore");
178 LOG.error("WriteTransaction object is null");
181 LOG.error("Netconf data gets from xml file is null !");