[ONOS-7621] Support injecting keystone auth info through network-cfg

Change-Id: I2439e257f0f576c46b68322b8c8f1c87fa2cc9ae
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackAuthJsonMatcher.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackAuthJsonMatcher.java
new file mode 100644
index 0000000..055fbe6
--- /dev/null
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackAuthJsonMatcher.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.openstacknode.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.openstacknode.api.OpenstackAuth;
+
+/**
+ * Hamcrest matcher for opensatck auth.
+ */
+public final class OpenstackAuthJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+    private final OpenstackAuth auth;
+
+    private static final String VERSION = "version";
+    private static final String PORT = "port";
+    private static final String PROTOCOL = "protocol";
+    private static final String USERNAME = "username";
+    private static final String PASSWORD = "password";
+    private static final String PROJECT = "project";
+    private static final String PERSPECTIVE = "perspective";
+
+    private OpenstackAuthJsonMatcher(OpenstackAuth auth) {
+        this.auth = auth;
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+        // check version
+        String jsonVersion = jsonNode.get(VERSION).asText();
+        String version = auth.version();
+        if (!jsonVersion.equals(version)) {
+            description.appendText("version was " + jsonVersion);
+            return false;
+        }
+
+        // check port
+        Integer jsonPort = jsonNode.get(PORT).asInt();
+        Integer port = auth.port();
+        if (!jsonPort.equals(port)) {
+            description.appendText("port was " + jsonPort);
+            return false;
+        }
+
+        // check protocol
+        String jsonProtocol = jsonNode.get(PROTOCOL).asText();
+        String protocol = auth.protocol().name();
+        if (!jsonProtocol.equals(protocol)) {
+            description.appendText("protocol was " + jsonProtocol);
+            return false;
+        }
+
+        // check username
+        String jsonUsername = jsonNode.get(USERNAME).asText();
+        String username = auth.username();
+        if (!jsonUsername.equals(username)) {
+            description.appendText("username was " + jsonUsername);
+            return false;
+        }
+
+        // check password
+        String jsonPassword = jsonNode.get(PASSWORD).asText();
+        String password = auth.password();
+        if (!jsonPassword.equals(password)) {
+            description.appendText("password was " + jsonPassword);
+            return false;
+        }
+
+        // check project
+        String jsonProject = jsonNode.get(PROJECT).asText();
+        String project = auth.project();
+        if (!jsonProject.equals(project)) {
+            description.appendText("project was " + jsonProject);
+            return false;
+        }
+
+        // check perspective
+        String jsonPerspective = jsonNode.get(PERSPECTIVE).asText();
+        String perspective = auth.perspective().name();
+        if (!jsonPerspective.equals(perspective)) {
+            description.appendText("perspective was " + jsonPerspective);
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(auth.toString());
+    }
+
+    /**
+     * Factory to allocate an openstack auth matcher.
+     *
+     * @param auth openstack auth object we are looking for
+     * @return matcher
+     */
+    public static OpenstackAuthJsonMatcher matchOpenstackAuth(OpenstackAuth auth) {
+        return new OpenstackAuthJsonMatcher(auth);
+    }
+}
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeCodecTest.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeCodecTest.java
index cea17c4..5019234 100644
--- a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeCodecTest.java
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeCodecTest.java
@@ -29,8 +29,10 @@
 import org.onosproject.core.CoreService;
 import org.onosproject.net.DeviceId;
 import org.onosproject.openstacknode.api.NodeState;
+import org.onosproject.openstacknode.api.OpenstackAuth;
 import org.onosproject.openstacknode.api.OpenstackNode;
 import org.onosproject.openstacknode.api.OpenstackPhyInterface;
+import org.onosproject.openstacknode.impl.DefaultOpenstackAuth;
 import org.onosproject.openstacknode.impl.DefaultOpenstackNode;
 import org.onosproject.openstacknode.impl.DefaultOpenstackPhyInterface;
 
@@ -55,6 +57,7 @@
     MockCodecContext context;
     JsonCodec<OpenstackNode> openstackNodeCodec;
     JsonCodec<OpenstackPhyInterface> openstackPhyIntfJsonCodec;
+    JsonCodec<OpenstackAuth> openstackAuthJsonCodec;
     final CoreService mockCoreService = createMock(CoreService.class);
     private static final String REST_APP_ID = "org.onosproject.rest";
 
