Search in sources :

Example 6 with FlowInfo

use of org.openkilda.model.FlowInfo in project open-kilda by telstra.

the class FlowService method processInventoryFlow.

/**
 * Process inventory flow.
 *
 * @param flows
 *            the flows
 * @param inventoryFlows
 *            the inventory flows
 */
private void processInventoryFlow(final List<FlowInfo> flows, final List<InventoryFlow> inventoryFlows) {
    List<FlowInfo> discrepancyFlow = new ArrayList<FlowInfo>();
    final Map<String, String> csNames = switchIntegrationService.getSwitchNames();
    for (InventoryFlow inventoryFlow : inventoryFlows) {
        int index = -1;
        for (FlowInfo flow : flows) {
            if (flow.getFlowid().equals(inventoryFlow.getId())) {
                index = flows.indexOf(flow);
                break;
            }
        }
        if (index >= 0) {
            FlowDiscrepancy discrepancy = new FlowDiscrepancy();
            discrepancy.setControllerDiscrepancy(false);
            if (flows.get(index).getMaximumBandwidth() != inventoryFlow.getMaximumBandwidth()) {
                discrepancy.setInventoryDiscrepancy(true);
                discrepancy.setBandwidth(true);
                FlowBandwidth flowBandwidth = new FlowBandwidth();
                flowBandwidth.setControllerBandwidth(flows.get(index).getMaximumBandwidth());
                flowBandwidth.setInventoryBandwidth(inventoryFlow.getMaximumBandwidth());
                discrepancy.setBandwidthValue(flowBandwidth);
            }
            if (("UP".equalsIgnoreCase(flows.get(index).getStatus()) && !"ACTIVE".equalsIgnoreCase(inventoryFlow.getState())) || ("DOWN".equalsIgnoreCase(flows.get(index).getStatus()) && "ACTIVE".equalsIgnoreCase(inventoryFlow.getState()))) {
                discrepancy.setInventoryDiscrepancy(true);
                discrepancy.setStatus(true);
                FlowState flowState = new FlowState();
                flowState.setControllerState(flows.get(index).getStatus());
                flowState.setInventoryState(inventoryFlow.getState());
                discrepancy.setStatusValue(flowState);
            }
            flows.get(index).setDiscrepancy(discrepancy);
            flows.get(index).setState(inventoryFlow.getState());
            flows.get(index).setInventoryFlow(true);
        } else {
            FlowInfo flowObj = new FlowInfo();
            flowConverter.toFlowInfo(flowObj, inventoryFlow, csNames);
            flowObj.setInventoryFlow(true);
            discrepancyFlow.add(flowObj);
        }
    }
    for (FlowInfo flow : flows) {
        boolean flag = false;
        for (InventoryFlow inventoryFlow : inventoryFlows) {
            if (flow.getFlowid().equals(inventoryFlow.getId())) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            FlowDiscrepancy discrepancy = new FlowDiscrepancy();
            discrepancy.setInventoryDiscrepancy(true);
            discrepancy.setControllerDiscrepancy(false);
            discrepancy.setStatus(true);
            discrepancy.setBandwidth(true);
            FlowBandwidth flowBandwidth = new FlowBandwidth();
            flowBandwidth.setControllerBandwidth(flow.getMaximumBandwidth());
            flowBandwidth.setInventoryBandwidth(0);
            discrepancy.setBandwidthValue(flowBandwidth);
            FlowState flowState = new FlowState();
            flowState.setControllerState(flow.getStatus());
            flowState.setInventoryState(null);
            discrepancy.setStatusValue(flowState);
            flow.setDiscrepancy(discrepancy);
        }
        flow.setControllerFlow(true);
    }
    flows.addAll(discrepancyFlow);
}
Also used : FlowInfo(org.openkilda.model.FlowInfo) FlowDiscrepancy(org.openkilda.model.FlowDiscrepancy) FlowState(org.openkilda.model.FlowState) InventoryFlow(org.openkilda.integration.source.store.dto.InventoryFlow) ArrayList(java.util.ArrayList) FlowBandwidth(org.openkilda.model.FlowBandwidth)

Example 7 with FlowInfo

use of org.openkilda.model.FlowInfo in project open-kilda by telstra.

the class FlowService method getAllFlows.

/**
 * get All Flows.
 *
 * @return SwitchRelationData
 */
