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 PciChangesFromPolicyToSDNRDmaapConsumer extends SdncDmaapConsumerImpl {
45 private static final Logger LOG = LoggerFactory.getLogger(PciChangesFromPolicyToSDNRDmaapConsumer.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";
50 private static final String PARAMETER_NAME = "parameter-name";
51 private static final String STRING_VALUE = "string-value";
52 private static final String PHYSICAL_CELL_ID_INPUT_FAP_SERVICE = "configuration-phy-cell-id-input.fap-service";
53 private static final String EVENT_HEADER = "event-header";
54 private static final String ACTION = "Action";
55 private static final String PAYLOAD = "Payload";
56 private static final String CONFIGURATIONS = "Configurations";
57 private static final String MODIFY_CONFIG = "ModifyConfig";
58 private static final String MAP_FILE_NAME = "pci-changes-from-policy-to-sdnr";
59 private static final String DATA = "data";
60 private static final String FAP_SERVICE = "FAPService";
61 private static final String SLI_PARAMETERS = "sli_parameters";
62 private static final String RPC_NAME = "rpc-name";
63 private static final String BODY = "body";
64 private static final String INPUT = "input";
66 private String rootDir;
68 protected VelocityEngine velocityEngine;
70 public PciChangesFromPolicyToSDNRDmaapConsumer() {
71 velocityEngine = new VelocityEngine();
72 Properties props = new Properties();
73 rootDir = System.getenv(DMAAPLISTENERROOT);
75 if ((rootDir == null) || (rootDir.length() == 0)) {
76 rootDir = "/opt/app/dmaap-listener/lib/";
79 rootDir = rootDir + "/lib/";
82 props.put("file.resource.loader.path", rootDir);
83 velocityEngine.init(props);
87 * for testing purposes
89 PciChangesFromPolicyToSDNRDmaapConsumer(Properties props) {
90 velocityEngine = new VelocityEngine();
91 velocityEngine.init(props);
94 protected String publish(String templatePath, String jsonString, JsonNode configurationsJsonNode) throws IOException, InvalidMessageException
96 if (templatePath.contains("anr-pci-changes-from-policy-to-sdnr")){
97 return publishPciChangesFromPolicyToSDNR(templatePath, configurationsJsonNode);
99 return publishFullMessage(templatePath, jsonString);
103 private String publishFullMessage(String templatePath, String jsonString) throws IOException
105 JSONObject jsonObj = new JSONObject(jsonString);
106 VelocityContext context = new VelocityContext();
107 for(Object key : jsonObj.keySet())
109 context.put((String)key, jsonObj.get((String)key));
112 String id = jsonObj.getJSONObject(EVENT_HEADER).get("id").toString();
113 context.put("req_id", id);
115 context.put("curr_time", Instant.now());
117 ObjectMapper oMapper = new ObjectMapper();
119 String rpcMsgbody = oMapper.writeValueAsString(jsonString);
120 context.put("full_message", rpcMsgbody);
122 Writer writer = new StringWriter();
123 velocityEngine.mergeTemplate(templatePath, "UTF-8", context, writer);
126 return writer.toString();
129 private String publishPciChangesFromPolicyToSDNR(String templatePath, JsonNode configurationsJsonNode) throws IOException, InvalidMessageException
131 String RPC_NAME_KEY_IN_VT = "rpc_name";
132 String RPC_NAME_VALUE_IN_VT = "configuration-phy-cell-id";
133 String ALIAS = "alias";
134 String X0005b9Lte = "X0005b9Lte";
136 VelocityContext context = new VelocityContext();
138 JSONObject numberOfEntries = new JSONObject();
139 JSONArray sliParametersArray = new JSONArray();
141 JsonNode configurations = configurationsJsonNode.get(CONFIGURATIONS);
145 if(configurations.isArray()) {
146 for(JsonNode dataNode:configurations) {
147 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, PHYSICAL_CELL_ID_INPUT_FAP_SERVICE+"["+entryCount+"]."+ALIAS)
148 .put(STRING_VALUE, dataNode.get(DATA).get(FAP_SERVICE).get(ALIAS)));
149 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, PHYSICAL_CELL_ID_INPUT_FAP_SERVICE+"["+entryCount+"]."+"cid")
150 .put(STRING_VALUE, dataNode.get(DATA).get(FAP_SERVICE).get("CellConfig").get("LTE").get("RAN").get("Common").get("CellIdentity")));
151 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, PHYSICAL_CELL_ID_INPUT_FAP_SERVICE+"["+entryCount+"]."+"phy-cell-id-in-use")
152 .put(STRING_VALUE, dataNode.get(DATA).get(FAP_SERVICE).get(X0005b9Lte).get("phyCellIdInUse")));
153 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, PHYSICAL_CELL_ID_INPUT_FAP_SERVICE+"["+entryCount+"]."+"pnf-name")
154 .put(STRING_VALUE, dataNode.get(DATA).get(FAP_SERVICE).get(X0005b9Lte).get("pnfName")));
158 numberOfEntries.put(PARAMETER_NAME, PHYSICAL_CELL_ID_INPUT_FAP_SERVICE+"-number-of-entries");
159 numberOfEntries.put(STRING_VALUE, entryCount);
161 sliParametersArray.put(numberOfEntries);
163 context.put(SLI_PARAMETERS, sliParametersArray);
165 context.put(RPC_NAME_KEY_IN_VT, RPC_NAME_VALUE_IN_VT);
167 Writer writer = new StringWriter();
168 velocityEngine.mergeTemplate(templatePath, "UTF-8", context, writer);
171 return writer.toString();
174 throw new InvalidMessageException("Configurations is not of Type Array. Could not read configuration changes");
180 public void processMsg(String msg) throws InvalidMessageException {
183 throw new InvalidMessageException("Null message");
186 ObjectMapper oMapper = new ObjectMapper();
187 JsonNode pciChangesRootNode;
189 pciChangesRootNode = oMapper.readTree(msg);
190 } catch (Exception e) {
191 throw new InvalidMessageException("Cannot parse json object", e);
194 JsonNode rpcname = pciChangesRootNode.get(RPC_NAME);
195 if(rpcname == null) {
196 LOG.info("Missing rpc-name node.");
200 if(!MODIFY_CONFIG.toLowerCase().equals(rpcname.textValue())) {
201 LOG.info("Unknown rpc name {}", rpcname);
205 JsonNode body = pciChangesRootNode.get(BODY);
207 LOG.info("Missing body node.");
211 JsonNode input = body.get(INPUT);
213 LOG.info("Missing input node.");
217 JsonNode action = input.get(ACTION);
219 LOG.info("Missing action node.");
223 if(!MODIFY_CONFIG.equals(action.textValue())) {
224 LOG.info("Unknown Action {}", action);
228 JsonNode payload = input.get(PAYLOAD);
229 if(payload == null) {
230 LOG.info("Missing payload node.");
234 String configurations = payload.asText();
236 if(!configurations.contains(CONFIGURATIONS)) {
237 LOG.info("Missing configurations node.");
241 JsonNode configurationsJsonNode;
243 configurationsJsonNode = oMapper.readTree(configurations);
244 } catch (Exception e) {
245 throw new InvalidMessageException("Cannot parse payload value", e);
248 String mapFilename = rootDir + MAP_FILE_NAME + ".map";
249 Map<String, String> fieldMap = loadMap(mapFilename);
250 if (fieldMap == null) {
254 if (!fieldMap.containsKey(SDNC_ENDPOINT)) {
257 String sdncEndpoint = fieldMap.get(SDNC_ENDPOINT);
259 if (!fieldMap.containsKey(TEMPLATE)) {
260 throw new InvalidMessageException("No SDNC template known for message ");
262 String templateName = fieldMap.get(TEMPLATE);
265 String rpcMsgbody = publish(templateName, msg, configurationsJsonNode);
266 String odlUrlBase = getProperty("sdnc.odl.url-base");
267 String odlUser = getProperty("sdnc.odl.user");
268 String odlPassword = getProperty("sdnc.odl.password");
270 if ((odlUrlBase != null) && (odlUrlBase.length() > 0)) {
271 SdncOdlConnection conn = SdncOdlConnection.newInstance(odlUrlBase + "/" + sdncEndpoint, odlUser, odlPassword);
273 conn.send("POST", "application/json", rpcMsgbody);
275 LOG.info("POST message body would be:\n" + rpcMsgbody);
277 } catch (Exception e) {
278 LOG.error("Unable to process message", e);
282 private Map<String, String> loadMap(String mapFilename) {
283 File mapFile = new File(mapFilename);
285 if (!mapFile.canRead()) {
286 LOG.error(String.format("Cannot read map file (%s)", mapFilename));
290 Map<String, String> results = new HashMap<>();
291 try (BufferedReader mapReader = new BufferedReader(new FileReader(mapFile))) {
295 while ((curLine = mapReader.readLine()) != null) {
296 curLine = curLine.trim();
298 if ((curLine.length() > 0) && (!curLine.startsWith("#")) && curLine.contains("=>")) {
299 String[] entry = curLine.split("=>");
300 if (entry.length == 2) {
301 results.put(entry[0].trim(), entry[1].trim());
306 } catch (Exception e) {
307 LOG.error("Caught exception reading map " + mapFilename, e);