@@ -63,9 +66,11 @@
         context = new MockCodecContext();
         openstackNodeCodec = new OpenstackNodeCodec();
         openstackPhyIntfJsonCodec = new OpenstackPhyInterfaceCodec();
+        openstackAuthJsonCodec = new OpenstackAuthCodec();
 
         assertThat(openstackNodeCodec, notNullValue());
         assertThat(openstackPhyIntfJsonCodec, notNullValue());
+        assertThat(openstackAuthJsonCodec, notNullValue());
 
         expect(mockCoreService.registerApplication(REST_APP_ID))
                 .andReturn(APP_ID).anyTimes();
@@ -73,8 +78,11 @@
         context.registerService(CoreService.class, mockCoreService);
     }
 
+    /**
+     * Tests the openstack compute node encoding.
+     */
     @Test
-    public void testOpenstackNodeEncode() {
+    public void testOpenstackComputeNodeEncode() {
 
         OpenstackPhyInterface phyIntf1 = DefaultOpenstackPhyInterface.builder()
                                 .network("mgmtnetwork")
@@ -100,9 +108,14 @@
         assertThat(nodeJson, matchesOpenstackNode(node));
     }
 
+    /**
+     * Tests the openstack compute node decoding.
+     *
+     * @throws IOException
+     */
     @Test
-    public void testOpenstackNodeDecode() throws IOException {
-        OpenstackNode node = getOpenstackNode("OpenstackNode.json");
+    public void testOpenstackComputeNodeDecode() throws IOException {
+        OpenstackNode node = getOpenstackNode("OpenstackComputeNode.json");
 
         assertThat(node.hostname(), is("compute-01"));
         assertThat(node.type().name(), is("COMPUTE"));
@@ -123,6 +136,55 @@
     }
 
     /**
+     * Tests the openstack controller node encoding.
+     */
+    @Test
+    public void testOpenstackControllerNodeEncode() {
+        OpenstackAuth auth = DefaultOpenstackAuth.builder()
+                .version("v2.0")
+                .port(35357)
+                .protocol(OpenstackAuth.Protocol.HTTP)
+                .project("admin")
+                .username("admin")
+                .password("nova")
+                .perspective(OpenstackAuth.Perspective.PUBLIC)
+                .build();
+
+        OpenstackNode node = DefaultOpenstackNode.builder()
+                .hostname("controller")
+                .type(OpenstackNode.NodeType.CONTROLLER)
+                .state(NodeState.INIT)
+                .managementIp(IpAddress.valueOf("172.16.130.10"))
+                .authentication(auth)
+                .build();
+
+        ObjectNode nodeJson = openstackNodeCodec.encode(node, context);
+        assertThat(nodeJson, matchesOpenstackNode(node));
+    }
+
+    /**
+     * Tests the openstack controller node decoding.
+     */
+    @Test
+    public void testOpenstackControllerNodeDecode() throws IOException {
+        OpenstackNode node = getOpenstackNode("OpenstackControllerNode.json");
+
+        assertThat(node.hostname(), is("controller"));
+        assertThat(node.type().name(), is("CONTROLLER"));
+        assertThat(node.managementIp().toString(), is("172.16.130.10"));
+
+        OpenstackAuth auth = node.authentication();
+
+        assertThat(auth.version(), is("v2.0"));
+        assertThat(auth.port(), is(35357));
+        assertThat(auth.protocol(), is(OpenstackAuth.Protocol.HTTP));
+        assertThat(auth.username(), is("admin"));
+        assertThat(auth.password(), is("nova"));
+        assertThat(auth.project(), is("admin"));
+        assertThat(auth.perspective(), is(OpenstackAuth.Perspective.PUBLIC));
+    }
+
+    /**
      * Reads in an openstack node from the given resource and decodes it.
      *
      * @param resourceName resource to use to read the JSON for the rule
@@ -164,6 +226,9 @@
             if (entityClass == OpenstackPhyInterface.class) {
                 return (JsonCodec<T>) openstackPhyIntfJsonCodec;
             }
+            if (entityClass == OpenstackAuth.class) {
+                return (JsonCodec<T>) openstackAuthJsonCodec;
+            }
             return manager.getCodec(entityClass);
         }
 
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonMatcher.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonMatcher.java
index b9c4464..ac1d471 100644
--- a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonMatcher.java
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/codec/OpenstackNodeJsonMatcher.java
@@ -19,6 +19,7 @@
 import org.hamcrest.Description;
 import org.hamcrest.TypeSafeDiagnosingMatcher;
 import org.onosproject.openstacknode.api.Constants;
+import org.onosproject.openstacknode.api.OpenstackAuth;
 import org.onosproject.openstacknode.api.OpenstackNode;
 import org.onosproject.openstacknode.api.OpenstackPhyInterface;
 
@@ -27,7 +28,7 @@
 import static org.onosproject.openstacknode.api.Constants.VLAN_INTF_NAME;
 
 /**
- * Hamcrest matcher for meters.
+ * Hamcrest matcher for openstack node.
  */
 public final class OpenstackNodeJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
 
@@ -35,6 +36,7 @@
     private static final String INTEGRATION_BRIDGE = "integrationBridge";
     private static final String STATE = "state";
     private static final String PHYSICAL_INTERFACES = "phyIntfs";
+    private static final String AUTHENTICATION = "authentication";
 
     private OpenstackNodeJsonMatcher(OpenstackNode node) {
         this.node = node;
@@ -67,11 +69,13 @@
         }
 
         // check integration bridge
-        String jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE).asText();
-        String intgBridge = node.intgBridge().toString();
-        if (!jsonIntgBridge.equals(intgBridge)) {
-            description.appendText("integration bridge was " + jsonIntgBridge);
-            return false;
+        JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE);
+        if (jsonIntgBridge != null) {
+            String intgBridge = node.intgBridge().toString();
+            if (!jsonIntgBridge.asText().equals(intgBridge)) {
+                description.appendText("integration bridge was " + jsonIntgBridge);
+                return false;
+            }
         }
 
         // check state
@@ -102,6 +106,17 @@
             }
         }
 
