Make use of Optional more idiomatic

Change-Id: I42b3261169e7cb8408f46c5831f72115f77fd779
diff --git a/apps/olt/app/src/main/java/org/onosproject/olt/impl/Olt.java b/apps/olt/app/src/main/java/org/onosproject/olt/impl/Olt.java
index 16e8a9e..bfc05c8 100644
--- a/apps/olt/app/src/main/java/org/onosproject/olt/impl/Olt.java
+++ b/apps/olt/app/src/main/java/org/onosproject/olt/impl/Olt.java
@@ -256,7 +256,7 @@
         CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
 
         TrafficSelector upstream = DefaultTrafficSelector.builder()
-                .matchVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
+                .matchVlanId(defaultVlan.orElse(DEFAULT_VLAN))
                 .matchInPort(subscriberPort)
                 .build();
 
@@ -276,7 +276,7 @@
 
         TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
                 .popVlan()
-                .setVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
+                .setVlanId(defaultVlan.orElse(DEFAULT_VLAN))
                 .setOutput(subscriberPort)
                 .build();
 
diff --git a/apps/pim/src/main/java/org/onosproject/pim/impl/PIMInterfaceManager.java b/apps/pim/src/main/java/org/onosproject/pim/impl/PIMInterfaceManager.java
index 248a233..9f56f10 100644
--- a/apps/pim/src/main/java/org/onosproject/pim/impl/PIMInterfaceManager.java
+++ b/apps/pim/src/main/java/org/onosproject/pim/impl/PIMInterfaceManager.java
@@ -193,21 +193,11 @@
                 .withPacketService(packetService)
                 .withInterface(intf);
 
-        if (config.getHelloInterval().isPresent()) {
-            builder.withHelloInterval(config.getHelloInterval().get());
-        }
-        if (config.getHoldTime().isPresent()) {
-            builder.withHoldTime(config.getHoldTime().get());
-        }
-        if (config.getPriority().isPresent()) {
-            builder.withPriority(config.getPriority().get());
-        }
-        if (config.getPropagationDelay().isPresent()) {
-            builder.withPropagationDelay(config.getPropagationDelay().get());
-        }
-        if (config.getOverrideInterval().isPresent()) {
-            builder.withOverrideInterval(config.getOverrideInterval().get());
-        }
+        config.getHelloInterval().ifPresent(builder::withHelloInterval);
+        config.getHoldTime().ifPresent(builder::withHoldTime);
+        config.getPriority().ifPresent(builder::withPriority);
+        config.getPropagationDelay().ifPresent(builder::withPropagationDelay);
+        config.getOverrideInterval().ifPresent(builder::withOverrideInterval);
 
         return builder.build();
     }
diff --git a/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java b/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java
index 209b0fb..dc1cc81 100644
--- a/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java
+++ b/apps/routing-api/src/main/java/org/onosproject/routing/config/BgpConfig.java
@@ -85,7 +85,7 @@
      */
     public BgpSpeakerConfig getSpeakerWithName(String name) {
         for (BgpConfig.BgpSpeakerConfig speaker : bgpSpeakers()) {
-            if (speaker.name().isPresent() && speaker.name().get().equals(name)) {
+            if (speaker.name().filter(name::equals).isPresent()) {
                 return speaker;
             }
         }
diff --git a/cli/src/main/java/org/onosproject/cli/app/ApplicationsListCommand.java b/cli/src/main/java/org/onosproject/cli/app/ApplicationsListCommand.java
index 9efa612..2f82f18 100644
--- a/cli/src/main/java/org/onosproject/cli/app/ApplicationsListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/app/ApplicationsListCommand.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.cli.app;
 
+import java.net.URI;
 import java.util.Collections;
 import java.util.List;
 
@@ -75,7 +76,7 @@
                         print(FMT, isActive ? "*" : " ",
                               app.id().id(), app.id().name(), app.version(), app.origin(),
                               app.category(), app.description(), app.features(),
-                              app.featuresRepo().isPresent() ? app.featuresRepo().get().toString() : "",
+                              app.featuresRepo().map(URI::toString).orElse(""),
                               app.requiredApps(), app.permissions(), app.url());
                     }
                 }
