Search in sources :

Example 11 with PersistenceException

use of org.openkilda.persistence.exceptions.PersistenceException in project open-kilda by telstra.

the class NetworkPortService method savePortProperties.

private PortProperties savePortProperties(Endpoint endpoint, boolean discoveryEnabled) {
    Switch sw = switchRepository.findById(endpoint.getDatapath()).orElseThrow(() -> new PersistenceException(format("Switch %s not found.", endpoint.getDatapath())));
    PortProperties portProperties = portPropertiesRepository.getBySwitchIdAndPort(endpoint.getDatapath(), endpoint.getPortNumber()).orElseGet(() -> {
        PortProperties newProps = PortProperties.builder().switchObj(sw).port(endpoint.getPortNumber()).build();
        portPropertiesRepository.add(newProps);
        return newProps;
    });
    portProperties.setDiscoveryEnabled(discoveryEnabled);
    return portProperties;
}
Also used : Switch(org.openkilda.model.Switch) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) PortProperties(org.openkilda.model.PortProperties)

Example 12 with PersistenceException

use of org.openkilda.persistence.exceptions.PersistenceException in project open-kilda by telstra.

the class OrientDbPersistenceImplementation method onContextClose.

@Override
public void onContextClose(PersistenceContext context) {
    OrientDbContextExtension contextExtension = getContextExtension(context);
    DelegatingFramedGraph<OrientGraph> currentGraph = contextExtension.removeGraph();
    if (currentGraph != null) {
        String threadName = Thread.currentThread().getName();
        // Commit an implicit transaction to release graph resources.
        try {
            log.trace("Committing a transaction on the graph: {} in {}", currentGraph, threadName);
            currentGraph.getBaseGraph().commit();
        } catch (Exception e) {
            log.error("Failed to commit a transaction in {}", threadName, e);
        }
        try {
            log.trace("Closing the framed graph: {} in {}", currentGraph, threadName);
            currentGraph.close();
        } catch (IOException e) {
            throw new PersistenceException(String.format("Failed to close graph in %s", threadName), e);
        }
    }
}
Also used : OrientGraph(org.apache.tinkerpop.gremlin.orientdb.OrientGraph) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) IOException(java.io.IOException) IOException(java.io.IOException) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException)

Example 13 with PersistenceException

use of org.openkilda.persistence.exceptions.PersistenceException in project open-kilda by telstra.

the class FermaTransactionAdapter method open.

@Override
public void open() throws Exception {
    try {
        closeForeignTransactionIfExist();
    } catch (PersistenceException e) {
        throw e;
    } catch (Exception e) {
        throw wrapException(e);
    }
    DelegatingFramedGraph<?> graph = getContextExtension().getGraphCreateIfMissing();
    WrappedTransaction transaction = graph.tx();
    if (transaction.isOpen()) {
        throw new PersistenceException("Attempt to reopen transaction: " + transaction);
    }
    log.debug("Opening a new transaction {} on graph {}", transaction, graph);
    transaction.open();
}
Also used : PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) WrappedTransaction(com.syncleus.ferma.WrappedTransaction) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException)

Example 14 with PersistenceException

use of org.openkilda.persistence.exceptions.PersistenceException in project open-kilda by telstra.

the class FermaMirrorGroupRepository method findFirstUnassignedGroupId.