public List<FlowInfo> getAllFlows(List<String> statuses, boolean controller) {
    List<FlowInfo> flows = new ArrayList<FlowInfo>();
    if (!CollectionUtil.isEmpty(statuses)) {
        statuses = statuses.stream().map((status) -> status.toLowerCase()).collect(Collectors.toList());
    }
    if (CollectionUtil.isEmpty(statuses) || statuses.contains("active")) {
        flows = flowsIntegrationService.getFlows();
        if (flows == null) {
            flows = new ArrayList<FlowInfo>();
        }
    }
    if (!controller) {
        if (storeService.getLinkStoreConfig().getUrls().size() > 0) {
            try {
                UserInfo userInfo = userService.getLoggedInUserInfo();
                if (userInfo.getPermissions().contains(IConstants.Permission.FW_FLOW_INVENTORY)) {
                    List<InventoryFlow> inventoryFlows = new ArrayList<InventoryFlow>();
                    String status = "";
                    for (String statusObj : statuses) {
                        if (StringUtil.isNullOrEmpty(status)) {
                            status += statusObj;
                        } else {
                            status += "," + statusObj;
                        }
                    }
                    inventoryFlows = flowStoreService.getFlowsWithParams(status);
                    processInventoryFlow(flows, inventoryFlows);
                }
            } catch (Exception ex) {
                LOGGER.error("Error occurred while retrieving flows from store", ex);
            }
        }
    }
    return flows;
}
Also used : FlowInfo(org.openkilda.model.FlowInfo) InventoryFlow(org.openkilda.integration.source.store.dto.InventoryFlow) ArrayList(java.util.ArrayList) UserInfo(org.usermanagement.model.UserInfo) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) IntegrationException(org.openkilda.integration.exception.IntegrationException) RequestValidationException(org.usermanagement.exception.RequestValidationException) AccessDeniedException(java.nio.file.AccessDeniedException)

Example 8 with FlowInfo

use of org.openkilda.model.FlowInfo in project open-kilda by telstra.

the class FlowService method getFlowById.

/**
 * Flow by flow id.
 *
 * @param flowId
 *            the flow id
 * @return the flow by id
 * @throws AccessDeniedException the access denied exception
 */
