2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.northbound.dmaapclient;
24 import java.io.BufferedReader;
26 import java.io.FileReader;
27 import java.io.IOException;
28 import java.io.StringWriter;
29 import java.io.Writer;
30 import java.time.Instant;
31 import java.util.HashMap;
33 import java.util.Properties;
34 import org.apache.velocity.VelocityContext;
35 import org.apache.velocity.app.VelocityEngine;
36 import org.json.JSONArray;
37 import org.json.JSONObject;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import com.fasterxml.jackson.databind.JsonNode;
41 import com.fasterxml.jackson.databind.ObjectMapper;
43 public class OofPciPocDmaapConsumers extends SdncDmaapConsumerImpl {
45 private static final Logger LOG = LoggerFactory.getLogger(OofPciPocDmaapConsumers.class);
46 private static final String SDNC_ENDPOINT = "SDNC.endpoint";
47 private static final String TEMPLATE = "SDNC.template";
48 private static final String DMAAPLISTENERROOT = "DMAAPLISTENERROOT";
49 private static final String UTF_8 = "UTF-8";
51 private static final String PARAMETER_NAME = "parameter-name";
52 private static final String STRING_VALUE = "string-value";
53 private static final String PHYSICAL_CELL_ID_INPUT_FAP_SERVICE = "configuration-phy-cell-id-input.fap-service";
54 private static final String EVENT_HEADER = "event-header";
55 private static final String ACTION = "Action";
56 private static final String CONFIGURATIONS = "Configurations";
57 private static final String MODIFY_CONFIG = "ModifyConfig";
58 private static final String DATA = "data";
59 private static final String FAP_SERVICE = "FAPService";
61 private static final String PAYLOAD = "Payload";
62 private static final String PCI_CHANGES_MAP_FILE_NAME = "pci-changes-from-policy-to-sdnr";
63 private static final String SLI_PARAMETERS = "sli_parameters";
64 private static final String RPC_NAME = "rpc-name";
65 private static final String BODY = "body";
66 private static final String INPUT = "input";
67 private static final String COMMON_HEADER = "CommonHeader";
68 private static final String TIME_STAMP = "TimeStamp";
69 private static final String REQUEST_ID = "RequestID";
70 private static final String SUB_REQUEST_ID = "SubRequestID";
72 private static final String TIME_STAMP_FOR_SLI = "timeStamp";
73 private static final String REQUEST_ID_FOR_SLI = "requestID";
74 private static final String SUB_REQUEST_ID_FOR_SLI = "subRequestID";
76 private static final String CONFIGURATION_PHY_CELL_ID_INPUT = "configuration-phy-cell-id-input.";
78 private static final String EMPTY = "";
79 private static final String ESCAPE_SEQUENCE_QUOTES = "\"";
81 private static final String GENERIC_NEIGHBOR_CONFIGURATION_INPUT = "generic-neighbor-configuration-input.";
82 private static final String GENERIC_NEIGHBOR_CONFIGURATION_INPUT_NEIGHBOR_LIST_IN_USE = GENERIC_NEIGHBOR_CONFIGURATION_INPUT.concat("neighbor-list-in-use");
83 private static final String MODIFY_CONFIG_ANR = "ModifyConfigANR";
84 private static final String ANR_CHANGES_MAP_FILE_NAME = "anr-changes-from-policy-to-sdnr";
86 private String rootDir;
88 protected VelocityEngine velocityEngine;
90 public OofPciPocDmaapConsumers() {
91 velocityEngine = new VelocityEngine();
92 Properties props = new Properties();
93 rootDir = System.getenv(DMAAPLISTENERROOT);
95 if ((rootDir == null) || (rootDir.length() == 0)) {
96 rootDir = "/opt/onap/sdnc/dmaap-listener/lib/";
99 rootDir = rootDir + "/lib/";
102 props.put("file.resource.loader.path", rootDir);
103 velocityEngine.init(props);
107 * for testing purposes
109 OofPciPocDmaapConsumers(Properties props) {
110 velocityEngine = new VelocityEngine();
111 velocityEngine.init(props);
114 protected String publish(String templatePath, String jsonString, JsonNode configurationsOrDataNode, boolean invokePciChangesPublish, boolean invokeAnrChangesPublish) throws IOException, InvalidMessageException
116 if (invokePciChangesPublish){
117 return publishPciChangesFromPolicyToSDNR(templatePath, configurationsOrDataNode, jsonString);
118 } else if (invokeAnrChangesPublish){
119 return publishANRChangesFromPolicyToSDNR(templatePath, configurationsOrDataNode, jsonString);
121 return publishFullMessage(templatePath, jsonString);
125 private String publishFullMessage(String templatePath, String jsonString) throws IOException
127 JSONObject jsonObj = new JSONObject(jsonString);
128 VelocityContext context = new VelocityContext();
129 for(Object key : jsonObj.keySet())
131 context.put((String)key, jsonObj.get((String)key));
134 String id = jsonObj.getJSONObject(EVENT_HEADER).get("id").toString();
135 context.put("req_id", id);
137 context.put("curr_time", Instant.now());
139 ObjectMapper oMapper = new ObjectMapper();
141 String rpcMsgbody = oMapper.writeValueAsString(jsonString);
142 context.put("full_message", rpcMsgbody);
144 Writer writer = new StringWriter();
145 velocityEngine.mergeTemplate(templatePath, UTF_8, context, writer);
148 return writer.toString();
151 private String publishANRChangesFromPolicyToSDNR(String templatePath, JsonNode dataNode, String msg) throws IOException, InvalidMessageException
153 VelocityContext context = new VelocityContext();
155 String RPC_NAME_KEY_IN_VT = "rpc_name";
156 String RPC_NAME_VALUE_IN_VT = "generic-neighbor-configuration";
158 String CELL_CONFIG = "CellConfig";
159 String ALIAS_LABEL = "alias";
162 String LTE_CELL = "LTECell";
163 String NEIGHBOR_LIST_IN_USE = "NeighborListInUse";
165 JSONObject numberOfEntries = new JSONObject();
166 JSONObject alias = new JSONObject();
167 JSONArray sliParametersArray = new JSONArray();
169 ObjectMapper oMapper = new ObjectMapper();
171 JsonNode dmaapMessageRootNode;
173 dmaapMessageRootNode = oMapper.readTree(msg);
174 } catch (Exception e) {
175 throw new InvalidMessageException("Cannot parse json object", e);
178 JsonNode commonHeader = dmaapMessageRootNode.get(BODY).get(INPUT).get(COMMON_HEADER);
180 JsonNode timeStamp = commonHeader.get(TIME_STAMP);
182 JsonNode requestID = commonHeader.get(REQUEST_ID);
184 JsonNode subRequestID = commonHeader.get(SUB_REQUEST_ID);
186 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, GENERIC_NEIGHBOR_CONFIGURATION_INPUT+TIME_STAMP_FOR_SLI).put(STRING_VALUE,timeStamp));
188 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, GENERIC_NEIGHBOR_CONFIGURATION_INPUT+REQUEST_ID_FOR_SLI).put(STRING_VALUE,requestID));
190 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, GENERIC_NEIGHBOR_CONFIGURATION_INPUT+SUB_REQUEST_ID_FOR_SLI).put(STRING_VALUE,subRequestID));
192 String aliasValue = dataNode.get(DATA).get(FAP_SERVICE).get(ALIAS_LABEL).textValue();
194 JsonNode nbrListInUse = dataNode.get(DATA).get(FAP_SERVICE).get(CELL_CONFIG).get(LTE).get(RAN).get(NEIGHBOR_LIST_IN_USE).get(LTE_CELL);
198 if(nbrListInUse.isArray()) {
199 for(JsonNode lteCell:nbrListInUse) {
200 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, GENERIC_NEIGHBOR_CONFIGURATION_INPUT_NEIGHBOR_LIST_IN_USE+"["+entryCount+"]."+"plmnid")
201 .put(STRING_VALUE, lteCell.get("PLMNID").toString().replace(ESCAPE_SEQUENCE_QUOTES, EMPTY)));
202 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, GENERIC_NEIGHBOR_CONFIGURATION_INPUT_NEIGHBOR_LIST_IN_USE+"["+entryCount+"]."+"cid")
203 .put(STRING_VALUE, lteCell.get("CID").toString().replace(ESCAPE_SEQUENCE_QUOTES, EMPTY)));
204 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, GENERIC_NEIGHBOR_CONFIGURATION_INPUT_NEIGHBOR_LIST_IN_USE+"["+entryCount+"]."+"phy-cell-id")
205 .put(STRING_VALUE, lteCell.get("PhyCellID").toString().replace(ESCAPE_SEQUENCE_QUOTES, EMPTY)));
206 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, GENERIC_NEIGHBOR_CONFIGURATION_INPUT_NEIGHBOR_LIST_IN_USE+"["+entryCount+"]."+"pnf-name")
207 .put(STRING_VALUE, lteCell.get("PNFName").toString().replace(ESCAPE_SEQUENCE_QUOTES, EMPTY)));
208 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, GENERIC_NEIGHBOR_CONFIGURATION_INPUT_NEIGHBOR_LIST_IN_USE+"["+entryCount+"]."+"blacklisted")
209 .put(STRING_VALUE, lteCell.get("Blacklisted").toString().replace(ESCAPE_SEQUENCE_QUOTES, EMPTY)));
214 alias.put(PARAMETER_NAME, GENERIC_NEIGHBOR_CONFIGURATION_INPUT+ALIAS_LABEL);
215 alias.put(STRING_VALUE, aliasValue);
217 numberOfEntries.put(PARAMETER_NAME, GENERIC_NEIGHBOR_CONFIGURATION_INPUT+"lte-cell-number-of-entries");
218 numberOfEntries.put(STRING_VALUE, entryCount);
220 sliParametersArray.put(alias);
221 sliParametersArray.put(numberOfEntries);
223 context.put(SLI_PARAMETERS, sliParametersArray);
225 context.put(RPC_NAME_KEY_IN_VT, RPC_NAME_VALUE_IN_VT);
227 Writer writer = new StringWriter();
228 velocityEngine.mergeTemplate(templatePath, UTF_8, context, writer);
231 return writer.toString();
234 throw new InvalidMessageException("nbrListInUse is not of Type Array. Could not read neighbor list elements");
239 private String publishPciChangesFromPolicyToSDNR(String templatePath, JsonNode configurationsJsonNode, String msg) throws IOException, InvalidMessageException
241 String RPC_NAME_KEY_IN_VT = "rpc_name";
242 String RPC_NAME_VALUE_IN_VT = "configuration-phy-cell-id";
243 String ALIAS = "alias";
244 String X0005b9Lte = "X0005b9Lte";
246 VelocityContext context = new VelocityContext();
248 JSONObject numberOfEntries = new JSONObject();
249 JSONArray sliParametersArray = new JSONArray();
251 JsonNode configurations = configurationsJsonNode.get(CONFIGURATIONS);
253 ObjectMapper oMapper = new ObjectMapper();
255 JsonNode dmaapMessageRootNode;
257 dmaapMessageRootNode = oMapper.readTree(msg);
258 } catch (Exception e) {
259 throw new InvalidMessageException("Cannot parse json object", e);
262 JsonNode commonHeader = dmaapMessageRootNode.get(BODY).get(INPUT).get(COMMON_HEADER);
264 JsonNode timeStamp = commonHeader.get(TIME_STAMP);
266 JsonNode requestID = commonHeader.get(REQUEST_ID);
268 JsonNode subRequestID = commonHeader.get(SUB_REQUEST_ID);
270 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, CONFIGURATION_PHY_CELL_ID_INPUT+TIME_STAMP_FOR_SLI).put(STRING_VALUE,timeStamp));
272 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, CONFIGURATION_PHY_CELL_ID_INPUT+REQUEST_ID_FOR_SLI).put(STRING_VALUE,requestID));
274 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, CONFIGURATION_PHY_CELL_ID_INPUT+SUB_REQUEST_ID_FOR_SLI).put(STRING_VALUE,subRequestID));
278 if(configurations.isArray()) {
279 for(JsonNode dataNode:configurations) {
280 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, PHYSICAL_CELL_ID_INPUT_FAP_SERVICE+"["+entryCount+"]."+ALIAS)
281 .put(STRING_VALUE, dataNode.get(DATA).get(FAP_SERVICE).get(ALIAS).toString().replace(ESCAPE_SEQUENCE_QUOTES, EMPTY)));
282 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, PHYSICAL_CELL_ID_INPUT_FAP_SERVICE+"["+entryCount+"]."+"cid")
283 .put(STRING_VALUE, dataNode.get(DATA).get(FAP_SERVICE).get("CellConfig").get("LTE").get("RAN").get("Common").get("CellIdentity").toString().replace(ESCAPE_SEQUENCE_QUOTES, EMPTY)));
284 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, PHYSICAL_CELL_ID_INPUT_FAP_SERVICE+"["+entryCount+"]."+"phy-cell-id-in-use")
285 .put(STRING_VALUE, dataNode.get(DATA).get(FAP_SERVICE).get(X0005b9Lte).get("phyCellIdInUse").toString().replace(ESCAPE_SEQUENCE_QUOTES, EMPTY)));
286 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, PHYSICAL_CELL_ID_INPUT_FAP_SERVICE+"["+entryCount+"]."+"pnf-name")
287 .put(STRING_VALUE, dataNode.get(DATA).get(FAP_SERVICE).get(X0005b9Lte).get("pnfName").toString().replace(ESCAPE_SEQUENCE_QUOTES, EMPTY)));
291 numberOfEntries.put(PARAMETER_NAME, PHYSICAL_CELL_ID_INPUT_FAP_SERVICE+"-number-of-entries");
292 numberOfEntries.put(STRING_VALUE, entryCount);
294 sliParametersArray.put(numberOfEntries);
296 context.put(SLI_PARAMETERS, sliParametersArray);
298 context.put(RPC_NAME_KEY_IN_VT, RPC_NAME_VALUE_IN_VT);
300 Writer writer = new StringWriter();
301 velocityEngine.mergeTemplate(templatePath, UTF_8, context, writer);
304 return writer.toString();
307 throw new InvalidMessageException("Configurations is not of Type Array. Could not read configuration changes");
313 public void processMsg(String msg) throws InvalidMessageException {
316 throw new InvalidMessageException("Null message");
319 ObjectMapper oMapper = new ObjectMapper();
320 JsonNode dmaapMessageRootNode;
322 dmaapMessageRootNode = oMapper.readTree(msg);
323 } catch (Exception e) {
324 throw new InvalidMessageException("Cannot parse json object", e);
328 JsonNode rpcnameNode = dmaapMessageRootNode.get(RPC_NAME);
329 if(rpcnameNode == null) {
330 LOG.info("Unable to identify the respective consumer to invoke. Please verify the dmaap message..");
334 String rpcname = rpcnameNode.textValue();
336 if(!MODIFY_CONFIG.toLowerCase().equals(rpcname) && !MODIFY_CONFIG_ANR.toLowerCase().equals(rpcname)) {
337 LOG.info("Unknown rpc name {}", rpcname);
341 if(MODIFY_CONFIG.toLowerCase().equals(rpcname)) {
342 invokePCIChangesConsumer(dmaapMessageRootNode, oMapper, msg);
346 if(MODIFY_CONFIG_ANR.toLowerCase().equals(rpcname)) {
347 invokeANRChangesConsumer(dmaapMessageRootNode, oMapper, msg);
353 private void invokeANRChangesConsumer(JsonNode dmaapMessageRootNode, ObjectMapper oMapper,
354 String msg) throws InvalidMessageException {
355 JsonNode body = dmaapMessageRootNode.get(BODY);
357 LOG.info("Missing body node.");
361 JsonNode input = body.get(INPUT);
363 LOG.info("Missing input node.");
367 JsonNode action = input.get(ACTION);
369 LOG.info("Missing action node.");
373 if(!MODIFY_CONFIG_ANR.equals(action.textValue())) {
374 LOG.info("Unknown Action {}", action);
378 JsonNode payload = input.get(PAYLOAD);
379 if(payload == null) {
380 LOG.info("Missing payload node.");
384 String payloadText = payload.asText();
386 if(!payloadText.contains(CONFIGURATIONS)) {
387 LOG.info("Missing configurations node.");
391 JsonNode configurationsJsonNode;
393 configurationsJsonNode = oMapper.readTree(payloadText);
394 } catch (Exception e) {
395 throw new InvalidMessageException("Cannot parse payload value", e);
398 String mapFilename = rootDir + ANR_CHANGES_MAP_FILE_NAME + ".map";
399 Map<String, String> fieldMap = loadMap(mapFilename);
400 if (fieldMap == null) {
404 if (!fieldMap.containsKey(SDNC_ENDPOINT)) {
407 String sdncEndpoint = fieldMap.get(SDNC_ENDPOINT);
409 if (!fieldMap.containsKey(TEMPLATE)) {
410 throw new InvalidMessageException("No SDNC template known for message ");
412 String templateName = fieldMap.get(TEMPLATE);
414 JsonNode configurations = configurationsJsonNode.get(CONFIGURATIONS);
416 if(configurations.isArray()) {
417 for(JsonNode dataNode:configurations) {
418 if(dataNode.get(DATA).get(FAP_SERVICE) == null) {
419 LOG.info("Could not make a rpc call. Missing fapService node for dataNode element::", dataNode.textValue());
421 buildAndInvokeANRChangesRPC(sdncEndpoint, templateName,msg, dataNode);
425 throw new InvalidMessageException("Configurations is not of Type Array. Could not read configuration changes");
429 private void invokePCIChangesConsumer(JsonNode dmaapMessageRootNode, ObjectMapper oMapper,
430 String msg) throws InvalidMessageException {
431 JsonNode body = dmaapMessageRootNode.get(BODY);
433 LOG.info("Missing body node.");
437 JsonNode input = body.get(INPUT);
439 LOG.info("Missing input node.");
443 JsonNode action = input.get(ACTION);
445 LOG.info("Missing action node.");
450 if(!MODIFY_CONFIG.equals(action.textValue())) {
451 LOG.info("Unknown Action {}", action);
455 JsonNode payload = input.get(PAYLOAD);
456 if(payload == null) {
457 LOG.info("Missing payload node.");
461 String configurations = payload.asText();
463 if(!configurations.contains(CONFIGURATIONS)) {
464 LOG.info("Missing configurations node.");
468 JsonNode configurationsJsonNode;
470 configurationsJsonNode = oMapper.readTree(configurations);
471 } catch (Exception e) {
472 throw new InvalidMessageException("Cannot parse payload value", e);
475 String mapFilename = rootDir + PCI_CHANGES_MAP_FILE_NAME + ".map";
476 Map<String, String> fieldMap = loadMap(mapFilename);
477 if (fieldMap == null) {
481 if (!fieldMap.containsKey(SDNC_ENDPOINT)) {
484 String sdncEndpoint = fieldMap.get(SDNC_ENDPOINT);
486 if (!fieldMap.containsKey(TEMPLATE)) {
487 throw new InvalidMessageException("No SDNC template known for message ");
489 String templateName = fieldMap.get(TEMPLATE);
491 buildAndInvokePCIChangesRPC(sdncEndpoint, templateName, msg, configurationsJsonNode);
494 private void buildAndInvokePCIChangesRPC(String sdncEndpoint, String templateName, String msg, JsonNode configurationsOrDataNode) {
496 String rpcMsgbody = publish(templateName, msg, configurationsOrDataNode, true, false);
497 String odlUrlBase = getProperty("sdnc.odl.url-base");
498 String odlUser = getProperty("sdnc.odl.user");
499 String odlPassword = getProperty("sdnc.odl.password");
501 if ((odlUrlBase != null) && (odlUrlBase.length() > 0)) {
502 SdncOdlConnection conn = SdncOdlConnection.newInstance(odlUrlBase + "/" + sdncEndpoint, odlUser, odlPassword);
504 conn.send("POST", "application/json", rpcMsgbody);
506 LOG.info("POST message body would be:\n" + rpcMsgbody);
508 } catch (Exception e) {
509 LOG.error("Unable to process message", e);
513 private void buildAndInvokeANRChangesRPC(String sdncEndpoint, String templateName, String msg, JsonNode configurationsOrDataNode) {
515 String rpcMsgbody = publish(templateName, msg, configurationsOrDataNode, false, true);
516 String odlUrlBase = getProperty("sdnc.odl.url-base");
517 String odlUser = getProperty("sdnc.odl.user");
518 String odlPassword = getProperty("sdnc.odl.password");
520 if ((odlUrlBase != null) && (odlUrlBase.length() > 0)) {
521 SdncOdlConnection conn = SdncOdlConnection.newInstance(odlUrlBase + "/" + sdncEndpoint, odlUser, odlPassword);
523 conn.send("POST", "application/json", rpcMsgbody);
525 LOG.info("POST message body would be:\n" + rpcMsgbody);
527 } catch (Exception e) {
528 LOG.error("Unable to process message", e);
532 private Map<String, String> loadMap(String mapFilename) {
533 File mapFile = new File(mapFilename);
535 if (!mapFile.canRead()) {
536 LOG.error(String.format("Cannot read map file (%s)", mapFilename));
540 Map<String, String> results = new HashMap<>();
541 try (BufferedReader mapReader = new BufferedReader(new FileReader(mapFile))) {
545 while ((curLine = mapReader.readLine()) != null) {
546 curLine = curLine.trim();
548 if ((curLine.length() > 0) && (!curLine.startsWith("#")) && curLine.contains("=>")) {
549 String[] entry = curLine.split("=>");
550 if (entry.length == 2) {
551 results.put(entry[0].trim(), entry[1].trim());
556 } catch (Exception e) {
557 LOG.error("Caught exception reading map " + mapFilename, e);