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