diff options
Diffstat (limited to 'src/main/java/org/onap/a1pesimulator/service/cell')
4 files changed, 374 insertions, 0 deletions
diff --git a/src/main/java/org/onap/a1pesimulator/service/cell/RanCellService.java b/src/main/java/org/onap/a1pesimulator/service/cell/RanCellService.java new file mode 100644 index 0000000..a12ed2c --- /dev/null +++ b/src/main/java/org/onap/a1pesimulator/service/cell/RanCellService.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package org.onap.a1pesimulator.service.cell; + +import java.util.Collection; +import java.util.Set; +import org.onap.a1pesimulator.data.Topology; +import org.onap.a1pesimulator.data.cell.CellDetails; +import org.onap.a1pesimulator.data.cell.CellWithStatus; +import org.onap.a1pesimulator.data.cell.RanCell; + +public interface RanCellService { + + Topology getTopology(); + + Set<String> getCellIds(); + + CellDetails getCellById(String id); + + RanCell getCells(); + + void failure(String id); + + void recoverFromFailure(String id); + + Collection<CellWithStatus> getAllCellsWithStatus(); +}
\ No newline at end of file diff --git a/src/main/java/org/onap/a1pesimulator/service/cell/RanCellServiceImpl.java b/src/main/java/org/onap/a1pesimulator/service/cell/RanCellServiceImpl.java new file mode 100644 index 0000000..0c0ab00 --- /dev/null +++ b/src/main/java/org/onap/a1pesimulator/service/cell/RanCellServiceImpl.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package org.onap.a1pesimulator.service.cell; + +import java.util.Collection; +import java.util.Set; +import java.util.stream.Collectors; +import org.onap.a1pesimulator.data.Topology; +import org.onap.a1pesimulator.data.cell.Cell; +import org.onap.a1pesimulator.data.cell.CellDetails; +import org.onap.a1pesimulator.data.cell.CellWithStatus; +import org.onap.a1pesimulator.data.cell.RanCell; +import org.onap.a1pesimulator.data.ue.UserEquipment; +import org.onap.a1pesimulator.service.ue.RanUeHolder; +import org.onap.a1pesimulator.service.ves.RanVesHolder; +import org.springframework.stereotype.Service; + +@Service +public class RanCellServiceImpl implements RanCellService { + + private final RanCellsHolder ranCellsHolder; + private final RanUeHolder ueHolder; + private final RanVesHolder vesHolder; + + public RanCellServiceImpl(RanCellsHolder ranCellsHolder, RanUeHolder ueHolder, RanVesHolder vesHolder) { + this.ranCellsHolder = ranCellsHolder; + this.ueHolder = ueHolder; + this.vesHolder = vesHolder; + } + + @Override + public Set<String> getCellIds() { + return ranCellsHolder.getCellIds(); + } + + @Override + public CellDetails getCellById(String id) { + CellDetails cellDetails = ranCellsHolder.getCellById(id); + cellDetails.setConnectedUserEquipments(getConnectedUserEquipments(cellDetails.getId())); + return cellDetails; + } + + @Override + public RanCell getCells() { + Collection<CellDetails> cellDetails = ranCellsHolder.getAllCells(); + cellDetails.forEach(cell -> cell.setConnectedUserEquipments(getConnectedUserEquipments(cell.getId()))); + return new RanCell(cellDetails, cellDetails.size()); + } + + @Override + public Topology getTopology() { + Collection<CellDetails> cellList = ranCellsHolder.getCellDetailsList(); + cellList.forEach(cell -> cell.setConnectedUserEquipments(getConnectedUserEquipments(cell.getId()))); + return Topology.builder().cells(cellList).userEquipments(ueHolder.getUserEquipments()).build(); + } + + private Set<String> getConnectedUserEquipments(String cellId) { + Collection<UserEquipment> cellUes = ueHolder.getUserEquipmentsConnectedToCell(cellId); + return cellUes.stream().map(UserEquipment::getId).collect(Collectors.toSet()); + } + + @Override + public void failure(String id) { + ranCellsHolder.markCellInFailure(id); + } + + @Override + public void recoverFromFailure(String id) { + ranCellsHolder.unmarkCellInFailure(id); + } + + @Override + public Collection<CellWithStatus> getAllCellsWithStatus() { + return ranCellsHolder.getAllCells().stream().map(this::wrapCellWithStatus).collect(Collectors.toList()); + } + + private CellWithStatus wrapCellWithStatus(CellDetails cell) { + Cell c = Cell.builder().identifier(cell.getId()).build(); + return CellWithStatus.builder().cell(c).failureMode(ranCellsHolder.isInFailureMode(cell.getId())) + .vesEnabled(vesHolder.isEventEnabled(cell.getId())).state(cell.getCurrentState()).build(); + } +} diff --git a/src/main/java/org/onap/a1pesimulator/service/cell/RanCellStateService.java b/src/main/java/org/onap/a1pesimulator/service/cell/RanCellStateService.java new file mode 100644 index 0000000..d69629e --- /dev/null +++ b/src/main/java/org/onap/a1pesimulator/service/cell/RanCellStateService.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package org.onap.a1pesimulator.service.cell; + +import java.util.Optional; +import org.onap.a1pesimulator.data.cell.CellDetails; +import org.onap.a1pesimulator.data.cell.state.CellStateEnum; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.messaging.simp.SimpMessagingTemplate; +import org.springframework.stereotype.Service; + +@Service +public class RanCellStateService { + + private static final Logger log = LoggerFactory.getLogger(RanCellStateService.class); + + private final RanCellsHolder cellsHolder; + private final SimpMessagingTemplate messagingTemplate; + + public static final String TOPIC_CELL = "/topic/cellStatus"; + + public RanCellStateService(RanCellsHolder cellsHolder, SimpMessagingTemplate messagingTemplate) { + this.cellsHolder = cellsHolder; + this.messagingTemplate = messagingTemplate; + } + + public void activateState(String identifier) { + Optional<CellDetails> cellDetails = getCell(identifier); + if (cellExist(cellDetails, identifier, "Activate")) { + boolean changed = nextStateIfPossible(cellDetails.get(), CellStateEnum.INACTIVE); + if (changed) { + sendCellNotification(cellDetails.get()); + } + } + } + + public void failingState(String identifier) { + Optional<CellDetails> cellDetails = getCell(identifier); + if (cellExist(cellDetails, identifier, "Failing")) { + boolean changed = nextStateIfPossible(cellDetails.get(), CellStateEnum.ACTIVE); + if (changed) { + sendCellNotification(cellDetails.get()); + } + } + } + + public void stopState(String identifier) { + Optional<CellDetails> cellDetails = getCell(identifier); + if (cellExist(cellDetails, identifier, "Stop")) { + boolean changed = previousStateIfPossible(cellDetails.get()); + if (changed) { + sendCellNotification(cellDetails.get()); + } + } + } + + private boolean cellExist(Optional<CellDetails> cellDetails, String identifier, String actionName) { + if (cellDetails.isEmpty()) { + log.info("Cell not found for {} identifier! '{}' action won't be executed!", identifier, actionName); + return false; + } + + return true; + } + + private boolean previousStateIfPossible(CellDetails cell) { + + CellStateEnum state = cell.getCellStateMachine().getState(); + if (state == CellStateEnum.SLEEPING || state == CellStateEnum.GOING_TO_SLEEP || state == CellStateEnum.ACTIVE) { + cell.previousState(); + } else { + log.info("Cell {} is in {} state! The changing of the state isn't allowed." + + "Supported states are: GOING_TO_SLEEP, SLEEPING, ACTIVE.", cell.getId(), + cell.getCellStateMachine().getState().value); + return false; + } + + return true; + } + + private boolean nextStateIfPossible(CellDetails cellDetails, CellStateEnum shouldBe) { + + if (cellDetails.getCellStateMachine().getState() == shouldBe) { + cellDetails.nextState(); + } else { + log.info( + "Cell {} is in {} state. The changing of the state isn't allowed. " + "The supported state is: {}!", + cellDetails.getId(), cellDetails.getCellStateMachine().getState().value, shouldBe.value); + return false; + } + + return true; + } + + private Optional<CellDetails> getCell(String identifier) { + CellDetails cell = null; + try { + cell = cellsHolder.getCellById(identifier); + } catch (RuntimeException e) { + log.info("Exception was thrown: ", e); + } + + if (cell == null) { + return Optional.empty(); + } + + return Optional.of(cell); + } + + private void sendCellNotification(CellDetails cellDetails) { + messagingTemplate.convertAndSend(TOPIC_CELL, cellDetails); + } +} diff --git a/src/main/java/org/onap/a1pesimulator/service/cell/RanCellsHolder.java b/src/main/java/org/onap/a1pesimulator/service/cell/RanCellsHolder.java new file mode 100644 index 0000000..86fa32c --- /dev/null +++ b/src/main/java/org/onap/a1pesimulator/service/cell/RanCellsHolder.java @@ -0,0 +1,118 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package org.onap.a1pesimulator.service.cell; + +import java.text.MessageFormat; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.TreeMap; +import java.util.function.BinaryOperator; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; +import org.onap.a1pesimulator.data.cell.CellDetails; +import org.onap.a1pesimulator.data.cell.CellList.Cell; +import org.onap.a1pesimulator.data.cell.CellList.CellData; +import org.onap.a1pesimulator.util.TopologyReader; +import org.springframework.stereotype.Service; + +@Service +public class RanCellsHolder { + + private Map<String, CellDetails> cellDetailsById; + private final Collection<CellInFailureMode> cellsInFailureMode = new HashSet<>(); + + private final TopologyReader topologyReader; + + public RanCellsHolder(TopologyReader topologyReader) { + this.topologyReader = topologyReader; + refresh(); + } + + public Set<String> getCellIds() { + return cellDetailsById.keySet(); + } + + public CellDetails getCellById(String id) { + if (!cellDetailsById.containsKey(id)) { + throw new RuntimeException(MessageFormat.format("Cell not found: {0}", id)); + } + return cellDetailsById.get(id); + } + + public Collection<CellDetails> getCellDetailsList() { + return cellDetailsById.values(); + } + + public Collection<CellDetails> getAllCells() { + return cellDetailsById.values(); + } + + public void markCellInFailure(String id) { + cellsInFailureMode.add(CellInFailureMode.builder().id(id).build()); + } + + public boolean isInFailureMode(String id) { + return cellsInFailureMode.stream().anyMatch(byIdPredicate(id)); + } + + public void unmarkCellInFailure(String id) { + cellsInFailureMode.removeIf(byIdPredicate(id)); + } + + public Optional<CellInFailureMode> getCellsInFailureMode(String id) { + return cellsInFailureMode.stream().filter(byIdPredicate(id)).findFirst(); + } + + @Getter + @Builder + public static class CellInFailureMode { + + private final String id; + @Setter + private Long sleepingModeDetectedTime; + } + + public void refresh() { + List<CellData> cellDatas = topologyReader.loadCellTopology().getCellList(); + cellDetailsById = cellDatas.stream().collect(Collectors.toMap(cellData -> cellData.getCell().getNodeId(), + this::toCellDetails, throwingMerger(), TreeMap::new)); + } + + public boolean hasChanged() { + return topologyReader.topologyCellHasChanged(); + } + + private <T> BinaryOperator<T> throwingMerger() { + return (u, v) -> { + throw new IllegalStateException(String.format("Duplicate key %s", u)); + }; + } + + private CellDetails toCellDetails(CellData data) { + Cell cell = data.getCell(); + return CellDetails.builder().id(cell.getNodeId()).latitude(cell.getLatitude()).longitude(cell.getLongitude()) + .build(); + } + + public static Predicate<CellInFailureMode> byIdPredicate(String id) { + return cell -> cell.getId().equals(id); + } +} |