@Override
public Optional<GroupId> findFirstUnassignedGroupId(SwitchId switchId, GroupId lowestGroupId, GroupId highestGroupId) {
    String switchIdAsStr = SwitchIdConverter.INSTANCE.toGraphProperty(switchId);
    Long lowestGroupIdAsLong = GroupIdConverter.INSTANCE.toGraphProperty(lowestGroupId);
    Long highestGroupIdAsLong = GroupIdConverter.INSTANCE.toGraphProperty(highestGroupId);
    try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(MirrorGroupFrame.FRAME_LABEL).has(MirrorGroupFrame.SWITCH_ID_PROPERTY, switchIdAsStr).has(MirrorGroupFrame.GROUP_ID_PROPERTY, P.gte(lowestGroupIdAsLong)).has(MirrorGroupFrame.GROUP_ID_PROPERTY, P.lt(highestGroupIdAsLong)).values(MirrorGroupFrame.GROUP_ID_PROPERTY).order().math("_ + 1").as("a").where(__.not(__.V().hasLabel(MirrorGroupFrame.FRAME_LABEL).has(MirrorGroupFrame.SWITCH_ID_PROPERTY, switchIdAsStr).values(MirrorGroupFrame.GROUP_ID_PROPERTY).where(P.eq("a")))).select("a").limit(1)).getRawTraversal()) {
        if (traversal.hasNext()) {
            return traversal.tryNext().map(l -> ((Double) l).longValue()).map(GroupIdConverter.INSTANCE::toEntityAttribute);
        }
    } catch (Exception e) {
        throw new PersistenceException("Failed to traverse", e);
    }
    try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(MirrorGroupFrame.FRAME_LABEL).has(MirrorGroupFrame.SWITCH_ID_PROPERTY, switchIdAsStr).has(MirrorGroupFrame.GROUP_ID_PROPERTY, lowestGroupIdAsLong)).getRawTraversal()) {
        if (!traversal.hasNext()) {
            return Optional.of(lowestGroupId);
        }
    } catch (Exception e) {
        throw new PersistenceException("Failed to traverse", e);
    }
    return Optional.empty();
}
Also used : GroupIdConverter(org.openkilda.persistence.ferma.frames.converters.GroupIdConverter) Collection(java.util.Collection) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) FermaPersistentImplementation(org.openkilda.persistence.ferma.FermaPersistentImplementation) SwitchIdConverter(org.openkilda.persistence.ferma.frames.converters.SwitchIdConverter) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Collectors(java.util.stream.Collectors) MirrorGroupData(org.openkilda.model.MirrorGroup.MirrorGroupData) MirrorGroupRepository(org.openkilda.persistence.repositories.MirrorGroupRepository) List(java.util.List) SwitchId(org.openkilda.model.SwitchId) MirrorGroup(org.openkilda.model.MirrorGroup) MirrorGroupFrame(org.openkilda.persistence.ferma.frames.MirrorGroupFrame) Optional(java.util.Optional) FlowMeterFrame(org.openkilda.persistence.ferma.frames.FlowMeterFrame) KildaBaseVertexFrame(org.openkilda.persistence.ferma.frames.KildaBaseVertexFrame) GroupId(org.openkilda.model.GroupId) PathIdConverter(org.openkilda.persistence.ferma.frames.converters.PathIdConverter) PathId(org.openkilda.model.PathId) P(org.apache.tinkerpop.gremlin.process.traversal.P) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException)

Example 15 with PersistenceException

use of org.openkilda.persistence.exceptions.PersistenceException in project open-kilda by telstra.

the class AvailableNetworkFactory method getAvailableNetwork.

/**
 * Gets a {@link AvailableNetwork}.
 *
 * @param flow                      the flow, for which {@link AvailableNetwork} is constructing.
 * @param reusePathsResources       reuse resources already allocated by {@param reusePathsResources} paths.
 * @return {@link AvailableNetwork} instance.
 */
