[ONOS-7553] Support injecting physical interfaces to openstack node
Change-Id: I5d746e9b4fa6015dbaec90d27ea7e1a7fa105e31
diff --git a/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackPhyInterfaceCodec.java b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackPhyInterfaceCodec.java
new file mode 100644
index 0000000..340a76d
--- /dev/null
+++ b/apps/openstacknode/app/src/main/java/org/onosproject/openstacknode/codec/OpenstackPhyInterfaceCodec.java
@@ -0,0 +1,66 @@
+/*
+ * 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.OpenstackPhyInterface;
+import org.onosproject.openstacknode.impl.DefaultOpenstackPhyInterface;
+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 physical interface codec used for serializing and de-serializing JSON string.
+ */
+public final class OpenstackPhyInterfaceCodec extends JsonCodec<OpenstackPhyInterface> {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final String NETWORK = "network";
+ private static final String INTERFACE = "intf";
+
+ private static final String MISSING_MESSAGE = " is required in OpenstackPhyInterface";
+
+ @Override
+ public ObjectNode encode(OpenstackPhyInterface phyIntf, CodecContext context) {
+ checkNotNull(phyIntf, "Openstack physical interface cannot be null");
+
+ return context.mapper().createObjectNode()
+ .put(NETWORK, phyIntf.network())
+ .put(INTERFACE, phyIntf.intf());
+ }
+
+ @Override
+ public OpenstackPhyInterface decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ String network = nullIsIllegal(json.get(NETWORK).asText(),
+ NETWORK + MISSING_MESSAGE);
+ String intf = nullIsIllegal(json.get(INTERFACE).asText(),
+ INTERFACE + MISSING_MESSAGE);
+
+ return DefaultOpenstackPhyInterface.builder()
+ .network(network)
+ .intf(intf)
+ .build();
+ }
+}