f23bb432ebaae4795ed24b4966157d706006788a
[netvirt.git] / vpnservice / ipv6service / impl / src / main / java / org / opendaylight / netvirt / ipv6service / VirtualPort.java
1 /*
2  * Copyright (c) 2015 Dell 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.netvirt.ipv6service;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class VirtualPort  {
22
23     private Uuid      intfUUID;
24     private Uuid      networkID;
25     private String    macAddress;
26     private Boolean   routerIntfFlag;
27     private String    dpId;
28     private String    deviceOwner;
29     private Long      ofPort;
30     private HashMap<Uuid, SubnetInfo> snetInfo;
31
32     // associated router if any
33     private VirtualRouter router = null;
34
35     // TODO:: Need Openflow port
36
37     /**
38      * Logger instance.
39      */
40     static final Logger logger = LoggerFactory.getLogger(VirtualPort.class);
41
42     public VirtualPort() {
43         snetInfo = new HashMap<Uuid, SubnetInfo>();
44     }
45
46     public Uuid getIntfUUID() {
47         return intfUUID;
48     }
49
50     public VirtualPort setIntfUUID(Uuid intfUUID) {
51         this.intfUUID = intfUUID;
52         return this;
53     }
54
55     public Uuid getNetworkID() {
56         return networkID;
57     }
58
59     public VirtualPort setNetworkID(Uuid networkID) {
60         this.networkID = networkID;
61         return this;
62     }
63
64     public VirtualPort setSubnetInfo(Uuid snetID, IpAddress fixedIp) {
65         SubnetInfo subnetInfo = snetInfo.get(snetID);
66         if (subnetInfo == null) {
67             subnetInfo = new SubnetInfo(snetID, fixedIp);
68             snetInfo.put(snetID, subnetInfo);
69         } else {
70             subnetInfo.setIpAddr(fixedIp);
71         }
72         return this;
73     }
74
75     public void clearSubnetInfo() {
76         snetInfo.clear();
77     }
78
79     public void removeSubnetInfo(Uuid snetID) {
80         this.snetInfo.remove(snetID);
81     }
82
83     public void setSubnet(Uuid snetID, VirtualSubnet subnet) {
84         SubnetInfo subnetInfo = snetInfo.get(snetID);
85         if (subnetInfo == null) {
86             logger.info("Subnet {} not associated with the virtual port {}",
87                 snetID, intfUUID);
88             return;
89         }
90         subnetInfo.setSubnet(subnet);
91     }
92
93     public List<VirtualSubnet> getSubnets() {
94         List<VirtualSubnet> subnetList = new ArrayList<>();
95         for (SubnetInfo subnetInfo : snetInfo.values()) {
96             if (subnetInfo.getSubnet() != null) {
97                 subnetList.add(subnetInfo.getSubnet());
98             }
99         }
100         return subnetList;
101     }
102
103     public List<IpAddress> getIpAddresses() {
104         List<IpAddress> ipAddrList = new ArrayList<>();
105         for (SubnetInfo subnetInfo : snetInfo.values()) {
106             ipAddrList.add(subnetInfo.getIpAddr());
107         }
108         return ipAddrList;
109     }
110
111     public String getMacAddress() {
112         return macAddress;
113     }
114
115     public VirtualPort setMacAddress(String macAddress) {
116         this.macAddress = macAddress;
117         return this;
118     }
119
120     public Boolean getRouterIntfFlag() {
121         return routerIntfFlag;
122     }
123
124     public VirtualPort setRouterIntfFlag(Boolean routerIntfFlag) {
125         this.routerIntfFlag = routerIntfFlag;
126         return this;
127     }
128
129     public void setRouter(VirtualRouter rtr) {
130         this.router = rtr;
131     }
132
133     public VirtualRouter getRouter() {
134         return router;
135     }
136
137     public VirtualPort setDeviceOwner(String deviceOwner) {
138         this.deviceOwner = deviceOwner;
139         return this;
140     }
141
142     public String getDeviceOwner() {
143         return deviceOwner;
144     }
145
146     public VirtualPort setDpId(String dpId) {
147         this.dpId = dpId;
148         return this;
149     }
150
151     public String getDpId() {
152         return dpId;
153     }
154
155     public void setOfPort(Long ofPort) {
156         this.ofPort = ofPort;
157     }
158
159     public Long getOfPort() {
160         return ofPort;
161     }
162
163     public void removeSelf() {
164         if (routerIntfFlag == true) {
165             if (router != null) {
166                 router.removeInterface(this);
167             }
168         }
169
170         for (SubnetInfo subnetInfo: snetInfo.values()) {
171             if (subnetInfo.getSubnet() != null) {
172                 subnetInfo.getSubnet().removeInterface(this);
173             }
174         }
175     }
176
177     @Override
178     public String toString() {
179         return "VirtualPort[IntfUUid=" + intfUUID + " subnetInfo="
180                 + snetInfo + " NetworkId=" + networkID + " mac=" + macAddress + " ofPort="
181                 + ofPort + " routerFlag=" + routerIntfFlag + " dpId=" + dpId + "]";
182     }
183
184     private class SubnetInfo {
185         private Uuid      subnetID;
186         private IpAddress ipAddr;
187         // associated subnet
188         private VirtualSubnet subnet = null;
189
190         SubnetInfo(Uuid subnetId, IpAddress ipAddr) {
191             this.subnetID = subnetId;
192             this.ipAddr = ipAddr;
193         }
194
195         public Uuid getSubnetID() {
196             return subnetID;
197         }
198
199         public IpAddress getIpAddr() {
200             return ipAddr;
201         }
202
203         public void setIpAddr(IpAddress ipAddr) {
204             this.ipAddr = ipAddr;
205         }
206
207         public VirtualSubnet getSubnet() {
208             return subnet;
209         }
210
211         public void setSubnet(VirtualSubnet subnet) {
212             this.subnet = subnet;
213         }
214
215         @Override
216         public String toString() {
217             return "subnetInfot[subnetId=" + subnetID + " ipAddr=" + ipAddr + " ]";
218         }
219     }
220 }