package org.batfish.bgp;

import com.google.auto.service.AutoService;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import org.batfish.common.BatfishException;
import org.batfish.common.BfConsts;
import org.batfish.common.NetworkSnapshot;
import org.batfish.common.plugin.ExternalBgpAdvertisementPlugin;
import org.batfish.common.plugin.Plugin;
import org.batfish.common.util.BatfishObjectMapper;
import org.batfish.datamodel.BgpAdvertisement;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

@AutoService(Plugin.class)
public class JsonExternalBgpAdvertisementPlugin extends ExternalBgpAdvertisementPlugin {

  @Override
  protected void externalBgpAdvertisementPluginInitialize() {}

  @Override
  public Set<BgpAdvertisement> loadExternalBgpAdvertisements(NetworkSnapshot snapshot) {
    Set<BgpAdvertisement> advertSet = new LinkedHashSet<>();
    String externalBgpAnnouncementsFileContents =
        _batfish.readExternalBgpAnnouncementsFile(snapshot);
    if (externalBgpAnnouncementsFileContents != null) {
      // Populate advertSet with BgpAdvertisements that
      // gets passed to populatePrecomputedBgpAdvertisements.
      // See populatePrecomputedBgpAdvertisements for the things that get
      // extracted from these advertisements.

      try {
        JSONObject jsonObj = new JSONObject(externalBgpAnnouncementsFileContents);

        JSONArray announcements = jsonObj.getJSONArray(BfConsts.PROP_BGP_ANNOUNCEMENTS);

        for (int index = 0; index < announcements.length(); index++) {
          JSONObject announcement = new JSONObject();
          JSONObject announcementSrc = announcements.getJSONObject(index);
          for (Iterator<?> i = announcementSrc.keys(); i.hasNext(); ) {
            String key = (String) i.next();
            announcement.put(key, announcementSrc.get(key));
          }
          BgpAdvertisement bgpAdvertisement =
              BatfishObjectMapper.mapper()
                  .readValue(announcement.toString(), BgpAdvertisement.class);
          advertSet.add(bgpAdvertisement);
        }

      } catch (JSONException | IOException e) {
        throw new BatfishException("Error processing external BGP advertisements file", e);
      }
    }
    return advertSet;
  }
}

@@ -19,16 +19,70 @@
 h.bgp;%0A%0A
+import com.fasterxml.jackson.annotation.JsonProperty;%0A
 import c
@@ -149,35 +149,8 @@
 on;%0A
-import java.util.Iterator;%0A
 impo
@@ -173,24 +173,47 @@
 kedHashSet;%0A
+import java.util.List;%0A
 import java.
@@ -270,44 +270,8 @@
 on;%0A
-import org.batfish.common.BfConsts;%0A
 impo
@@ -518,264 +518,246 @@
 nt;%0A
-import org.codehaus.jettison.json.JSONArray;%0Aimport org.codehaus.jettison.json.JSONException;%0Aimport org.codehaus.jettison.json.JSONObject;%0A%0A@AutoService(Plugin.class)%0Apublic class JsonExternalBgpAdvertisementPlugin extends ExternalBgpAdvertisementPlugin %7B
+%0A@AutoService(Plugin.class)%0Apublic class JsonExternalBgpAdvertisementPlugin extends ExternalBgpAdvertisementPlugin %7B%0A%0A  private static class Announcements %7B%0A    @JsonProperty(%22Announcements%22)%0A    List%3CBgpAdvertisement%3E _announcements;%0A  %7D
 %0A%0A  
@@ -935,69 +935,8 @@
 ) %7B%0A
-    Set%3CBgpAdvertisement%3E advertSet = new LinkedHashSet%3C%3E();%0A
     
@@ -1361,611 +1361,38 @@
     
-JSONObject jsonObj = new JSONObject(externalBgpAnnouncementsFileContents);%0A%0A        JSONArray announcements = jsonObj.getJSONArray(BfConsts.PROP_BGP_ANNOUNCEMENTS);%0A%0A        for (int index = 0; index %3C announcements.length(); index++) %7B%0A          JSONObject announcement = new JSONObject();%0A          JSONObject announcementSrc = announcements.getJSONObject(index);%0A          for (Iterator%3C?%3E i = announcementSrc.keys(); i.hasNext(); ) %7B%0A            String key = (String) i.next();%0A            announcement.put(key, announcementSrc.get(key));%0A          %7D%0A          BgpAdvertisement bgpAdvertisement
+Announcements parsedAdverts
  =%0A
-  
     
@@ -1444,18 +1444,16 @@
         
-  
 .readVal
@@ -1459,49 +1459,59 @@
 lue(
-announcement.toString(), BgpAdvertis
+externalBgpAnnouncementsFileContents, Announc
 ement
+s
 .cla
@@ -1527,54 +1527,66 @@
     
-  advertSet.add(bgpAdvertis
+return new LinkedHashSet%3C%3E(parsedAdverts._announc
 ement
+s
 );%0A
-        %7D%0A%0A
     
@@ -1600,24 +1600,8 @@
 ch (
-JSONException %7C 
 IOEx
@@ -1730,25 +1730,37 @@
  return 
-advertSet
+new LinkedHashSet%3C%3E()
 ;%0A  %7D%0A%7D%0A

use Jackson instead of Jettison (#8284)