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