public FlowInfo getFlowById(String flowId, boolean controller) throws AccessDeniedException {
    FlowInfo flowInfo = new FlowInfo();
    FlowV2 flow = null;
    try {
        flow = flowsIntegrationService.getFlowById(flowId);
    } catch (InvalidResponseException ex) {
        LOGGER.error("Error occurred while retrieving flows from controller", ex);
        if (controller) {
            throw new InvalidResponseException(ex.getCode(), ex.getResponse());
        }
    }
    Map<String, String> csNames = switchIntegrationService.getSwitchNames();
    if (flow != null) {
        flowInfo = flowConverter.toFlowV2Info(flow, csNames);
    }
    UserInfo userInfo = userService.getLoggedInUserInfo();
    try {
        if (!controller && userInfo.getPermissions().contains(IConstants.Permission.FW_FLOW_INVENTORY)) {
            if (storeService.getLinkStoreConfig().getUrls().size() > 0) {
                InventoryFlow inventoryFlow = flowStoreService.getFlowById(flowId);
                if (flow == null && inventoryFlow.getId() == null) {
                    throw new RequestValidationException("Can not get flow: Flow " + flowId + " not found");
                } else if (flow != null && inventoryFlow.getId() != null) {
                    flowInfo.setState(inventoryFlow.getState());
                    FlowDiscrepancy discrepancy = new FlowDiscrepancy();
                    discrepancy.setControllerDiscrepancy(false);
                    if (flowInfo.getMaximumBandwidth() != (inventoryFlow.getMaximumBandwidth() == null ? 0 : inventoryFlow.getMaximumBandwidth())) {
                        discrepancy.setBandwidth(true);
                        FlowBandwidth flowBandwidth = new FlowBandwidth();
                        flowBandwidth.setControllerBandwidth(flow.getMaximumBandwidth());
                        flowBandwidth.setInventoryBandwidth(inventoryFlow.getMaximumBandwidth());
                        discrepancy.setBandwidthValue(flowBandwidth);
                    }
                    if (("UP".equalsIgnoreCase(flowInfo.getStatus()) && !"ACTIVE".equalsIgnoreCase(inventoryFlow.getState())) || ("DOWN".equalsIgnoreCase(flowInfo.getStatus()) && "ACTIVE".equalsIgnoreCase(inventoryFlow.getState()))) {
                        discrepancy.setStatus(true);
                        FlowState flowState = new FlowState();
                        flowState.setControllerState(flow.getStatus());
                        flowState.setInventoryState(inventoryFlow.getState());
                        discrepancy.setStatusValue(flowState);
                    }
                    flowInfo.setInventoryFlow(true);
                    flowInfo.setDiscrepancy(discrepancy);
                } else if (inventoryFlow.getId() == null && flow != null) {
                    FlowDiscrepancy discrepancy = new FlowDiscrepancy();
                    discrepancy.setInventoryDiscrepancy(true);
                    discrepancy.setControllerDiscrepancy(false);
                    discrepancy.setStatus(true);
                    discrepancy.setBandwidth(true);
                    FlowBandwidth flowBandwidth = new FlowBandwidth();
                    flowBandwidth.setControllerBandwidth(flow.getMaximumBandwidth());
                    flowBandwidth.setInventoryBandwidth(0);
                    discrepancy.setBandwidthValue(flowBandwidth);
                    FlowState flowState = new FlowState();
                    flowState.setControllerState(flow.getStatus());
                    flowState.setInventoryState(null);
                    discrepancy.setStatusValue(flowState);
                    flowInfo.setDiscrepancy(discrepancy);
                } else {
                    flowConverter.toFlowInfo(flowInfo, inventoryFlow, csNames);
                }
            }
        }
        if (flow == null) {
            throw new RequestValidationException("Can not get flow: Flow " + flowId + " not found");
        }
    } catch (Exception ex) {
        LOGGER.error("Error occurred while retrieving flows from store", ex);
        throw new RequestValidationException(ex.getMessage());
    }
    return flowInfo;
}
Also used : FlowInfo(org.openkilda.model.FlowInfo) FlowDiscrepancy(org.openkilda.model.FlowDiscrepancy) FlowState(org.openkilda.model.FlowState) FlowV2(org.openkilda.integration.model.FlowV2) InventoryFlow(org.openkilda.integration.source.store.dto.InventoryFlow) UserInfo(org.usermanagement.model.UserInfo) FlowBandwidth(org.openkilda.model.FlowBandwidth) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) RequestValidationException(org.usermanagement.exception.RequestValidationException) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) IntegrationException(org.openkilda.integration.exception.IntegrationException) RequestValidationException(org.usermanagement.exception.RequestValidationException) AccessDeniedException(java.nio.file.AccessDeniedException)

Example 9 with FlowInfo

use of org.openkilda.model.FlowInfo in project open-kilda by telstra.

the class FlowControllerTest method testGetFlowByIdWhenFlowIdNotPassed.

@Test
public void testGetFlowByIdWhenFlowIdNotPassed() throws Exception {
    FlowInfo flowInfo = new FlowInfo();
    try {
        when(flowService.getFlowById(TestFlowMock.FLOW_ID_NULL, TestFlowMock.CONTROLLER_FLAG)).thenReturn(flowInfo);
        mockMvc.perform(get("/api/flows/{flowId}", TestFlowMock.FLOW_ID_NULL, true).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isMethodNotAllowed());
        assertTrue(true);
    } catch (Exception e) {
        assertTrue(false);
    }
}
Also used : FlowInfo(org.openkilda.model.FlowInfo) Test(org.junit.Test)

Aggregations

FlowInfo (org.openkilda.model.FlowInfo)9 InventoryFlow (org.openkilda.integration.source.store.dto.InventoryFlow)3 AccessDeniedException (java.nio.file.AccessDeniedException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 IntegrationException (org.openkilda.integration.exception.IntegrationException)2 InvalidResponseException (org.openkilda.integration.exception.InvalidResponseException)2 FlowEndpoint (org.openkilda.integration.model.FlowEndpoint)2 FlowBandwidth (org.openkilda.model.FlowBandwidth)2 FlowDiscrepancy (org.openkilda.model.FlowDiscrepancy)2 FlowState (org.openkilda.model.FlowState)2 RequestValidationException (org.usermanagement.exception.RequestValidationException)2 UserInfo (org.usermanagement.model.UserInfo)2 Permissions (org.openkilda.auth.model.Permissions)1 FlowV2 (org.openkilda.integration.model.FlowV2)1 FlowV2Endpoint (org.openkilda.integration.model.FlowV2Endpoint)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1