diff --git a/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java b/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java
index 3bddee4..07d2440 100644
--- a/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java
+++ b/core/api/src/main/java/org/onosproject/net/packet/DefaultPacketRequest.java
@@ -105,7 +105,7 @@
                 .add("priority", priority)
                 .add("appId", appId)
                 .add("nodeId", nodeId)
-                .add("applies to", deviceId.isPresent() ? deviceId.get() : "all")
+                .add("applies to", deviceId.map(DeviceId::toString).orElse("all"))
                 .toString();
     }
-}
\ No newline at end of file
+}
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/ApplicationCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/ApplicationCodec.java
index 4886e60..18ecedb 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/ApplicationCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/ApplicationCodec.java
@@ -23,6 +23,8 @@
 import org.onosproject.codec.JsonCodec;
 import org.onosproject.core.Application;
 
+import java.net.URI;
+
 import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
@@ -52,8 +54,7 @@
                 .put("readme", StringEscapeUtils.escapeJson(app.readme()))
                 .put("origin", app.origin())
                 .put("url", app.url())
-                .put("featuresRepo", app.featuresRepo().isPresent() ?
-                        app.featuresRepo().get().toString() : "")
+                .put("featuresRepo", app.featuresRepo().map(URI::toString).orElse(""))
                 .put("state", service.getState(app.id()).toString());
 
         result.set("features", features);
diff --git a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
index 53a9a68..c16f558 100644
--- a/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
+++ b/core/net/src/main/java/org/onosproject/net/driver/impl/DriverManager.java
@@ -148,7 +148,7 @@
                 .filter(d -> matches(d, mfr, hw, sw)).findFirst();
 
         // If no matching driver is found, return default.
-        return optional.isPresent() ? optional.get() : drivers.get(DEFAULT);
+        return optional.orElse(drivers.get(DEFAULT));
     }
 
     // Matches the given driver using ERE matching against the given criteria.
diff --git a/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java b/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java
index cd7335d..7340fc5 100644
--- a/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java
+++ b/core/net/src/main/java/org/onosproject/net/edgeservice/impl/EdgeManager.java
@@ -129,18 +129,16 @@
 
     @Override
     public void emitPacket(ByteBuffer data, Optional<TrafficTreatment> treatment) {
-        TrafficTreatment.Builder builder = treatment.isPresent() ?
-                DefaultTrafficTreatment.builder(treatment.get()) :
-                DefaultTrafficTreatment.builder();
+        TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
+                .orElse(DefaultTrafficTreatment.builder());
         getEdgePoints().forEach(p -> packetService.emit(packet(builder, p, data)));
     }
 
     @Override
     public void emitPacket(DeviceId deviceId, ByteBuffer data,
                            Optional<TrafficTreatment> treatment) {
-        TrafficTreatment.Builder builder = treatment.isPresent() ?
-                DefaultTrafficTreatment.builder(treatment.get()) :
-                DefaultTrafficTreatment.builder();
+        TrafficTreatment.Builder builder = treatment.map(DefaultTrafficTreatment::builder)
+                .orElse(DefaultTrafficTreatment.builder());
         getEdgePoints(deviceId).forEach(p -> packetService.emit(packet(builder, p, data)));
     }
 
