The visa mentions Germany in valid to field.
This means that, for unknown reasons, the visa is only valid for Germany.
You should try to find out why this is so. The reason would have been entered into the VIS information system when the visa was issued.
Visa Code Article 2 Definitions
4. ‘visa with limited territorial validity’ means a visa valid for the ...
Was this denial of entry legal?
Probably not. Type D visas are automatically valid for short visits (subject to the 90/180 rule) to other Schengen countries, regardless of the "valid for" entry, perhaps unless there is an explicit annotation restricting short visits. (I'm not sure about the explicit annotations, more specifically how it works ...