+        // check openstack auth
+        JsonNode jsonAuth = jsonNode.get(AUTHENTICATION);
+        if (jsonAuth != null) {
+            OpenstackAuth auth = node.authentication();
+            OpenstackAuthJsonMatcher authMatcher =
+                    OpenstackAuthJsonMatcher.matchOpenstackAuth(auth);
+            if (!authMatcher.matches(jsonAuth)) {
+                return false;
+            }
+        }
+
         // check physical interfaces
         JsonNode jsonPhyIntfs = jsonNode.get(PHYSICAL_INTERFACES);
         if (jsonPhyIntfs != null) {
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackAuthTest.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackAuthTest.java
new file mode 100644
index 0000000..6442aa0
--- /dev/null
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackAuthTest.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.openstacknode.impl;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+import org.onosproject.openstacknode.api.OpenstackAuth;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+/**
+ * Unit tests for DefaultOpenstackAuth.
+ */
+public class DefaultOpenstackAuthTest {
+
+    private static final String USERNAME = "admin";
+    private static final String PASSWORD = "nova";
+    private static final String PROJECT = "admin";
+    private static final String VERSION_1 = "v2.0";
+    private static final String VERSION_2 = "v3";
+    private static final Integer PORT_1 = 35357;
+    private static final Integer PORT_2 = 5000;
+    private static final OpenstackAuth.Protocol PROTOCOL_1 = OpenstackAuth.Protocol.HTTP;
+    private static final OpenstackAuth.Protocol PROTOCOL_2 = OpenstackAuth.Protocol.HTTPS;
+
+    private static final OpenstackAuth OS_AUTH_1 =
+                         createOpenstackAuth(VERSION_1, PORT_1, PROTOCOL_1);
+    private static final OpenstackAuth OS_AUTH_2 =
+                         createOpenstackAuth(VERSION_2, PORT_2, PROTOCOL_2);
+    private static final OpenstackAuth OS_AUTH_3 =
+                         createOpenstackAuth(VERSION_1, PORT_1, PROTOCOL_1);
+
+    private static OpenstackAuth createOpenstackAuth(String version,
+                                              Integer port,
+                                              OpenstackAuth.Protocol protocol) {
+        return DefaultOpenstackAuth.builder()
+                .version(version)
+                .port(port)
+                .protocol(protocol)
+                .username(USERNAME)
+                .password(PASSWORD)
+                .project(PROJECT)
+                .perspective(OpenstackAuth.Perspective.PUBLIC)
+                .build();
+    }
+
+    @Test
+    public void testEquality() {
+        new EqualsTester().addEqualityGroup(OS_AUTH_1, OS_AUTH_3)
+                .addEqualityGroup(OS_AUTH_2)
+                .testEquals();
+    }
+
+    @Test
+    public void testConstruction() {
+        OpenstackAuth auth = OS_AUTH_1;
+
+        assertThat(auth.version(), is("v2.0"));
+        assertThat(auth.port(), is(35357));
+        assertThat(auth.protocol(), is(OpenstackAuth.Protocol.HTTP));
+        assertThat(auth.username(), is("admin"));
+        assertThat(auth.password(), is("nova"));
+        assertThat(auth.project(), is("admin"));
+        assertThat(auth.perspective(), is(OpenstackAuth.Perspective.PUBLIC));
+    }
+}
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandlerTest.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandlerTest.java
index fccfd1b..ff59237 100644
--- a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandlerTest.java
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeHandlerTest.java
@@ -69,6 +69,7 @@
 import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
 import org.onosproject.net.provider.ProviderId;
 import org.onosproject.openstacknode.api.NodeState;
+import org.onosproject.openstacknode.api.OpenstackAuth;
 import org.onosproject.openstacknode.api.OpenstackNode;
 import org.onosproject.openstacknode.api.OpenstackPhyInterface;
 import org.onosproject.ovsdb.controller.OvsdbClientService;
@@ -407,7 +408,7 @@
                 intgBridge.id(),
                 ipAddr,
                 ipAddr,
-                null, null, state, phyIntfs);
+                null, null, state, phyIntfs, null);
     }
 
     private static OpenstackNode createGatewayNode(String hostname,
@@ -422,7 +423,7 @@
                 intgBridge.id(),
                 ipAddr,
                 ipAddr,
-                null, uplinkPort, state, null);
+                null, uplinkPort, state, null, null);
     }
 
     private static final class TestDevice extends DefaultDevice {
@@ -480,7 +481,8 @@
                                   String vlanIntf,
                                   String uplinkPort,
                                   NodeState state,
-                                  Set<OpenstackPhyInterface> phyIntfs) {
+                                  Set<OpenstackPhyInterface> phyIntfs,
+                                  OpenstackAuth auth) {
             super(hostname,
                     type,
                     intgBridge,
@@ -489,7 +491,8 @@
                     vlanIntf,
                     uplinkPort,
                     state,
-                    phyIntfs);
+                    phyIntfs,
+                    auth);
         }
 
         @Override
