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