Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / adapters / network / ContrailSubnet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.adapters.network;
24
25 import com.fasterxml.jackson.annotation.JsonProperty;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.onap.so.logger.ErrorCode;
31 import org.onap.so.logger.MessageEnum;
32 import org.onap.so.openstack.beans.HostRoute;
33 import org.onap.so.openstack.beans.Pool;
34 import org.onap.so.openstack.beans.Subnet;
35 import org.onap.so.openstack.utils.MsoCommonUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39
40 public class ContrailSubnet {
41
42     private static final Logger logger = LoggerFactory.getLogger(ContrailSubnet.class);
43     @Autowired
44     private MsoCommonUtils msoCommonUtils;
45
46     @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet")
47     private ContrailSubnetIp subnet = new ContrailSubnetIp();
48
49     @JsonProperty("network_ipam_refs_data_ipam_subnets_default_gateway")
50     private String defaultGateway;
51
52     @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet_name")
53     private String subnetName;
54
55     @JsonProperty("network_ipam_refs_data_ipam_subnets_enable_dhcp")
56     private Boolean enableDhcp;
57
58     @JsonProperty("network_ipam_refs_data_ipam_subnets_addr_from_start")
59     private Boolean addrFromStart = true;
60     /**
61      * future - leave this commented private String subnet_uuid; private String dns_server_address; private List<String>
62      * dns_nameservers; private String dhcp_option_list;
63      **/
64
65     @JsonProperty("network_ipam_refs_data_ipam_subnets_allocation_pools")
66     private List<ContrailSubnetPool> allocationPools = new ArrayList<>();
67
68     @JsonProperty("network_ipam_refs_data_ipam_subnets_host_routes")
69     private final ContrailSubnetHostRoutes host_routes = new ContrailSubnetHostRoutes();
70
71     public ContrailSubnet() {
72         super();
73     }
74
75     public String getDefaultGateway() {
76         return defaultGateway;
77     }
78
79     public void setDefaultGateway(String defaultGateway) {
80         this.defaultGateway = defaultGateway;
81     }
82
83     public ContrailSubnetIp getSubnet() {
84         return subnet;
85     }
86
87     public void setSubnet(ContrailSubnetIp subnet) {
88         this.subnet = subnet;
89     }
90
91     public Boolean isEnableDhcp() {
92         return enableDhcp;
93     }
94
95     public void setEnableDhcp(Boolean enableDhcp) {
96         this.enableDhcp = enableDhcp;
97     }
98
99     public String getSubnetName() {
100         return subnetName;
101     }
102
103     public void setSubnetName(String subnetName) {
104         this.subnetName = subnetName;
105     }
106
107     public List<ContrailSubnetPool> getAllocationPools() {
108         return allocationPools;
109     }
110
111     public void setPools(List<ContrailSubnetPool> allocationPools) {
112         this.allocationPools = allocationPools;
113     }
114
115     public Boolean isAddrFromStart() {
116         return addrFromStart;
117     }
118
119     public void setAddrFromStart(Boolean addrFromStart) {
120         this.addrFromStart = addrFromStart;
121     }
122
123     public JsonNode toJsonNode() {
124         JsonNode node = null;
125         try {
126             ObjectMapper mapper = new ObjectMapper();
127             node = mapper.convertValue(this, JsonNode.class);
128         } catch (Exception e) {
129             logger.error("{} {} Error creating JsonNode for Contrail Subnet: {} ", MessageEnum.RA_MARSHING_ERROR,
130                     ErrorCode.SchemaError.getValue(), subnetName, e);
131         }
132
133         return node;
134     }
135
136     public String toJsonString() {
137         String jsonString = null;
138         try {
139             ObjectMapper mapper = new ObjectMapper();
140             jsonString = mapper.writeValueAsString(this);
141         } catch (Exception e) {
142             logger.error("{} {} Error creating JsonString for Contrail Subnet: {} ", MessageEnum.RA_MARSHING_ERROR,
143                     ErrorCode.SchemaError.getValue(), subnetName, e);
144         }
145
146         return jsonString;
147     }
148
149     // poulate contrail subnet with input(from bopel) subnet
150     public void populateWith(Subnet inputSubnet) {
151         if (inputSubnet != null) {
152             if (!msoCommonUtils.isNullOrEmpty(inputSubnet.getSubnetName()))
153                 subnetName = inputSubnet.getSubnetName();
154             else
155                 subnetName = inputSubnet.getSubnetId();
156             enableDhcp = inputSubnet.getEnableDHCP();
157             defaultGateway = inputSubnet.getGatewayIp();
158             if (!msoCommonUtils.isNullOrEmpty(inputSubnet.getCidr())) {
159                 int idx = inputSubnet.getCidr().indexOf("/");
160                 if (idx != -1) {
161                     subnet.setIpPrefix(inputSubnet.getCidr().substring(0, idx));
162                     subnet.setIpPrefixLen(inputSubnet.getCidr().substring(idx + 1));
163                 }
164             }
165             if (inputSubnet.getAllocationPools() != null) {
166                 for (Pool pool : inputSubnet.getAllocationPools()) {
167                     if (!msoCommonUtils.isNullOrEmpty(pool.getStart())
168                             && !msoCommonUtils.isNullOrEmpty(pool.getEnd())) {
169                         ContrailSubnetPool csp = new ContrailSubnetPool();
170                         csp.populateWith(pool);
171                         allocationPools.add(csp);
172                     }
173                 }
174             }
175             if (inputSubnet.getHostRoutes() != null) {
176                 List<ContrailSubnetHostRoute> hrList = host_routes.getHost_routes();
177                 for (HostRoute hr : inputSubnet.getHostRoutes()) {
178                     if (!msoCommonUtils.isNullOrEmpty(hr.getPrefix())
179                             || !msoCommonUtils.isNullOrEmpty(hr.getNextHop())) {
180                         ContrailSubnetHostRoute cshr = new ContrailSubnetHostRoute();
181                         cshr.populateWith(hr);
182                         hrList.add(cshr);
183                     }
184                 }
185             }
186         }
187     }
188
189     @Override
190     public String toString() {
191         StringBuilder buf = new StringBuilder();
192         for (ContrailSubnetPool pool : allocationPools) {
193             buf.append(pool.toString());
194         }
195         return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + defaultGateway + " enable_dhcp="
196                 + enableDhcp + " addr_from_start=" + addrFromStart + " subnet_name=" + subnetName + " allocation_pools="
197                 + buf + " ]";
198     }
199
200 }