ONOS-5629: Enable encapsulation in SDN-IP

Change-Id: I4c3dbe877fd009302938b228fc9af40225d75329
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/SdnIpCommand.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/SdnIpCommand.java
new file mode 100644
index 0000000..47b50d5
--- /dev/null
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/SdnIpCommand.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2015-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.sdnip.cli;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.EncapsulationType;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.sdnip.SdnIp;
+import org.onosproject.sdnip.config.SdnIpConfig;
+
+/**
+ * CLI to interact with the SDN-IP application.
+ */
+@Command(scope = "onos", name = "sdnip",
+        description = "Manages the SDN-IP application")
+public class SdnIpCommand extends AbstractShellCommand {
+
+    // Color codes and style
+    private static final String BOLD = "\u001B[1m";
+    private static final String COLOR_ERROR = "\u001B[31m";
+    private static final String RESET = "\u001B[0m";
+
+    // Messages and string formatter
+    private static final String ENCAP_NOT_FOUND =
+            COLOR_ERROR + "Encapsulation type " + BOLD + "%s" + RESET +
+                    COLOR_ERROR + " not found" + RESET;
+
+    private static final String SDNIP_COMMAND_NOT_FOUND =
+            COLOR_ERROR + "SDN-IP command " + BOLD + "%s" + RESET + COLOR_ERROR +
+                    " not found" + RESET;
+
+    @Argument(index = 0, name = "command", description = "Command name" +
+            " {set-encap}",
+            required = true, multiValued = false)
+    String command = null;
+
+    @Argument(index = 1, name = "encapType", description = "The encapsulation" +
+            " type {NONE | VLAN | MPLS}",
+            required = true, multiValued = false)
+    String encapType = null;
+
+    @Override
+    protected void execute() {
+        if (command != null) {
+            switch (command) {
+                case "set-encap":
+                    setEncap(encapType);
+                    break;
+                default:
+                    print(SDNIP_COMMAND_NOT_FOUND, command);
+            }
+        }
+    }
+
+    /**
+     * Sets the encapsulation type for SDN-IP.
+     *
+     * @param encap the encapsulation type
+     */
+    private void setEncap(String encap) {
+        EncapsulationType encapType = EncapsulationType.enumFromString(encap);
+        if (encapType.equals(EncapsulationType.NONE) &&
+                !encapType.toString().equals(encap)) {
+            print(ENCAP_NOT_FOUND, encap);
+            return;
+        }
+
+        NetworkConfigService configService = get(NetworkConfigService.class);
+        CoreService coreService = get(CoreService.class);
+        ApplicationId appId = coreService.getAppId(SdnIp.SDN_IP_APP);
+
+        SdnIpConfig config = configService.addConfig(appId, SdnIpConfig.class);
+
+        config.setEncap(encapType);
+        config.apply();
+
+        //configService.applyConfig(appId, SdnIpConfig.class, config.node());
+    }
+}
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/completer/SdnIpCommandCompleter.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/completer/SdnIpCommandCompleter.java
new file mode 100644
index 0000000..593522f
--- /dev/null
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/completer/SdnIpCommandCompleter.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2015-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.sdnip.cli.completer;
+
+import com.google.common.collect.Lists;
+import org.onosproject.cli.AbstractChoicesCompleter;
+
+import java.util.List;
+
+/**
+ * SDN-IP command completer.
+ */
+public class SdnIpCommandCompleter extends AbstractChoicesCompleter {
+
+    @Override
+    public List<String> choices() {
+        return Lists.newArrayList("set-encap");
+    }
+}
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/completer/SdnIpEncapCompleter.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/completer/SdnIpEncapCompleter.java
new file mode 100644
index 0000000..bd3a49c
--- /dev/null
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/completer/SdnIpEncapCompleter.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2015-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.sdnip.cli.completer;
+
+import org.onosproject.cli.AbstractChoicesCompleter;
+import org.onosproject.net.EncapsulationType;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * SDN-IP encapsulation type completer.
+ */
+public class SdnIpEncapCompleter extends AbstractChoicesCompleter {
+
+    @Override
+    public List<String> choices() {
+        return Arrays.stream(EncapsulationType.values())
+                .map(Enum::toString)
+                .collect(Collectors.toList());
+    }
+}
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/completer/package-info.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/completer/package-info.java
new file mode 100644
index 0000000..382b96c
--- /dev/null
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/completer/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-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.
+ */
+
+/**
+ * Completers implementation for the SDN-IP CLI.
+ */
+package org.onosproject.sdnip.cli.completer;
\ No newline at end of file
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/package-info.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/package-info.java
new file mode 100644
index 0000000..621bcc8
--- /dev/null
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-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.
+ */
+
+/**
+ * CLI implementation for SDN-IP services.
+ */
+package org.onosproject.sdnip.cli;
\ No newline at end of file