useStaleLinkAge for VanishStaleLink

Also include gerrit #22247

Change-Id: Ifd8079172a096bef0edf89daca97395c2d35a5ce
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/LinksWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/LinksWebResource.java
index 3c74d94..cb4f0a4 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/LinksWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/LinksWebResource.java
@@ -23,7 +23,9 @@
 import org.onosproject.rest.AbstractWebResource;
 
 import javax.ws.rs.GET;
+import javax.ws.rs.POST;
 import javax.ws.rs.Path;
+import javax.ws.rs.Consumes;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
@@ -31,6 +33,13 @@
 
 import static org.onosproject.net.DeviceId.deviceId;
 import static org.onosproject.net.PortNumber.portNumber;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.cfg.ConfigProperty;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import java.io.IOException;
+import java.io.InputStream;
+
 
 /**
  * Manage inventory of infrastructure links.
@@ -97,4 +106,56 @@
         }
     }
 
+    /**
+     * Get useStaleLinkAge active status.
+     * Returns current status of the VanishedStaleLink.
+     *
+     * @onos.rsModel VanishedLink
+     * @return 200 ok with the VanishedStaleLink status.
+     */
+    @GET
+    @Path("{usestalelinkage}")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response getVanishStaleLink() {
+        ObjectNode root = mapper().createObjectNode();
+        ComponentConfigService useStaleLink = get(ComponentConfigService.class);
+
+        for (ConfigProperty prop : useStaleLink.getProperties("org.onosproject.provider.lldp.impl.LldpLinkProvider")) {
+            if (prop.name().equals("useStaleLinkAge")) {
+                root.put("active", Boolean.valueOf(prop.value()));
+                break;
+            }
+        }
+        return ok(root).build();
+    }
+
+    /**
+     * Set useStaleLinkAge status.
+     *
+     * @onos.rsModel VanishedLink
+     * @param stream input JSON
+     * @return 200 ok.
+     * BAD_REQUEST if the JSON is invalid
+     */
+    @POST
+    @Path("{usestalelinkage}")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response setVanishStaleLink(InputStream stream) {
+        try {
+            // Parse the input stream
+            ObjectNode root = (ObjectNode) mapper().readTree(stream);
+            if (root.has("active")) {
+                ComponentConfigService useStaleLink = get(ComponentConfigService.class);
+                useStaleLink.setProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
+                   "useStaleLinkAge", String.valueOf(root.get("active")));
+            }
+        } catch (IOException ex) {
+            throw new IllegalArgumentException(ex);
+        }
+        return Response
+                .ok()
+                .build();
+    }
+
 }