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