aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/ovsdb/ctl/src/main/java/org/onosproject/ovsdb/controller/impl/OvsdbJsonRpcHandler.java
blob: 1956a1eb04f2ab45ab101683f7cbbf69c13651f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright 2015 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.ovsdb.controller.impl;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import org.onosproject.ovsdb.controller.OvsdbNodeId;
import org.onosproject.ovsdb.controller.driver.OvsdbProviderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.base.Strings;

/**
 * Channel handler deals with the node connection and dispatches
 * ovsdb messages to the appropriate locations.
 */
public final class OvsdbJsonRpcHandler extends ChannelInboundHandlerAdapter {
    protected static final Logger log = LoggerFactory
            .getLogger(OvsdbJsonRpcHandler.class);
    private OvsdbNodeId ovsdbNodeId;
    private OvsdbProviderService ovsdbProviderService;

    /**
     * Constructor from a OvsdbNodeId ovsdbNodeId.
     *
     * @param ovsdbNodeId the ovsdbNodeId to use
     */
    public OvsdbJsonRpcHandler(OvsdbNodeId ovsdbNodeId) {
        super();
        this.ovsdbNodeId = ovsdbNodeId;
    }

    /**
     * Gets the ovsdbProviderService instance.
     *
     * @return the instance of the ovsdbProviderService
     */
    public OvsdbProviderService getOvsdbProviderService() {
        return ovsdbProviderService;
    }

    /**
     * Sets the ovsdbProviderService instance.
     *
     * @param ovsdbNodeDriver the ovsdbNodeDriver to use
     */
    public void setOvsdbProviderService(OvsdbProviderService ovsdbNodeDriver) {
        this.ovsdbProviderService = ovsdbNodeDriver;
    }

    /**
     * Gets the OvsdbNodeId instance.
     *
     * @return the instance of the OvsdbNodeId
     */
    public OvsdbNodeId getNodeId() {
        return ovsdbNodeId;
    }

    /**
     * Sets the ovsdb node id.
     *
     * @param ovsdbNodeId the ovsdbNodeId to use
     */
    public void setNodeId(OvsdbNodeId ovsdbNodeId) {
        this.ovsdbNodeId = ovsdbNodeId;
    }

    /**
     * Processes an JsonNode message received on the channel.
     *
     * @param jsonNode The OvsdbJsonRpcHandler that received the message
     */
    private void processOvsdbMessage(JsonNode jsonNode) {

        log.debug("Handle ovsdb message");

        if (jsonNode.has("result")) {

            log.debug("Handle ovsdb result");
            ovsdbProviderService.processResult(jsonNode);

        } else if (jsonNode.hasNonNull("method")) {

            log.debug("Handle ovsdb request");
            if (jsonNode.has("id")
                    && !Strings.isNullOrEmpty(jsonNode.get("id").asText())) {
                ovsdbProviderService.processRequest(jsonNode);
            }

        }
        return;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        log.debug("Receive message from ovsdb");
        if (msg instanceof JsonNode) {
            JsonNode jsonNode = (JsonNode) msg;
            processOvsdbMessage(jsonNode);
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext context, Throwable cause) {
        log.error("Exception inside channel handling pipeline.", cause);
        context.close();
    }
}