diff --git a/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Compiling.java b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Compiling.java
index 6cef3a4..1f5eb38 100644
--- a/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Compiling.java
+++ b/core/net/src/main/java/org/onosproject/net/intent/impl/phase/Compiling.java
@@ -56,11 +56,11 @@
         try {
             List<Intent> compiled = processor.compile(data.intent(),
                     //TODO consider passing an optional here in the future
-                    stored.isPresent() ? stored.get().installables() : null);
+                    stored.map(IntentData::installables).orElse(null));
             return Optional.of(new Installing(processor, new IntentData(data, compiled), stored));
         } catch (IntentException e) {
             log.debug("Unable to compile intent {} due to: {}", data.intent(), e);
-            if (stored.isPresent() && !stored.get().installables().isEmpty()) {
+            if (stored.filter(x -> x.installables().isEmpty()).isPresent()) {
                 // removing orphaned flows and deallocating resources
                 return Optional.of(new Withdrawing(processor, new IntentData(data, stored.get().installables())));
             } else {
diff --git a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/StoragePartition.java b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/StoragePartition.java
index acf88fa..d8fb580 100644
--- a/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/StoragePartition.java
+++ b/core/store/primitives/src/main/java/org/onosproject/store/primitives/impl/StoragePartition.java
@@ -121,11 +121,8 @@
     }
 
     private CompletableFuture<Void> closeServer() {
-        if (server.isPresent()) {
-            return server.get().close();
-        } else {
-            return CompletableFuture.completedFuture(null);
-        }
+        return server.map(StoragePartitionServer::close)
+                .orElse(CompletableFuture.completedFuture(null));
     }
 
     private CompletableFuture<Void> closeClient() {
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/CentecV350Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/CentecV350Pipeline.java
index 2c6e24c..9203a00 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/CentecV350Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/CentecV350Pipeline.java
@@ -482,15 +482,11 @@
     }
 
     private void pass(Objective obj) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onSuccess(obj);
-        }
+        obj.context().ifPresent(context -> context.onSuccess(obj));
     }
 
     private void fail(Objective obj, ObjectiveError error) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onError(obj, error);
-        }
+        obj.context().ifPresent(context -> context.onError(obj, error));
     }
 
     private void initializePipeline() {
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java
index 7e22501..baa08b8 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/DefaultSingleTablePipeline.java
@@ -156,16 +156,13 @@
         flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() {
             @Override
             public void onSuccess(FlowRuleOperations ops) {
-                if (objective.context().isPresent()) {
-                    objective.context().get().onSuccess(objective);
-                }
+                objective.context().ifPresent(context -> context.onSuccess(objective));
             }
 
             @Override
             public void onError(FlowRuleOperations ops) {
-                if (objective.context().isPresent()) {
-                    objective.context().get().onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED);
-                }
+                objective.context()
+                        .ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED));
             }
         }));
     }
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OFDPA2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OFDPA2Pipeline.java
index f949bdf..2f58bfb 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OFDPA2Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OFDPA2Pipeline.java
@@ -998,14 +998,10 @@
     }
 
     protected static void pass(Objective obj) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onSuccess(obj);
-        }
+        obj.context().ifPresent(context -> context.onSuccess(obj));
     }
 
     protected static void fail(Objective obj, ObjectiveError error) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onError(obj, error);
-        }
+        obj.context().ifPresent(context -> context.onError(obj, error));
     }
 }
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java
index 46a360b..7e9dfec 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OVSCorsaPipeline.java
@@ -477,15 +477,11 @@
     }
 
     protected void pass(Objective obj) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onSuccess(obj);
-        }
+        obj.context().ifPresent(context -> context.onSuccess(obj));
     }
 
     protected void fail(Objective obj, ObjectiveError error) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onError(obj, error);
-        }
+        obj.context().ifPresent(context -> context.onError(obj, error));
     }
 
     protected void initializePipeline() {
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OltPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OltPipeline.java
index 211ed7d..f59c527 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OltPipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OltPipeline.java
@@ -623,15 +623,11 @@
 
 
     private void fail(Objective obj, ObjectiveError error) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onError(obj, error);
-        }
+        obj.context().ifPresent(context -> context.onError(obj, error));
     }
 
     private void pass(Objective obj) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onSuccess(obj);
-        }
+        obj.context().ifPresent(context -> context.onSuccess(obj));
     }
 
 
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenVSwitchPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenVSwitchPipeline.java
index 9e24a17..1a7f576 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenVSwitchPipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenVSwitchPipeline.java
@@ -367,14 +367,10 @@
     }
 
     private void fail(Objective obj, ObjectiveError error) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onError(obj, error);
-        }
+        obj.context().ifPresent(context -> context.onError(obj, error));
     }
 
     private void pass(Objective obj) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onSuccess(obj);