public AvailableNetwork getAvailableNetwork(Flow flow, Collection<PathId> reusePathsResources) throws RecoverableException {
    BuildStrategy buildStrategy = BuildStrategy.from(config.getNetworkStrategy());
    AvailableNetwork network = new AvailableNetwork();
    try {
        // Reads all active links from the database and creates representation of the network.
        getAvailableIsls(buildStrategy, flow).forEach(link -> addIslAsEdge(link, network));
        if (!reusePathsResources.isEmpty() && !flow.isIgnoreBandwidth()) {
            reusePathsResources.stream().filter(pathId -> flowPathRepository.findById(pathId).map(path -> !path.isIgnoreBandwidth()).orElse(false)).forEach(pathId -> {
                // ISLs occupied by the flow (take the bandwidth already occupied by the flow into account).
                islRepository.findActiveByPathAndBandwidthAndEncapsulationType(pathId, flow.getBandwidth(), flow.getEncapsulationType()).forEach(link -> addIslAsEdge(link, network));
            });
        }
    } catch (PersistenceException e) {
        throw new RecoverableException("An error from the database", e);
    }
    if (flow.getDiverseGroupId() != null) {
        log.info("Filling AvailableNetwork diverse weights for group with id {}", flow.getDiverseGroupId());
        Collection<PathId> flowPaths = flowPathRepository.findPathIdsByFlowDiverseGroupId(flow.getDiverseGroupId());
        if (!reusePathsResources.isEmpty()) {
            flowPaths = flowPaths.stream().filter(s -> !reusePathsResources.contains(s)).collect(Collectors.toList());
        }
        Collection<PathId> affinityPathIds = flowPathRepository.findPathIdsByFlowAffinityGroupId(flow.getAffinityGroupId());
        flowPaths.forEach(pathId -> flowPathRepository.findById(pathId).filter(flowPath -> !affinityPathIds.contains(flowPath.getPathId()) || flowPath.getFlowId().equals(flow.getFlowId())).ifPresent(flowPath -> {
            network.processDiversitySegments(flowPath.getSegments(), flow);
            network.processDiversitySegmentsWithPop(flowPath.getSegments());
        }));
    }
    // The main flow should not take into account the rest of the flows in this affinity group.
    if (flow.getAffinityGroupId() != null && !flow.getAffinityGroupId().equals(flow.getFlowId())) {
        log.info("Filling AvailableNetwork affinity weights for group with id {}", flow.getAffinityGroupId());
        flowPathRepository.findByFlowId(flow.getAffinityGroupId()).stream().filter(flowPath -> !flowPath.isProtected()).filter(FlowPath::isForward).map(FlowPath::getSegments).forEach(network::processAffinitySegments);
    }
    return network;
}
Also used : IslImmutableView(org.openkilda.persistence.repositories.IslRepository.IslImmutableView) FlowPath(org.openkilda.model.FlowPath) FlowPathRepository(org.openkilda.persistence.repositories.FlowPathRepository) Collection(java.util.Collection) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) Collectors(java.util.stream.Collectors) RecoverableException(org.openkilda.pce.exception.RecoverableException) ArrayList(java.util.ArrayList) RepositoryFactory(org.openkilda.persistence.repositories.RepositoryFactory) Edge(org.openkilda.pce.model.Edge) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Flow(org.openkilda.model.Flow) Node(org.openkilda.pce.model.Node) IslRepository(org.openkilda.persistence.repositories.IslRepository) PathId(org.openkilda.model.PathId) AvailableNetwork(org.openkilda.pce.impl.AvailableNetwork) PathId(org.openkilda.model.PathId) PersistenceException(org.openkilda.persistence.exceptions.PersistenceException) AvailableNetwork(org.openkilda.pce.impl.AvailableNetwork) FlowPath(org.openkilda.model.FlowPath) RecoverableException(org.openkilda.pce.exception.RecoverableException)

Aggregations

PersistenceException (org.openkilda.persistence.exceptions.PersistenceException)16 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 PathId (org.openkilda.model.PathId)6 Collection (java.util.Collection)5 Optional (java.util.Optional)5 FermaPersistentImplementation (org.openkilda.persistence.ferma.FermaPersistentImplementation)5 PathIdConverter (org.openkilda.persistence.ferma.frames.converters.PathIdConverter)5 P (org.apache.tinkerpop.gremlin.process.traversal.P)4 GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)4 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)4 SwitchId (org.openkilda.model.SwitchId)4 FlowMeterFrame (org.openkilda.persistence.ferma.frames.FlowMeterFrame)4 KildaBaseVertexFrame (org.openkilda.persistence.ferma.frames.KildaBaseVertexFrame)4 SwitchIdConverter (org.openkilda.persistence.ferma.frames.converters.SwitchIdConverter)4 OrientGraph (org.apache.tinkerpop.gremlin.orientdb.OrientGraph)2 Flow (org.openkilda.model.Flow)2 FlowMeter (org.openkilda.model.FlowMeter)2 FlowMeterData (org.openkilda.model.FlowMeter.FlowMeterData)2 FlowPath (org.openkilda.model.FlowPath)2