aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcaegen2/services/sonhms/child/Graph.java
blob: a76d0dd1d22b587fedb519ad6357ffa607f3af37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*******************************************************************************
 *  ============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 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.onap.dcaegen2.services.sonhms.model.CellNeighbourList;
import org.onap.dcaegen2.services.sonhms.model.CellPciPair;
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;
    }

}