ONOS-6106 Adding Missing Fields in the " get flows and get devices" rest api

Change-Id: I6657b8d75388e850acaa53fe4f5be261e45a9bca
diff --git a/core/common/BUCK b/core/common/BUCK
index a00d4fd..75999d5 100644
--- a/core/common/BUCK
+++ b/core/common/BUCK
@@ -1,6 +1,7 @@
 SRC_DEPS = [
     '//lib:CORE_DEPS',
     '//incubator/api:onos-incubator-api',
+    '//core/api:onos-api',
 ]
 
 TEST_DEPS = [
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/DeviceCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/DeviceCodec.java
index 2409767..d51cb8c 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/DeviceCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/DeviceCodec.java
@@ -25,6 +25,7 @@
 import org.onosproject.net.Device.Type;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.DriverService;
 import org.onosproject.net.provider.ProviderId;
 
 import static com.google.common.base.Preconditions.checkNotNull;
@@ -38,26 +39,31 @@
     // JSON fieldNames
     private static final String ID = "id";
     private static final String TYPE = "type";
+    private static final String AVAILABLE = "available";
+    private static final String ROLE = "role";
     private static final String MFR = "mfr";
     private static final String HW = "hw";
     private static final String SW = "sw";
     private static final String SERIAL = "serial";
     private static final String CHASSIS_ID = "chassisId";
+    private static final String DRIVER = "driver";
 
 
     @Override
     public ObjectNode encode(Device device, CodecContext context) {
         checkNotNull(device, "Device cannot be null");
         DeviceService service = context.getService(DeviceService.class);
+        DriverService driveService = context.getService(DriverService.class);
         ObjectNode result = context.mapper().createObjectNode()
                 .put(ID, device.id().toString())
                 .put(TYPE, device.type().name())
-                .put("available", service.isAvailable(device.id()))
-                .put("role", service.getRole(device.id()).toString())
+                .put(AVAILABLE, service.isAvailable(device.id()))
+                .put(ROLE, service.getRole(device.id()).toString())
                 .put(MFR, device.manufacturer())
                 .put(HW, device.hwVersion())
                 .put(SW, device.swVersion())
                 .put(SERIAL, device.serialNumber())
+                .put(DRIVER, driveService.getDriver(device.id()).name())
                 .put(CHASSIS_ID, device.chassisId().toString());
         return annotate(result, device, context);
     }
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/FlowEntryCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/FlowEntryCodec.java
index 1f2d359..9a2fd44 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/FlowEntryCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/FlowEntryCodec.java
@@ -54,6 +54,7 @@
                 .put("life", flowEntry.life()) //FIXME life is destroying precision (seconds granularity is default)
                 .put("packets", flowEntry.packets())
                 .put("bytes", flowEntry.bytes())
+                .put("liveType", flowEntry.liveType().toString())
                 .put("lastSeen", flowEntry.lastSeen());
 
         if (flowEntry.treatment() != null) {
diff --git a/core/common/src/test/java/org/onosproject/codec/impl/DeviceCodecTest.java b/core/common/src/test/java/org/onosproject/codec/impl/DeviceCodecTest.java
index 321385c..40334df 100644
--- a/core/common/src/test/java/org/onosproject/codec/impl/DeviceCodecTest.java
+++ b/core/common/src/test/java/org/onosproject/codec/impl/DeviceCodecTest.java
@@ -20,18 +20,30 @@
 import static org.hamcrest.Matchers.notNullValue;
 import static org.hamcrest.Matchers.is;
 import static org.onosproject.codec.impl.JsonCodecUtils.assertJsonEncodable;
-
+import com.google.common.collect.ImmutableMap;
 import org.junit.Test;
 import org.onosproject.codec.JsonCodec;
 import org.onosproject.net.DefaultDevice;
 import org.onosproject.net.Device;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.createMock;
 import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.DriverService;
 import org.onosproject.net.device.DeviceServiceAdapter;
+import org.onosproject.net.driver.DefaultDriver;
+import org.onosproject.net.driver.TestBehaviourImpl;
+import org.onosproject.net.driver.TestBehaviour;
+import org.onosproject.net.driver.TestBehaviourTwo;
+import org.onosproject.net.driver.TestBehaviourTwoImpl;
+
+import java.util.ArrayList;
 
 /**
  * Unit test for DeviceCodec.
  */
 public class DeviceCodecTest {
+    DriverService mockDriverService = createMock(DriverService.class);
 
     private Device device = new DefaultDevice(JsonCodecUtils.PID,
                                               JsonCodecUtils.DID1,
@@ -42,15 +54,28 @@
                                               JsonCodecUtils.SN,
                                               JsonCodecUtils.CID,
                                               JsonCodecUtils.A1);
-
-
+    private DefaultDriver driver = new DefaultDriver("ovs", new ArrayList<>(), "Circus", "lux", "1.2a",
+            ImmutableMap.of(TestBehaviour.class,
+                    TestBehaviourImpl.class,
+                    TestBehaviourTwo.class,
+                    TestBehaviourTwoImpl.class),
+            ImmutableMap.of("foo", "bar"));
 
     @Test
     public void deviceCodecTest() {
         final MockCodecContext context = new MockCodecContext();
+
+        expect(mockDriverService.getDriver(JsonCodecUtils.DID1))
+                .andReturn(driver)
+                .anyTimes();
+
+        replay(mockDriverService);
+
         context.registerService(DeviceService.class, new DeviceServiceAdapter());
+        context.registerService(DriverService.class, mockDriverService);
         final JsonCodec<Device> codec = context.codec(Device.class);
         assertThat(codec, is(notNullValue()));
+
         final Device pojoIn = device;
 
         assertJsonEncodable(context, codec, pojoIn);
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/DevicesWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/DevicesWebResource.java
index 76641da..b16846e 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/DevicesWebResource.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/DevicesWebResource.java
@@ -88,8 +88,9 @@
     public Response removeDevice(@PathParam("id") String id) {
         Device device = nullIsNotFound(get(DeviceService.class).getDevice(deviceId(id)),
                                        DEVICE_NOT_FOUND);
+        ObjectNode result = codec(Device.class).encode(device, this);
         get(DeviceAdminService.class).removeDevice(deviceId(id));
-        return ok(codec(Device.class).encode(device, this)).build();
+        return ok(result).build();
     }
 
     /**
diff --git a/web/api/src/main/resources/definitions/DeviceGet.json b/web/api/src/main/resources/definitions/DeviceGet.json
index 03e67d5..5573359 100644
--- a/web/api/src/main/resources/definitions/DeviceGet.json
+++ b/web/api/src/main/resources/definitions/DeviceGet.json
@@ -10,6 +10,7 @@
     "hw",
     "sw",
     "serial",
+    "driver",
     "chassisId",
     "annotations"
   ],
@@ -46,6 +47,10 @@
       "type": "string",
       "example": "123"
     },
+    "driver": {
+      "type": "string",
+      "example": "ovs"
+    },
     "chassisId": {
       "type": "string",
       "example": "1"
@@ -67,6 +72,10 @@
           "type": "string",
           "example": "OF_13"
         },
+        "driver": {
+          "type": "string",
+          "example": "ovs"
+        },
         "channelId": {
           "type": "string",
           "example": "10.128.12.4:34689"
diff --git a/web/api/src/main/resources/definitions/DeviceGetPorts.json b/web/api/src/main/resources/definitions/DeviceGetPorts.json
index 3a5230c..9193e4a 100644
--- a/web/api/src/main/resources/definitions/DeviceGetPorts.json
+++ b/web/api/src/main/resources/definitions/DeviceGetPorts.json
@@ -10,6 +10,7 @@
     "hw",
     "sw",
     "serial",
+    "driver",
     "chassisId",
     "annotations"
   ],
@@ -46,6 +47,10 @@
       "type": "string",
       "example": "123"
     },
+    "driver": {
+      "type": "string",
+      "example": "ovs"
+    },
     "chassisId": {
       "type": "string",
       "example": "1"
@@ -129,4 +134,4 @@
       }
     }
   }
-}
\ No newline at end of file
+}
diff --git a/web/api/src/main/resources/definitions/DevicesGet.json b/web/api/src/main/resources/definitions/DevicesGet.json
index 4cf4d9d..937c684 100644
--- a/web/api/src/main/resources/definitions/DevicesGet.json
+++ b/web/api/src/main/resources/definitions/DevicesGet.json
@@ -23,6 +23,7 @@
           "hw",
           "sw",
           "serial",
+          "driver",
           "chassisId",
           "annotations"
         ],
@@ -59,6 +60,10 @@
             "type": "string",
             "example": "123"
           },
+          "driver": {
+            "type": "string",
+            "example": "ovs"
+          },
           "chassisId": {
             "type": "string",
             "example": "1"
diff --git a/web/api/src/main/resources/definitions/FlowEntries.json b/web/api/src/main/resources/definitions/FlowEntries.json
index 15f8628..edc4070 100644
--- a/web/api/src/main/resources/definitions/FlowEntries.json
+++ b/web/api/src/main/resources/definitions/FlowEntries.json
@@ -27,6 +27,7 @@
           "life",
           "packets",
           "bytes",
+          "liveType",
           "lastSeen"
         ],
         "properties": {
@@ -85,6 +86,10 @@
             "format": "int64",
             "example": 1826226
           },
+          "liveType": {
+            "type": "string",
+            "example": "UNKNOWN"
+          },
           "lastSeen": {
             "type": "integer",
             "format": "int64",
diff --git a/web/api/src/main/resources/definitions/FlowRules.json b/web/api/src/main/resources/definitions/FlowRules.json
index 026b726..acdbe7c 100644
--- a/web/api/src/main/resources/definitions/FlowRules.json
+++ b/web/api/src/main/resources/definitions/FlowRules.json
@@ -22,7 +22,8 @@
           "priority",
           "timeout",
           "isPermanent",
-          "deviceId"
+          "deviceId",
+          "liveType"
         ],
         "properties": {
           "id": {
@@ -61,6 +62,10 @@
             "type": "string",
             "example": "of:0000000000000003"
           },
+          "liveType": {
+            "type": "string",
+            "example": "UNKNOWN"
+          },
           "treatment": {
             "type": "object",
             "title": "treatment",
diff --git a/web/api/src/test/java/org/onosproject/rest/resources/DevicesResourceTest.java b/web/api/src/test/java/org/onosproject/rest/resources/DevicesResourceTest.java
index 6b6386f..c4e474b 100644
--- a/web/api/src/test/java/org/onosproject/rest/resources/DevicesResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/resources/DevicesResourceTest.java
@@ -19,6 +19,7 @@
 import com.eclipsesource.json.JsonArray;
 import com.eclipsesource.json.JsonObject;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import org.hamcrest.Description;
 import org.hamcrest.TypeSafeMatcher;
 import org.junit.After;
@@ -35,9 +36,17 @@
 import org.onosproject.net.MastershipRole;
 import org.onosproject.net.Port;
 import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.DriverService;
+import org.onosproject.net.driver.DefaultDriver;
+import org.onosproject.net.driver.TestBehaviourImpl;
+import org.onosproject.net.driver.TestBehaviour;
+import org.onosproject.net.driver.TestBehaviourTwo;
+import org.onosproject.net.driver.TestBehaviourTwoImpl;
 
 import javax.ws.rs.NotFoundException;
 import javax.ws.rs.client.WebTarget;
+
+import java.util.ArrayList;
 import java.util.List;
 
 import static org.easymock.EasyMock.createMock;
@@ -61,6 +70,13 @@
  */
 public class DevicesResourceTest extends ResourceTest {
     DeviceService mockDeviceService;
+    DriverService mockDriverService;
+    DefaultDriver driver = new DefaultDriver("ovs", new ArrayList<>(), "Circus", "lux", "1.2a",
+            ImmutableMap.of(TestBehaviour.class,
+                    TestBehaviourImpl.class,
+                    TestBehaviourTwo.class,
+                    TestBehaviourTwoImpl.class),
+            ImmutableMap.of("foo", "bar"));
 
     /**
      * Hamcrest matcher to check that an device representation in JSON matches
@@ -99,6 +115,7 @@
 
             // check HW version field
             String jsonHwVersion = jsonDevice.get("hw").asString();
+
             if (!jsonHwVersion.equals(device.hwVersion())) {
                 reason = "hw Version " + device.hwVersion();
                 return false;
@@ -212,7 +229,7 @@
     @Before
     public void setUpMocks() {
         mockDeviceService = createMock(DeviceService.class);
-
+        mockDriverService = createMock(DriverService.class);
         expect(mockDeviceService.isAvailable(isA(DeviceId.class)))
                 .andReturn(true)
                 .anyTimes();
@@ -220,12 +237,14 @@
                 .andReturn(MastershipRole.MASTER)
                 .anyTimes();
 
+
         // Register the services needed for the test
         CodecManager codecService =  new CodecManager();
         codecService.activate();
         ServiceDirectory testDirectory =
                 new TestServiceDirectory()
                         .add(DeviceService.class, mockDeviceService)
+                        .add(DriverService.class, mockDriverService)
                         .add(CodecService.class, codecService);
 
         BaseResource.setServiceDirectory(testDirectory);
@@ -267,6 +286,22 @@
 
         replay(mockDeviceService);
 
+        expect(mockDriverService.getDriver(did("dev1")))
+                .andReturn(driver)
+                .anyTimes();
+
+        expect(mockDriverService.getDriver(did("dev2")))
+                .andReturn(driver)
+                .anyTimes();
+
+        expect(mockDriverService.getDriver(did("dev3")))
+                .andReturn(driver)
+                .anyTimes();
+
+        replay(mockDriverService);
+
+
+
         WebTarget wt = target();
         String response = wt.path("devices").request().get(String.class);
         assertThat(response, containsString("{\"devices\":["));
@@ -295,11 +330,14 @@
         String deviceIdString = "testdevice";
         DeviceId deviceId = did(deviceIdString);
         Device device = device(deviceIdString);
-
         expect(mockDeviceService.getDevice(deviceId))
                 .andReturn(device)
                 .once();
         replay(mockDeviceService);
+        expect(mockDriverService.getDriver(deviceId))
+                .andReturn(driver)
+                .anyTimes();
+        replay(mockDriverService);
 
         WebTarget wt = target();
         String response = wt.path("devices/" + deviceId).request().get(String.class);
@@ -317,6 +355,7 @@
         DeviceId deviceId = did(deviceIdString);
         Device device = device(deviceIdString);
 
+
         Port port1 = new DefaultPort(device, portNumber(1), true);
         Port port2 = new DefaultPort(device, portNumber(2), true);
         Port port3 = new DefaultPort(device, portNumber(3), true);
@@ -331,6 +370,12 @@
                 .once();
         replay(mockDeviceService);
 
+        expect(mockDriverService.getDriver(deviceId))
+                .andReturn(driver)
+                .anyTimes();
+        replay(mockDriverService);
+
+
         WebTarget wt = target();
         String response =
                 wt.path("devices/" + deviceId + "/ports").request()
diff --git a/web/api/src/test/java/org/onosproject/rest/resources/IntentsResourceTest.java b/web/api/src/test/java/org/onosproject/rest/resources/IntentsResourceTest.java
index ef08c9f..efabf66f 100644
--- a/web/api/src/test/java/org/onosproject/rest/resources/IntentsResourceTest.java
+++ b/web/api/src/test/java/org/onosproject/rest/resources/IntentsResourceTest.java
@@ -164,6 +164,11 @@
         }
 
         @Override
+        public FlowLiveType liveType() {
+            return FlowLiveType.IMMEDIATE;
+        }
+
+        @Override
         public long life(TimeUnit timeUnit) {
             return SECONDS.convert(baseValue + 11, timeUnit);
         }