[ONOS-6465] gRPC Protocol and controller

Change-Id: I0ae997f234ce95a78db2db1917f2cbbe3696ccfd
diff --git a/protocols/grpc/ctl/BUCK b/protocols/grpc/ctl/BUCK
new file mode 100644
index 0000000..764d980
--- /dev/null
+++ b/protocols/grpc/ctl/BUCK
@@ -0,0 +1,20 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//protocols/grpc/api:onos-protocols-grpc-api',
+    '//lib:grpc-core-1.3.0',
+    '//lib:grpc-protobuf-1.3.0',
+    '//lib:grpc-stub-1.3.0',
+    '//lib:grpc-netty-1.3.0',
+    '//lib:grpc-auth-1.3.0',
+]
+
+TEST_DEPS = [
+    '//lib:TEST_ADAPTERS',
+    '//utils/osgi:onlab-osgi-tests',
+]
+
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+    test_deps = TEST_DEPS,
+)
+
diff --git a/protocols/grpc/ctl/pom.xml b/protocols/grpc/ctl/pom.xml
new file mode 100644
index 0000000..eb327f9
--- /dev/null
+++ b/protocols/grpc/ctl/pom.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2016-present Open Networking Laboratory
+  ~
+  ~ 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.
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>onos-grpc-protocol</artifactId>
+        <groupId>org.onosproject</groupId>
+        <version>1.11.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-grpc-protocol-ctl</artifactId>
+
+    <packaging>bundle</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-grpc-protocol-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.onosproject</groupId>
+                <artifactId>onos-maven-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
+    
+</project>
\ No newline at end of file
diff --git a/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/GrpcControllerImpl.java b/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/GrpcControllerImpl.java
new file mode 100644
index 0000000..4ce097d
--- /dev/null
+++ b/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/GrpcControllerImpl.java
@@ -0,0 +1,127 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.grpc.ctl;
+
+import com.google.common.collect.ImmutableSet;
+import io.grpc.ManagedChannel;
+import io.grpc.ManagedChannelBuilder;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.grpc.api.GrpcChannelId;
+import org.onosproject.grpc.api.GrpcController;
+import org.onosproject.grpc.api.GrpcObserverHandler;
+import org.onosproject.grpc.api.GrpcStreamObserverId;
+import org.onosproject.net.DeviceId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Default implementation of the GrpcController.
+ */
+@Component(immediate = true)
+@Service
+public class GrpcControllerImpl implements GrpcController {
+
+    public static final Logger log = LoggerFactory
+            .getLogger(GrpcControllerImpl.class);
+
+    private Map<GrpcStreamObserverId, GrpcObserverHandler> observers;
+    private Map<GrpcChannelId, ManagedChannel> channels;
+    private Map<GrpcChannelId, ManagedChannelBuilder<?>> channelBuilders;
+
+    @Activate
+    public void activate() {
+        observers = new ConcurrentHashMap<>();
+        channels = new ConcurrentHashMap<>();
+        channelBuilders = new ConcurrentHashMap<>();
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        channels.values().forEach(ManagedChannel::shutdown);
+        observers.clear();
+        channels.clear();
+        channelBuilders.clear();
+        log.info("Stopped");
+    }
+
+    @Override
+    public void addObserver(GrpcStreamObserverId observerId, GrpcObserverHandler grpcObserverHandler) {
+        grpcObserverHandler.bindObserver(channels.get(observerId.serviceId().channelId()));
+        observers.put(observerId, grpcObserverHandler);
+    }
+
+    @Override
+    public void removeObserver(GrpcStreamObserverId observerId) {
+        observers.get(observerId).removeObserver();
+        observers.remove(observerId);
+    }
+
+    @Override
+    public Optional<GrpcObserverHandler> getObserverManager(GrpcStreamObserverId observerId) {
+        return Optional.ofNullable(observers.get(observerId));
+    }
+
+    @Override
+    public ManagedChannel connectChannel(GrpcChannelId channelId, ManagedChannelBuilder<?> channelBuilder) {
+        ManagedChannel channel = channelBuilder.build();
+
+        channel.getState(true);
+        channelBuilders.put(channelId, channelBuilder);
+        channels.put(channelId, channel);
+        return channel;
+    }
+
+    @Override
+    public void disconnectChannel(GrpcChannelId channelId) {
+        channels.get(channelId).shutdown();
+        channels.remove(channelId);
+        channelBuilders.remove(channelId);
+    }
+
+    @Override
+    public Map<GrpcChannelId, ManagedChannel> getChannels() {
+        return channels;
+    }
+
+    @Override
+    public Collection<ManagedChannel> getChannels(final DeviceId deviceId) {
+        final Set<ManagedChannel> deviceChannels = new HashSet<>();
+        channels.forEach((k, v) -> {
+            if (k.deviceId().equals(deviceId)) {
+                deviceChannels.add(v);
+            }
+        });
+
+        return ImmutableSet.copyOf(deviceChannels);
+    }
+
+    @Override
+    public Optional<ManagedChannel> getChannel(GrpcChannelId channelId) {
+        return Optional.ofNullable(channels.get(channelId));
+    }
+}
diff --git a/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/package-info.java b/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/package-info.java
new file mode 100644
index 0000000..c29204a
--- /dev/null
+++ b/protocols/grpc/ctl/src/main/java/org/onosproject/grpc/ctl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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 for gRPC protocol implementations.
+ */
+package org.onosproject.grpc.ctl;
\ No newline at end of file