774df919fdc1006e9f5d272ce388fdec02faa293
[nemo.git] / nemo-tools / sandbox / src / main / java / org / opendaylight / nemo / tool / sandbox / models / Link.java
1 /*
2  * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
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
9 package org.opendaylight.nemo.tool.sandbox.models;
10
11 import org.opendaylight.nemo.tool.sandbox.CmdExecutor;
12
13 /**
14  * Created by hj on 12/8/15.
15  */
16 public class Link {
17     private final Connector srcInterface;
18     private final Connector dstInterface;
19
20     public Link(Connector srcInterface, Connector dstInterface) {
21         this.srcInterface = srcInterface;
22         this.dstInterface = dstInterface;
23     }
24
25     public Connector getSrcConnector() {
26         return srcInterface;
27     }
28
29     public Connector getDstConnector() {
30         return dstInterface;
31     }
32
33     public void install() {
34         String linkAddCmd = "ip link add name " + srcInterface.getConnectorName() + " type veth peer name " + dstInterface.getConnectorName();
35         try {
36             CmdExecutor.sshExecute(linkAddCmd);
37         } catch (Exception e) {
38             e.printStackTrace();
39         }
40     }
41
42     public void uninstall() {
43         String linkDelCmd = "ip link del " + srcInterface.getConnectorName();
44         try {
45             CmdExecutor.sshExecute(linkDelCmd);
46         } catch (Exception e) {
47             e.printStackTrace();
48         }
49     }
50
51     @Override
52     public boolean equals(Object o) {
53         if (this == o) return true;
54         if (o == null || getClass() != o.getClass()) return false;
55
56         Link link = (Link) o;
57
58         if (dstInterface != null ? !dstInterface.equals(link.dstInterface) : link.dstInterface != null) return false;
59         if (srcInterface != null ? !srcInterface.equals(link.srcInterface) : link.srcInterface != null) return false;
60
61         return true;
62     }
63
64     @Override
65     public int hashCode() {
66         int result = srcInterface != null ? srcInterface.hashCode() : 0;
67         result = 31 * result + (dstInterface != null ? dstInterface.hashCode() : 0);
68         return result;
69     }
70
71     @Override
72     public String toString() {
73         return "Link{" +
74                 "srcInterface=" + srcInterface +
75                 ", dstInterface=" + dstInterface +
76                 '}';
77     }
78 }