2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl.yangmodels;
24 import com.fasterxml.jackson.annotation.JsonInclude;
25 import com.fasterxml.jackson.annotation.JsonInclude.Include;
26 import com.fasterxml.jackson.annotation.JsonProperty;
27 import com.google.common.base.Strings;
28 import java.util.ArrayList;
29 import java.util.List;
31 import lombok.AllArgsConstructor;
34 import lombok.NoArgsConstructor;
36 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService;
37 import org.onap.cps.ncmp.api.inventory.CompositeState;
38 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
39 import org.onap.cps.utils.CpsValidator;
42 * Cm Handle which follows the Yang resource dmi registry model when persisting data to DMI or the DB.
48 @JsonInclude(Include.NON_NULL)
49 public class YangModelCmHandle {
53 @JsonProperty("dmi-service-name")
54 private String dmiServiceName;
56 @JsonProperty("dmi-data-service-name")
57 private String dmiDataServiceName;
59 @JsonProperty("state")
60 private CompositeState compositeState;
62 @JsonProperty("dmi-model-service-name")
63 private String dmiModelServiceName;
65 @JsonProperty("additional-properties")
66 private List<Property> dmiProperties;
68 @JsonProperty("public-properties")
69 private List<Property> publicProperties;
72 * Create a yangModelCmHandle.
74 * @param dmiServiceName dmi service name
75 * @param dmiDataServiceName dmi data service name
76 * @param dmiModelServiceName dmi model service name
77 * @param ncmpServiceCmHandle the cm handle
78 * @return instance of yangModelCmHandle
80 public static YangModelCmHandle toYangModelCmHandle(final String dmiServiceName,
81 final String dmiDataServiceName,
82 final String dmiModelServiceName,
83 final NcmpServiceCmHandle ncmpServiceCmHandle) {
84 CpsValidator.validateNameCharacters(ncmpServiceCmHandle.getCmHandleId());
85 final YangModelCmHandle yangModelCmHandle = new YangModelCmHandle();
86 yangModelCmHandle.setId(ncmpServiceCmHandle.getCmHandleId());
87 yangModelCmHandle.setDmiServiceName(dmiServiceName);
88 yangModelCmHandle.setDmiDataServiceName(dmiDataServiceName);
89 yangModelCmHandle.setDmiModelServiceName(dmiModelServiceName);
90 yangModelCmHandle.setDmiProperties(asYangModelCmHandleProperties(ncmpServiceCmHandle.getDmiProperties()));
91 yangModelCmHandle.setPublicProperties(asYangModelCmHandleProperties(
92 ncmpServiceCmHandle.getPublicProperties()));
93 yangModelCmHandle.setCompositeState(ncmpServiceCmHandle.getCompositeState());
94 return yangModelCmHandle;
98 * Resolve a dmi service name.
100 * @param requiredService indicates what typo of service is required
101 * @return dmi service name
103 public String resolveDmiServiceName(final RequiredDmiService requiredService) {
104 if (isNullEmptyOrBlank(dmiServiceName)) {
105 if (RequiredDmiService.DATA.equals(requiredService)) {
106 return dmiDataServiceName;
108 return dmiModelServiceName;
110 return dmiServiceName;
113 private static List<Property> asYangModelCmHandleProperties(final Map<String, String> propertiesAsMap) {
114 final List<Property> yangModelCmHandleProperties = new ArrayList<>(propertiesAsMap.size());
115 for (final Map.Entry<String, String> entry : propertiesAsMap.entrySet()) {
116 yangModelCmHandleProperties.add(new YangModelCmHandle.Property(entry.getKey(), entry.getValue()));
118 return yangModelCmHandleProperties;
121 private static boolean isNullEmptyOrBlank(final String serviceName) {
122 return Strings.isNullOrEmpty(serviceName) || serviceName.isBlank();
127 public static class Property {
130 private final String name;
133 private final String value;