2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.adapters.network.mappers;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Optional;
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;
36 public class ContrailSubnetMapper {
38 private final Subnet inputSubnet;
39 public ContrailSubnetMapper(Subnet inputSubnet) {
40 this.inputSubnet = inputSubnet;
43 public ContrailSubnet map() {
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());
52 Optional<ContrailSubnetIp> csIp = createSubnet(inputSubnet);
53 if (csIp.isPresent()) {
54 result.setSubnet(csIp.get());
56 Optional<List<ContrailSubnetPool>> pools = this.createContrailSubnetPool(inputSubnet);
57 if (pools.isPresent()) {
58 result.setAllocationPools(pools.get());
60 Optional<ContrailSubnetHostRoutes> routes = this.createContrailSubnetHostRoutes(inputSubnet);
61 if (routes.isPresent()) {
62 result.setHostRoutes(routes.get());
69 protected String getSubnetName(Subnet subnet) {
71 if (!isNullOrEmpty(subnet.getSubnetName())) {
72 result = subnet.getSubnetName();
74 result = subnet.getSubnetId();
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()) ) {
87 pools.add(new ContrailSubnetPoolMapper(pool).map());
90 if (!pools.isEmpty()) {
91 result = Optional.of(pools);
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());
108 if (!hrList.isEmpty()) {
109 hostRoutesObj.setHostRoutes(hrList);
110 result = Optional.of(hostRoutesObj);
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();
123 csIp.setIpPrefix(subnet.getCidr().substring(0, idx));
124 csIp.setIpPrefixLen(subnet.getCidr().substring(idx+1));
125 result = Optional.of(csIp);
132 protected boolean isNullOrEmpty (String s) {
133 return s == null || s.isEmpty();