diff --git a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeTest.java b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeTest.java
index 47ce53d..505b907 100644
--- a/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeTest.java
+++ b/apps/openstacknode/app/src/test/java/org/onosproject/openstacknode/impl/DefaultOpenstackNodeTest.java
@@ -91,21 +91,6 @@
     }
 
     /**
-     * Checks building a node without integration bridge ID fails with
-     * proper exception.
-     */
-    @Test(expected = IllegalArgumentException.class)
-    public void testBuildWithoutIntegrationBridgeId() {
-        DefaultOpenstackNode.builder()
-                .hostname(HOSTNAME_1)
-                .type(OpenstackNode.NodeType.COMPUTE)
-                .managementIp(TEST_IP)
-                .dataIp(TEST_IP)
-                .state(NodeState.INIT)
-                .build();
-    }
-
-    /**
      * Checks building a node without management IP address fails with
      * proper exception.
      */
diff --git a/apps/openstacknode/app/src/test/resources/org/onosproject/openstacknode/codec/OpenstackNode.json b/apps/openstacknode/app/src/test/resources/org/onosproject/openstacknode/codec/OpenstackComputeNode.json
similarity index 100%
rename from apps/openstacknode/app/src/test/resources/org/onosproject/openstacknode/codec/OpenstackNode.json
rename to apps/openstacknode/app/src/test/resources/org/onosproject/openstacknode/codec/OpenstackComputeNode.json
diff --git a/apps/openstacknode/app/src/test/resources/org/onosproject/openstacknode/codec/OpenstackControllerNode.json b/apps/openstacknode/app/src/test/resources/org/onosproject/openstacknode/codec/OpenstackControllerNode.json
new file mode 100644
index 0000000..8745d27
--- /dev/null
+++ b/apps/openstacknode/app/src/test/resources/org/onosproject/openstacknode/codec/OpenstackControllerNode.json
@@ -0,0 +1,14 @@
+{
+  "hostname": "controller",
+  "type": "CONTROLLER",
+  "managementIp": "172.16.130.10",
+  "authentication": {
+    "version": "v2.0",
+    "port": 35357,
+    "protocol": "HTTP",
+    "project": "admin",
+    "username": "admin",
+    "password": "nova",
+    "perspective": "PUBLIC"
+  }
+}
\ No newline at end of file