ONOS-6555 Default pipeconf implementation and builder

Change-Id: I80ac4f6e939d30a943653a1d63d5cff07b368620
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DefaultPipeconf.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DefaultPipeconf.java
deleted file mode 100644
index ac2dbd7..0000000
--- a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DefaultPipeconf.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * Copyright 2017-present Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.onosproject.drivers.bmv2;
-
-import com.eclipsesource.json.Json;
-import com.google.common.collect.ImmutableMap;
-import org.onosproject.bmv2.model.Bmv2PipelineModelParser;
-import org.onosproject.net.driver.Behaviour;
-import org.onosproject.net.pi.model.PiPipeconf;
-import org.onosproject.net.pi.model.PiPipeconfId;
-import org.onosproject.net.pi.model.PiPipelineInterpreter;
-import org.onosproject.net.pi.model.PiPipelineModel;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.util.Collection;
-import java.util.Map;
-import java.util.Optional;
-
-/**
- * Implementation of the default pipeconf for a BMv2 device.
- */
-public final class Bmv2DefaultPipeconf implements PiPipeconf {
-
-    private static final String PIPECONF_ID = "bmv2-default-pipeconf";
-    private static final String JSON_PATH = "/default.json";
-    private static final String P4INFO_PATH = "/default.p4info";
-
-    private final PiPipeconfId id;
-    private final PiPipelineModel pipelineModel;
-    private final InputStream jsonConfigStream = this.getClass().getResourceAsStream(JSON_PATH);
-    private final InputStream p4InfoStream = this.getClass().getResourceAsStream(P4INFO_PATH);
-    private final Map<Class<? extends Behaviour>, Class<? extends Behaviour>> behaviours;
-
-    Bmv2DefaultPipeconf() {
-        this.id = new PiPipeconfId(PIPECONF_ID);
-        try {
-            this.pipelineModel = Bmv2PipelineModelParser.parse(
-                    Json.parse(new BufferedReader(new InputStreamReader(jsonConfigStream))).asObject());
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
-
-        this.behaviours = ImmutableMap.of(
-                PiPipelineInterpreter.class, Bmv2DefaultInterpreter.class
-                // TODO: reuse default single table pipeliner.
-        );
-    }
-
-    @Override
-    public PiPipeconfId id() {
-        return this.id;
-    }
-
-    @Override
-    public PiPipelineModel pipelineModel() {
-        return pipelineModel;
-    }
-
-    @Override
-    public Collection<Class<? extends Behaviour>> behaviours() {
-        return behaviours.keySet();
-    }
-
-    @Override
-    public Optional<Class<? extends Behaviour>> implementation(Class<? extends Behaviour> behaviour) {
-        return Optional.ofNullable(behaviours.get(behaviour));
-    }
-
-    @Override
-    public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
-        return behaviours.containsKey(behaviourClass);
-    }
-
-    @Override
-    public Optional<InputStream> extension(ExtensionType type) {
-
-        switch (type) {
-            case BMV2_JSON:
-                return Optional.of(jsonConfigStream);
-            case P4_INFO_TEXT:
-                return Optional.of(p4InfoStream);
-            default:
-                return Optional.empty();
-        }
-    }
-}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DefaultPipeconfFactory.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DefaultPipeconfFactory.java
new file mode 100644
index 0000000..4903c3e
--- /dev/null
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2DefaultPipeconfFactory.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.drivers.bmv2;
+
+import org.onosproject.bmv2.model.Bmv2PipelineModelParser;
+import org.onosproject.net.pi.model.DefaultPiPipeconf;
+import org.onosproject.net.pi.model.PiPipeconf;
+import org.onosproject.net.pi.model.PiPipeconfId;
+import org.onosproject.net.pi.model.PiPipelineInterpreter;
+
+import java.io.InputStream;
+
+import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
+import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
+
+/**
+ * Factory of pipeconf implementation for the default.p4 program on BMv2.
+ */
+final class Bmv2DefaultPipeconfFactory {
+
+    private static final String PIPECONF_ID = "bmv2-default-pipeconf";
+    private static final String JSON_PATH = "/default.json";
+    private static final String P4INFO_PATH = "/default.p4info";
+
+    private Bmv2DefaultPipeconfFactory() {
+        // Hides constructor.
+    }
+
+    static PiPipeconf get() {
+
+        final InputStream jsonConfigStream = Bmv2DefaultPipeconfFactory.class.getResourceAsStream(JSON_PATH);
+        final InputStream p4InfoStream = Bmv2DefaultPipeconfFactory.class.getResourceAsStream(P4INFO_PATH);
+
+        return DefaultPiPipeconf.builder()
+                .withId(new PiPipeconfId(PIPECONF_ID))
+                .withPipelineModel(Bmv2PipelineModelParser.parse(jsonConfigStream))
+                // TODO: reuse default single table pipeliner.
+                .addBehaviour(PiPipelineInterpreter.class, Bmv2DefaultInterpreter.class)
+                .addExtension(P4_INFO_TEXT, p4InfoStream)
+                .addExtension(BMV2_JSON, jsonConfigStream)
+                .build();
+    }
+}
diff --git a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PipelineProgrammable.java b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PipelineProgrammable.java
index 735c142..88fc0ab 100644
--- a/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PipelineProgrammable.java
+++ b/drivers/bmv2/src/main/java/org/onosproject/drivers/bmv2/Bmv2PipelineProgrammable.java
@@ -39,7 +39,7 @@
  */
 public class Bmv2PipelineProgrammable extends AbstractHandlerBehaviour implements PiPipelineProgrammable {
 
-    private static final PiPipeconf DEFAULT_PIPECONF = new Bmv2DefaultPipeconf();
+    private static final PiPipeconf DEFAULT_PIPECONF = Bmv2DefaultPipeconfFactory.get();
 
     private final Logger log = getLogger(getClass());