[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) {