[ONOS-7621] Support injecting keystone auth info through network-cfg
Change-Id: I2439e257f0f576c46b68322b8c8f1c87fa2cc9ae
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackAuthCodec.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackAuthCodec.java
new file mode 100644
index 0000000..5556d9c
--- /dev/null
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackAuthCodec.java
@@ -0,0 +1,100 @@
+/*
+ * 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.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.openstacknode.api.OpenstackAuth;
+import org.onosproject.openstacknode.impl.DefaultOpenstackAuth;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Openstack keystone authentication codec used for serializing and
+ * de-serializing JSON string.
+ */
+public class OpenstackAuthCodec extends JsonCodec<OpenstackAuth> {
+
+ private final Logger log = getLogger(getClass());
+
+ 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 static final String MISSING_MESSAGE = " is required in OpenstackAuth";
+
+ @Override
+ public ObjectNode encode(OpenstackAuth auth, CodecContext context) {
+ checkNotNull(auth, "Openstack auth cannot be null");
+
+ ObjectNode result = context.mapper().createObjectNode()
+ .put(VERSION, auth.version())
+ .put(PORT, auth.port())
+ .put(PROTOCOL, auth.protocol().name())
+ .put(USERNAME, auth.username())
+ .put(PASSWORD, auth.password())
+ .put(PROJECT, auth.project());
+
+ if (auth.perspective() != null) {
+ result.put(PERSPECTIVE, auth.perspective().name());
+ }
+
+ return result;
+ }
+
+ @Override
+ public OpenstackAuth decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ String version = nullIsIllegal(json.get(VERSION).asText(),
+ VERSION + MISSING_MESSAGE);
+ Integer port = nullIsIllegal(json.get(PORT).asInt(),
+ PORT + MISSING_MESSAGE);
+ String protocol = nullIsIllegal(json.get(PROTOCOL).asText(),
+ PROTOCOL + MISSING_MESSAGE);
+ String username = nullIsIllegal(json.get(USERNAME).asText(),
+ USERNAME + MISSING_MESSAGE);
+ String password = nullIsIllegal(json.get(PASSWORD).asText(),
+ PASSWORD + MISSING_MESSAGE);
+ String project = nullIsIllegal(json.get(PROJECT).asText(),
+ PROJECT + MISSING_MESSAGE);
+
+ DefaultOpenstackAuth.Builder authBuilder = DefaultOpenstackAuth.builder()
+ .version(version)
+ .port(port)
+ .protocol(OpenstackAuth.Protocol.valueOf(protocol))
+ .username(username)
+ .password(password)
+ .project(project);
+
+ if (json.get(PERSPECTIVE) != null) {
+ authBuilder.perspective(
+ OpenstackAuth.Perspective.valueOf(json.get(PERSPECTIVE).asText()));
+ }
+
+ return authBuilder.build();
+ }
+}