Honeynode test tool
[transportpce.git] / tests / honeynode / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / read / DeviceReaderFactory.java
1 /*
2  * Copyright (c) 2018 Orange and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package io.fd.honeycomb.transportpce.device.read;
17
18 import com.google.common.util.concurrent.Futures;
19 import com.google.inject.Inject;
20 import com.google.inject.name.Named;
21
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;
26
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.Future;
29
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;
38
39 /**
40  * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
41  */
42 public final class DeviceReaderFactory implements ReaderFactory {
43
44     private static final Logger LOG = LoggerFactory.getLogger(DeviceReaderFactory.class);
45     public static final InstanceIdentifier<OrgOpenroadmDevice> DEVICE_CONTAINER_ID =
46             InstanceIdentifier.create(OrgOpenroadmDevice.class);
47
48     @Inject
49     @Named("device-databroker")
50     private DataBroker dataBroker;
51
52     @Inject
53     private DeviceConfiguration deviceConfiguration;
54
55
56     @Override
57     public void init(final ModifiableReaderRegistryBuilder registry) {
58         registry.add(new BindingBrokerReader<>(DEVICE_CONTAINER_ID, dataBroker,LogicalDatastoreType.OPERATIONAL,
59                 OrgOpenroadmDeviceBuilder.class));
60         if(writeXMLDataToOper()) {
61             loadConfigData();
62         }
63     }
64
65     /**
66      * Write xml data from {@link DeviceConfiguration}
67      * to operational data.
68      *
69      */
70     public boolean writeXMLDataToOper() {
71         Boolean res = false;
72         LOG.info("writting xml file data to oper datastore");
73         OrgOpenroadmDevice device = this.deviceConfiguration.getDataDevice();
74         if (device !=null) {
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();
84                 try {
85                     Futures.getChecked(future, ExecutionException.class);
86                     LOG.info("device '{}' writed to oper datastore", deviceId);
87                     res = true;
88                 } catch (ExecutionException e) {
89                     LOG.error("Failed to write Element '{}' to oper datastore", deviceId);
90                 }
91             } else {
92                 LOG.error("WriteTransaction object is null");
93             }
94         } else {
95             LOG.error("device data operation gets from xml file is null !");
96         }
97         return res;
98     }
99
100     /**
101      * Load data to config
102      * device datastore.
103      *
104      */
105     public boolean loadConfigData() {
106         Boolean res = false;
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 !");
110         if (xml != null) {
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();
119                     try {
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");
124                       }
125                 } else {
126                     LOG.error("WriteTransaction object is null");
127                 }
128             } else {
129                 LOG.error("device gets from xml is null !!");
130             }
131         } else {
132             LOG.error("device ID from input is not the same from xml file");
133         }
134         return res;
135     }
136 }