Honeynode Enhancements
[transportpce.git] / tests / honeynode / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / read / PmReaderFactory.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.configuration.PmConfiguration;
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.pm.rev161014.CurrentPmlist;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.rev161014.CurrentPmlistBuilder;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * @author Martial COULIBALY ( mcoulibaly.ext@orange.com ) on behalf of Orange
41  */
42 public class PmReaderFactory implements ReaderFactory {
43
44     private static final Logger LOG = LoggerFactory.getLogger(PmReaderFactory.class);
45     public static final InstanceIdentifier<CurrentPmlist> PM_CONTAINER_ID =
46             InstanceIdentifier.create(CurrentPmlist.class);
47
48     @Inject
49     @Named("device-databroker")
50     private DataBroker dataBroker;
51
52     @Inject
53     private PmConfiguration pmConfiguration;
54
55
56     @Override
57     public void init(final ModifiableReaderRegistryBuilder registry) {
58         registry.add(new BindingBrokerReader<>(PM_CONTAINER_ID, dataBroker,LogicalDatastoreType.OPERATIONAL,
59                 CurrentPmlistBuilder.class));
60         writeXMLDataToOper();
61     }
62
63     /**
64      * Write xml data from {@link PmConfiguration}
65      * to operational data.
66      *
67      */
68     public boolean writeXMLDataToOper() {
69         Boolean res = false;
70         LOG.info("writting xml pm file data to oper datastore");
71         CurrentPmlist pmList = this.pmConfiguration.getDataPm();
72         if (pmList !=null && pmList.getCurrentPm().size() > 0) {
73             LOG.info("Getting pm info from xml file for device ");
74             CurrentPmlistBuilder result  = new CurrentPmlistBuilder(pmList);
75             InstanceIdentifier<CurrentPmlist> iid = InstanceIdentifier.create(CurrentPmlist.class);
76             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
77             if (writeTx != null ) {
78                 LOG.info("WriteTransaction is ok, copy currentPmList to oper datastore");
79                 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, result.build());
80                 Future<Void> future = writeTx.submit();
81                 try {
82                     Futures.getChecked(future, ExecutionException.class);
83                     LOG.info("currentPmList writed to oper datastore");
84                     res = true;
85                 } catch (ExecutionException e) {
86                     LOG.error("Failed to write currentPmList  to oper datastore");
87                 }
88             } else {
89                 LOG.error("WriteTransaction object is null");
90             }
91         } else {
92             LOG.error("currentPmList data operation gets from xml file is null !");
93         }
94         return res;
95     }
96
97 }