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.openstack.mappers;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Optional;
28 import org.javatuples.Pair;
29 import org.onap.so.openstack.beans.NetworkInfo;
30 import org.onap.so.openstack.beans.NetworkStatus;
31 import com.woorea.openstack.quantum.model.Network;
32 import com.woorea.openstack.quantum.model.Segment;
34 public class NetworkInfoMapper {
36 private final Network network;
37 private final Map<String, NetworkStatus> networkStatusMap = new HashMap<>();
39 public NetworkInfoMapper(Network network) {
40 this.network = network;
41 configureNetworkStatusMap();
45 * Capture the data from a Neutron Network object.
47 * For MSO, there are assumptions regarding all networks. - Everything will be a provider network - All provider
48 * networks are VLANs - Multiple VLANs are supported, and indicated by multi-provider segments. Each will have the
49 * same physical network & network type "vlan".
53 public NetworkInfo map() {
54 final NetworkInfo info = new NetworkInfo();
55 if (network == null) {
56 info.setStatus(NetworkStatus.NOTFOUND);
58 info.setName(network.getName());
59 info.setId(network.getId());
60 info.setStatus(this.mapStatus(network.getStatus()));
61 Pair<Optional<String>, List<Integer>> result = locateVlanInformation(network);
62 Optional<String> value0 = result.getValue0();
63 if (value0.isPresent()) {
64 info.setProvider(value0.get());
66 info.setVlans(result.getValue1());
67 info.setSubnets(network.getSubnets());
72 protected NetworkStatus mapStatus(String status) {
73 return networkStatusMap.getOrDefault(status, NetworkStatus.UNKNOWN);
76 protected Pair<Optional<String>, List<Integer>> locateVlanInformation(Network network) {
77 final List<Integer> vlans = new ArrayList<>();
78 Optional<String> provider = Optional.empty();
79 if (network.getProviderPhysicalNetwork() != null) {
80 provider = Optional.ofNullable(network.getProviderPhysicalNetwork());
81 if ("vlan".equals(network.getProviderNetworkType())) {
82 vlans.add(network.getProviderSegmentationId());
84 } else if (network.getSegments() != null && !network.getSegments().isEmpty()) {
85 Segment s = network.getSegments().get(0);
86 provider = Optional.ofNullable(s.getProviderPhysicalNetwork());
87 if ("vlan".equals(s.getProviderNetworkType())) {
88 for (Segment s1 : network.getSegments()) {
89 vlans.add(s1.getProviderSegmentationId());
94 return Pair.with(provider, vlans);
97 private void configureNetworkStatusMap() {
98 networkStatusMap.put("ACTIVE", NetworkStatus.ACTIVE);
99 networkStatusMap.put("DOWN", NetworkStatus.DOWN);
100 networkStatusMap.put("BUILD", NetworkStatus.BUILD);
101 networkStatusMap.put("ERROR", NetworkStatus.ERROR);