-        }
+        obj.context().ifPresent(context -> context.onSuccess(obj));
     }
 }
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenstackPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenstackPipeline.java
index 7b1a156..a8b3861 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenstackPipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/OpenstackPipeline.java
@@ -273,15 +273,11 @@
 
 
     private void pass(Objective obj) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onSuccess(obj);
-        }
+        obj.context().ifPresent(context -> context.onSuccess(obj));
     }
 
     private void fail(Objective obj, ObjectiveError error) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onError(obj, error);
-        }
+        obj.context().ifPresent(context -> context.onError(obj, error));
     }
 }
 
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/PicaPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/PicaPipeline.java
index 6cb5b82..14b729c 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/PicaPipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/PicaPipeline.java
@@ -450,15 +450,11 @@
     }
 
     private void pass(Objective obj) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onSuccess(obj);
-        }
+        obj.context().ifPresent(context -> context.onSuccess(obj));
     }
 
     private void fail(Objective obj, ObjectiveError error) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onError(obj, error);
-        }
+        obj.context().ifPresent(context -> context.onError(obj, error));
     }
 
     private void initializePipeline() {
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/SoftRouterPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/SoftRouterPipeline.java
index be1c00d..55e8bbf 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/SoftRouterPipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/SoftRouterPipeline.java
@@ -179,15 +179,11 @@
     }
 
     private void pass(Objective obj) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onSuccess(obj);
-        }
+        obj.context().ifPresent(context -> context.onSuccess(obj));
     }
 
     private void fail(Objective obj, ObjectiveError error) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onError(obj, error);
-        }
+        obj.context().ifPresent(context -> context.onError(obj, error));
     }
 
     private void initializePipeline() {
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/SpringOpenTTP.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/SpringOpenTTP.java
index 1277c75..5c65e11 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/SpringOpenTTP.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/SpringOpenTTP.java
@@ -998,15 +998,11 @@
     }
 
     private void pass(Objective obj) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onSuccess(obj);
-        }
+        obj.context().ifPresent(context -> context.onSuccess(obj));
     }
 
     protected void fail(Objective obj, ObjectiveError error) {
-        if (obj.context().isPresent()) {
-            obj.context().get().onError(obj, error);
-        }
+        obj.context().ifPresent(context -> context.onError(obj, error));
     }
 
     private class InnerGroupListener implements GroupListener {
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java
index 9da2177..d297080 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/intf/impl/InterfaceManager.java
@@ -109,7 +109,7 @@
                 .filter(i -> i.name().equals(name))
                 .findAny();
 
-        return intf.isPresent() ? intf.get() : null;
+        return intf.orElse(null);
     }
 
     @Override
@@ -142,11 +142,7 @@
                         .anyMatch(intfIp -> intfIp.subnetAddress().contains(ip)))
                 .findFirst();
 
-        if (match.isPresent()) {
-            return match.get();
-        }
-
-        return null;
+        return match.orElse(null);
     }
 
     @Override
diff --git a/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java b/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java
index 4618cd0..6d9ff13 100644
--- a/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java
+++ b/tools/package/maven-plugin/src/main/java/org/onosproject/maven/OnosSwaggerMojo.java
@@ -211,7 +211,7 @@
     private JavaAnnotation getPathAnnotation(JavaClass javaClass) {
         Optional<JavaAnnotation> optional = javaClass.getAnnotations()
                 .stream().filter(a -> a.getType().getName().equals(PATH)).findAny();
-        return optional.isPresent() ? optional.get() : null;
+        return optional.orElse(null);
     }
 
     // Checks whether a class's methods are REST methods and then places all the
@@ -375,7 +375,7 @@
             Optional<JavaAnnotation> optional = javaParameter.getAnnotations().stream().filter(
                     annotation -> annotation.getType().getName().equals(PATH_PARAM) ||
                             annotation.getType().getName().equals(QUERY_PARAM)).findAny();
-            JavaAnnotation pathType = optional.isPresent() ? optional.get() : null;
+            JavaAnnotation pathType = optional.orElse(null);
 
             String annotationName = javaParameter.getName();