28c9b3477d0f2c7f303e6e391da924b3fed016a3
[nemo.git] / nemo-tools / sandbox / src / main / java / org / opendaylight / nemo / tool / sandbox / models / Node.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 import java.util.ArrayList;
16 import java.util.List;
17
18 /**
19  * Created by hj on 12/8/15.
20  */
21 abstract public class Node {
22     private static Logger log = LoggerFactory.getLogger(Node.class);
23     private final String name;
24     private final NodeType nodeType;
25     protected List<Connector> connectors;
26
27     public Node(NodeType nodeType, String name) {
28         this.nodeType = nodeType;
29         this.name = name;
30         this.connectors = new ArrayList<Connector>();
31     }
32
33     public NodeType getNodeType() {
34         return nodeType;
35     }
36
37     public String getName() {
38         return name;
39     }
40
41     public void addConnectors(Connector connector) {
42         connectors.add(connector);
43     }
44
45     public List<Connector> getConnectors() {
46         return connectors;
47     }
48
49     public void install() {
50         List<String> commands = generateCommands();
51         if (commands == null) {
52             log.error("Commands should not be null.");
53             return;
54         }
55         for (String command : commands) {
56             try {
57                 CmdExecutor.sshExecute(command);
58             } catch (Exception e) {
59                 log.error("Error while execute [{}].", command, name, nodeType);
60                 // TODO Auto-generated catch block
61                 log.error(e);
62             }
63         }
64     }
65
66     abstract protected List<String> generateCommands();
67
68     @Override
69     public boolean equals(Object o) {
70         if (this == o) return true;
71         if (o == null || getClass() != o.getClass()) return false;
72
73         Node node = (Node) o;
74
75         if (name != null ? !name.equals(node.name) : node.name != null) return false;
76         if (nodeType != node.nodeType) return false;
77
78         return true;
79     }
80
81     @Override
82     public int hashCode() {
83         int result = name != null ? name.hashCode() : 0;
84         result = 31 * result + (nodeType != null ? nodeType.hashCode() : 0);
85         return result;
86     }
87 }