Merge "Refactor ConvertORTopoToTapiTopoTest"
[transportpce.git] / inventory / src / main / java / org / opendaylight / transportpce / inventory / INode121.java
1 /*
2  * Copyright © 2017 AT&T and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.transportpce.inventory;
10
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.transportpce.inventory.utils.StringUtils.getCurrentTimestamp;
13 import static org.opendaylight.transportpce.inventory.utils.StringUtils.prepareDashString;
14 import static org.opendaylight.transportpce.inventory.utils.StringUtils.prepareEmptyString;
15
16 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
17 import java.sql.Connection;
18 import java.sql.PreparedStatement;
19 import java.sql.ResultSet;
20 import java.sql.SQLException;
21 import java.util.Map;
22 import java.util.Optional;
23 import java.util.concurrent.ExecutionException;
24 import javax.sql.DataSource;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.transportpce.common.Timeouts;
28 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
29 import org.opendaylight.transportpce.inventory.query.Queries;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceData;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.CpSlots;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.CpSlotsKey;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.Ports;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.pack.PortsKey;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacksKey;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.ConnectionPorts;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.ConnectionPortsKey;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.external.links.ExternalLink;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.external.links.ExternalLinkKey;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.Interface;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.interfaces.grp.InterfaceKey;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.internal.links.InternalLink;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.internal.links.InternalLinkKey;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.ConnectionMap;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.ConnectionMapKey;
48 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Degree;
49 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.DegreeKey;
50 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Info;
51 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Protocols;
52 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
53 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
54 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.SharedRiskGroup;
55 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.SharedRiskGroupKey;
56 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.physical.links.PhysicalLink;
57 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.physical.links.PhysicalLinkKey;
58 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.shelf.Slots;
59 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.shelf.SlotsKey;
60 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.shelves.Shelves;
61 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.shelves.ShelvesKey;
62 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev161014.Interface1;
63 import org.opendaylight.yang.gen.v1.http.org.openroadm.ethernet.interfaces.rev161014.ethernet.container.EthernetBuilder;
64 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.Protocols1;
65 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.PortConfig;
66 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.PortConfigKey;
67 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.nbr.list.IfName;
68 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.lldp.container.lldp.nbr.list.IfNameKey;
69 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.channel.interfaces.rev161014.och.container.OchBuilder;
70 import org.opendaylight.yang.gen.v1.http.org.openroadm.optical.transport.interfaces.rev161014.ots.container.OtsBuilder;
71 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.odu.attributes.Tcm;
72 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.odu.attributes.TcmKey;
73 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.odu.container.OduBuilder;
74 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.opu.opu.msi.ExpMsi;
75 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.opu.opu.msi.ExpMsiKey;
76 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.opu.opu.msi.RxMsi;
77 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.opu.opu.msi.RxMsiKey;
78 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.opu.opu.msi.TxMsi;
79 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.opu.opu.msi.TxMsiKey;
80 import org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.otu.container.OtuBuilder;
81 import org.opendaylight.yang.gen.v1.http.org.openroadm.wavelength.map.rev161014.wavelength.map.g.Wavelengths;
82 import org.opendaylight.yang.gen.v1.http.org.openroadm.wavelength.map.rev161014.wavelength.map.g.WavelengthsKey;
83 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
84 import org.slf4j.Logger;
85 import org.slf4j.LoggerFactory;
86
87 @SuppressFBWarnings(
88     value = "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING",
89     justification = "TODO review the SQL statement generation process")
90 public class INode121 {
91
92     private static final Logger LOG = LoggerFactory.getLogger(INode121.class);
93
94     private final DataSource dataSource;
95     private final DeviceTransactionManager deviceTransactionManager;
96
97     public INode121(DataSource dataSource, DeviceTransactionManager deviceTransactionManager) {
98         this.dataSource = dataSource;
99         this.deviceTransactionManager = deviceTransactionManager;
100     }
101
102     public boolean addNode(String deviceId) {
103
104         InstanceIdentifier<Info> infoIID = InstanceIdentifier
105             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
106             .child(Info.class)
107             .build();
108         Optional<Info> infoOpt =
109                 deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL, infoIID,
110                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
111         Info deviceInfo;
112         if (!infoOpt.isPresent()) {
113             LOG.warn("Could not get device info from DataBroker");
114             return false;
115         }
116         deviceInfo = infoOpt.orElseThrow();
117         boolean sqlResult = false;
118         String query = Queries.getQuery().deviceInfoInsert().get();
119         LOG.info("Running {} query ", query);
120         try (Connection connection = dataSource.getConnection();
121                 PreparedStatement preparedStatement = connection.prepareStatement(query)) {
122             Object[] prepareParameters = prepareDeviceInfoParameters(deviceInfo);
123             for (int i = 0; i < prepareParameters.length; i++) {
124                 LOG.debug("Parameter {} has value {}", i + 1, prepareParameters[i]);
125                 preparedStatement.setObject(i + 1, prepareParameters[i]);
126
127             }
128             int executeUpdate = preparedStatement.executeUpdate();
129             LOG.info("{} entries were added", executeUpdate);
130             sqlResult = true;
131
132             LOG.debug("iNode AddNode call complete");
133             getRoadmShelves(deviceId);
134             LOG.debug("iNode getRoadmShelves call complete");
135             getCircuitPacks(deviceId);
136             LOG.debug("iNode getCircuitPacks call complete");
137
138             LOG.debug("iNode persist interfaces call");
139             persistDevInterfaces(deviceId, connection);
140             LOG.debug("iNode persist interfaces call complete");
141
142
143             LOG.debug("iNode persist protocols call");
144             persistDevProtocols(deviceId, connection);
145             LOG.debug("iNode persist protocols call complete");
146
147
148             LOG.debug("iNode persist wavelength map call");
149             persistDevWavelengthMap(deviceId, connection);
150             LOG.debug("iNode persist wavelength map call complete");
151
152             LOG.debug("iNode persist internal links map call");
153             persistDevInternalLinks(deviceId, connection);
154             LOG.debug("iNode persist internal links map call complete");
155
156             LOG.debug("iNode persist Physical links map call");
157             persistDevPhysicalLinks(deviceId, connection);
158             LOG.debug("iNode persist Physical links map call complete");
159
160             LOG.debug("iNode persist External links map call");
161             persistDevExternalLinks(deviceId, connection);
162             LOG.debug("iNode persist External links map call complete");
163
164             LOG.debug("iNode persist degree map call");
165             persistDevDegree(deviceId, connection);
166             LOG.debug("iNode persist degree map call complete");
167
168             LOG.debug("iNode persist srg map call");
169             persistDevSrg(deviceId, connection);
170             LOG.debug("iNode persist srg map call complete");
171
172             LOG.debug("iNode persist Roadm Connections call");
173             persistDevRoadmConnections(deviceId, connection);
174             LOG.debug("iNode persist Roadm Connections call complete");
175
176             LOG.debug("iNode persist Connection Map call");
177             persistDevConnectionMap(deviceId, connection);
178             LOG.debug("iNode persist Connection Map call complete");
179
180         } catch (SQLException | InterruptedException | ExecutionException e) {
181             LOG.error("Something wrong when storing node into DB", e);
182         }
183         return sqlResult;
184     }
185
186     public boolean nodeExists(String nodeId) {
187         String selectTableSQL = "select count(*) node_exists from inv_dev_info where node_id = ?";
188         int nodeExists = 0;
189         LOG.info("Checking if {} exists in DB", nodeId);
190         try (Connection connection = dataSource.getConnection();
191              PreparedStatement preparedStmt = connection.prepareStatement(selectTableSQL)) {
192             preparedStmt.setString(1, nodeId);
193             try (ResultSet rs = preparedStmt.executeQuery()) {
194                 while (rs.next()) {
195                     nodeExists = rs.getInt("node_exists");
196                     LOG.debug("Found {} devices matching {}", nodeExists, nodeId);
197                 }
198             }
199         } catch (SQLException e) {
200             LOG.error("Something wrong when fetching node in DB", e);
201         }
202         return nodeExists != 0;
203     }
204
205     public void getRoadmShelves(String nodeId) throws InterruptedException, ExecutionException {
206         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier
207             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
208             .build();
209         Optional<OrgOpenroadmDevice> deviceObject = deviceTransactionManager.getDataFromDevice(nodeId,
210                 LogicalDatastoreType.OPERATIONAL, deviceIID, Timeouts.DEVICE_READ_TIMEOUT,
211                 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
212         if (!deviceObject.isPresent()) {
213             return;
214         }
215         Map<ShelvesKey, Shelves> shelvesMap = deviceObject.orElseThrow().nonnullShelves();
216         LOG.info("Shelves size {}", shelvesMap.size());
217         try (Connection connection = requireNonNull(dataSource.getConnection())) {
218             for (Map.Entry<ShelvesKey, Shelves> shelveEntry : shelvesMap.entrySet()) {
219                 Shelves shelve = shelveEntry.getValue();
220                 String shelfName = shelve.getShelfName();
221                 LOG.info("Getting Shelve Details of {}", shelfName);
222                 if (shelve.getSlots() != null) {
223                     LOG.info("Slot Size {} ", shelve.getSlots().size());
224                     persistShelveSlots(nodeId, shelve, connection);
225                 } else {
226                     LOG.info("No Slots for shelf {}", shelfName);
227                 }
228
229                 persistShelves(nodeId, connection, shelve);
230             }
231         } catch (SQLException e1) {
232             LOG.error("Something wrong when fetching ROADM shelves in DB", e1);
233         }
234     }
235
236     public void getCircuitPacks(String nodeId) throws InterruptedException, ExecutionException {
237         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier
238             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
239             .build();
240         Optional<OrgOpenroadmDevice> deviceObject =
241                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, deviceIID,
242                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
243         if (!deviceObject.isPresent()) {
244             LOG.warn("Device object {} was not found", nodeId);
245             return;
246         }
247         Map<CircuitPacksKey, CircuitPacks> circuitPacksMap = deviceObject.orElseThrow().nonnullCircuitPacks();
248         LOG.info("Circuit pack size {}", circuitPacksMap.size());
249
250         try (Connection connection = requireNonNull(dataSource.getConnection())) {
251             for (Map.Entry<CircuitPacksKey, CircuitPacks> circuitPackEntry : circuitPacksMap.entrySet()) {
252                 CircuitPacks cp = circuitPackEntry.getValue();
253
254                 if (cp.getCpSlots() != null) {
255                     persistCircuitPacksSlots(nodeId, cp, connection);
256                 }
257                 LOG.info("Everything {}", cp);
258                 LOG.info("CP is {}", cp);
259
260                 //persistPorts(cp, connection);
261                 if (cp.getPorts() != null) {
262                     persistCPPorts(nodeId, connection, cp);
263                 }
264                 persistCircuitPacks(nodeId, connection, cp);
265             }
266         } catch (SQLException e1) {
267             LOG.error("Something wrong when fetching Circuit Packs in DB", e1);
268         }
269     }
270
271     private void persistCircuitPacks(String nodeId, Connection connection, CircuitPacks cp) {
272         Object[] parameters = prepareCircuitPacksParameters(nodeId, cp);
273         String query = Queries.getQuery().deviceCircuitPackInsert().get();
274         LOG.info("Running {} query ", query);
275         try (PreparedStatement stmt = connection.prepareStatement(query)) {
276             for (int j = 0; j < parameters.length; j++) {
277                 stmt.setObject(j + 1, parameters[j]);
278             }
279             stmt.execute();
280             stmt.clearParameters();
281         } catch (SQLException e) {
282             LOG.error("Something wrong when storing Circuit Packs in DB", e);
283         }
284     }
285
286     private void persistShelves(String nodeId, Connection connection, Shelves shelve) {
287         Object[] shelvesParameter = prepareShelvesParameters(nodeId, shelve);
288         String query = Queries.getQuery().deviceShelfInsert().get();
289         LOG.info("Running {} query ", query);
290         try (PreparedStatement preparedStmt = connection.prepareStatement(query)) {
291             for (int j = 0; j < shelvesParameter.length; j++) {
292                 preparedStmt.setObject(j + 1, shelvesParameter[j]);
293             }
294             preparedStmt.execute();
295             preparedStmt.clearParameters();
296         } catch (SQLException e) {
297             LOG.error("Something wrong when storing shelves in DB", e);
298         }
299     }
300
301     private void persistShelveSlots(String nodeId, Shelves shelves, Connection connection) {
302         String startTimetampStr = getCurrentTimestamp();
303         Map<SlotsKey, Slots> slotsMap = shelves.nonnullSlots();
304         for (Map.Entry<SlotsKey, Slots> slotEntry : slotsMap.entrySet()) {
305             Slots slot = slotEntry.getValue();
306             LOG.info("Getting Slot Details of {}", slot.getSlotName());
307             Object[] parameters = new Object[]{nodeId,
308                 shelves.getShelfName(),
309                 slot.getSlotName(),
310                 slot.getLabel(),
311                 slot.getProvisionedCircuitPack(),
312                 "0",
313                 startTimetampStr,
314                 startTimetampStr};
315             String query = Queries.getQuery().deviceShelfSlotInsert().get();
316             LOG.info("Running {} query ", query);
317             try (PreparedStatement stmt = connection.prepareStatement(query)) {
318                 for (int j = 0; j < parameters.length; j++) {
319                     stmt.setObject(j + 1, parameters[j]);
320                 }
321                 stmt.execute();
322                 stmt.clearParameters();
323             } catch (SQLException e) {
324                 LOG.error("Something wrong when storing shelves slots in DB", e);
325             }
326         }
327     }
328
329
330     private void persistCircuitPacksSlots(String nodeId, CircuitPacks circuitPacks, Connection connection) {
331         String startTimetampStr = getCurrentTimestamp();
332         Map<CpSlotsKey, CpSlots> cpSlotsMap = circuitPacks.nonnullCpSlots();
333         for (Map.Entry<CpSlotsKey, CpSlots> cpSlotEntry: cpSlotsMap.entrySet()) {
334             CpSlots cpSlot = cpSlotEntry.getValue();
335
336             Object[] parameters = new Object[]{nodeId,
337                 circuitPacks.getCircuitPackName(),
338                 cpSlot.getSlotName(),
339                 cpSlot.getLabel(),
340                 cpSlot.getProvisionedCircuitPack(),
341                 "-1",
342                 "-1",
343                 startTimetampStr,
344                 startTimetampStr};
345             String query = Queries.getQuery().deviceCPSlotInsert().get();
346             LOG.info("Running {} query ", query);
347             try (PreparedStatement stmt = connection.prepareStatement(query)) {
348                 for (int j = 0; j < parameters.length; j++) {
349                     stmt.setObject(j + 1, parameters[j]);
350                 }
351                 stmt.execute();
352                 stmt.clearParameters();
353             } catch (SQLException e) {
354                 LOG.error("Something wrong when storing Cirtcuits Packs slots in DB", e);
355             }
356         }
357     }
358
359     /**
360      * Prepares parameters for device insert query.
361      *
362      * @param deviceInfo device information
363      * @return Object an object
364      */
365     private static Object[] prepareDeviceInfoParameters(Info deviceInfo) {
366         String startTimetampStr = getCurrentTimestamp();
367
368         String nodeId = prepareDashString(deviceInfo.getNodeId());
369         Long nodeNumber = deviceInfo.getNodeNumber().toJava();
370         String nodeTypeEnu = deviceInfo.getNodeType().getName();
371         String clli = prepareDashString(deviceInfo.getClli());
372         String vendor = prepareDashString(deviceInfo.getVendor());
373         String model = prepareDashString(deviceInfo.getModel());
374         String serialId = prepareDashString(deviceInfo.getSerialId());
375         String ipAddress = prepareDashString(deviceInfo.getIpAddress().getIpv4Address().getValue());
376         String prefixLength = prepareDashString(deviceInfo.getPrefixLength());
377         String defaultGateway = prepareDashString(deviceInfo.getDefaultGateway().getIpv4Address().getValue());
378         String sourceEnum = deviceInfo.getSource().getName();
379         String currentIpAddress = prepareDashString(deviceInfo.getCurrentIpAddress().getIpv4Address().getValue());
380         String currentPrefixLength = prepareDashString(deviceInfo.getCurrentPrefixLength());
381         String currentDefaultGateway = prepareDashString(deviceInfo.getDefaultGateway().getIpv4Address().getValue());
382         String macAddress = prepareDashString(deviceInfo.getMacAddress().getValue());
383         String softwareVersion = prepareDashString(deviceInfo.getSoftwareVersion());
384         //String openroadmVersion = "1.2.1";
385         String template = prepareDashString(deviceInfo.getTemplate());
386         String currentDatetime = prepareDashString(deviceInfo.getCurrentDatetime().getValue());
387         String geoLatitude = (deviceInfo.getGeoLocation() != null
388             ? prepareDashString(deviceInfo.getGeoLocation().getLatitude()) : "");
389         String geoLongitude = (deviceInfo.getGeoLocation() != null
390             ? prepareDashString(deviceInfo.getGeoLocation().getLongitude()) : "");
391         String maxDegrees = (deviceInfo.getMaxDegrees() == null ? "-1" : prepareDashString(deviceInfo.getMaxDegrees()));
392         String maxSrgs = (deviceInfo.getMaxSrgs() == null ? "-1" : prepareDashString(deviceInfo.getMaxSrgs()));
393         String swVersion = prepareDashString(deviceInfo.getSoftwareVersion()); //sw_version
394         String swValidationTimer = prepareDashString(""); //sw_validation_timer
395         String activationDateTime = prepareDashString(""); //activation_date_time
396         //Integer maxNumBin15minHistoricalPm = null;
397         //Integer maxNumBin24hourHistoricalPm = null;
398         /*jsonDevInfo = JsonStringBuilder.getDevInfoJson().replace("$$NODE-ID$$",nodeId)
399                 .replace("$$NODE-NUMBER$$", nodeNumber)
400                 .replace("$$NODE-TYPE$$",nodeType)
401                 .replace("$$CLLI$$",clli)
402                 .replace("$$VENDOR$$",vendor)
403                 .replace("$$MODEL$$",model)
404                 .replace("$$SERIAL-ID$$",serialId)
405                 .replace("$$IPADDRESS$$",ipAddress)
406                 .replace("$$PREFIX-LENGTH$$",prefixLength)
407                 .replace("$$DEFAULTGATEWAY$$",defaultGateway)
408                 .replace("$$SOURCE$$",String.valueOf(source))
409                 .replace("$$CURRENT-IPADDRESS$$",currentIpAddress)
410                 .replace("$$CURRENT-PREFIX-LENGTH$$",currentPrefixLength)
411                 .replace("$$CURRENT-DEFAULTGATEWAY$$",currentDefailtGateway)
412                 .replace("$$MACADDRESS$$",macAddress)
413                 .replace("$$SOFTWAREVERSION$$",softwareVersion)
414                 .replace("$$OPENROADM-VERSION$$",openroadmVersion)
415                 .replace("$$TEMPLATE$$",template)
416                 .replace("$$CURRENT-DATETIME$$",currentDatetime)
417                 .replace("$$LATITUDE$$",latitude)
418                 .replace("$$LONGITUDE$$",longitude)
419                 .replace("$$MAX-DEGREES$$",maxDegrees)
420                 .replace("$$MAX-SRGS$$",maxSrgs)
421                 .replace("$$MAX-NUM-BIN-15MIN-HISTORICAL-PM$$",prepareDashString(""))
422                 .replace("$$MAX-NUM-BIN-24HOUR-HISTORICAL-PM$$",prepareDashString(""))
423                 .replace("$$SW-VERSION$$",swVersion)
424                 .replace("$$SW-VALIDATION-TIMER$$",swValidationTimer)
425                 .replace("$$ACTIVATION-DATE-TIME$$",activationDateTime);*/
426
427
428         return new Object[]{
429             nodeId,
430             nodeNumber,
431             nodeTypeEnu,
432             clli,
433             vendor,
434             model,
435             serialId,
436             ipAddress,
437             prefixLength,
438             defaultGateway,
439             sourceEnum,
440             currentIpAddress,
441             currentPrefixLength,
442             currentDefaultGateway,
443             macAddress,
444             softwareVersion,
445             //openroadmVersion,
446             "1.2.1",
447             template,
448             currentDatetime,
449             geoLatitude,
450             geoLongitude,
451             maxDegrees,
452             maxSrgs,
453             //maxNumBin15minHistoricalPm,
454             //maxNumBin24hourHistoricalPm,
455             null, null,
456             swVersion,
457             swValidationTimer,
458             activationDateTime,
459             startTimetampStr,
460             startTimetampStr
461         };
462     }
463
464
465     private static Object[] prepareShelvesParameters(String nodeId, Shelves shelve) {
466         String startTimestamp = getCurrentTimestamp();
467
468         return new Object[]{nodeId,
469             shelve.getShelfName(),
470             shelve.getShelfType(),
471             shelve.getRack(),
472             shelve.getShelfPosition(),
473             (shelve.getAdministrativeState() == null ? null : shelve.getAdministrativeState().getIntValue()),
474             shelve.getVendor(),
475             shelve.getModel(),
476             shelve.getSerialId(),
477             shelve.getType(),
478             shelve.getProductCode(),
479             (shelve.getManufactureDate() == null ? null : shelve.getManufactureDate().getValue()),
480             shelve.getClei(),
481             shelve.getHardwareVersion(),
482             (shelve.getOperationalState() == null ? null : shelve.getOperationalState().getIntValue()),
483             (shelve.getEquipmentState() == null ? null : shelve.getEquipmentState().getIntValue()),
484             (shelve.getDueDate() == null ? null : shelve.getDueDate().getValue()),
485             startTimestamp,
486             startTimestamp};
487     }
488
489
490     private static Object[] prepareCPPortsParameters(String nodeId, CircuitPacks circuitPacks, Ports cpPort) {
491
492         String circuitPackName = circuitPacks.getCircuitPackName();
493         String portName = cpPort.getPortName();
494         String portType = cpPort.getPortType();
495         String portQualEnu = String.valueOf((cpPort.getPortQual() == null ? "-1" : cpPort.getPortQual().getName()));
496         String portWavelengthTypeEnu = "-1"; //cpPort.getPortWavelengthType().getIntValue(); /* Check error*/
497         String portDirectionEnu = String.valueOf((cpPort.getPortDirection() == null ? "" :
498             cpPort.getPortDirection().getName()));
499         String label = cpPort.getLabel();
500         String circuitId = cpPort.getCircuitId();
501         String administrativeStateEnu = (cpPort.getAdministrativeState() == null ? "" :
502             cpPort.getAdministrativeState().getName());
503         String operationalStateEnu =
504             (cpPort.getOperationalState() == null ? "" : cpPort.getOperationalState().getName());
505         String logicalConnectionPoint = cpPort.getLogicalConnectionPoint();
506         String parentPortCircuitPackName = (cpPort.getPartnerPort() == null ? "" :
507             (cpPort.getPartnerPort().getCircuitPackName() == null ? "" : cpPort.getPartnerPort().getCircuitPackName()));
508         String partnerPortPortName = (cpPort.getPartnerPort() == null ? "" :
509             (cpPort.getPartnerPort().getPortName() == null ? "" : cpPort.getPartnerPort().getPortName()));
510         String partnerPortCircuitPackName = (cpPort.getParentPort() == null ? "" :
511             (cpPort.getParentPort().getCircuitPackName() == null ? "" : cpPort.getParentPort().getCircuitPackName()));
512         String parentPortPortName = (cpPort.getParentPort() == null ? "" :
513             (cpPort.getParentPort().getPortName() == null ? "" : cpPort.getParentPort().toString()));
514         String roadmPortPortPowerCapabilityMinRx = (cpPort.getRoadmPort() == null ? "" :
515             (cpPort.getRoadmPort().getPortPowerCapabilityMinRx() == null ? "" :
516                 cpPort.getRoadmPort().getPortPowerCapabilityMinRx().toString()));
517         String roadmPortPortPowerCapabilityMinTx = (cpPort.getRoadmPort() == null ? "" :
518             (cpPort.getRoadmPort().getPortPowerCapabilityMinTx() == null ? "" :
519                 cpPort.getRoadmPort().getPortPowerCapabilityMinTx().toString()));
520         String roadmPortPortPowerCapabilityMaxRx = (cpPort.getRoadmPort() == null ? "" :
521             (cpPort.getRoadmPort().getPortPowerCapabilityMaxRx() == null ? "" :
522                 cpPort.getRoadmPort().getPortPowerCapabilityMaxRx().toString()));
523         String roadmPortPortPowerCapabilityMaxTx = (cpPort.getRoadmPort() == null ? "" :
524             (cpPort.getRoadmPort().getPortPowerCapabilityMaxTx() == null ? "" :
525                 cpPort.getRoadmPort().getPortPowerCapabilityMaxTx().toString()));
526         //String roadmPortCapableWavelengths = "";
527         //String roadmPortAvailableWavelengths = "";
528         //String roadmPortUsedWavelengths = "";
529         String transponderPortPortPowerCapabilityMinRx = (cpPort.getTransponderPort() == null ? "" :
530             (cpPort.getTransponderPort().getPortPowerCapabilityMinRx() == null ? "" :
531                 cpPort.getTransponderPort().getPortPowerCapabilityMinRx().toString()));
532         String transponderPortPortPowerCapabilityMinTx = (cpPort.getTransponderPort() == null ? "" :
533             (cpPort.getTransponderPort().getPortPowerCapabilityMinTx() == null ? "" :
534                 cpPort.getTransponderPort().getPortPowerCapabilityMinTx().toString()));
535         String transponderPortPortPowerCapabilityMaxRx = (cpPort.getTransponderPort() == null ? "" :
536             (cpPort.getTransponderPort().getPortPowerCapabilityMaxRx() == null ? "" :
537                 cpPort.getTransponderPort().getPortPowerCapabilityMaxRx().toString()));
538         String transponderPortPortPowerCapabilityMaxTx = (cpPort.getTransponderPort() == null ? "" :
539             (cpPort.getTransponderPort().getPortPowerCapabilityMaxTx() == null ? "" :
540                 cpPort.getTransponderPort().getPortPowerCapabilityMaxTx().toString()));
541         //String transponderPortCapableWavelengths = "";
542         String otdrPortLaunchCableLength = (cpPort.getOtdrPort() == null ? "" :
543             (cpPort.getOtdrPort().getLaunchCableLength() == null ? "" :
544                 cpPort.getOtdrPort().getLaunchCableLength().toString()));
545         String otdrPortPortDirection = (cpPort.getOtdrPort() == null ? "-1" :
546             (cpPort.getOtdrPort().getPortDirection() == null ? "-1" :
547                 Integer.toString(cpPort.getOtdrPort().getPortDirection().getIntValue())));
548         //String ilaPortPortPowerCapabilityMixRx = "";
549         //String ilaPortPortPowerCapabilityMixTx = "";
550         //String ilaPortPortPowerCapabilityMaxRx = "";
551         //String ilaPortPortPowerCapabilityMaxTx = "";
552
553         String startTimestamp = getCurrentTimestamp();
554
555         return new Object[]{nodeId,
556             circuitPackName,
557             portName,
558             portType,
559             portQualEnu,
560             portWavelengthTypeEnu,
561             portDirectionEnu,
562             label,
563             circuitId,
564             administrativeStateEnu,
565             operationalStateEnu,
566             logicalConnectionPoint,
567             partnerPortCircuitPackName,
568             partnerPortPortName,
569             parentPortCircuitPackName,
570             parentPortPortName,
571             roadmPortPortPowerCapabilityMinRx,
572             roadmPortPortPowerCapabilityMinTx,
573             roadmPortPortPowerCapabilityMaxRx,
574             roadmPortPortPowerCapabilityMaxTx,
575             //roadmPortCapableWavelengths,
576             //roadmPortAvailableWavelengths,
577             //roadmPortUsedWavelengths,
578             "", "", "",
579             transponderPortPortPowerCapabilityMinRx,
580             transponderPortPortPowerCapabilityMinTx,
581             transponderPortPortPowerCapabilityMaxRx,
582             transponderPortPortPowerCapabilityMaxTx,
583             //transponderPortCapableWavelengths,
584             "",
585             otdrPortLaunchCableLength,
586             otdrPortPortDirection,
587             //ilaPortPortPowerCapabilityMixRx,
588             //ilaPortPortPowerCapabilityMixTx,
589             //ilaPortPortPowerCapabilityMaxRx,
590             //ilaPortPortPowerCapabilityMaxTx,
591             "", "", "", "",
592             startTimestamp,
593             startTimestamp
594         };
595     }
596
597
598     private static Object[] prepareCircuitPacksParameters(String nodeId, CircuitPacks cpack) {
599         String startTimestamp = getCurrentTimestamp();
600         return new Object[]{nodeId,
601             cpack.getCircuitPackName(),
602             cpack.getCircuitPackType(),
603             cpack.getCircuitPackProductCode(),
604             (cpack.getAdministrativeState() == null ? "" : cpack.getAdministrativeState().getIntValue()),
605             cpack.getVendor(),
606             cpack.getModel(),
607             cpack.getSerialId(),
608             cpack.getType(),
609             cpack.getProductCode(),
610             (cpack.getManufactureDate() == null ? "" : cpack.getManufactureDate().getValue()),
611             cpack.getClei(),
612             cpack.getHardwareVersion(),
613             (cpack.getOperationalState() == null ? -1 : cpack.getOperationalState().getIntValue()),
614             cpack.getCircuitPackCategory().getType().getIntValue(),
615             cpack.getCircuitPackCategory().getExtension(),
616             (cpack.getEquipmentState() == null ? -1 : cpack.getEquipmentState().getIntValue()),
617             cpack.getCircuitPackMode(),
618             cpack.getShelf(),
619             cpack.getSlot(),
620             cpack.getSubSlot(),
621             prepareEmptyString(cpack.getDueDate()),
622             prepareEmptyString((cpack.getParentCircuitPack() == null) ? "" :
623                 ((cpack.getParentCircuitPack().getCircuitPackName() == null) ? "" :
624                     cpack.getParentCircuitPack().getCircuitPackName())
625             ),
626             prepareEmptyString((cpack.getParentCircuitPack() == null) ? "" :
627                 ((cpack.getParentCircuitPack().getCpSlotName() == null) ? "" :
628                     cpack.getParentCircuitPack().getCpSlotName())
629             ),
630             startTimestamp,
631             startTimestamp};
632     }
633
634
635     private void persistCPPorts(String nodeId, Connection connection, CircuitPacks circuitPacks) {
636         @NonNull
637         Map<PortsKey, Ports> nonnullPorts = circuitPacks.nonnullPorts();
638         for (Map.Entry<PortsKey, Ports> entry : nonnullPorts.entrySet()) {
639             Object[] cpPortsParameters = prepareCPPortsParameters(nodeId, circuitPacks, entry.getValue());
640             String query = Queries.getQuery().deviceCPPortInsert().get();
641             LOG.info("Running {} query ", query);
642             try (PreparedStatement preparedStmt = connection.prepareStatement(query)) {
643                 for (int j = 0; j < cpPortsParameters.length; j++) {
644                     preparedStmt.setObject(j + 1, cpPortsParameters[j]);
645                 }
646                 preparedStmt.execute();
647                 preparedStmt.clearParameters();
648             } catch (SQLException e) {
649                 LOG.error("Something wrong when storing Cirtcuits Packs Ports in DB", e);
650             }
651         }
652     }
653
654
655     private Object[] prepareDevInterfaceParameters(String nodeId, Interface deviceInterface, Connection connection) {
656
657         String ethernetDuplexEnu = "";
658         String ethernetAutoNegotiationEnu = "";
659         String maintTestsignalTestpatternEnu = "";
660         String maintTestsignalTypeEnu = "";
661         String otuFecEnu = "";
662         String otuMaintTypeEnu = "";
663         //String otsFiberTypeEnu = "";
664         String ethernetSpeed = "-1";
665         String ethernetFec = "";
666         String ethernetMtu = "-1";
667         String ethernetCurrSpeed = "";
668         String ethernetCurrDuplex = "-1";
669         //String mciMcttpMinFreq = "";
670         //String mciMcttpMaxFreq = "";
671         //String mciMcttpCenterFreq = "";
672         //String mciMcttpSlotWidth = "";
673         //String mciNmcCtpFrequency = "";
674         //String mciNmcCtpWidth = "";
675         String ochRate = "";
676         //String ochFrequency = "";
677         //String ochWidth = "";
678         String ochWavelengthNumber = "";
679         String ochModulationFormat = "";
680         String ochTransmitPower = "";
681         String otsSpanLossReceive = "";
682         String otsSpanLossTransmit = "";
683         //String otsIngressSpanLossAgingMargin = "";
684         //String otsEolMaxLoadPin = "";
685         String oduRate = "";
686         //String oduFunction = "";
687         String oduMonitoringMode = "";
688         //String oduNoOamFunction = "";
689         String oduProactiveDelayMeasurementEnabled = "";
690         //String oduPoaTribPortNumber = "-1";
691         //String oduTxSapi = "";
692         //String oduTxDapi = "";
693         //String oduTxOperator = "";
694         //String oduAcceptedSapi = "";
695         //String oduAcceptedDapi = "";
696         //String oduAcceptedOperator = "";
697         //String oduExpectedSapi = "";
698         //String oduExpectedDapi = "";
699         //String oduTimActEnabled = "";
700         //String oduTimDetectMode = "";
701         //String oduDegmIntervals = "-1";
702         //String oduDegthrPercentage = "-1";
703         String opuPayloadType = "";
704         String opuRxPayloadType = "";
705         String opuExpPayloadType = "";
706         String opuPayloadInterface = "";
707         String maintTestsignalEnabled = "";
708         String maintTestsignalBiterrors = "-1";
709         String maintTestsignalBiterrorsterminal = "-1";
710         String maintTestsignalSyncseconds = "-1";
711         String maintTestsignalSyncsecondsterminal = "-1";
712         String otuRate = "";
713         //String otuTxSapi = "";
714         //String otuTxDapi = "";
715         //String otuTxOperator = "";
716         //String otuAcceptedSapi = "";
717         //String otuAcceptedDapi = "";
718         //String otuAcceptedOperator = "";
719         //String otuExpectedSapi = "";
720         //String otuExpectedDapi = "";
721         //String otuTimActEnabled = "";
722         //String otuTimDetectMode = "";
723         //String otuDegmIntervals = "-1";
724         //String otuDegthrPercentage = "-1";
725         String otuMaintLoopbackEnabled = "";
726         //String mtOtuRate = "";
727         //String mtOtuFec = "";
728         //String mtOtuMaintLoopback = "";
729         //String mtOtuEnabled = "";
730         //String mtOtuType = "";
731
732         String name = deviceInterface.getName();
733         String description = deviceInterface.getDescription();
734         String type = deviceInterface.getType().toString();
735         String administrativeStateEnu = deviceInterface.getAdministrativeState().getName();
736         int operationalState = deviceInterface.getOperationalState().getIntValue();
737         String circuitId = deviceInterface.getCircuitId();
738         String supportingInterface = deviceInterface.getSupportingInterface();
739         String supportingCircuitPackName = deviceInterface.getSupportingCircuitPackName();
740         String supportingPort = deviceInterface.getSupportingPort();
741
742         switch (deviceInterface.getType().toString()) {
743
744             case "ethernet":
745                 //EthernetBuilder ethIfBuilder = new EthernetBuilder();
746                 EthernetBuilder ethIfBuilder = new EthernetBuilder(deviceInterface.augmentation(Interface1.class)
747                     .getEthernet());
748                 ethernetSpeed = (ethIfBuilder.getSpeed() == null ? "-1" :
749                     Integer.toString(ethIfBuilder.getSpeed().intValue()));
750                 ethernetFec = ethIfBuilder.getFec().getName();
751                 ethernetDuplexEnu = (ethIfBuilder.getDuplex() == null ? "" : ethIfBuilder.getDuplex().getName());
752                 ethernetMtu = ethIfBuilder.getMtu().toString();
753                 ethernetAutoNegotiationEnu = ethIfBuilder.getAutoNegotiation().getName();
754                 ethernetCurrSpeed = ethIfBuilder.getCurrSpeed();
755                 ethernetCurrDuplex = ethIfBuilder.getCurrDuplex();
756                 break;
757
758             case "och":
759                 OchBuilder ochIfBuilder = new OchBuilder(deviceInterface.augmentation(
760                     org.opendaylight.yang.gen.v1
761                         .http.org.openroadm.optical.channel.interfaces.rev161014.Interface1.class)
762                     .getOch());
763                 ochRate = ochIfBuilder.getRate().toString();
764                 ochWavelengthNumber = ochIfBuilder.getWavelengthNumber().toString();
765                 ochModulationFormat = ochIfBuilder.getModulationFormat().getName();
766                 ochTransmitPower = ochIfBuilder.getTransmitPower().toString();
767                 break;
768
769             case "ots":
770                 OtsBuilder otsIfBuilder = new OtsBuilder(deviceInterface.augmentation(
771                     org.opendaylight.yang.gen.v1
772                         .http.org.openroadm.optical.transport.interfaces.rev161014.Interface1.class)
773                     .getOts());
774                 //otsFiberTypeEnu = String.valueOf(otsIfBuilder.getFiberType().getIntValue());
775                 otsSpanLossReceive = otsIfBuilder.getSpanLossReceive().toString();
776                 otsSpanLossTransmit = otsIfBuilder.getSpanLossTransmit().toString();
777                 break;
778
779             case "odu":
780                 OduBuilder oduIfBuilder = new OduBuilder(deviceInterface.augmentation(
781                         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.odu.interfaces.rev161014.Interface1.class)
782                     .getOdu());
783                 oduRate = String.valueOf(oduIfBuilder.getRate());
784                 oduMonitoringMode = oduIfBuilder.getMonitoringMode().getName();
785                 oduProactiveDelayMeasurementEnabled = oduIfBuilder.getProactiveDelayMeasurementEnabled().toString();
786
787                 persistDevInterfaceTcm(nodeId, name, oduIfBuilder, connection);
788                 persistDevInterfaceOtnOduTxMsi(nodeId, name, oduIfBuilder, connection);
789                 persistDevInterfaceOtnOduRxMsi(nodeId, name, oduIfBuilder, connection);
790                 persistDevInterfaceOtnOduExpMsi(nodeId, name, oduIfBuilder, connection);
791
792                 opuPayloadType = oduIfBuilder.getOpu().getPayloadType();
793                 opuRxPayloadType = oduIfBuilder.getOpu().getRxPayloadType();
794                 opuExpPayloadType = oduIfBuilder.getOpu().getExpPayloadType();
795                 opuPayloadInterface = oduIfBuilder.getOpu().getPayloadInterface();
796                         /*persistDevInterfaceOtnOduTxMsi(nodeId,name,oduIfBuilder,connection);
797                         persistDevInterfaceOtnOduRxMsi(nodeId,name,oduIfBuilder,connection);
798                         persistDevInterfaceOtnOduExpMsi(nodeId,name,oduIfBuilder,connection); */
799                 maintTestsignalEnabled = oduIfBuilder.getMaintTestsignal().getEnabled().toString();
800                 maintTestsignalTestpatternEnu = oduIfBuilder.getMaintTestsignal().getTestPattern().getName();
801                 maintTestsignalTypeEnu = oduIfBuilder.getMaintTestsignal().getType().getName();
802                 maintTestsignalBiterrors = Integer.toString(
803                         oduIfBuilder.getMaintTestsignal().getBitErrors().intValue());
804                 maintTestsignalBiterrorsterminal = oduIfBuilder.getMaintTestsignal().getBitErrorsTerminal().toString();
805                 maintTestsignalSyncseconds = oduIfBuilder.getMaintTestsignal().getSyncSeconds();
806                 maintTestsignalSyncsecondsterminal = oduIfBuilder.getMaintTestsignal().getSyncSecondsTerminal();
807                 break;
808
809             case "otu":
810                 OtuBuilder otuIfBuilder =
811                     new OtuBuilder(deviceInterface.augmentation(
812                         org.opendaylight.yang.gen.v1.http.org.openroadm.otn.otu.interfaces.rev161014.Interface1.class)
813                     .getOtu());
814                 otuRate = otuIfBuilder.getRate().toString();
815                 otuFecEnu = otuIfBuilder.getFec().getName();
816                 otuMaintLoopbackEnabled = otuIfBuilder.getMaintLoopback().getEnabled().toString();
817                 otuMaintTypeEnu = otuIfBuilder.getMaintLoopback().getType().getName();
818                 break;
819
820             default:
821                 LOG.error("could not get interface type");
822
823         }
824
825         String startTimestamp = getCurrentTimestamp();
826
827         return new Object[]{nodeId,
828             name,
829             description,
830             type,
831             administrativeStateEnu,
832             Integer.toString(operationalState),
833             circuitId,
834             supportingInterface,
835             supportingCircuitPackName,
836             supportingPort,
837             ethernetSpeed,
838             ethernetFec,
839             ethernetDuplexEnu,
840             ethernetMtu,
841             ethernetAutoNegotiationEnu,
842             ethernetCurrSpeed,
843             ethernetCurrDuplex,
844             //mciMcttpMinFreq,
845             //mciMcttpMaxFreq,
846             //mciMcttpCenterFreq,
847             //mciMcttpSlotWidth,
848             //mciNmcCtpFrequency,
849             //mciNmcCtpWidth,
850             "", "", "", "", "", "",
851             ochRate,
852             //ochFrequency,
853             //ochWidth,
854             "", "",
855             ochWavelengthNumber,
856             ochModulationFormat,
857             ochTransmitPower,
858             //otsFiberTypeEnu,
859             otsSpanLossReceive,
860             otsSpanLossTransmit,
861             //otsIngressSpanLossAgingMargin,
862             //otsEolMaxLoadPin,
863             "", "",
864             oduRate,
865             //oduFunction,
866             "",
867             oduMonitoringMode,
868             //oduNoOamFunction,
869             "",
870             oduProactiveDelayMeasurementEnabled,
871             //oduPoaTribPortNumber,
872             //oduTxSapi,
873             //oduTxDapi,
874             //oduTxOperator,
875             //oduAcceptedSapi,
876             //oduAcceptedDapi,
877             //oduAcceptedOperator,
878             //oduExpectedSapi,
879             //oduExpectedDapi,
880             //oduTimActEnabled,
881             //oduTimDetectMode,
882             //oduDegmIntervals,
883             //oduDegthrPercentage,
884             "-1", "", "", "", "", "","", "", "", "", "", "-1", "-1",
885             opuPayloadType,
886             opuRxPayloadType,
887             opuExpPayloadType,
888             opuPayloadInterface,
889             maintTestsignalEnabled,
890             maintTestsignalTestpatternEnu,
891             maintTestsignalTypeEnu,
892             maintTestsignalBiterrors,
893             maintTestsignalBiterrorsterminal,
894             maintTestsignalSyncseconds,
895             maintTestsignalSyncsecondsterminal,
896             otuRate,
897             otuFecEnu,
898             //otuTxSapi,
899             //otuTxDapi,
900             //otuTxOperator,
901             //otuAcceptedSapi,
902             //otuAcceptedDapi,
903             //otuAcceptedOperator,
904             //otuExpectedSapi,
905             //otuExpectedDapi,
906             //otuTimActEnabled,
907             //otuTimDetectMode,
908             //otuDegmIntervals,
909             //otuDegthrPercentage,
910             "", "", "", "", "", "","", "", "", "", "-1", "-1",
911             otuMaintLoopbackEnabled,
912             otuMaintTypeEnu,
913             //mtOtuRate,
914             //mtOtuFec,
915             //mtOtuMaintLoopback,
916             //mtOtuEnabled,
917             //mtOtuType,
918             "", "", "", "", "",
919             startTimestamp,
920             startTimestamp
921         };
922
923     }
924
925     private static Object[] prepareDevInterfaceTcmParameters(String nodeId, String interfaceName, Tcm tcm) {
926
927         String layer = tcm.getLayer().toString();
928         String monitoringModeEnu = tcm.getMonitoringMode().getName();
929         String ltcActEnabled = tcm.getLtcActEnabled().toString();
930         String proactiveDelayMeasurementEnabled = tcm.getProactiveDelayMeasurementEnabled().toString();
931         //String tcmDirectionEnu = "";
932         //String timDetectModeEnu = "";
933         //String txSapi = "";
934         //String txDapi = "";
935         //String txOperator = "";
936         //String acceptedSapi = "";
937         //String acceptedDapi = "";
938         //String acceptedOperator = "";
939         //String expectedSapi = "";
940         //String expectedDapi = "";
941         //String timActEnabled = "";
942         //String degmIntervals = "";
943         //String degthrPercentage = "";
944         String startTimestamp = getCurrentTimestamp();
945
946         return new Object[]{nodeId,
947             interfaceName,
948             layer,
949             monitoringModeEnu,
950             ltcActEnabled,
951             proactiveDelayMeasurementEnabled,
952             //tcmDirectionEnu,
953             //txSapi,
954             //txDapi,
955             //txOperator,
956             //acceptedSapi,
957             //acceptedDapi,
958             //acceptedOperator,
959             //expectedSapi,
960             //expectedDapi,
961             //timActEnabled,
962             //timDetectModeEnu,
963             //degmIntervals,
964             //degthrPercentage,
965             "", "", "", "", "", "", "", "", "", "", "", "", "",
966             startTimestamp,
967             startTimestamp};
968
969     }
970
971     private static Object[] prepareDevInterfaceOtnOduTxMsiParameters(String nodeId, String interfaceName, TxMsi txMsi) {
972
973         String tribSlot = txMsi.getTribSlot().toString();
974         String odtuType = txMsi.getOdtuType().toString();
975         String tribPort = txMsi.getTribPort().toString();
976         String tribPortPayload = txMsi.getTribPortPayload();
977
978         String startTimestamp = getCurrentTimestamp();
979
980         return new Object[]{nodeId,
981             interfaceName,
982             tribSlot,
983             odtuType,
984             tribPort,
985             tribPortPayload,
986             startTimestamp,
987             startTimestamp
988         };
989
990     }
991
992     private static Object[] prepareDevInterfaceOtnOduRxMsiParameters(String nodeId, String interfaceName, RxMsi rxMsi) {
993
994         String tribSlot = rxMsi.getTribSlot().toString();
995         String odtuType = rxMsi.getOdtuType().toString();
996         String tribPort = rxMsi.getTribPort().toString();
997         String tribPortPayload = rxMsi.getTribPortPayload();
998
999         String startTimestamp = getCurrentTimestamp();
1000
1001         return new Object[]{nodeId,
1002             interfaceName,
1003             tribSlot,
1004             odtuType,
1005             tribPort,
1006             tribPortPayload,
1007             startTimestamp,
1008             startTimestamp
1009         };
1010
1011     }
1012
1013
1014     private static Object[] prepareDevInterfaceOtnOduExpMsiParameters(String nodeId, String interfaceName,
1015         ExpMsi expMsi) {
1016
1017         String tribSlot = expMsi.getTribSlot().toString();
1018         String odtuType = expMsi.getOdtuType().toString();
1019         String tribPort = expMsi.getTribPort().toString();
1020         String tribPortPayload = expMsi.getTribPortPayload();
1021
1022         String startTimestamp = getCurrentTimestamp();
1023
1024         return new Object[]{nodeId,
1025             interfaceName,
1026             tribSlot,
1027             odtuType,
1028             tribPort,
1029             tribPortPayload,
1030             startTimestamp,
1031             startTimestamp
1032         };
1033
1034     }
1035
1036     private void persistDevInterfaces(String nodeId, Connection connection) {
1037
1038         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier
1039             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1040             .build();
1041         Optional<OrgOpenroadmDevice> deviceObject =
1042                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, deviceIID,
1043                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1044         if (!deviceObject.isPresent()) {
1045             return;
1046         }
1047         Map<InterfaceKey, Interface> interfaceMap = deviceObject.orElseThrow().nonnullInterface();
1048         for (Map.Entry<InterfaceKey, Interface> interfaceEntrySet : interfaceMap.entrySet()) {
1049             Interface deviceInterface = interfaceEntrySet.getValue();
1050             Object[] parameters = prepareDevInterfaceParameters(nodeId, deviceInterface, connection);
1051
1052             String query = Queries.getQuery().deviceInterfacesInsert().get();
1053             LOG.info("Running {} query ", query);
1054             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1055                 for (int j = 0; j < parameters.length; j++) {
1056                     stmt.setObject(j + 1, parameters[j]);
1057                 }
1058                 stmt.execute();
1059                 stmt.clearParameters();
1060             } catch (SQLException e) {
1061                 LOG.error("Something wrong when storing devices interfaces in DB", e);
1062             }
1063         }
1064     }
1065
1066     private void persistDevProtocols(String nodeId, Connection connection) {
1067
1068         InstanceIdentifier<Protocols> protocolsIID = InstanceIdentifier
1069             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1070             .child(Protocols.class)
1071             .build();
1072         Optional<Protocols> protocolObject =
1073                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION, protocolsIID,
1074                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1075         if (!protocolObject.isPresent() || protocolObject.orElseThrow().augmentation(Protocols1.class) == null) {
1076             LOG.error("LLDP subtree is missing");
1077             return;
1078         }
1079         String adminstatusEnu = protocolObject.orElseThrow().augmentation(Protocols1.class).getLldp().getGlobalConfig()
1080             .getAdminStatus().getName();
1081         String msgTxtInterval = protocolObject.orElseThrow().augmentation(Protocols1.class).getLldp().getGlobalConfig()
1082             .getMsgTxInterval().toString();
1083         String mxgTxHoldMultiplier = protocolObject.orElseThrow().augmentation(Protocols1.class).getLldp()
1084             .getGlobalConfig().getMsgTxHoldMultiplier().toString();
1085         String startTimestamp = getCurrentTimestamp();
1086         persistDevProtocolLldpPortConfig(nodeId, connection);
1087         persistDevProtocolLldpNbrList(nodeId, connection);
1088
1089         Object[] parameters = {nodeId,
1090             adminstatusEnu,
1091             msgTxtInterval,
1092             mxgTxHoldMultiplier,
1093             startTimestamp,
1094             startTimestamp
1095         };
1096
1097         String query = Queries.getQuery().deviceProtocolInsert().get();
1098         LOG.info("Running {} query ", query);
1099         try (PreparedStatement stmt = connection.prepareStatement(query)) {
1100             for (int j = 0; j < parameters.length; j++) {
1101                 stmt.setObject(j + 1, parameters[j]);
1102             }
1103             stmt.execute();
1104             stmt.clearParameters();
1105         } catch (SQLException e) {
1106             LOG.error("Something wrong when storing devices protocols in DB", e);
1107         }
1108     }
1109
1110
1111     private void persistDevProtocolLldpPortConfig(String nodeId, Connection connection) {
1112
1113         InstanceIdentifier<Protocols> protocolsIID = InstanceIdentifier
1114             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1115             .child(Protocols.class)
1116             .build();
1117         Optional<Protocols> protocolObject =
1118                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION, protocolsIID,
1119                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1120         if (!protocolObject.isPresent() || protocolObject.orElseThrow().augmentation(Protocols1.class) == null) {
1121             LOG.error("LLDP subtree is missing");
1122             return;
1123         }
1124         String startTimestamp = getCurrentTimestamp();
1125         @NonNull
1126         Map<PortConfigKey, PortConfig> portConfigMap = protocolObject.orElseThrow()
1127             .augmentation(Protocols1.class).getLldp().nonnullPortConfig();
1128         for (Map.Entry<PortConfigKey, PortConfig> entry : portConfigMap.entrySet()) {
1129             PortConfig portConfig = entry.getValue();
1130             String ifName = portConfig.getIfName();
1131             String adminStatusEnu = portConfig.getAdminStatus().getName();
1132
1133             Object[] parameters = {nodeId,
1134                 ifName,
1135                 adminStatusEnu,
1136                 startTimestamp,
1137                 startTimestamp
1138             };
1139
1140             String query = Queries.getQuery().deviceProtocolPortConfigInsert().get();
1141             LOG.info("Running {} query ", query);
1142             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1143                 for (int j = 0; j < parameters.length; j++) {
1144                     stmt.setObject(j + 1, parameters[j]);
1145                 }
1146                 stmt.execute();
1147                 stmt.clearParameters();
1148             } catch (SQLException e) {
1149                 LOG.error("Something wrong when storing devices protocols LLDP Port config in DB", e);
1150             }
1151
1152         }
1153
1154     }
1155
1156     private void persistDevProtocolLldpNbrList(String nodeId, Connection connection) {
1157
1158         InstanceIdentifier<Protocols> protocolsIID = InstanceIdentifier
1159             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1160             .child(Protocols.class)
1161             .build();
1162         Optional<Protocols> protocolObject =
1163                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION, protocolsIID,
1164                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1165         if (!protocolObject.isPresent()) {
1166             LOG.error("Protocols is missing");
1167             return;
1168         }
1169         if (protocolObject.orElseThrow().augmentation(Protocols1.class).getLldp().getNbrList() == null) {
1170             protocolObject =
1171                     deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, protocolsIID,
1172                             Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1173             if (protocolObject.orElseThrow().augmentation(Protocols1.class).getLldp().getNbrList() == null) {
1174                 LOG.error("LLDP nbrlist subtree is missing for {}", nodeId);
1175                 return;
1176             }
1177         }
1178         String startTimestamp = getCurrentTimestamp();
1179         Map<IfNameKey, IfName> ifNameMap =
1180                 protocolObject.orElseThrow().augmentation(Protocols1.class).getLldp().getNbrList().nonnullIfName();
1181         for (Map.Entry<IfNameKey, IfName> ifNameEntry : ifNameMap.entrySet()) {
1182
1183             IfName ifNameObj = ifNameEntry.getValue();
1184             String ifName = ifNameObj.getIfName();
1185             String remotesysname = ifNameObj.getRemoteSysName();
1186             String remotemgmtaddresssubtype = ifNameObj.getRemoteMgmtAddressSubType().getName();
1187             String remotemgmtaddress = ifNameObj.getRemoteMgmtAddress().getIpv4Address().toString();
1188             String remoteportidsubtypeEnu = ifNameObj.getRemotePortIdSubType().getName();
1189             String remoteportid = ifNameObj.getRemotePortId();
1190             String remotechassisidsubtypeEnu = ifNameObj.getRemoteChassisIdSubType().getName();
1191             String remotechassisid = ifNameObj.getRemoteChassisId();
1192
1193             Object[] parameters = {nodeId,
1194                 ifName,
1195                 remotesysname,
1196                 remotemgmtaddresssubtype,
1197                 remotemgmtaddress,
1198                 remoteportidsubtypeEnu,
1199                 remoteportid,
1200                 remotechassisidsubtypeEnu,
1201                 remotechassisid,
1202                 startTimestamp,
1203                 startTimestamp
1204             };
1205
1206             String query = Queries.getQuery().deviceProtocolLldpNbrlistInsert().get();
1207             LOG.info("Running {} query ", query);
1208             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1209                 for (int j = 0; j < parameters.length; j++) {
1210                     stmt.setObject(j + 1, parameters[j]);
1211                 }
1212                 stmt.execute();
1213                 stmt.clearParameters();
1214             } catch (SQLException e) {
1215                 LOG.error("Something wrong when storing devices protocols LLDP list number in DB", e);
1216             }
1217
1218         }
1219     }
1220
1221     private void persistDevInternalLinks(String nodeId, Connection connection) {
1222
1223         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier
1224             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1225             .build();
1226         Optional<OrgOpenroadmDevice> deviceObject =
1227                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, deviceIID,
1228                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1229         if (!deviceObject.isPresent()) {
1230             return;
1231         }
1232         if (deviceObject.orElseThrow().getInternalLink() == null) {
1233             deviceObject = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
1234                     deviceIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1235             if (deviceObject.orElseThrow().getInternalLink() == null) {
1236                 LOG.info("External links not found for {}", nodeId);
1237                 return;
1238             }
1239         }
1240         @NonNull
1241         Map<InternalLinkKey, InternalLink> internalLinkMap = deviceObject.orElseThrow().nonnullInternalLink();
1242         String startTimestamp = getCurrentTimestamp();
1243         for (Map.Entry<InternalLinkKey, InternalLink> internalLinkEntry: internalLinkMap.entrySet()) {
1244             InternalLink internalLink = internalLinkEntry.getValue();
1245             String internalLinkName = internalLink.getInternalLinkName();
1246             String sourceCircuitPackName = internalLink.getSource().getCircuitPackName();
1247             String sourcePortName = internalLink.getSource().getPortName();
1248             String destinationCircuitPackName = internalLink.getDestination().getCircuitPackName();
1249             String destinationPortName = internalLink.getDestination().getPortName();
1250
1251             Object[] parameters = { nodeId, internalLinkName, sourceCircuitPackName, sourcePortName,
1252                 destinationCircuitPackName, destinationPortName, startTimestamp, startTimestamp };
1253             String query = Queries.getQuery().deviceInternalLinkInsert().get();
1254             LOG.info("Running {} query ", query);
1255             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1256                 for (int j = 0; j < parameters.length; j++) {
1257                     stmt.setObject(j + 1, parameters[j]);
1258                 }
1259                 stmt.execute();
1260                 stmt.clearParameters();
1261             } catch (SQLException e) {
1262                 LOG.error("Something wrong when storing devices internal links", e);
1263             }
1264         }
1265     }
1266
1267
1268     private void persistDevExternalLinks(String nodeId, Connection connection) {
1269
1270         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier
1271             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1272             .build();
1273         Optional<OrgOpenroadmDevice> deviceObject =
1274                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, deviceIID,
1275                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1276         if (!deviceObject.isPresent()) {
1277             return;
1278         }
1279         if (deviceObject.orElseThrow().getExternalLink() == null) {
1280             deviceObject = deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION,
1281                     deviceIID, Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1282             if (deviceObject.orElseThrow().getExternalLink() == null) {
1283                 LOG.info("External links not found for {}", nodeId);
1284                 return;
1285             }
1286         }
1287         String startTimestamp = getCurrentTimestamp();
1288         @NonNull
1289         Map<ExternalLinkKey, ExternalLink> externalLinkMap = deviceObject.orElseThrow().nonnullExternalLink();
1290         for (Map.Entry<ExternalLinkKey, ExternalLink> externalLinkEntry: externalLinkMap.entrySet()) {
1291             ExternalLink externalLink = externalLinkEntry.getValue();
1292             String externalLinkName = externalLink.getExternalLinkName();
1293             String sourceNodeId = externalLink.getSource().getNodeId();
1294             String sourceCircuitPackName = externalLink.getSource().getCircuitPackName();
1295             String sourcePortName = externalLink.getSource().getPortName();
1296             String destinationNodeId = externalLink.getDestination().getNodeId();
1297             String destinationCircuitPackName = externalLink.getDestination().getCircuitPackName();
1298             String destinationPortName = externalLink.getDestination().getPortName();
1299
1300             Object[] parameters = { nodeId, externalLinkName, sourceNodeId, sourceCircuitPackName,
1301                 sourcePortName, destinationNodeId, destinationCircuitPackName, destinationPortName,
1302                 startTimestamp, startTimestamp };
1303
1304             String query = Queries.getQuery().deviceExternalLinkInsert().get();
1305             LOG.info("Running {} query ", query);
1306             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1307                 for (int j = 0; j < parameters.length; j++) {
1308                     stmt.setObject(j + 1, parameters[j]);
1309                 }
1310                 stmt.execute();
1311                 stmt.clearParameters();
1312             } catch (SQLException e) {
1313                 LOG.error("Something wrong when storing devices external links", e);
1314             }
1315         }
1316     }
1317
1318     private void persistDevPhysicalLinks(String nodeId, Connection connection) {
1319
1320         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier
1321             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1322             .build();
1323         Optional<OrgOpenroadmDevice> deviceObject =
1324                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, deviceIID,
1325                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1326         if (!deviceObject.isPresent()) {
1327             LOG.error("No device with node Id {}", nodeId);
1328             return;
1329         }
1330         if (deviceObject.orElseThrow().getPhysicalLink() == null) {
1331             deviceObject =
1332                     deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION, deviceIID,
1333                             Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1334             if (!deviceObject.isPresent()) {
1335                 LOG.error("No device with node Id {}", nodeId);
1336                 return;
1337             }
1338             if (deviceObject.orElseThrow().getPhysicalLink() == null) {
1339                 LOG.info("Physical links not found for {}", nodeId);
1340                 return;
1341             }
1342         }
1343
1344         String startTimestamp = getCurrentTimestamp();
1345         @NonNull
1346         Map<PhysicalLinkKey, PhysicalLink> physicalLinkMap = deviceObject.orElseThrow().nonnullPhysicalLink();
1347         for (Map.Entry<PhysicalLinkKey, PhysicalLink> physicalLinkEntry : physicalLinkMap.entrySet()) {
1348             PhysicalLink physicalLink = physicalLinkEntry.getValue();
1349             String physicalLinkName = physicalLink.getPhysicalLinkName();
1350             String sourceCircuitPackName = physicalLink.getSource().getCircuitPackName();
1351             String sourcePortName = physicalLink.getSource().getPortName();
1352             String destinationCircuitPackName = physicalLink.getDestination().getCircuitPackName();
1353             String destinationPortName = physicalLink.getDestination().getPortName();
1354
1355             Object[] parameters = {nodeId,
1356                 physicalLinkName,
1357                 sourceCircuitPackName,
1358                 sourcePortName,
1359                 destinationCircuitPackName,
1360                 destinationPortName,
1361                 startTimestamp,
1362                 startTimestamp
1363             };
1364
1365             String query = Queries.getQuery().devicePhysicalLinkInsert().get();
1366             LOG.info("Running {} query ", query);
1367             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1368                 for (int j = 0; j < parameters.length; j++) {
1369                     stmt.setObject(j + 1, parameters[j]);
1370                 }
1371                 stmt.execute();
1372                 stmt.clearParameters();
1373             } catch (SQLException e) {
1374                 LOG.error("Something wrong when storing devices physical links", e);
1375             }
1376
1377         }
1378     }
1379
1380     private void persistDevDegree(String nodeId, Connection connection) {
1381
1382         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier
1383             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1384             .build();
1385         Optional<OrgOpenroadmDevice> deviceObject =
1386                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, deviceIID,
1387                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1388
1389
1390         /*if (deviceObject.get().getDegree()==null){
1391             deviceObject =
1392                     deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION, deviceIID,
1393                             Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1394         } */
1395         if (!deviceObject.isPresent()) {
1396             LOG.error("Cannot get device for node {}", nodeId);
1397             return;
1398         }
1399         String startTimestamp = getCurrentTimestamp();
1400         @NonNull
1401         Map<DegreeKey, Degree> degreeMap = deviceObject.orElseThrow().nonnullDegree();
1402         for (Map.Entry<DegreeKey, Degree> degreeEntry : degreeMap.entrySet()) {
1403             Degree degree = degreeEntry.getValue();
1404             String degreeNumber = degree.getDegreeNumber().toString();
1405             String maxWavelengths = degree.getMaxWavelengths().toString();
1406             String otdrPortCircuitPackName =
1407                     (degree.getOtdrPort() == null ? "" : degree.getOtdrPort().getCircuitPackName());
1408             String otdrPortPortName =
1409                     (degree.getOtdrPort() == null ? "" : degree.getOtdrPort().getPortName());
1410             // String mcCapabilitiesSlotWidthGranularity = "";
1411             // String mcCapabilitiesCenterFreqGranularity = "";
1412             // String mcCapabilitiesMinSlots = "-1";
1413             // String mcCapabilitiesMaxSlots = "-1";
1414             persistDevDegreeCircuitPack(nodeId, degree, degreeNumber, connection);
1415             persistDevDegreeConnectionPort(nodeId, degree, degreeNumber, connection);
1416
1417             Object[] parameters = { nodeId, degreeNumber, maxWavelengths, otdrPortCircuitPackName, otdrPortPortName,
1418                     // mcCapabilitiesSlotWidthGranularity,
1419                     // mcCapabilitiesCenterFreqGranularity,
1420                     // mcCapabilitiesMinSlots,
1421                     // mcCapabilitiesMaxSlots,
1422                 "", "", "-1", "-1", startTimestamp, startTimestamp };
1423
1424             String query = Queries.getQuery().deviceDegreeInsert().get();
1425             LOG.info("Running {} query ", query);
1426             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1427                 for (int j = 0; j < parameters.length; j++) {
1428                     stmt.setObject(j + 1, parameters[j]);
1429                 }
1430                 stmt.execute();
1431                 stmt.clearParameters();
1432             } catch (SQLException e) {
1433                 LOG.error("Something wrong when storing devices degrees", e);
1434             }
1435
1436         }
1437     }
1438
1439
1440     private void persistDevDegreeCircuitPack(String nodeId, Degree degree, String degreeNumber, Connection connection) {
1441
1442         String startTimestamp = getCurrentTimestamp();
1443         @NonNull
1444         Map<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.CircuitPacksKey,
1445                 org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.CircuitPacks>
1446             circuitPacksMap = degree.nonnullCircuitPacks();
1447         for (Map.Entry<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.CircuitPacksKey,
1448                 org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.CircuitPacks> circuitPackEntry :
1449                     circuitPacksMap.entrySet()) {
1450
1451             String circuitPackIndex = circuitPackEntry.getValue().getIndex().toString();
1452             String circuitPackName = circuitPackEntry.getValue().getCircuitPackName();
1453
1454             Object[] parameters = {nodeId,
1455                 degreeNumber,
1456                 circuitPackIndex,
1457                 circuitPackName,
1458                 startTimestamp,
1459                 startTimestamp
1460             };
1461
1462             String query = Queries.getQuery().deviceDegreeCircuitPackInsert().get();
1463             LOG.info("Running {} query ", query);
1464             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1465                 for (int j = 0; j < parameters.length; j++) {
1466                     stmt.setObject(j + 1, parameters[j]);
1467                 }
1468                 stmt.execute();
1469                 stmt.clearParameters();
1470             } catch (SQLException e) {
1471                 LOG.error("Something wrong when storing devices degrees circuit packs", e);
1472             }
1473
1474         }
1475     }
1476
1477     private void persistDevDegreeConnectionPort(String nodeId, Degree degree, String degreeNumber,
1478         Connection connection) {
1479
1480         String startTimestamp = getCurrentTimestamp();
1481         @NonNull
1482         Map<ConnectionPortsKey, ConnectionPorts> connectionPortsMap = degree.nonnullConnectionPorts();
1483         for (Map.Entry<ConnectionPortsKey, ConnectionPorts> portEntry : connectionPortsMap.entrySet()) {
1484             String connectionPortIndex = portEntry.getValue().getIndex().toString();
1485             String circuitPackName = portEntry.getValue().getCircuitPackName();
1486             String portName = portEntry.getValue().getPortName();
1487
1488             Object[] parameters = {nodeId,
1489                 degreeNumber,
1490                 connectionPortIndex,
1491                 circuitPackName,
1492                 portName,
1493                 startTimestamp,
1494                 startTimestamp
1495             };
1496
1497             String query = Queries.getQuery().deviceDegreeConnectionPortInsert().get();
1498             LOG.info("Running {} query ", query);
1499             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1500                 for (int j = 0; j < parameters.length; j++) {
1501                     stmt.setObject(j + 1, parameters[j]);
1502                 }
1503                 stmt.execute();
1504                 stmt.clearParameters();
1505             } catch (SQLException e) {
1506                 LOG.error("Something wrong when storing devices degrees connection ports", e);
1507             }
1508
1509         }
1510     }
1511
1512
1513     private void persistDevSrg(String nodeId, Connection connection) {
1514
1515         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier
1516             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1517             .build();
1518         Optional<OrgOpenroadmDevice> deviceObject =
1519                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, deviceIID,
1520                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1521         if (!deviceObject.isPresent()) {
1522             LOG.error("No device found in operational datastore for node {}", nodeId);
1523             return;
1524         }
1525
1526         if (deviceObject.orElseThrow().getSharedRiskGroup() == null) {
1527             deviceObject =
1528                     deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION, deviceIID,
1529                             Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1530             if (!deviceObject.isPresent()) {
1531                 LOG.error("No device found in configuration datastore for node {}", nodeId);
1532                 return;
1533             }
1534         }
1535
1536         @NonNull
1537         Map<SharedRiskGroupKey, SharedRiskGroup> sharedRiskGroupMap = deviceObject.orElseThrow()
1538             .nonnullSharedRiskGroup();
1539         if (sharedRiskGroupMap.isEmpty()) {
1540             LOG.info("no srg found for node {} ", nodeId);
1541             return;
1542         }
1543         String startTimestamp = getCurrentTimestamp();
1544         for (Map.Entry<SharedRiskGroupKey, SharedRiskGroup> groupEntry : sharedRiskGroupMap.entrySet()) {
1545             SharedRiskGroup sharedRiskGroup = groupEntry.getValue();
1546             //String currentProvisionedAddDropPorts = "-1";
1547             //String mcCapSlotWidthGranularity = "";
1548             //String mcCapCenterFreqGranularity = "";
1549             //String mcCapMinSlots = "-1";
1550             //String mcCapMaxSlots = "-1";
1551             String maxAddDropPorts = sharedRiskGroup.getMaxAddDropPorts().toString();
1552             String srgNumber = sharedRiskGroup.getSrgNumber().toString();
1553             String wavelengthDuplicationEnu = sharedRiskGroup.getWavelengthDuplication().getName();
1554             persistDevSrgCircuitPacks(nodeId, sharedRiskGroup, srgNumber, connection);
1555
1556             Object[] parameters = {nodeId,
1557                 maxAddDropPorts,
1558                 //currentProvisionedAddDropPorts,
1559                 "-1",
1560                 srgNumber,
1561                 wavelengthDuplicationEnu,
1562                 //mcCapSlotWidthGranularity,
1563                 //mcCapCenterFreqGranularity,
1564                 //mcCapMinSlots,
1565                 //mcCapMaxSlots,
1566                 "", "", "", "",
1567                 startTimestamp,
1568                 startTimestamp
1569             };
1570
1571             String query = Queries.getQuery().deviceSharedRiskGroupInsert().get();
1572             LOG.info("Running {} query ", query);
1573             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1574                 for (int j = 0; j < parameters.length; j++) {
1575                     stmt.setObject(j + 1, parameters[j]);
1576                 }
1577                 stmt.execute();
1578                 stmt.clearParameters();
1579             } catch (SQLException e) {
1580                 LOG.error("Something wrong when storing devices SRG", e);
1581             }
1582
1583         }
1584     }
1585
1586
1587     private void persistDevSrgCircuitPacks(String nodeId, SharedRiskGroup sharedRiskGroup, String srgNumber,
1588         Connection connection) {
1589
1590         String startTimestamp = getCurrentTimestamp();
1591         @NonNull
1592         Map<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacksKey,
1593                 org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks>
1594                 circuitPacksMap = sharedRiskGroup.nonnullCircuitPacks();
1595         for (Map.Entry<org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacksKey,
1596                 org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.srg.CircuitPacks> entry :
1597                     circuitPacksMap.entrySet()) {
1598
1599             String circuitPackindex = entry.getValue().getIndex().toString();
1600             String circuitPackName = entry.getValue().getCircuitPackName();
1601
1602             Object[] parameters = {nodeId,
1603                 srgNumber,
1604                 circuitPackindex,
1605                 circuitPackName,
1606                 startTimestamp,
1607                 startTimestamp
1608             };
1609
1610             String query = Queries.getQuery().deviceSrgCircuitPackInsert().get();
1611             LOG.info("Running {} query ", query);
1612             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1613                 for (int j = 0; j < parameters.length; j++) {
1614                     stmt.setObject(j + 1, parameters[j]);
1615                 }
1616                 stmt.execute();
1617                 stmt.clearParameters();
1618             } catch (SQLException e) {
1619                 LOG.error("Something wrong when storing devices SRG circuit packs", e);
1620             }
1621
1622         }
1623     }
1624
1625     private void persistDevRoadmConnections(String nodeId, Connection connection) {
1626
1627         //int opticalcontrolmodeEnu=-1;
1628
1629         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier
1630             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1631             .build();
1632         Optional<OrgOpenroadmDevice> deviceObject =
1633                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, deviceIID,
1634                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1635         if (!deviceObject.isPresent()) {
1636             LOG.error("No device found in operational datastore for node {}", nodeId);
1637             return;
1638         }
1639         if (deviceObject.orElseThrow().getRoadmConnections() == null) {
1640             deviceObject =
1641                     deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.CONFIGURATION, deviceIID,
1642                             Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1643             if (!deviceObject.isPresent()) {
1644                 LOG.error("No device found in configuration datastore for node {}", nodeId);
1645                 return;
1646             }
1647         }
1648
1649         @NonNull
1650         Map<RoadmConnectionsKey, RoadmConnections> connectionsMap = deviceObject.orElseThrow()
1651             .nonnullRoadmConnections();
1652         if (connectionsMap.isEmpty()) {
1653             LOG.info("ROADM Dev Connections not found!! for {}", nodeId);
1654             return;
1655         }
1656         String startTimestamp = getCurrentTimestamp();
1657         for (Map.Entry<RoadmConnectionsKey, RoadmConnections> entry : connectionsMap.entrySet()) {
1658             RoadmConnections roadmConnections = entry.getValue();
1659             String connectionNumber = roadmConnections.getConnectionNumber();
1660             //String connectionName = "";
1661             String wavelengthNumber = roadmConnections.getWavelengthNumber().toString();
1662             String opticalcontrolmodeEnu = roadmConnections.getOpticalControlMode().getName();
1663             String targetOutputPower = roadmConnections.getTargetOutputPower().toString();
1664             String srcIf = roadmConnections.getSource().getSrcIf();
1665             String dstIf = roadmConnections.getDestination().getDstIf();
1666
1667             Object[] parameters = {nodeId,
1668                 //connectionName,
1669                 "",
1670                 connectionNumber,
1671                 wavelengthNumber,
1672                 opticalcontrolmodeEnu,
1673                 targetOutputPower,
1674                 srcIf,
1675                 dstIf,
1676                 startTimestamp,
1677                 startTimestamp
1678             };
1679
1680             String query = Queries.getQuery().deviceRoadmConnectionsInsert().get();
1681             LOG.info("Running {} query ", query);
1682             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1683                 for (int j = 0; j < parameters.length; j++) {
1684                     stmt.setObject(j + 1, parameters[j]);
1685                 }
1686                 stmt.execute();
1687                 stmt.clearParameters();
1688             } catch (SQLException e) {
1689                 LOG.error("Something wrong when storing devices ROADM connection ", e);
1690             }
1691         }
1692     }
1693
1694
1695     private void persistDevConnectionMap(String nodeId, Connection connection) {
1696
1697         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier
1698             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1699             .build();
1700         Optional<OrgOpenroadmDevice> deviceObject =
1701                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, deviceIID,
1702                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1703         if (!deviceObject.isPresent()) {
1704             LOG.error("No device found in operational datastore for node {}", nodeId);
1705             return;
1706         }
1707         String startTimestamp = getCurrentTimestamp();
1708         @NonNull
1709         Map<ConnectionMapKey, ConnectionMap> connectionsMap = deviceObject.orElseThrow().nonnullConnectionMap();
1710         for (Map.Entry<ConnectionMapKey, ConnectionMap> entry : connectionsMap.entrySet()) {
1711             ConnectionMap connectionMap = entry.getValue();
1712             String connectionMapNumber = connectionMap.getConnectionMapNumber().toString();
1713             String sourceCircuitPackName = connectionMap.getSource().getCircuitPackName();
1714             String sourcePortName = connectionMap.getSource().getCircuitPackName();
1715
1716
1717             Object[] parameters = {nodeId,
1718                 connectionMapNumber,
1719                 sourceCircuitPackName,
1720                 sourcePortName,
1721                 startTimestamp,
1722                 startTimestamp
1723             };
1724
1725             String query = Queries.getQuery().deviceConnectionMapInsert().get();
1726             LOG.info("Running {} query ", query);
1727             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1728                 for (int j = 0; j < parameters.length; j++) {
1729                     stmt.setObject(j + 1, parameters[j]);
1730                 }
1731                 stmt.execute();
1732                 stmt.clearParameters();
1733             } catch (SQLException e) {
1734                 LOG.error("Something wrong when storing devices connection map", e);
1735             }
1736
1737         }
1738     }
1739
1740     private void persistDevWavelengthMap(String nodeId, Connection connection) {
1741
1742         InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier
1743             .builderOfInherited(OrgOpenroadmDeviceData.class, OrgOpenroadmDevice.class)
1744             .build();
1745         Optional<OrgOpenroadmDevice> deviceObject =
1746                 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, deviceIID,
1747                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
1748         if (!deviceObject.isPresent()) {
1749             LOG.error("No device found in operational datastore for node {}", nodeId);
1750             return;
1751         }
1752
1753         String startTimestamp = getCurrentTimestamp();
1754         @NonNull
1755         Map<WavelengthsKey, Wavelengths> wavelengthsMap = deviceObject.orElseThrow().getWavelengthMap()
1756             .nonnullWavelengths();
1757         for (Map.Entry<WavelengthsKey, Wavelengths> entry : wavelengthsMap.entrySet()) {
1758             Wavelengths wavelengths = entry.getValue();
1759             String wavelengthNumber = wavelengths.getWavelengthNumber().toString();
1760             String centerFrequency = wavelengths.getCenterFrequency().toString();
1761             String wavelength = wavelengths.getWavelength().toString();
1762
1763
1764             Object[] parameters = {nodeId,
1765                 wavelengthNumber,
1766                 centerFrequency,
1767                 wavelength,
1768                 startTimestamp,
1769                 startTimestamp
1770             };
1771
1772             String query = Queries.getQuery().deviceWavelengthInsert().get();
1773             LOG.info("Running {} query ", query);
1774             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1775                 for (int j = 0; j < parameters.length; j++) {
1776                     stmt.setObject(j + 1, parameters[j]);
1777                 }
1778                 stmt.execute();
1779                 stmt.clearParameters();
1780             } catch (SQLException e) {
1781                 LOG.error("Something wrong when storing devices wavelength map", e);
1782             }
1783
1784         }
1785     }
1786
1787
1788     private void persistDevInterfaceTcm(String nodeId, String interfaceName, OduBuilder oduBuilder,
1789         Connection connection) {
1790
1791         Map<TcmKey, Tcm> tcmMap = oduBuilder.getTcm();
1792         for (Map.Entry<TcmKey, Tcm> entry :  tcmMap.entrySet()) {
1793             Tcm tcm = entry.getValue();
1794
1795             Object[] parameters = prepareDevInterfaceTcmParameters(nodeId, interfaceName, tcm);
1796
1797             String query = Queries.getQuery().deviceInterfacesInsert().get();
1798             LOG.info("Running {} query ", query);
1799             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1800                 for (int j = 0; j < parameters.length; j++) {
1801                     stmt.setObject(j + 1, parameters[j]);
1802                 }
1803                 stmt.execute();
1804                 stmt.clearParameters();
1805             } catch (SQLException e) {
1806                 LOG.error("Something wrong when storing devices interface tcm", e);
1807             }
1808         }
1809     }
1810
1811     private void persistDevInterfaceOtnOduTxMsi(String nodeId, String interfaceName, OduBuilder oduBuilder,
1812         Connection connection) {
1813
1814         Map<TxMsiKey, TxMsi> txMsiMap = oduBuilder.getOpu().getMsi().nonnullTxMsi();
1815         for (Map.Entry<TxMsiKey, TxMsi> entry :  txMsiMap.entrySet()) {
1816
1817             TxMsi txMsi = entry.getValue();
1818
1819             Object[] parameters = prepareDevInterfaceOtnOduTxMsiParameters(nodeId, interfaceName, txMsi);
1820
1821             String query = Queries.getQuery().deviceInterfaceOtnOduTxMsiInsert().get();
1822             LOG.info("Running {} query ", query);
1823             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1824                 for (int j = 0; j < parameters.length; j++) {
1825                     stmt.setObject(j + 1, parameters[j]);
1826                 }
1827                 stmt.execute();
1828                 stmt.clearParameters();
1829             } catch (SQLException e) {
1830                 LOG.error("Something wrong when storing devices interface OTN ODU Tx MSI", e);
1831             }
1832         }
1833     }
1834
1835
1836     private void persistDevInterfaceOtnOduRxMsi(String nodeId, String interfaceName, OduBuilder oduBuilder,
1837         Connection connection) {
1838         Map<RxMsiKey, RxMsi> rxMsiMap = oduBuilder.getOpu().getMsi().nonnullRxMsi();
1839         for (Map.Entry<RxMsiKey, RxMsi> entry : rxMsiMap.entrySet()) {
1840             RxMsi rxMsi = entry.getValue();
1841
1842             Object[] parameters = prepareDevInterfaceOtnOduRxMsiParameters(nodeId, interfaceName, rxMsi);
1843
1844             String query = Queries.getQuery().deviceInterfaceOtnOduRxMsiInsert().get();
1845             LOG.info("Running {} query ", query);
1846             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1847                 for (int j = 0; j < parameters.length; j++) {
1848                     stmt.setObject(j + 1, parameters[j]);
1849                 }
1850                 stmt.execute();
1851                 stmt.clearParameters();
1852             } catch (SQLException e) {
1853                 LOG.error("Something wrong when storing devices interface OTN ODU Rx MSI", e);
1854             }
1855         }
1856     }
1857
1858
1859     private void persistDevInterfaceOtnOduExpMsi(String nodeId, String interfaceName, OduBuilder oduBuilder,
1860         Connection connection) {
1861         @NonNull
1862         Map<ExpMsiKey, ExpMsi> expMsiMap = oduBuilder.getOpu().getMsi().nonnullExpMsi();
1863         for (Map.Entry<ExpMsiKey, ExpMsi> entry : expMsiMap.entrySet()) {
1864             ExpMsi expMsi = entry.getValue();
1865
1866             Object[] parameters = prepareDevInterfaceOtnOduExpMsiParameters(nodeId, interfaceName, expMsi);
1867
1868             String query = Queries.getQuery().deviceInterfaceOtnOduExpMsiInsert().get();
1869             LOG.info("Running {} query ", query);
1870             try (PreparedStatement stmt = connection.prepareStatement(query)) {
1871                 for (int j = 0; j < parameters.length; j++) {
1872                     stmt.setObject(j + 1, parameters[j]);
1873                 }
1874                 stmt.execute();
1875                 stmt.clearParameters();
1876             } catch (SQLException e) {
1877                 LOG.error("Something wrong when storing devices interface OTN ODU Exp MSI", e);
1878             }
1879         }
1880     }
1881
1882
1883 }