c281dbd9e54e58fe4cc5d48a1a987ee26f903706
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / heatbridge / utils / HeatBridgeUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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 /*
22  * Copyright (C) 2018 Bell Canada. All rights reserved.
23  *
24  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
25  * the License. You may obtain a copy of the License at
26  *
27  * http://www.apache.org/licenses/LICENSE-2.0
28  *
29  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
30  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
31  * specific language governing permissions and limitations under the License.
32  */
33
34 package org.onap.so.heatbridge.utils;
35
36 import com.google.common.base.Preconditions;
37 import com.google.common.base.Strings;
38 import java.util.List;
39 import java.util.Optional;
40 import java.util.stream.Collectors;
41 import javax.annotation.Nonnull;
42 import org.apache.commons.collections.CollectionUtils;
43 import org.onap.aai.domain.yang.SriovVf;
44 import org.onap.aai.domain.yang.Vserver;
45
46 public final class HeatBridgeUtils {
47
48     /**
49      * IaaS naming convention for compute/p-interface to openstack/physical-network name mapping
50      */
51     private static final String OS_SIDE_SHARED_SRIOV_PREFIX = "shared-";
52     private static final String OS_SIDE_DEDICATED_SRIOV_PREFIX = "dedicated-";
53     private static final String COMPUTE_SIDE_SHARED_SRIOV_PREFIX = "sriov-s-";
54     private static final String COMPUTE_SIDE_DEDICATED_SRIOV_PREFIX = "sriov-d-";
55
56     private HeatBridgeUtils() {
57         throw new IllegalStateException("Trying to instantiate a utility class.");
58     }
59
60     public static Optional<String> getMatchingPserverPifName(@Nonnull final String physicalNetworkName) {
61         Preconditions.checkState(!Strings.isNullOrEmpty(physicalNetworkName),
62                 "Physical network name is null or empty!");
63         if (physicalNetworkName.contains(OS_SIDE_DEDICATED_SRIOV_PREFIX)) {
64             return Optional.of(
65                     physicalNetworkName.replace(OS_SIDE_DEDICATED_SRIOV_PREFIX, COMPUTE_SIDE_DEDICATED_SRIOV_PREFIX));
66         } else if (physicalNetworkName.contains(OS_SIDE_SHARED_SRIOV_PREFIX)) {
67             return Optional
68                     .of(physicalNetworkName.replace(OS_SIDE_SHARED_SRIOV_PREFIX, COMPUTE_SIDE_SHARED_SRIOV_PREFIX));
69         }
70         return Optional.of(physicalNetworkName);
71     }
72
73     public static List<String> extractPciIdsFromVServer(Vserver vserver) {
74         return vserver.getLInterfaces().getLInterface().stream()
75                 .filter(lInterface -> lInterface.getSriovVfs() != null
76                         && CollectionUtils.isNotEmpty(lInterface.getSriovVfs().getSriovVf()))
77                 .flatMap(lInterface -> lInterface.getSriovVfs().getSriovVf().stream()).map(SriovVf::getPciId)
78                 .collect(Collectors.toList());
79     }
80
81 }