001/*
002Copyright 2022 The OpenFunction Authors.
003
004Licensed under the Apache License, Version 2.0 (the "License");
005you may not use this file except in compliance with the License.
006You may obtain a copy of the License at
007
008    http://www.apache.org/licenses/LICENSE-2.0
009
010Unless required by applicable law or agreed to in writing, software
011distributed under the License is distributed on an "AS IS" BASIS,
012WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013See the License for the specific language governing permissions and
014limitations under the License.
015*/
016
017package dev.openfunction.functions;
018
019import org.apache.commons.lang3.StringUtils;
020
021import java.util.Map;
022import java.util.Objects;
023
024public class Component {
025    private static final String ComponentTypeBinding = "bindings";
026    private static final String ComponentTypePubsub = "pubsub";
027
028    @Deprecated
029    private String uri;
030    private String topic;
031    private String componentName;
032    private String componentType;
033    private Map<String, String> metadata;
034    private String operation;
035
036    @Deprecated
037    public String getUri() {
038        if (!StringUtils.isBlank(uri)) {
039            return uri;
040        } else if (!StringUtils.isBlank(topic)) {
041            return topic;
042        } else {
043            return componentName;
044        }
045    }
046
047    @Deprecated
048    public void setUri(String uri) {
049        this.uri = uri;
050    }
051
052    public String getComponentName() {
053        return componentName;
054    }
055
056    public void setComponentName(String componentName) {
057        this.componentName = componentName;
058    }
059
060    public String getComponentType() {
061        return componentType;
062    }
063
064    public void setComponentType(String componentType) {
065        this.componentType = componentType;
066    }
067
068    public Map<String, String> getMetadata() {
069        return metadata;
070    }
071
072    public void setMetadata(Map<String, String> metadata) {
073        this.metadata = metadata;
074    }
075
076    public String getOperation() {
077        return operation;
078    }
079
080    public void setOperation(String operation) {
081        this.operation = operation;
082    }
083
084    public String getTopic() {
085        if (StringUtils.isNotBlank(topic)) {
086            return topic;
087        } else if (StringUtils.isNotBlank(uri) && !Objects.equals(uri, componentName)) {
088            return uri;
089        } else {
090            return null;
091        }
092    }
093
094    public void setTopic(String topic) {
095        this.topic = topic;
096    }
097
098    public boolean isPubsub() {
099        return componentType.startsWith(ComponentTypePubsub);
100    }
101    public boolean isBinding() {
102        return componentType.startsWith(ComponentTypeBinding);
103    }
104
105}
106