d3ccb35a6085df671b4f359673719e1590f50ece
[so.git] / adapters / mso-adapters-rest-interface / src / main / java / org / onap / so / adapters / network / mappers / ContrailSubnetMapper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.network.mappers;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Optional;
26
27 import org.onap.so.adapters.network.beans.ContrailSubnet;
28 import org.onap.so.adapters.network.beans.ContrailSubnetHostRoute;
29 import org.onap.so.adapters.network.beans.ContrailSubnetHostRoutes;
30 import org.onap.so.adapters.network.beans.ContrailSubnetIp;
31 import org.onap.so.adapters.network.beans.ContrailSubnetPool;
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
36 public class ContrailSubnetMapper {
37
38         private final Subnet inputSubnet;
39         public ContrailSubnetMapper(Subnet inputSubnet) {
40                 this.inputSubnet = inputSubnet;
41         }
42         
43         public ContrailSubnet map() {
44                 
45                 final ContrailSubnet result = new ContrailSubnet();
46                 if (inputSubnet != null) {
47                         final String subnetname = this.getSubnetName(inputSubnet);
48                         result.setSubnetName(subnetname);
49                         result.setEnableDhcp(inputSubnet.getEnableDHCP());
50                         result.setDefaultGateway(inputSubnet.getGatewayIp());
51
52                         Optional<ContrailSubnetIp> csIp = createSubnet(inputSubnet);
53                         if (csIp.isPresent()) {
54                                 result.setSubnet(csIp.get());
55                         }
56                         Optional<List<ContrailSubnetPool>> pools = this.createContrailSubnetPool(inputSubnet);
57                         if (pools.isPresent()) {
58                                 result.setAllocationPools(pools.get());
59                         }
60                         Optional<ContrailSubnetHostRoutes> routes = this.createContrailSubnetHostRoutes(inputSubnet);
61                         if (routes.isPresent()) {
62                                 result.setHostRoutes(routes.get());
63                         }
64                 }
65                 
66                 return result;
67         }
68
69         protected String getSubnetName(Subnet subnet) {
70                 final String result;
71                 if (!isNullOrEmpty(subnet.getSubnetName())) {
72                         result = subnet.getSubnetName();
73                 } else {
74                         result = subnet.getSubnetId();
75                 }
76                 
77                 return result;
78         }
79         
80         protected Optional<List<ContrailSubnetPool>> createContrailSubnetPool(final Subnet subnet) {
81                 Optional<List<ContrailSubnetPool>> result = Optional.empty();
82                 if (subnet.getAllocationPools() != null) {
83                         List<ContrailSubnetPool> pools = new ArrayList<>();
84                         for (Pool pool : subnet.getAllocationPools()) {
85                                 if ( !isNullOrEmpty(pool.getStart()) && !isNullOrEmpty(pool.getEnd()) ) {
86                                 
87                                         pools.add(new ContrailSubnetPoolMapper(pool).map());
88                                 }
89                         }
90                         if (!pools.isEmpty()) {
91                                 result = Optional.of(pools);
92                         }
93                 }
94                 
95                 return result;
96         }
97         
98         protected Optional<ContrailSubnetHostRoutes> createContrailSubnetHostRoutes(final Subnet subnet) {
99                 Optional<ContrailSubnetHostRoutes> result = Optional.empty();
100                 if (subnet.getHostRoutes() != null) {
101                         ContrailSubnetHostRoutes hostRoutesObj = new ContrailSubnetHostRoutes();
102                         List<ContrailSubnetHostRoute> hrList = new ArrayList<>();
103                         for (HostRoute hr : subnet.getHostRoutes()) {
104                                 if ( !isNullOrEmpty(hr.getPrefix()) || !isNullOrEmpty(hr.getNextHop()) ) {
105                                         hrList.add(new ContrailSubnetHostRouteMapper(hr).map());
106                                 }
107                         }
108                         if (!hrList.isEmpty()) {
109                                 hostRoutesObj.setHostRoutes(hrList);
110                                 result = Optional.of(hostRoutesObj);
111                         }
112                 }
113                 
114                 return result;
115                 
116         }
117         protected Optional<ContrailSubnetIp> createSubnet(final Subnet subnet) {
118                 Optional<ContrailSubnetIp> result = Optional.empty();
119                 if (!isNullOrEmpty(subnet.getCidr()) ) {
120                         int idx = subnet.getCidr().indexOf("/");
121                         final ContrailSubnetIp csIp = new ContrailSubnetIp();
122                         if (idx != -1) {
123                                 csIp.setIpPrefix(subnet.getCidr().substring(0, idx));
124                                 csIp.setIpPrefixLen(subnet.getCidr().substring(idx+1));
125                                 result = Optional.of(csIp);
126                         }
127                 }
128                 
129                 return result;
130         }
131
132         protected boolean isNullOrEmpty (String s) {
133                 return s == null || s.isEmpty();
134         }
135 }