def correct_db_restart(self):
        """Ensure DB is consistent after unexpected restarts. """
        LOG.info("Checking consistency of DB")
        # Any Segments allocated that's not in Network or FW DB, release it
        seg_netid_dict = self.service_segs.get_seg_netid_src(fw_const.FW_CONST)
        vlan_netid_dict = self.service_vlans.get_seg_netid_src(
            fw_const.FW_CONST)
        for netid in seg_netid_dict:
            net = self.get_network(netid)
            fw_net = self.get_fw_by_netid(netid)
            if not net or not fw_net:
                if netid in vlan_netid_dict:
                    vlan_net = vlan_netid_dict[netid]
                else:
                    vlan_net = None
                self.delete_os_nwk_db(netid, seg_netid_dict[netid], vlan_net)
                LOG.info("Allocated segment for net %s not in DB "
                         "returning", net)
                return
        # Any VLANs allocated that's not in Network or FW DB, release it
        # For Virtual case, this list will be empty
        for netid in vlan_netid_dict:
            net = self.get_network(netid)
            fw_net = self.get_fw_by_netid(netid)
            if not net or not fw_net:
                if netid in seg_netid_dict:
                    vlan_net = seg_netid_dict[netid]
                else:
                    vlan_net = None
                self.delete_os_nwk_db(netid, vlan_net, vlan_netid_dict[netid])
                LOG.info("Allocated vlan for net %s not in DB returning",
                         net)
                return
        # Release all IP's from DB that has no NetID or SubnetID
        self.service_in_ip.release_subnet_no_netid()
        self.service_out_ip.release_subnet_no_netid()
        # It leaves out following possibilities not covered by above.
        # 1. Crash can happen just after creating FWID in DB (for init state)
        # 2. Crash can happen after 1 + IP address allocation
        # 3. Crash can happen after 2 + create OS network
        # IP address allocated will be freed as above.
        # Only OS network will remain for case 3.
        # Also, create that FW DB entry only if that FWID didn't exist.

        # Delete all dummy networks created for dummy router from OS if it's
        # ID is not in NetDB
        # Delete all dummy routers and its associated networks/subnetfrom OS
        # if it's ID is not in FWDB
        fw_dict = self.get_all_fw_db()
        for fw_id in fw_dict:
            rtr_nwk = fw_id[0:4] + fw_const.DUMMY_SERVICE_NWK + (
                fw_id[len(fw_id) - 4:])
            net_list = self.os_helper.get_network_by_name(rtr_nwk)
            # TODO(padkrish) Come back to finish this. Not sure of this.
            # The router interface should be deleted first and then the network
            # Try using show_router
            for net in net_list:
                # Check for if it's there in NetDB
                net_db_item = self.get_network(net.get('id'))
                if not net_db_item:
                    self.os_helper.delete_network_all_subnets(net.get('id'))
                    LOG.info("Router Network %s not in DB, returning",
                             net.get('id'))
                    return
            rtr_name = fw_id[0:4] + fw_const.DUMMY_SERVICE_RTR + (
                fw_id[len(fw_id) - 4:])
            rtr_list = self.os_helper.get_rtr_by_name(rtr_name)
            for rtr in rtr_list:
                fw_db_item = self.get_fw_by_rtrid(rtr.get('id'))
                if not fw_db_item:
                    # There should be only one
                    if not net_list:
                        LOG.error("net_list len is 0, router net not "
                                  "found")
                        return
                    fw_type = fw_dict[fw_id].get('fw_type')
                    if fw_type == fw_const.FW_TENANT_EDGE:
                        rtr_net = net_list[0]
                        rtr_subnet_lt = (
                            self.os_helper.get_subnets_for_net(rtr_net))
                        if rtr_subnet_lt is None:
                            LOG.error("router subnet not found for "
                                      "net %s", rtr_net)
                            return
                        rtr_subnet_id = rtr_subnet_lt[0].get('id')
                        LOG.info("Deleted dummy router network %s",
                                 rtr.get('id'))
                        ret = self.delete_os_dummy_rtr_nwk(rtr.get('id'),
                                                           rtr_net.get('id'),
                                                           rtr_subnet_id)
                        return ret
        LOG.info("Done Checking consistency of DB, no issues")
Ensure DB is consistent after unexpected restarts.