diff options
Diffstat (limited to 'src/main/java/com/wipro/www/sonhms/child')
6 files changed, 1096 insertions, 0 deletions
diff --git a/src/main/java/com/wipro/www/sonhms/child/ChildThread.java b/src/main/java/com/wipro/www/sonhms/child/ChildThread.java new file mode 100644 index 0000000..718cc46 --- /dev/null +++ b/src/main/java/com/wipro/www/sonhms/child/ChildThread.java @@ -0,0 +1,245 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * pcims + * ================================================================================ + * Copyright (C) 2018 Wipro Limited. + * ============================================================================== + * 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. + * ============LICENSE_END========================================================= + ******************************************************************************/ + +package com.wipro.www.sonhms.child; + + +import com.wipro.www.sonhms.Configuration; +import com.wipro.www.sonhms.dao.ClusterDetailsRepository; +import com.wipro.www.sonhms.model.FapServiceList; +import com.wipro.www.sonhms.model.ThreadId; +import com.wipro.www.sonhms.restclient.AsyncResponseBody; +import com.wipro.www.sonhms.utils.BeanUtil; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; +import org.slf4j.Logger; +import org.slf4j.MDC; + + +public class ChildThread implements Runnable { + + private BlockingQueue<List<String>> childStatusUpdate; + private BlockingQueue<FapServiceList> queue = new LinkedBlockingQueue<>(); + // static BlockingQueue<AsyncResponseBody> asynchronousResponse = new + // LinkedBlockingQueue<>(); + private static Map<Long, AsyncResponseBody> responseMap = new HashMap<>(); + private Graph cluster; + private ThreadId threadId; + FapServiceList fapServiceList = new FapServiceList(); + private static final Logger log = org.slf4j.LoggerFactory.getLogger(ChildThread.class); + + /** + * Constructor with parameters. + */ + public ChildThread(BlockingQueue<List<String>> childStatusUpdate, Graph cluster, + BlockingQueue<FapServiceList> queue, ThreadId threadId) { + super(); + this.childStatusUpdate = childStatusUpdate; + this.queue = queue; + this.threadId = threadId; + this.cluster = cluster; + } + + /** + * Puts notification in queue. + */ + public void putInQueue(FapServiceList fapserviceList) { + try { + queue.put(fapserviceList); + } catch (InterruptedException e) { + log.error(" The Thread is Interrupted", e); + Thread.currentThread().interrupt(); + } + } + + /** + * Puts notification in queue with notify. + */ + public void putInQueueWithNotify(FapServiceList fapserviceList) { + synchronized (queue) { + try { + queue.put(fapserviceList); + queue.notifyAll(); + } catch (InterruptedException e) { + log.error(" The Thread is Interrupted", e); + Thread.currentThread().interrupt(); + } + + } + + } + + /** + * Puts response in queue. + */ + public static void putResponse(Long threadId, AsyncResponseBody obj) { + synchronized (responseMap) { + responseMap.put(threadId, obj); + } + + } + + public static Map<Long, AsyncResponseBody> getResponseMap() { + return responseMap; + } + + @Override + public void run() { + + threadId.setChildThreadId(Thread.currentThread().getId()); + synchronized (threadId) { + threadId.notifyAll(); + } + + MDC.put("logFileName", Thread.currentThread().getName()); + log.debug("Starting child thread"); + + try { + fapServiceList = queue.take(); + if (log.isDebugEnabled()) { + log.debug("fapServicelist: {}", fapServiceList); + } + } catch (InterruptedException e1) { + log.error("InterruptedException is {}", e1); + Thread.currentThread().interrupt(); + } + + ClusterFormation clusterFormation = new ClusterFormation(queue); + StateOof oof = new StateOof(childStatusUpdate); + ClusterModification clusterModification = new ClusterModification(); + Detection detect = new Detection(); + + try { + String networkId = fapServiceList.getCellConfig().getLte().getRan().getNeighborListInUse() + .getLteNeighborListInUseLteCell().get(0).getPlmnid(); + + Boolean done = false; + + while (!done) { + + Map<String, ArrayList<Integer>> collisionConfusionResult = detect.detectCollisionConfusion(cluster); + Boolean trigger = clusterFormation.triggerOrWait(collisionConfusionResult); + + if (!trigger) { + collisionConfusionResult = clusterFormation.waitForNotification(collisionConfusionResult, cluster); + } + oof.triggerOof(collisionConfusionResult, networkId); + + if (isNotificationsBuffered()) { + List<FapServiceList> fapServiceLists = bufferNotification(); + for (FapServiceList fapService : fapServiceLists) { + cluster = clusterModification.clustermod(cluster, fapService); + } + String cellPciNeighbourString = cluster.getPciNeighbourJson(); + UUID clusterId = cluster.getGraphId(); + ClusterDetailsRepository clusterDetailsRepository = BeanUtil + .getBean(ClusterDetailsRepository.class); + clusterDetailsRepository.updateCluster(cellPciNeighbourString, clusterId.toString()); + + } else { + done = true; + } + + } + + } catch (Exception e) { + log.error("{}", e); + } + + cleanup(); + } + + private boolean isNotificationsBuffered() { + synchronized (queue) { + + try { + while (queue.isEmpty()) { + queue.wait(); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return false; + } + } + return true; + } + + /** + * cleanup resources. + */ + private void cleanup() { + log.debug("cleaning up database and killing child thread"); + ClusterDetailsRepository clusterDetailsRepository = BeanUtil.getBean(ClusterDetailsRepository.class); + clusterDetailsRepository.deleteByChildThreadId(threadId.getChildThreadId()); + log.debug("Child thread :{} {}", Thread.currentThread().getId(), "completed"); + MDC.remove("logFileName"); + + } + + /** + * Buffer Notification. + */ + public List<FapServiceList> bufferNotification() { + + // Processing Buffered notifications + + List<FapServiceList> fapServiceLists = new ArrayList<>(); + + Configuration config = Configuration.getInstance(); + + int bufferTime = config.getBufferTime(); + + Timestamp currentTime = new Timestamp(System.currentTimeMillis()); + log.debug("Current time {}", currentTime); + + Timestamp laterTime = new Timestamp(System.currentTimeMillis()); + log.debug("Later time {}", laterTime); + + long difference = laterTime.getTime() - currentTime.getTime(); + while (difference < bufferTime) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + log.error("InterruptedException {}", e); + Thread.currentThread().interrupt(); + + } + laterTime = new Timestamp(System.currentTimeMillis()); + difference = laterTime.getTime() - currentTime.getTime(); + + log.debug("Timer has run for seconds {}", difference); + + if (!queue.isEmpty()) { + FapServiceList fapService; + fapService = queue.poll(); + fapServiceLists.add(fapService); + } + } + return fapServiceLists; + } + +} diff --git a/src/main/java/com/wipro/www/sonhms/child/ClusterFormation.java b/src/main/java/com/wipro/www/sonhms/child/ClusterFormation.java new file mode 100644 index 0000000..7b3d5bf --- /dev/null +++ b/src/main/java/com/wipro/www/sonhms/child/ClusterFormation.java @@ -0,0 +1,144 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * pcims + * ================================================================================ + * Copyright (C) 2018 Wipro Limited. + * ============================================================================== + * 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. + * ============LICENSE_END========================================================= + ******************************************************************************/ + +package com.wipro.www.sonhms.child; + +import com.wipro.www.sonhms.ConfigPolicy; +import com.wipro.www.sonhms.Configuration; +import com.wipro.www.sonhms.dao.ClusterDetailsRepository; +import com.wipro.www.sonhms.entity.ClusterDetails; +import com.wipro.www.sonhms.model.FapServiceList; +import com.wipro.www.sonhms.utils.BeanUtil; + +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Map; +import java.util.Properties; +import java.util.UUID; +import java.util.concurrent.BlockingQueue; + +import org.slf4j.Logger; + + +public class ClusterFormation { + + private static final Logger log = org.slf4j.LoggerFactory.getLogger(ClusterFormation.class); + private BlockingQueue<FapServiceList> queue; + private ClusterModification clusterModification; + private Detection detect; + Properties confProp; + ClusterDetails details = new ClusterDetails(); + + public ClusterFormation() { + this.detect = new Detection(); + } + + /** + * parameterized constructor. + * + */ + public ClusterFormation(BlockingQueue<FapServiceList> queue) { + super(); + this.queue = queue; + this.detect = new Detection(); + this.clusterModification = new ClusterModification(); + } + + /** + * Determines whether to trigger Oof or wait for notifications. + */ + public Boolean triggerOrWait(Map<String, ArrayList<Integer>> collisionConfusionResult) { + // determine collision or confusion + + Configuration configuration = Configuration.getInstance(); + int collisionSum = 0; + int confusionSum = 0; + + for (Map.Entry<String, ArrayList<Integer>> entry : collisionConfusionResult.entrySet()) { + + ArrayList<Integer> arr; + arr = entry.getValue(); + // check for 0 collision and confusion + if (!arr.isEmpty()) { + collisionSum = collisionSum + arr.get(0); + confusionSum = confusionSum + arr.get(1); + } + } + return ((collisionSum >= configuration.getMinCollision()) && (confusionSum >= configuration.getMinConfusion())); + + } + + /** + * Waits for notifications. + */ + public Map<String, ArrayList<Integer>> waitForNotification(Map<String, ArrayList<Integer>> collisionConfusionResult, + Graph cluster) { + + FapServiceList newNotification; + ConfigPolicy config = ConfigPolicy.getInstance(); + int timer = 60; + try { + timer = (int) config.getConfig().get("PCI_NEIGHBOR_CHANGE_CLUSTER_TIMEOUT_IN_SECS"); + } catch (NullPointerException e) { + log.debug("Policy config not available. Using default timeout - 60 seconds"); + } + + Timestamp currentTime = new Timestamp(System.currentTimeMillis()); + log.debug("Current Time {}", currentTime); + + Timestamp laterTime = new Timestamp(System.currentTimeMillis()); + log.debug("LaterTime {}", laterTime); + + long difference = laterTime.getTime() - currentTime.getTime(); + + int flag = 0; + + while (difference < (timer * 1000)) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + log.error("Interrupted Exception is {}", e); + Thread.currentThread().interrupt(); + } + + laterTime = new Timestamp(System.currentTimeMillis()); + difference = laterTime.getTime() - currentTime.getTime(); + + if ((difference < (timer * 1000)) && (!queue.isEmpty())) { + newNotification = queue.poll(); + cluster = clusterModification.clustermod(cluster, newNotification); + + // update cluster in DB + String cellPciNeighbourString = cluster.getPciNeighbourJson(); + UUID clusterId = cluster.getGraphId(); + ClusterDetailsRepository clusterDetailsRepository = BeanUtil.getBean(ClusterDetailsRepository.class); + clusterDetailsRepository.updateCluster(cellPciNeighbourString, clusterId.toString()); + flag++; + + } + } + if (flag != 0) { + return detect.detectCollisionConfusion(cluster); + + } + return collisionConfusionResult; + } + +} diff --git a/src/main/java/com/wipro/www/sonhms/child/ClusterModification.java b/src/main/java/com/wipro/www/sonhms/child/ClusterModification.java new file mode 100644 index 0000000..9926cf4 --- /dev/null +++ b/src/main/java/com/wipro/www/sonhms/child/ClusterModification.java @@ -0,0 +1,147 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * pcims + * ================================================================================ + * Copyright (C) 2018 Wipro Limited. + * ============================================================================== + * 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. + * ============LICENSE_END========================================================= + ******************************************************************************/ + +package com.wipro.www.sonhms.child; + +import com.wipro.www.sonhms.model.CellPciPair; +import com.wipro.www.sonhms.model.FapServiceList; +import com.wipro.www.sonhms.model.LteNeighborListInUseLteCell; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.slf4j.Logger; + + + +public class ClusterModification { + private static final Logger log = org.slf4j.LoggerFactory.getLogger(ClusterModification.class); + + /** + * Forms a modified cluster for the existing cluster. + */ + + public Graph clustermod(Graph cluster, FapServiceList fapser) { + + int phycellId = fapser.getX0005b9Lte().getPhyCellIdInUse(); + String cellId = fapser.getCellConfig().getLte().getRan().getCellIdentity(); + CellPciPair mainCellPciPair = new CellPciPair(); + mainCellPciPair.setCellId(cellId); + mainCellPciPair.setPhysicalCellId(phycellId); + List<LteNeighborListInUseLteCell> newNeighbourList; + newNeighbourList = fapser.getCellConfig().getLte().getRan().getNeighborListInUse() + .getLteNeighborListInUseLteCell(); + + Map<CellPciPair, ArrayList<CellPciPair>> clusterMap; + clusterMap = cluster.getCellPciNeighbourMap(); + + // coe + + List<CellPciPair> tempCellPair = new ArrayList<>(); + for (Map.Entry<CellPciPair, ArrayList<CellPciPair>> entry : clusterMap.entrySet()) { + CellPciPair oldClusterKeys = entry.getKey(); + tempCellPair.add(oldClusterKeys); + } + + for (CellPciPair entry : tempCellPair) { + String cell = entry.getCellId(); + int physicalCell = entry.getPhysicalCellId(); + CellPciPair mapVal = new CellPciPair(); + mapVal.setCellId(cell); + mapVal.setPhysicalCellId(physicalCell); + + if (cellId.equals(cell)) { + + // removes the old neighbours and adds new neighbours for that cell + cluster.updateVertex(mapVal, mainCellPciPair); + + } + + } + + /////// update cluster with new pci values for the same cell + + if (clusterMap.containsKey(mainCellPciPair)) { + ArrayList<CellPciPair> oldClusterArray; + oldClusterArray = clusterMap.get(mainCellPciPair); + oldClusterArray.clear(); + + for (int i = 0; i < newNeighbourList.size(); i++) { + String cid = newNeighbourList.get(i).getAlias(); + int phy = newNeighbourList.get(i).getPhyCellId(); + CellPciPair val2 = new CellPciPair(); + val2.setCellId(cid); + val2.setPhysicalCellId(phy); + cluster.addEdge(mainCellPciPair, val2); + } + + } + + for (CellPciPair entry : tempCellPair) { + String cell = entry.getCellId(); + int physicalCell = entry.getPhysicalCellId(); + CellPciPair mapVal = new CellPciPair(); + mapVal.setCellId(cell); + mapVal.setPhysicalCellId(physicalCell); + for (int j = 0; j < newNeighbourList.size(); j++) { + String cid1 = newNeighbourList.get(j).getAlias(); + int phy1 = newNeighbourList.get(j).getPhyCellId(); + CellPciPair val3 = new CellPciPair(); + val3.setCellId(cid1); + val3.setPhysicalCellId(phy1); + + if (cid1.equals(cell)) { + + // removes the old neighbours and adds new neighbours for that cell + cluster.updateVertex(mapVal, val3); + + } + + } + } + + for (int j = 0; j < newNeighbourList.size(); j++) { + String cid1 = newNeighbourList.get(j).getAlias(); + int phy1 = newNeighbourList.get(j).getPhyCellId(); + CellPciPair val3 = new CellPciPair(); + val3.setCellId(cid1); + val3.setPhysicalCellId(phy1); + if (clusterMap.containsKey(val3)) { + cluster.addEdge(mainCellPciPair, val3); + } + + } + + for (int k = 0; k < newNeighbourList.size(); k++) { + String cid2 = newNeighbourList.get(k).getAlias(); + int phy2 = newNeighbourList.get(k).getPhyCellId(); + CellPciPair val5 = new CellPciPair(); + val5.setCellId(cid2); + val5.setPhysicalCellId(phy2); + cluster.addEdge(mainCellPciPair, val5); + } + + log.debug("Modified Cluster {}", cluster); + + return cluster; + } + +}
\ No newline at end of file diff --git a/src/main/java/com/wipro/www/sonhms/child/Detection.java b/src/main/java/com/wipro/www/sonhms/child/Detection.java new file mode 100644 index 0000000..fc2e941 --- /dev/null +++ b/src/main/java/com/wipro/www/sonhms/child/Detection.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * pcims + * ================================================================================ + * Copyright (C) 2018 Wipro Limited. + * ============================================================================== + * 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. + * ============LICENSE_END========================================================= + ******************************************************************************/ + +package com.wipro.www.sonhms.child; + +import com.wipro.www.sonhms.model.CellPciPair; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import org.slf4j.Logger; + + +public class Detection { + + private static final Logger log = org.slf4j.LoggerFactory.getLogger(Detection.class); + + /** + * Returns a map with key as cellid and its value is a list of its collision and + * confusion. + */ + + public Map<String, ArrayList<Integer>> detectCollisionConfusion(Graph cluster) { + + Map<CellPciPair, ArrayList<CellPciPair>> clusterMap = cluster.getCellPciNeighbourMap(); + HashMap<String, ArrayList<Integer>> hash = new HashMap<>(); + + for (Map.Entry<CellPciPair, ArrayList<CellPciPair>> entry : clusterMap.entrySet()) { + int collisionCount = 0; + int confusionCount = 0; + CellPciPair val = entry.getKey(); + String cellId = val.getCellId(); + int pci = val.getPhysicalCellId(); + ArrayList<CellPciPair> arr; + // getting colision and confusion count + ArrayList<Integer> counts = new ArrayList<>(); + // gets the value for the key + arr = entry.getValue(); + if (!arr.isEmpty()) { + for (int i = 0; i < arr.size(); i++) { + if (pci == arr.get(i).getPhysicalCellId()) { + collisionCount++; + } + + } + counts.add(collisionCount); + + for (int j = 0; j < arr.size(); j++) { + for (int k = j + 1; k < arr.size(); k++) { + if (arr.get(j).getPhysicalCellId() == arr.get(k).getPhysicalCellId()) { + confusionCount++; + + } + } + + } + counts.add(confusionCount); + log.debug("count {}", counts); + + } + hash.put(cellId, counts); + + } + log.debug("collison and confusion map {}", hash); + + return hash; + } + +} diff --git a/src/main/java/com/wipro/www/sonhms/child/Graph.java b/src/main/java/com/wipro/www/sonhms/child/Graph.java new file mode 100644 index 0000000..5e48dfc --- /dev/null +++ b/src/main/java/com/wipro/www/sonhms/child/Graph.java @@ -0,0 +1,202 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * pcims + * ================================================================================ + * Copyright (C) 2018 Wipro Limited. + * ============================================================================== + * 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. + * ============LICENSE_END========================================================= + ******************************************************************************/ + +package com.wipro.www.sonhms.child; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.wipro.www.sonhms.model.CellNeighbourList; +import com.wipro.www.sonhms.model.CellPciPair; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.UUID; + +import java.util.concurrent.ConcurrentHashMap; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.slf4j.Logger; + +public class Graph { + private static final Logger log = org.slf4j.LoggerFactory.getLogger(Graph.class); + + // symbol table: key = string vertex, value = set of neighboring vertices + private Map<CellPciPair, ArrayList<CellPciPair>> cellPciNeighbourMap; + private UUID graphId; + + /** + * Parameterized constructor. + */ + @SuppressWarnings("unchecked") + public Graph(String clusterInfo) { + JSONArray cells = new JSONArray(clusterInfo); + + Map<CellPciPair, ArrayList<CellPciPair>> cellMap = new HashMap<>(); + for (int i = 0; i < cells.length(); i++) { + JSONObject cell = (JSONObject) cells.get(i); + CellPciPair cellPciPair = new CellPciPair(cell.getString("cellId"), cell.getInt("physicalCellId")); + ObjectMapper mapper = new ObjectMapper(); + ArrayList<CellPciPair> neighbours = new ArrayList<>(); + try { + neighbours = mapper.readValue(cell.getString("neighbours"), ArrayList.class); + } catch (JSONException | IOException e) { + log.debug("Error parsing json: {}", e); + } + cellMap.put(cellPciPair, neighbours); + + } + + this.cellPciNeighbourMap = cellMap; + } + + public UUID getGraphId() { + return graphId; + } + + public void setGraphId(UUID graphId) { + this.graphId = graphId; + } + + public Map<CellPciPair, ArrayList<CellPciPair>> getCellPciNeighbourMap() { + return cellPciNeighbourMap; + } + + public void setCellPciNeighbourMap(Map<CellPciPair, ArrayList<CellPciPair>> cellPciNeighbourMap) { + this.cellPciNeighbourMap = cellPciNeighbourMap; + } + + /** + * Initializes an empty graph with no vertices or edges. + */ + public Graph() { + this.cellPciNeighbourMap = new ConcurrentHashMap<>(); + } + + // throw an exception if v is not a vertex + private void validateVertex(CellPciPair start) { + if (!hasVertex(start)) { + throw new IllegalArgumentException(start + " is not a vertex"); + } + } + + /** + * Adds the edge v-w to this graph (if it is not already an edge). + */ + public void addEdge(CellPciPair start, CellPciPair end) { + if (!hasVertex(start)) { + addVertex(start); + } + if (!hasVertex(end)) { + addVertex(end); + } + if (!hasEdge(start, end)) { + this.cellPciNeighbourMap.get(start).add(end); + } + } + + /** + * Adds vertex v to this graph (if it is not already a vertex). + */ + public void addVertex(CellPciPair start) { + if (!hasVertex(start)) { + this.cellPciNeighbourMap.put(start, new ArrayList<CellPciPair>()); + } + } + + /** + * Returns true if v is a vertex in this graph. + */ + public boolean hasVertex(CellPciPair start) { + return this.cellPciNeighbourMap.containsKey(start); + } + + /** + * Returns true if v-w is an edge in this graph. + */ + public boolean hasEdge(CellPciPair start, CellPciPair end) { + validateVertex(start); + validateVertex(end); + return this.cellPciNeighbourMap.get(start).contains(end); + } + + /** + * Updates Vertex. + */ + public void updateVertex(CellPciPair oldPair, CellPciPair newPair) { + int oldPci = oldPair.getPhysicalCellId(); + int newPci = newPair.getPhysicalCellId(); + + if (oldPci != newPci) { + + this.cellPciNeighbourMap.put(newPair, this.cellPciNeighbourMap.get(oldPair)); + this.cellPciNeighbourMap.remove(oldPair); + + } + for (Map.Entry<CellPciPair, ArrayList<CellPciPair>> entry : this.cellPciNeighbourMap.entrySet()) { + + ArrayList<CellPciPair> al = entry.getValue(); + for (int i = 0; i < al.size(); i++) { + int pci = al.get(i).getPhysicalCellId(); + if ((pci != newPci) && al.contains(oldPair)) { + al.remove(oldPair); + al.add(newPair); + } + } + } + log.debug("Final Map {}", cellPciNeighbourMap); + + } + + @Override + public String toString() { + return "Graph [cellPciNeighbourMap=" + cellPciNeighbourMap + ", graphId=" + graphId + "]"; + } + + /** + * Convert Graph into a json. + */ + public String getPciNeighbourJson() { + + List<CellNeighbourList> cells = new ArrayList<>(); + + for (Entry<CellPciPair, ArrayList<CellPciPair>> entry : cellPciNeighbourMap.entrySet()) { + CellPciPair key = entry.getKey(); + JSONArray neighbours = new JSONArray(cellPciNeighbourMap.get(key)); + CellNeighbourList cell = new CellNeighbourList(key.getCellId(), key.getPhysicalCellId(), + neighbours.toString()); + cells.add(cell); + } + ObjectMapper mapper = new ObjectMapper(); + String pciNeighbourJson = ""; + try { + pciNeighbourJson = mapper.writeValueAsString(cells); + } catch (JsonProcessingException e) { + log.debug("Error while processing json: {}", e); + } + return pciNeighbourJson; + } + +} diff --git a/src/main/java/com/wipro/www/sonhms/child/StateOof.java b/src/main/java/com/wipro/www/sonhms/child/StateOof.java new file mode 100644 index 0000000..3215c46 --- /dev/null +++ b/src/main/java/com/wipro/www/sonhms/child/StateOof.java @@ -0,0 +1,271 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * pcims + * ================================================================================ + * Copyright (C) 2018 Wipro Limited. + * ============================================================================== + * 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. + * ============LICENSE_END========================================================= + ******************************************************************************/ + +package com.wipro.www.sonhms.child; + +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.wipro.www.sonhms.ConfigPolicy; +import com.wipro.www.sonhms.Configuration; +import com.wipro.www.sonhms.dao.CellInfoRepository; +import com.wipro.www.sonhms.dao.SonRequestsRepository; +import com.wipro.www.sonhms.dmaap.PolicyDmaapClient; +import com.wipro.www.sonhms.entity.CellInfo; +import com.wipro.www.sonhms.entity.PciRequests; +import com.wipro.www.sonhms.exceptions.ConfigDbNotFoundException; +import com.wipro.www.sonhms.exceptions.OofNotFoundException; +import com.wipro.www.sonhms.model.CellConfig; +import com.wipro.www.sonhms.model.CellPciPair; +import com.wipro.www.sonhms.model.Common; +import com.wipro.www.sonhms.model.Configurations; +import com.wipro.www.sonhms.model.Data; +import com.wipro.www.sonhms.model.FapService; +import com.wipro.www.sonhms.model.Lte; +import com.wipro.www.sonhms.model.Payload; +import com.wipro.www.sonhms.model.PolicyNotification; +import com.wipro.www.sonhms.model.Ran; +import com.wipro.www.sonhms.model.X0005b9Lte; +import com.wipro.www.sonhms.restclient.AsyncResponseBody; +import com.wipro.www.sonhms.restclient.OofRestClient; +import com.wipro.www.sonhms.restclient.SdnrRestClient; +import com.wipro.www.sonhms.restclient.Solution; +import com.wipro.www.sonhms.restclient.SonSolution; +import com.wipro.www.sonhms.utils.BeanUtil; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; +import java.util.concurrent.BlockingQueue; + +import org.slf4j.Logger; + +public class StateOof { + private static final Logger log = org.slf4j.LoggerFactory.getLogger(StateOof.class); + private BlockingQueue<List<String>> childStatusUpdate; + + public StateOof() { + + } + + /** + * Parameterized Constructor. + * + */ + public StateOof(BlockingQueue<List<String>> childStatusUpdate) { + super(); + this.childStatusUpdate = childStatusUpdate; + } + + /** + * Triggers OOF. + * @throws OofNotFoundException when trigger oof fails + */ + public void triggerOof(Map<String, ArrayList<Integer>> result, String networkId) throws OofNotFoundException { + // check for 0 collision and 0 confusion + ArrayList<String> cellidList = new ArrayList<>(); + ArrayList<String> cellIds = new ArrayList<>(); + + for (Map.Entry<String, ArrayList<Integer>> entry : result.entrySet()) { + String key = entry.getKey(); + ArrayList<Integer> arr; + arr = entry.getValue(); + if (!arr.isEmpty()) { + Set<Integer> set = new HashSet<>(arr); + if (((set.size() == 1) && !set.contains(0)) || (set.size() != 1)) { + cellIds.add(key); + + } + } + + } + + for (String cell : cellIds) { + log.debug("cellidList entries: {}", cell); + cellidList.add(cell); + } + log.debug("the cells triggering the oof are {}", cellidList); + + UUID transactionId = UUID.randomUUID(); + + Configuration config = Configuration.getInstance(); + int numSolutions = config.getNumSolutions(); + List<String> optimizers = config.getOptimizers(); + + String oofResponse = OofRestClient.queryOof(numSolutions, transactionId.toString(), "create", cellidList, + networkId, optimizers); + log.debug("Synchronous Response {}", oofResponse); + + List<String> childStatus = new ArrayList<>(); + childStatus.add(Long.toString(Thread.currentThread().getId())); + childStatus.add("triggeredOof"); + try { + childStatusUpdate.put(childStatus); + } catch (InterruptedException e1) { + log.debug("Interrupted execption {}", e1); + Thread.currentThread().interrupt(); + + } + + // Store Request details in Database + + PciRequests pciRequest = new PciRequests(); + + long childThreadId = Thread.currentThread().getId(); + pciRequest.setTransactionId(transactionId.toString()); + pciRequest.setChildThreadId(childThreadId); + SonRequestsRepository pciRequestsRepository = BeanUtil.getBean(SonRequestsRepository.class); + pciRequestsRepository.save(pciRequest); + + while (!ChildThread.getResponseMap().containsKey(childThreadId)) { + + } + + AsyncResponseBody asynResponseBody = ChildThread.getResponseMap().get(childThreadId); + + try { + sendToPolicy(asynResponseBody, networkId); + } catch (ConfigDbNotFoundException e1) { + log.debug("Config DB is unreachable: {}", e1); + } + + pciRequestsRepository = BeanUtil.getBean(SonRequestsRepository.class); + pciRequestsRepository.deleteByChildThreadId(childThreadId); + + childStatus = new ArrayList<>(); + childStatus.add(Long.toString(Thread.currentThread().getId())); + childStatus.add("success"); + try { + childStatusUpdate.put(childStatus); + } catch (InterruptedException e) { + log.debug("InterruptedException {}", e); + Thread.currentThread().interrupt(); + + } + + } + + /** + * Sends Dmaap notification to Policy. + * + * @throws ConfigDbNotFoundException + * when config db is unreachable + */ + private void sendToPolicy(AsyncResponseBody async, String networkId) throws ConfigDbNotFoundException { + + if (log.isDebugEnabled()) { + log.debug(async.toString()); + } + + List<Solution> solutions; + solutions = async.getSolutions(); + + Map<String, List<CellPciPair>> pnfs = getPnfs(solutions); + + for (Map.Entry<String, List<CellPciPair>> entry : pnfs.entrySet()) { + String pnfName = entry.getKey(); + List<CellPciPair> cellPciPairs = entry.getValue(); + + String notification = getNotificationString(pnfName, cellPciPairs, networkId); + log.debug("Policy Notification: {}", notification); + PolicyDmaapClient policy = new PolicyDmaapClient(); + boolean status = policy.sendNotificationToPolicy(notification); + log.debug("sent Message: {}", status); + if (status) { + log.debug("Message sent to policy"); + } else { + log.debug("Sending notification to policy failed"); + } + + } + } + + private String getNotificationString(String pnfName, List<CellPciPair> cellPciPairs, String networkId) { + ArrayList<Configurations> configurations = new ArrayList<>(); + for (CellPciPair cellPciPair : cellPciPairs) { + String cellId = cellPciPair.getCellId(); + int pci = cellPciPair.getPhysicalCellId(); + Configurations configuration = new Configurations(new Data(new FapService(cellId, + new X0005b9Lte(pci, pnfName), new CellConfig(new Lte(new Ran(new Common(cellId))))))); + configurations.add(configuration); + } + + Payload payload = new Payload(configurations); + ObjectMapper mapper = new ObjectMapper(); + String payloadString = ""; + try { + payloadString = mapper.writeValueAsString(payload); + } catch (JsonProcessingException e) { + log.debug("JSON processing exception: {}", e); + } + PolicyNotification policyNotification = new PolicyNotification(); + ConfigPolicy configPolicy = ConfigPolicy.getInstance(); + String closedLoopControlName = (String) configPolicy.getConfig().get("PCI_MODCONFIG_POLICY_NAME"); + policyNotification.setClosedLoopControlName(closedLoopControlName); + policyNotification.setPayload(payloadString); + + mapper.setSerializationInclusion(Include.NON_NULL); + String notification = ""; + try { + notification = mapper.writeValueAsString(policyNotification); + } catch (JsonProcessingException e1) { + log.debug("JSON processing exception: {}", e1); + } + return notification; + } + + private Map<String, List<CellPciPair>> getPnfs(List<Solution> solutions) throws ConfigDbNotFoundException { + + Map<String, List<CellPciPair>> pnfs = new HashMap<>(); + + for (Solution solution : solutions) { + List<SonSolution> pciSolutions = solution.getPciSolutions(); + for (SonSolution pciSolution : pciSolutions) { + String cellId = pciSolution.getCellId(); + int pci = pciSolution.getPci(); + + String pnfName = ""; + CellInfoRepository cellInfoRepository = BeanUtil.getBean(CellInfoRepository.class); + Optional<CellInfo> cellInfo = cellInfoRepository.findById(cellId); + if (cellInfo.isPresent()) { + pnfName = cellInfo.get().getPnfName(); + } else { + pnfName = SdnrRestClient.getPnfName(cellId); + cellInfoRepository.save(new CellInfo(cellId, pnfName)); + } + if (pnfs.containsKey(pnfName)) { + pnfs.get(pnfName).add(new CellPciPair(cellId, pci)); + } else { + List<CellPciPair> cellPciPairs = new ArrayList<>(); + cellPciPairs.add(new CellPciPair(cellId, pci)); + pnfs.put(pnfName, cellPciPairs); + } + } + + } + return pnfs; + } + +} |