/* (non-Javadoc)
  * @see org.opennms.features.pluginmgr.PluginManager#getInstalledLicenceList(java.lang.String)
  */
 @Override
 public synchronized LicenceList getInstalledLicenceList(String karafInstance) {
   if (karafInstance == null) throw new RuntimeException("karafInstance cannot be null");
   if (!pluginModelJaxb.getKarafDataMap().containsKey(karafInstance)) {
     return null;
   }
   KarafEntryJaxb karafEntry = pluginModelJaxb.getKarafDataMap().get(karafInstance);
   if (karafEntry == null) return null;
   return karafEntry.getInstalledLicenceList();
 }
  /* (non-Javadoc)
   * @see org.opennms.features.pluginmgr.PluginManager#getInstalledLicenceList(java.lang.String)
   */
  @Override
  public synchronized void updateInstalledLicenceList(
      LicenceList licenceList, String karafInstance) {
    if (karafInstance == null) throw new RuntimeException("karafInstance cannot be null");
    if (licenceList == null) throw new RuntimeException("licenceList cannot be null");

    SortedMap<String, KarafManifestEntryJaxb> karafInstances = getKarafInstances();
    if (!karafInstances.containsKey(karafInstance))
      throw new RuntimeException("system does not know karafInstance=" + karafInstance);

    if (!pluginModelJaxb.getKarafDataMap().containsKey(karafInstance)) {
      throw new RuntimeException("no karaf entry entry exists for Karaf Instance=" + karafInstance);
    }
    KarafEntryJaxb karafEntry = pluginModelJaxb.getKarafDataMap().get(karafInstance);
    karafEntry.getInstalledLicenceList().getLicenceList().clear();
    karafEntry.getInstalledLicenceList().getLicenceList().addAll(licenceList.getLicenceList());
    persist();
  }
  /* (non-Javadoc)
   * @see org.opennms.features.pluginmgr.PluginManager#refreshKarafEntry(java.lang.String)
   */
  @Override
  public synchronized KarafEntryJaxb refreshKarafEntry(String karafInstance) {
    if (karafInstance == null) throw new RuntimeException("karafInstance cannot be null");

    SortedMap<String, KarafManifestEntryJaxb> karafInstances = getKarafInstances();
    if (!karafInstances.containsKey(karafInstance))
      throw new RuntimeException("system does not know karafInstance=" + karafInstance);

    KarafManifestEntryJaxb karafManifest = karafInstances.get(karafInstance);
    String karafInstanceUrl = karafManifest.getKarafInstanceUrl();

    // only update remote if accessible
    // else just return current value
    if (karafManifest.getRemoteIsAccessible() == null || !karafManifest.getRemoteIsAccessible()) {
      if (!pluginModelJaxb.getKarafDataMap().containsKey(karafInstance)) {
        // if there is no entry, create an empty one
        KarafEntryJaxb karafEntryJaxb = new KarafEntryJaxb();
        pluginModelJaxb.getKarafDataMap().put(karafInstance, karafEntryJaxb);
      }
      return pluginModelJaxb.getKarafDataMap().get(karafInstance);

    } else {

      KarafEntryJaxb karafEntryJaxb = new KarafEntryJaxb();

      try {

        // getting karaf installed licences and system id
        LicenceManagerClientRestJerseyImpl licenceManagerClient =
            new LicenceManagerClientRestJerseyImpl();
        licenceManagerClient.setBaseUrl(karafInstanceUrl);
        licenceManagerClient.setUserName(karafManifest.getKarafInstanceUserName());
        licenceManagerClient.setPassword(karafManifest.getKarafInstancePassword());

        licenceManagerClient.setBasePath(LICENCE_MGR_BASE_PATH);
        LicenceList installedLicenceList;
        try {
          installedLicenceList = licenceManagerClient.getLicenceMap();
          karafEntryJaxb.setInstalledLicenceList(installedLicenceList);
          String systemId = licenceManagerClient.getSystemId();
          karafEntryJaxb.setSystemId(systemId);
          // if the remote has a system id and manifest does not, set manifest to remote value
          if (karafManifest.getManifestSystemId() == null)
            karafManifest.setManifestSystemId(systemId);
        } catch (Exception e) {
          throw new RuntimeException(
              "problem refreshing installed licences for "
                  + "karafInstance="
                  + karafInstance
                  + " karafInstanceUrl="
                  + karafInstanceUrl
                  + ": ",
              e);
        }

        // getting installed plugins
        ProductRegisterClientRestJerseyImpl productRegisterClient =
            new ProductRegisterClientRestJerseyImpl();
        productRegisterClient.setBaseUrl(karafInstanceUrl);
        productRegisterClient.setUserName(karafManifest.getKarafInstanceUserName());
        productRegisterClient.setPassword(karafManifest.getKarafInstancePassword());

        // getting karaf installed plugins
        productRegisterClient.setBasePath(PRODUCT_REG_BASE_PATH);

        ProductSpecList installedPlugins;
        try {
          installedPlugins = productRegisterClient.getList();

          List<LicenceEntry> licenceList =
              karafEntryJaxb.getInstalledLicenceList().getLicenceList();

          // tests if plugins need a licence and if the licence is authenticated
          for (ProductMetadata installedPlugin : installedPlugins.getProductSpecList()) {
            if (installedPlugin.getLicenceKeyRequired() != null
                && installedPlugin.getLicenceKeyRequired() == true) {
              // if plugin needs a licence then check if licence is already verified
              // ignores exception if no licence is installed
              Boolean licenceKeyAuthenticated = false;
              for (LicenceEntry licenceEntry : licenceList) {
                if (licenceEntry.getProductId().equals(installedPlugin.getProductId())) {
                  // only check where licence is installed
                  // note will throw exception if licence is not installed when checked
                  licenceKeyAuthenticated =
                      licenceManagerClient.isAuthenticated(installedPlugin.getProductId());
                }
              }
              installedPlugin.setLicenceKeyAuthenticated(licenceKeyAuthenticated);
            }
          }

          karafEntryJaxb.setInstalledPlugins(installedPlugins);
        } catch (Exception e) {
          throw new RuntimeException(
              "problem refreshing installed plugins for "
                  + "karafInstance="
                  + karafInstance
                  + " karafInstanceUrl="
                  + karafInstanceUrl
                  + ": ",
              e);
        }

        karafEntryJaxb.setKarafInstanceLastUpdated(new Date());

        pluginModelJaxb.getKarafDataMap().put(karafInstance, karafEntryJaxb);

        persist();

      } catch (Exception e) {
        throw new RuntimeException(
            "problem updating data from karaf Instance '" + karafInstance + "'", e);
      }

      return karafEntryJaxb;
    }
  }