dd1ca7086cdb110ce115fb80e75cf185850b1cc8
[ccsdk/features.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.ccsdk.features.sdnr.northbound.oofpcipoc;
23
24 import java.io.Writer;
25 import java.io.StringWriter;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map;
29 import java.io.IOException;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32
33 import org.apache.velocity.VelocityContext;
34 import org.apache.velocity.app.VelocityEngine;
35 import org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.handlenotif.pojos.CellConfig;
36 import org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.handlenotif.pojos.FAPServiceList;
37 import org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.handlenotif.pojos.LTE;
38 import org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.handlenotif.pojos.LTENeighborListInUseLTECell;
39 import org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.handlenotif.pojos.NeighborListInUse;
40 import org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.handlenotif.pojos.PayloadObject;
41 import org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.handlenotif.pojos.RAN;
42 import org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.handlenotif.pojos.RadioAccess;
43 import org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.handlenotif.pojos.X0005b9Lte;
44
45 import org.opendaylight.mdsal.binding.api.DataBroker;
46
47 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.rev190308.OofpcipocListener;
48 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.rev190308.NbrlistChangeNotification;
49 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.rev190308.NetconfConfigChange;
50 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.rev190308.nbrlist.change.notification.*;
51 import org.opendaylight.yang.gen.v1.org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.rev190308.nbrlist.change.notification.fap.service.*;
52
53 import com.fasterxml.jackson.core.JsonProcessingException;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58 import org.json.JSONArray;
59 import org.json.JSONObject;
60
61
62 /**
63  * Defines a base implementation for your listener. This class extends from a
64  * helper class which provides storage for the most commonly used components of
65  * the MD-SAL. Additionally the base class provides some basic logging and
66  * initialization / clean up methods.
67  *
68  */
69 public class OofpcipocHandleNotif implements AutoCloseable, OofpcipocListener {
70
71         private static final Logger LOG = LoggerFactory.getLogger(OofpcipocHandleNotif.class);
72
73         private static final String APPLICATION_NAME = "Oofpcipoc";
74
75         private static final String TRUE = "true";
76         private static final String FALSE = "false";
77         private static final String UTF_8 = "UTF-8";
78
79         private static final String PARAMETER_NAME = "parameter-name";
80     private static final String STRING_VALUE = "string-value";
81
82         private final ExecutorService executor;
83
84         protected DataBroker dataBroker;
85
86         private OofpcipocClient OofpcipocClient;
87
88         public OofpcipocHandleNotif() {
89
90                 this.LOG.info("Creating listener for {}", APPLICATION_NAME);
91                 executor = Executors.newFixedThreadPool(1);
92                 this.dataBroker = null;
93                 this.OofpcipocClient = null;
94         }
95         
96         public void setDataBroker(DataBroker dataBroker) {
97                 this.dataBroker = dataBroker;
98         }
99         
100         public void setClient(OofpcipocClient OofpcipocClient) {
101                 this.OofpcipocClient = OofpcipocClient;
102         }
103
104         public void init() {
105                 LOG.info("Placeholder: Initializing listener  for {}", APPLICATION_NAME);
106         }
107
108         @Override
109         public void close() throws Exception {
110                 LOG.info("Closing listener for {}", APPLICATION_NAME);
111                 executor.shutdown();
112                 LOG.info("Successfully closed listener for {}", APPLICATION_NAME);
113         }
114
115         @Override
116         public void onNbrlistChangeNotification(final NbrlistChangeNotification notification) {
117
118                 LOG.info("Reached onNbrlistChangeNotification");
119
120                 LOG.info("Number of FAPService Entries Changed {}", notification.getFapServiceNumberOfEntriesChanged());
121
122                 // START: Create RadioAccess payload object/string from the notification
123
124                 String payloadString = null;
125                 PayloadObject payloadObject = new PayloadObject();
126                 org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.handlenotif.pojos.Payload payload = new org.onap.ccsdk.features.sdnr.northbound.oofpcipoc.handlenotif.pojos.Payload();
127                 RadioAccess radioAccess = new RadioAccess();
128                 List<FAPServiceList> fAPServiceList = new ArrayList<>();
129
130                 radioAccess.setFAPServiceNumberOfEntries(notification.getFapServiceNumberOfEntriesChanged().toString());
131                 Map<FapServiceKey, FapService> fapSvcs = notification.getFapService();
132
133                 for (Map.Entry<FapServiceKey,FapService> entry : fapSvcs.entrySet() ) {
134
135                         FapService fapSvc = entry.getValue();
136                         FAPServiceList fapServiceElement = new FAPServiceList();
137
138                         fapServiceElement.setAlias(fapSvc.getAlias());
139                         fapServiceElement.setX0005b9Lte(new X0005b9Lte(fapSvc.getPhyCellIdInUse().toString(), fapSvc.getPnfName()));
140
141                         List<LTENeighborListInUseLTECell> lTENeighborListInUseLTECell = new ArrayList<>();
142                         Map<LteRanNeighborListInUseLteCellChangedKey, LteRanNeighborListInUseLteCellChanged> lteRanElements = fapSvc.getLteRanNeighborListInUseLteCellChanged();
143                         for (Map.Entry<LteRanNeighborListInUseLteCellChangedKey, LteRanNeighborListInUseLteCellChanged> lteRanEntry : lteRanElements.entrySet()) {
144                                 LteRanNeighborListInUseLteCellChanged lteRanElement = lteRanEntry.getValue(); 
145                                 LTENeighborListInUseLTECell lTENeighborListInUseLTECellElement = new LTENeighborListInUseLTECell();
146                                 lTENeighborListInUseLTECellElement.setAlias(lteRanElement.getCid());
147                                 lTENeighborListInUseLTECellElement.setBlacklisted(lteRanElement.isBlacklisted().toString());
148                                 lTENeighborListInUseLTECellElement.setCid(lteRanElement.getCid());
149                                 lTENeighborListInUseLTECellElement.setEnable(TRUE);
150                                 lTENeighborListInUseLTECellElement.setMustInclude(TRUE);
151                                 lTENeighborListInUseLTECellElement.setPhyCellId(lteRanElement.getPhyCellId().toString());
152                                 lTENeighborListInUseLTECellElement.setPlmnid(lteRanElement.getPlmnid());
153                                 lTENeighborListInUseLTECellElement.setPnfName(lteRanElement.getPnfName());
154
155                                 lTENeighborListInUseLTECell.add(lTENeighborListInUseLTECellElement);
156                         }
157
158                         NeighborListInUse neighborListInUse = new NeighborListInUse();
159                         neighborListInUse.setLTECellNumberOfEntries(
160                                         String.valueOf(fapSvc.getLteRanNeighborListInUseLteCellChanged().size()));
161                         neighborListInUse.setLTENeighborListInUseLTECell(lTENeighborListInUseLTECell);
162
163                         CellConfig cellConfig = new CellConfig();
164                         LTE lTE = new LTE();
165                         RAN rAN = new RAN();
166
167                         rAN.setCellIdentity(fapSvc.getCid());
168                         rAN.setNeighborListInUse(neighborListInUse);
169
170                         lTE.setRAN(rAN);
171                         cellConfig.setLTE(lTE);
172                         fapServiceElement.setCellConfig(cellConfig);
173
174                         fAPServiceList.add(fapServiceElement);
175                 }
176                 radioAccess.setFAPServiceList(fAPServiceList);
177                 payload.setRadioAccess(radioAccess);
178                 payloadObject.setPayload(payload);
179
180                 ObjectMapper mapper = new ObjectMapper();
181
182                 try {
183                         payloadString = mapper.writeValueAsString(payloadObject.getPayload());
184                         LOG.info("Stringified Payload Object::" + payloadString + "\n" + "\n");
185                 } catch (JsonProcessingException jsonProcessingException) {
186                         LOG.error("Error while processing Payload Object", jsonProcessingException);
187                         return;
188                 }
189
190                 // END: Create RadioAccess payload object/string from the notification
191
192                 // START: Build RPC message and invoke RPC
193
194                 String rpcMessageBody = null;
195
196                 try {
197                         rpcMessageBody = buildHandleNbrlistChangeNotifRPCMsg(radioAccess, payloadString, "/opt/onap/ccsdk/data/properties/rpc-message-sliapi-execute.vt");
198                         LOG.debug("rpc message body::" + rpcMessageBody);
199                         invokeRPC(rpcMessageBody);
200                 } catch (Exception e) {
201                         LOG.error("Unable to build rpc message body::", e);
202                         return;
203                 }
204         }
205         private void invokeRPC(String rpcMsgbody) {
206                 try {
207             String odlUrlBase = "http://sdnc.onap:8282/restconf/operations"; //using cluster SDNC URL
208             String odlUser = "admin";
209             String odlPassword = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
210             String sdncEndpoint = " SLI-API:execute-graph";
211
212             if ((odlUrlBase != null) && (odlUrlBase.length() > 0)) {
213                 SdncOdlConnection conn = SdncOdlConnection.newInstance(odlUrlBase + "/" + sdncEndpoint, odlUser, odlPassword);
214
215                 conn.send("POST", "application/json", rpcMsgbody);
216             } else {
217                 LOG.info("POST message body would be:\n" + rpcMsgbody);
218             }
219         } catch (Exception e) {
220             LOG.error("Unable to process message", e);
221         }
222         }
223
224         private String buildHandleNbrlistChangeNotifRPCMsg(RadioAccess radioAccess, String payloadString, String templatePath) throws IOException {
225                 VelocityEngine velocityEngine = new VelocityEngine();
226                 velocityEngine.init();
227
228                 String SLI_PARAMETERS = "sli_parameters";
229                 String HANDLE_NBRLIST_CHANGE_NOTIF = "handle-nbrlist-change-notif";
230                 String HANDLE_NBRLIST_CHANGE_NOTIF_INPUT = HANDLE_NBRLIST_CHANGE_NOTIF+"-input.";
231                 String HANDLE_NBRLIST_CHANGE_NOTIF_INPUT_FAP_SERVICE = HANDLE_NBRLIST_CHANGE_NOTIF_INPUT+"fap-service";
232                 String FAP_SERVICE_NO_OF_ENTRIES_CHANGED= "fap-service-number-of-entries-changed";
233
234                 JSONArray sliParametersArray = new JSONArray();
235
236                 VelocityContext context = new VelocityContext();
237                 context.put("rpc_name", "handle-nbrlist-change-notif");
238
239                 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, HANDLE_NBRLIST_CHANGE_NOTIF_INPUT+FAP_SERVICE_NO_OF_ENTRIES_CHANGED)
240                         .put(STRING_VALUE, radioAccess.getFAPServiceNumberOfEntries()));
241
242                 int count = 0;
243
244                 for(FAPServiceList fapServiceListElement: radioAccess.getFAPServiceList()) {
245
246                         String prefix = HANDLE_NBRLIST_CHANGE_NOTIF_INPUT_FAP_SERVICE+"["+count+"].";
247
248                         sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, prefix+"alias")
249                                 .put(STRING_VALUE, fapServiceListElement.getAlias()));
250                         sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, prefix+"cid")
251                                 .put(STRING_VALUE, fapServiceListElement.getAlias()));
252                         sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, prefix+"phy-cell-id-in-use")
253                                 .put(STRING_VALUE, fapServiceListElement.getX0005b9Lte().getPhyCellIdInUse()));
254                         sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, prefix+"pnf-name")
255                                 .put(STRING_VALUE, fapServiceListElement.getX0005b9Lte().getPnfName()));
256
257                         sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, prefix+"lte-cell-number-of-entries")
258                                 .put(STRING_VALUE, fapServiceListElement.getCellConfig().getLTE().getRAN().getNeighborListInUse().getLTECellNumberOfEntries()));
259
260                         int lteNbrListInUseCount = 0;
261                         for(LTENeighborListInUseLTECell lTENeighborListInUseLTECellElement: fapServiceListElement.getCellConfig().getLTE().getRAN().getNeighborListInUse().getLTENeighborListInUseLTECell()) {
262                                 String lteNbrListPrefix = "lte-ran-neighbor-list-in-use-lte-cell-changed["+lteNbrListInUseCount+"].";
263                                 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, prefix+lteNbrListPrefix+"plmnid")
264                                         .put(STRING_VALUE, lTENeighborListInUseLTECellElement.getPlmnid()));
265                                 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, prefix+lteNbrListPrefix+"cid")
266                                         .put(STRING_VALUE, lTENeighborListInUseLTECellElement.getCid()));
267                                 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, prefix+lteNbrListPrefix+"phy-cell-id")
268                                         .put(STRING_VALUE, lTENeighborListInUseLTECellElement.getPhyCellId()));
269                                 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, prefix+lteNbrListPrefix+"pnf-name")
270                                         .put(STRING_VALUE, lTENeighborListInUseLTECellElement.getPnfName()));
271                                 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, prefix+lteNbrListPrefix+"blacklisted")
272                                         .put(STRING_VALUE, lTENeighborListInUseLTECellElement.getBlacklisted()));
273                                 lteNbrListInUseCount++;
274                         }
275
276                         count++;
277                 }
278
279                 sliParametersArray.put(new JSONObject().put(PARAMETER_NAME, HANDLE_NBRLIST_CHANGE_NOTIF_INPUT+"payload")
280                         .put(STRING_VALUE, payloadString));
281
282                 context.put(SLI_PARAMETERS, sliParametersArray);
283
284                 Writer writer = new StringWriter();
285                 velocityEngine.mergeTemplate(templatePath, UTF_8, context, writer);
286                 writer.flush();
287
288                 return writer.toString();
289         }
290
291         @Override
292         public void onNetconfConfigChange(final NetconfConfigChange notification) {
293
294         }
295
296 }