Added more ignorable files to .gitignore
[ovsdb.git] / plugin / src / main / java / org / opendaylight / ovsdb / plugin / Connection.java
1 /*
2  * Copyright (C) 2013 Red Hat, Inc.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Authors : Madhu Venugopal, Brent Salisbury
9  */
10 package org.opendaylight.ovsdb.plugin;
11
12 import io.netty.channel.Channel;
13
14 import org.opendaylight.controller.sal.core.ConstructionException;
15 import org.opendaylight.controller.sal.core.Node;
16 import org.opendaylight.controller.sal.utils.Status;
17 import org.opendaylight.controller.sal.utils.StatusCode;
18 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class Connection {
23     private Node node;
24     private String identifier;
25     private Channel channel;
26     private OvsdbRPC rpc;
27
28     public Long getIdCounter() {
29         return idCounter;
30     }
31
32     public void setIdCounter(Long idCounter) {
33         this.idCounter = idCounter;
34     }
35
36     private Long idCounter;
37
38     private static final Logger logger = LoggerFactory.getLogger(Connection.class);
39
40     public Connection(String identifier, Channel channel) {
41
42         super();
43
44         this.identifier = identifier;
45         this.channel = channel;
46         this.idCounter = 0L;
47         try {
48             node = new Node("OVS", identifier);
49         } catch (ConstructionException e) {
50             logger.error("Error creating OVS node with identifier " + identifier, e);
51         }
52     }
53
54     public String getIdentifier() {
55         return identifier;
56     }
57
58     public void setIdentifier(String identifier) {
59         this.identifier = identifier;
60     }
61
62     public Channel getChannel() {
63         return this.channel;
64     }
65
66     public void setChannel(Channel channel) {
67         this.channel = channel;
68     }
69
70     public Node getNode() {
71         return node;
72     }
73
74     public void setNode(Node node) {
75         this.node = node;
76     }
77
78     public OvsdbRPC getRpc() {
79         return rpc;
80     }
81
82     public void setRpc(OvsdbRPC rpc) {
83         this.rpc = rpc;
84     }
85
86     public void sendMessage(String message) {
87         channel.writeAndFlush(message);
88         this.idCounter++;
89     }
90
91     public Status disconnect() {
92         channel.close();
93         return new Status(StatusCode.SUCCESS);
94     }
95
96     @Override
97     public int hashCode() {
98         final int prime = 31;
99         int result = 1;
100         result = prime * result + ((identifier == null) ? 0 : identifier.hashCode());
101         return result;
102     }
103
104     @Override
105     public boolean equals(Object obj) {
106         if (this == obj) return true;
107         if (obj == null) return false;
108         if (getClass() != obj.getClass()) return false;
109         Connection other = (Connection) obj;
110         if (identifier == null) {
111             if (other.identifier != null) return false;
112         } else if (!identifier.equals(other.identifier)) return false;
113         return true;
114     }
115 }