You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现YANG模型中my-dp列表的条件删除约束?

解决YANG模型中my-dp实例的删除约束问题

你原来的must约束无法生效,是因为它的逻辑等价于恒真条件——key-dp作为列表的键必然存在,所以(count(high-bw)>=1 and key-dp) or (count(high-bw)=0)永远成立,起不到任何约束作用。

要实现你需要的规则(high-bw为空时my-dp可正常删除;high-bw非空时禁止删除my-dp),可以用以下两种方案:

方案一:利用YANG when语句结合操作变量

通过when语句限制my-dp实例的删除操作,仅当high-bw为空时允许删除:

module efg {
  container deps {
    container my-deps {
      list my-dp {
        key "key-dp";
        leaf key-dp {
          type string;
        }
        
        list high-bw {
          type boolean;
        }
        // ... other leaf definitions ...

        // 核心约束:仅当操作不是删除,或删除时high-bw为空才允许操作
        when "not(operation='delete') or count(high-bw) = 0";
      }
    }
  }
}

这里的operation是YANG内置的操作上下文变量,当执行edit-config的删除操作时,operation='delete'会被置为真。这个约束会直接拒绝所有针对非空high-bw的my-dp实例的删除请求。

方案二:用Action封装删除操作并添加约束

如果希望强制用户通过指定接口删除my-dp,可以定义一个专用的删除Action,在Action的输入中添加约束验证:

module efg {
  container deps {
    container my-deps {
      list my-dp {
        key "key-dp";
        leaf key-dp {
          type string;
        }
        
        list high-bw {
          type boolean;
        }
        // ... other leaf definitions ...
      }

      // 封装删除my-dp的操作
      action delete-my-dp {
        input {
          leaf target-key {
            type string;
            description "要删除的my-dp实例的key";
          }
        }

        // 约束:仅当目标my-dp的high-bw为空时允许删除
        must "count(my-dp[key-dp=current()/target-key]/high-bw) = 0";
      }
    }
  }
}

这种方式要求用户必须调用delete-my-dp Action来删除实例,直接通过edit-config删除的话需要服务器端额外做验证,但Action的约束可以在模型层面明确规则。

内容的提问来源于stack exchange,提问作者KungFuPanda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 02:45:22