aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcaegen2/services/sonhms/child/Graph.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/onap/dcaegen2/services/sonhms/child/Graph.java')
-rw-r--r--src/main/java/org/onap/dcaegen2/services/sonhms/child/Graph.java242
1 files changed, 242 insertions, 0 deletions
diff --git a/src/main/java/org/onap/dcaegen2/services/sonhms/child/Graph.java b/src/main/java/org/onap/dcaegen2/services/sonhms/child/Graph.java
new file mode 100644
index 0000000..eedbd94
--- /dev/null
+++ b/src/main/java/org/onap/dcaegen2/services/sonhms/child/Graph.java
@@ -0,0 +1,242 @@
+/*******************************************************************************
+ * ============LICENSE_START=======================================================
+ * son-handler
+ * ================================================================================
+ * Copyright (C) 2019 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 org.onap.dcaegen2.services.sonhms.child;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.onap.dcaegen2.services.sonhms.model.CellNeighbourList;
+import org.onap.dcaegen2.services.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.
+ */
+ 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"),
+ new TypeReference<ArrayList<CellPciPair>>() { });
+ } 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;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((cellPciNeighbourMap == null) ? 0 : cellPciNeighbourMap.hashCode());
+ result = prime * result + ((graphId == null) ? 0 : graphId.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ Graph other = (Graph) obj;
+ if (cellPciNeighbourMap == null) {
+ if (other.cellPciNeighbourMap != null) {
+ return false;
+ }
+ } else if (!cellPciNeighbourMap.equals(other.cellPciNeighbourMap)) {
+ return false;
+ }
+ if (graphId == null) {
+ if (other.graphId != null) {
+ return false;
+ }
+ } else if (!graphId.equals(other.graphId)) {
+ return false;
+ }
+ return true;
+ }
+
+}