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