Add INFO.yaml for nemo
[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 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 /**
15  * Created by hj on 12/8/15.
16  */
17 public class Link {
18     private static Logger log = LoggerFactory.getLogger(Link.class);
19     private final Connector srcInterface;
20     private final Connector dstInterface;
21
22     public Link(Connector srcInterface, Connector dstInterface) {
23         this.srcInterface = srcInterface;
24         this.dstInterface = dstInterface;
25     }
26
27     public Connector getSrcConnector() {
28         return srcInterface;
29     }
30
31     public Connector getDstConnector() {
32         return dstInterface;
33     }
34
35     public void install() {
36         String linkAddCmd = "ip link add name " + srcInterface.getConnectorName() + " type veth peer name " + dstInterface.getConnectorName();
37         try {
38             CmdExecutor.sshExecute(linkAddCmd);
39         } catch (Exception e) {
40             // TODO Auto-generated catch block
41             log.error("Exception:",e);
42         }
43     }
44
45     public void uninstall() {
46         String linkDelCmd = "ip link del " + srcInterface.getConnectorName();
47         try {
48             CmdExecutor.sshExecute(linkDelCmd);
49         } catch (Exception e) {
50             // TODO Auto-generated catch block
51             log.error("Exception:",e);
52         }
53     }
54
55     @Override
56     public boolean equals(Object o) {
57         if (this == o) return true;
58         if (o == null || getClass() != o.getClass()) return false;
59
60         Link link = (Link) o;
61
62         if (dstInterface != null ? !dstInterface.equals(link.dstInterface) : link.dstInterface != null) return false;
63         if (srcInterface != null ? !srcInterface.equals(link.srcInterface) : link.srcInterface != null) return false;
64
65         return true;
66     }
67
68     @Override
69     public int hashCode() {
70         int result = srcInterface != null ? srcInterface.hashCode() : 0;
71         result = 31 * result + (dstInterface != null ? dstInterface.hashCode() : 0);
72         return result;
73     }
74
75     @Override
76     public String toString() {
77         return "Link{" +
78                 "srcInterface=" + srcInterface +
79                 ", dstInterface=" + dstInterface +
80                 '}';
81     }
82 }